Everything clean and working now.
This commit is contained in:
parent
923d9f8379
commit
8fc7c2ece6
9 changed files with 137 additions and 107 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@
|
||||||
*.pyo
|
*.pyo
|
||||||
*~
|
*~
|
||||||
*.swp
|
*.swp
|
||||||
|
*.db
|
||||||
*.egg-info
|
*.egg-info
|
||||||
.venv
|
.venv
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
|
@ -9,13 +9,48 @@
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
|
|
||||||
|
from lxml.cssselect import CSSSelector
|
||||||
|
from lxml.etree import fromstring
|
||||||
|
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
|
|
||||||
class Url(object):
|
class Url(object):
|
||||||
def __init__(self, context, path):
|
def __init__(self, context, path):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
|
class Model(object):
|
||||||
|
def __init__(self, context, model):
|
||||||
|
self.context = context
|
||||||
|
self.model = model
|
||||||
|
|
||||||
|
class Template(object):
|
||||||
|
def __init__(self, template_name, context):
|
||||||
|
self.template_name = template_name
|
||||||
|
self.context = context
|
||||||
|
self.doc = None
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
if self.doc is None:
|
||||||
|
self.doc = fromstring(render_to_string(self.template_name, self.context))
|
||||||
|
return self.doc
|
||||||
|
|
||||||
|
def select_element(self, selector):
|
||||||
|
sel = CSSSelector(selector)
|
||||||
|
return sel(self.load())
|
||||||
|
|
||||||
|
def _to_contain(self, selector):
|
||||||
|
return len(self.select_element(selector)) > 0
|
||||||
|
|
||||||
|
def get_text(self, selector):
|
||||||
|
return "".join((c.text for c in self.select_element(selector)))
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.template_name
|
||||||
|
|
||||||
@Vows.assertion
|
@Vows.assertion
|
||||||
def to_be_mapped(topic):
|
def to_be_mapped(topic):
|
||||||
verify_url_is_mapped_to_method(topic)
|
verify_url_is_mapped_to_method(topic)
|
||||||
|
@ -56,3 +91,75 @@ def to_be_http_response(topic):
|
||||||
def to_have_contents_of(topic, expected):
|
def to_have_contents_of(topic, expected):
|
||||||
expect(topic.content).to_be_like(expected)
|
expect(topic.content).to_be_like(expected)
|
||||||
|
|
||||||
|
@Vows.assertion
|
||||||
|
def to_contain(topic, selector):
|
||||||
|
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
|
||||||
|
assert topic._to_contain(selector), "Expected template(%s) to have an element(%s), but it don't have" % \
|
||||||
|
(unicode(topic), selector)
|
||||||
|
|
||||||
|
@Vows.assertion
|
||||||
|
def not_to_contain(topic, selector):
|
||||||
|
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
|
||||||
|
assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \
|
||||||
|
(unicode(topic), selector)
|
||||||
|
|
||||||
|
@Vows.assertion
|
||||||
|
def to_be_cruddable(topic, defaults={}):
|
||||||
|
import django.db.models.fields as fields
|
||||||
|
instance = __create_or_update_instance(topic, None, defaults)
|
||||||
|
|
||||||
|
assert instance, "An instance could not be created for model %s" % topic.model.__name__
|
||||||
|
|
||||||
|
retrieved = topic.model.objects.get(id=instance.id)
|
||||||
|
assert retrieved.id == instance.id, "An instance could not be retrieved for model %s with id %d" % (topic.model.__name__, instance.id)
|
||||||
|
|
||||||
|
for key, value in defaults.iteritems():
|
||||||
|
assert value == getattr(retrieved, key), "The default specified value of '%s' should have been set in the '%s' property of the instance but it was not" % (value, key)
|
||||||
|
|
||||||
|
updated = __create_or_update_instance(topic, retrieved, defaults)
|
||||||
|
|
||||||
|
for field, value in topic.model._meta._field_cache:
|
||||||
|
if field.__class__ == fields.AutoField:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if field.name in defaults:
|
||||||
|
continue
|
||||||
|
|
||||||
|
assert getattr(updated, field.name) != getattr(instance, field.name), "The instance should have been updated but the field %s is the same in both the original instance and the updated one (%s)." % (field.name, getattr(updated, field.name))
|
||||||
|
|
||||||
|
instance.delete()
|
||||||
|
object_count = topic.model.objects.count()
|
||||||
|
assert object_count == 0, "Object should have been deleted, but it wasn't (count: %d)" % object_count
|
||||||
|
|
||||||
|
def __create_or_update_instance(topic, instance, defaults):
|
||||||
|
import django.db.models.fields as fields
|
||||||
|
arguments = {}
|
||||||
|
for field, value in topic.model._meta._field_cache:
|
||||||
|
if field.__class__ == fields.AutoField:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if field.name in defaults:
|
||||||
|
arguments[field.name] = defaults[field.name]
|
||||||
|
continue
|
||||||
|
|
||||||
|
if field.__class__ == fields.CharField:
|
||||||
|
__add_char_value_for(field, instance, arguments)
|
||||||
|
|
||||||
|
if instance:
|
||||||
|
for key, value in arguments.iteritems():
|
||||||
|
setattr(instance, key, value)
|
||||||
|
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
|
return topic.model.objects.create(**arguments)
|
||||||
|
|
||||||
|
def __add_char_value_for(field, instance, arguments):
|
||||||
|
value = "monty python"
|
||||||
|
if instance:
|
||||||
|
value = getattr(instance, field.name) + '2'
|
||||||
|
if field.max_length:
|
||||||
|
if instance:
|
||||||
|
value = value[:len(value) - 2] + '2'
|
||||||
|
value = (value * field.max_length)[:field.max_length]
|
||||||
|
arguments[field.name] = value
|
||||||
|
|
|
@ -12,11 +12,11 @@ import os
|
||||||
|
|
||||||
from pyvows import Vows
|
from pyvows import Vows
|
||||||
|
|
||||||
from django_pyvows.assertions import Url
|
from django_pyvows.assertions import Url, Model, Template
|
||||||
from django_pyvows.model_assertions import Model
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
|
|
||||||
class DjangoContext(Vows.Context):
|
class DjangoContext(Vows.Context):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _start_environment(cls, settings_path):
|
def _start_environment(cls, settings_path):
|
||||||
if not settings_path:
|
if not settings_path:
|
||||||
|
@ -25,9 +25,14 @@ class DjangoContext(Vows.Context):
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
super(DjangoContext, self).__init__(parent)
|
super(DjangoContext, self).__init__(parent)
|
||||||
|
if not parent:
|
||||||
|
DjangoContext._start_environment(self._get_settings())
|
||||||
|
|
||||||
def _get_settings(self):
|
def _get_settings(self):
|
||||||
return 'settings'
|
if 'DJANGO_SETTINGS_MODULE' in os.environ:
|
||||||
|
return os.environ['DJANGO_SETTINGS_MODULE']
|
||||||
|
else:
|
||||||
|
return 'settings'
|
||||||
|
|
||||||
def _url(self, path):
|
def _url(self, path):
|
||||||
return Url(self, path)
|
return Url(self, path)
|
||||||
|
@ -35,17 +40,6 @@ class DjangoContext(Vows.Context):
|
||||||
def _template(self, template_name, context):
|
def _template(self, template_name, context):
|
||||||
return Template(template_name, context)
|
return Template(template_name, context)
|
||||||
|
|
||||||
class DjangoSubContext(Vows.Context):
|
|
||||||
|
|
||||||
def _url(self, path):
|
|
||||||
return self.parent._url(path)
|
|
||||||
|
|
||||||
def _template(self, template_name, context={}):
|
|
||||||
return self.parent._template(template_name, context)
|
|
||||||
|
|
||||||
def _url(self, path):
|
|
||||||
return Url(self, path)
|
|
||||||
|
|
||||||
def _request(self, **kw):
|
def _request(self, **kw):
|
||||||
return HttpRequest(**kw)
|
return HttpRequest(**kw)
|
||||||
|
|
||||||
|
|
|
@ -1,77 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# django-pyvows extensions
|
|
||||||
# https://github.com/rafaelcaricio/django-pyvows
|
|
||||||
|
|
||||||
# Licensed under the MIT license:
|
|
||||||
# http://www.opensource.org/licenses/mit-license
|
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
|
||||||
|
|
||||||
from pyvows import Vows, expect
|
|
||||||
|
|
||||||
class Model(object):
|
|
||||||
def __init__(self, context, model):
|
|
||||||
self.context = context
|
|
||||||
self.model = model
|
|
||||||
|
|
||||||
@Vows.assertion
|
|
||||||
def to_be_cruddable(topic, defaults={}):
|
|
||||||
import django.db.models.fields as fields
|
|
||||||
instance = __create_or_update_instance(topic, None, defaults)
|
|
||||||
|
|
||||||
assert instance, "An instance could not be created for model %s" % topic.model.__name__
|
|
||||||
|
|
||||||
retrieved = topic.model.objects.get(id=instance.id)
|
|
||||||
assert retrieved.id == instance.id, "An instance could not be retrieved for model %s with id %d" % (topic.model.__name__, instance.id)
|
|
||||||
|
|
||||||
for key, value in defaults.iteritems():
|
|
||||||
assert value == getattr(retrieved, key), "The default specified value of '%s' should have been set in the '%s' property of the instance but it was not" % (value, key)
|
|
||||||
|
|
||||||
updated = __create_or_update_instance(topic, retrieved, defaults)
|
|
||||||
|
|
||||||
for field, value in topic.model._meta._field_cache:
|
|
||||||
if field.__class__ == fields.AutoField:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if field.name in defaults:
|
|
||||||
continue
|
|
||||||
|
|
||||||
assert getattr(updated, field.name) != getattr(instance, field.name), "The instance should have been updated but the field %s is the same in both the original instance and the updated one (%s)." % (field.name, getattr(updated, field.name))
|
|
||||||
|
|
||||||
instance.delete()
|
|
||||||
object_count = topic.model.objects.count()
|
|
||||||
assert object_count == 0, "Object should have been deleted, but it wasn't (count: %d)" % object_count
|
|
||||||
|
|
||||||
def __create_or_update_instance(topic, instance, defaults):
|
|
||||||
import django.db.models.fields as fields
|
|
||||||
arguments = {}
|
|
||||||
for field, value in topic.model._meta._field_cache:
|
|
||||||
if field.__class__ == fields.AutoField:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if field.name in defaults:
|
|
||||||
arguments[field.name] = defaults[field.name]
|
|
||||||
continue
|
|
||||||
|
|
||||||
if field.__class__ == fields.CharField:
|
|
||||||
__add_char_value_for(field, instance, arguments)
|
|
||||||
|
|
||||||
if instance:
|
|
||||||
for key, value in arguments.iteritems():
|
|
||||||
setattr(instance, key, value)
|
|
||||||
|
|
||||||
instance.save()
|
|
||||||
return instance
|
|
||||||
|
|
||||||
return topic.model.objects.create(**arguments)
|
|
||||||
|
|
||||||
def __add_char_value_for(field, instance, arguments):
|
|
||||||
value = "monty python"
|
|
||||||
if instance:
|
|
||||||
value = getattr(instance, field.name) + '2'
|
|
||||||
if field.max_length:
|
|
||||||
if instance:
|
|
||||||
value = value[:len(value) - 2] + '2'
|
|
||||||
value = (value * field.max_length)[:field.max_length]
|
|
||||||
arguments[field.name] = value
|
|
|
@ -11,12 +11,12 @@
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
DjangoContext._start_environment('sandbox.settings')
|
DjangoContext._start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.models import StringModel
|
from sandbox.main.models import StringModel
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class ModelVows(Vows.Context):
|
class ModelVows(DjangoContext):
|
||||||
|
|
||||||
class MainModel(DjangoContext):
|
class MainModel(DjangoContext):
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,12 @@
|
||||||
# http://www.opensource.org/licenses/mit-license
|
# http://www.opensource.org/licenses/mit-license
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext, DjangoSubContext
|
|
||||||
import django_pyvows.assertions
|
from django_pyvows.context import DjangoContext
|
||||||
|
from django_pyvows.assertions import *
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
|
@ -18,6 +21,7 @@ from sandbox.main.views import home
|
||||||
class DefaultsVows(DjangoContext):
|
class DefaultsVows(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
|
del os.environ['DJANGO_SETTINGS_MODULE']
|
||||||
return self._get_settings()
|
return self._get_settings()
|
||||||
|
|
||||||
def should_be_using_the_default_settings(self, topic):
|
def should_be_using_the_default_settings(self, topic):
|
||||||
|
|
|
@ -9,8 +9,9 @@
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext, DjangoSubContext
|
|
||||||
import django_pyvows.assertions
|
from django_pyvows.context import DjangoContext
|
||||||
|
from django_pyvows.assertions import *
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class TemplateVows(DjangoContext):
|
class TemplateVows(DjangoContext):
|
||||||
|
@ -18,7 +19,7 @@ class TemplateVows(DjangoContext):
|
||||||
def _get_settings(self):
|
def _get_settings(self):
|
||||||
return 'sandbox.settings'
|
return 'sandbox.settings'
|
||||||
|
|
||||||
class IndexTemplate(DjangoSubContext):
|
class IndexTemplate(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return self._template('index.html', {
|
return self._template('index.html', {
|
||||||
|
@ -34,7 +35,7 @@ class TemplateVows(DjangoContext):
|
||||||
def should_be_index_file(self, topic):
|
def should_be_index_file(self, topic):
|
||||||
expect(unicode(topic)).to_equal('index.html')
|
expect(unicode(topic)).to_equal('index.html')
|
||||||
|
|
||||||
class Paragraph(DjangoSubContext):
|
class Paragraph(DjangoContext):
|
||||||
|
|
||||||
def topic(self, template):
|
def topic(self, template):
|
||||||
return template.get_text('p.my-text')
|
return template.get_text('p.my-text')
|
||||||
|
|
|
@ -9,18 +9,18 @@
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext, DjangoSubContext
|
|
||||||
import django_pyvows.assertions
|
from django_pyvows.context import DjangoContext
|
||||||
|
from django_pyvows.assertions import *
|
||||||
|
|
||||||
|
DjangoContext._start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class UrlVows(DjangoContext):
|
class UrlVows(DjangoContext):
|
||||||
|
|
||||||
def _get_settings(self):
|
class Home(DjangoContext):
|
||||||
return 'sandbox.settings'
|
|
||||||
|
|
||||||
class Home(DjangoSubContext):
|
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return self._url('^$')
|
return self._url('^$')
|
||||||
|
|
|
@ -11,12 +11,12 @@
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
DjangoContext._start_environment('sandbox.settings')
|
DjangoContext._start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class ViewVows(Vows.Context):
|
class ViewVows(DjangoContext):
|
||||||
|
|
||||||
class Home(DjangoContext):
|
class Home(DjangoContext):
|
||||||
|
|
||||||
|
|
Reference in a new issue