Merge with contribuition from heynemann.

This commit is contained in:
Rafael Caricio 2011-08-18 10:28:18 -03:00
commit 923d9f8379
9 changed files with 219 additions and 57 deletions

View file

@ -1,2 +1,5 @@
pyvows:
@env PYTHONPATH=$$PYTHONPATH:. pyvows --cover --cover_package=django_pyvows --cover_threshold=100 vows/
db:
@env PYTHONPATH=$$PYTHONPATH:. mysql -u root -e 'DROP DATABASE IF EXISTS django_pyvows' && mysql -u root -e 'CREATE DATABASE IF NOT EXISTS django_pyvows' && python vows/sandbox/manage.py syncdb

View file

@ -8,54 +8,14 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
from lxml.cssselect import CSSSelector
from lxml.etree import fromstring
from django.template.loader import render_to_string
from pyvows import Vows
from pyvows import Vows, expect
from django.http import HttpResponse
class Url(object):
def __init__(self, context, path):
self.context = context
self.path = path
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
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_mapped(topic):
verify_url_is_mapped_to_method(topic)
@ -76,7 +36,7 @@ def verify_url_is_mapped_to_method(topic, method=None, assert_method_as_well=Fal
matches_method = False
for urlpattern in urlpatterns:
regex = urlpattern.regex
pattern = regex.pattern
pattern = regex.pattern
actual_method = urlpattern.callback
if topic.path == pattern:
@ -87,3 +47,12 @@ def verify_url_is_mapped_to_method(topic, method=None, assert_method_as_well=Fal
assert found, "Expected url(%s) to be mapped but it wasn't"
if assert_method_as_well:
assert matches_method, "Expected url(%s) to match method(%s), but it didn't"
@Vows.assertion
def to_be_http_response(topic):
expect(topic).to_be_instance_of(HttpResponse)
@Vows.assertion
def to_have_contents_of(topic, expected):
expect(topic.content).to_be_like(expected)

View file

@ -11,19 +11,20 @@
import os
from pyvows import Vows
from django_pyvows.assertions import Url, Template
from django_pyvows.assertions import Url
from django_pyvows.model_assertions import Model
from django.http import HttpRequest
class DjangoContext(Vows.Context):
@classmethod
def _start_environment(cls, settings_path):
if not settings_path:
raise RuntimeError('The settings_path argument is required.')
os.environ['DJANGO_SETTINGS_MODULE'] = settings_path
def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
os.environ['DJANGO_SETTINGS_MODULE'] = self._get_settings()
#Gotta set settings environment variable first
from django.test.utils import setup_test_environment #, teardown_test_environment
setup_test_environment()
def _get_settings(self):
return 'settings'
@ -42,3 +43,11 @@ class DjangoSubContext(Vows.Context):
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):
return HttpRequest(**kw)
def _model(self, model_class):
return Model(self, model_class)

View file

@ -0,0 +1,77 @@
#!/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

View file

@ -8,4 +8,21 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
from pyvows import Vows
from pyvows import Vows, expect
from django_pyvows.context import DjangoContext
@Vows.batch
class ContextTest(Vows.Context):
def topic(self):
return DjangoContext._start_environment(None)
def should_be_an_error(self, topic):
expect(topic).to_be_an_error()
def should_be_runtime_error(self, topic):
expect(topic).to_be_an_error_like(RuntimeError)
def should_have_nice_error_message(self, topic):
expect(topic).to_have_an_error_message_of('The settings_path argument is required.')

34
vows/model_vows.py Normal file
View file

@ -0,0 +1,34 @@
#!/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
from django_pyvows.context import DjangoContext
DjangoContext._start_environment('sandbox.settings')
from sandbox.main.models import StringModel
@Vows.batch
class ModelVows(Vows.Context):
class MainModel(DjangoContext):
def topic(self):
return self._model(StringModel)
def should_be_cruddable_when_model_only_has_a_string(self, topic):
expect(topic).to_be_cruddable()
def should_be_cruddable_when_string_passed(self, topic):
expect(topic).to_be_cruddable({
'name': 'something'
})

View file

@ -1,3 +1,14 @@
#!/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 django.db import models
# Create your models here.
class StringModel(models.Model):
name = models.CharField(max_length=100)

View file

@ -1,4 +1,15 @@
#!/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
# Django settings for sandbox project.
import os.path
DEBUG = True
@ -14,12 +25,12 @@ MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db.db', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
}
}
@ -119,6 +130,7 @@ INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'sandbox.main',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:

30
vows/view_vows.py Normal file
View file

@ -0,0 +1,30 @@
#!/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
from django_pyvows.context import DjangoContext
DjangoContext._start_environment('sandbox.settings')
from sandbox.main.views import home
@Vows.batch
class ViewVows(Vows.Context):
class Home(DjangoContext):
def topic(self):
return home(self._request())
def should_be_instance_of_http_response(self, topic):
expect(topic).to_be_http_response()
def should_be_hello_world(self, topic):
expect(topic).to_have_contents_of('Hello World')