Crud assertions started with strings
This commit is contained in:
parent
6b8d23d477
commit
7d5b0ae101
9 changed files with 150 additions and 36 deletions
3
Makefile
3
Makefile
|
@ -1,2 +1,5 @@
|
||||||
pyvows:
|
pyvows:
|
||||||
@env PYTHONPATH=$$PYTHONPATH:. pyvows --cover --cover_package=django_pyvows --cover_threshold=100 vows/
|
@env PYTHONPATH=$$PYTHONPATH:. pyvows --cover --cover_package=django_pyvows --cover_threshold=100 vows/
|
||||||
|
|
||||||
|
db:
|
||||||
|
@env PYTHONPATH=$$PYTHONPATH:. mysql -u root -e 'CREATE DATABASE IF NOT EXISTS django_pyvows' && python vows/sandbox/manage.py syncdb
|
||||||
|
|
|
@ -12,17 +12,19 @@ import os
|
||||||
|
|
||||||
from pyvows import Vows
|
from pyvows import Vows
|
||||||
from django_pyvows.assertions import Url
|
from django_pyvows.assertions import Url
|
||||||
|
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
|
||||||
|
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):
|
def __init__(self, parent):
|
||||||
super(DjangoContext, self).__init__(parent)
|
super(DjangoContext, self).__init__(parent)
|
||||||
if not hasattr(self, '_get_settings'):
|
|
||||||
raise RuntimeError('The context %s needs a _get_settings method that returns the DJANGO_SETTINGS_MODULE environment variable value.' % self.__class__.__name__)
|
|
||||||
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
|
from django.test.utils import setup_test_environment #, teardown_test_environment
|
||||||
|
|
||||||
setup_test_environment()
|
setup_test_environment()
|
||||||
|
@ -33,11 +35,5 @@ class DjangoContext(Vows.Context):
|
||||||
def _request(self, **kw):
|
def _request(self, **kw):
|
||||||
return HttpRequest(**kw)
|
return HttpRequest(**kw)
|
||||||
|
|
||||||
class DjangoSubContext(Vows.Context):
|
def _model(self, model_class):
|
||||||
|
return Model(self, model_class)
|
||||||
def _url(self, path):
|
|
||||||
return self.parent._url(path)
|
|
||||||
|
|
||||||
def _request(self, **kw):
|
|
||||||
return self.parent._request(**kw)
|
|
||||||
|
|
||||||
|
|
72
django_pyvows/model_assertions.py
Normal file
72
django_pyvows/model_assertions.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#!/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):
|
||||||
|
import django.db.models.fields as fields
|
||||||
|
instance = __create_instance(topic)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
updated = __update_instance(topic, retrieved)
|
||||||
|
|
||||||
|
for field, value in topic.model._meta._field_cache:
|
||||||
|
if field.__class__ == fields.AutoField:
|
||||||
|
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))
|
||||||
|
|
||||||
|
def __create_instance(topic):
|
||||||
|
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.__class__ == fields.CharField:
|
||||||
|
__add_char_value_for(field, None, arguments)
|
||||||
|
|
||||||
|
return topic.model.objects.create(**arguments)
|
||||||
|
|
||||||
|
def __update_instance(topic, instance):
|
||||||
|
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.__class__ == fields.CharField:
|
||||||
|
__add_char_value_for(field, instance, arguments)
|
||||||
|
|
||||||
|
for key, value in arguments.iteritems():
|
||||||
|
setattr(instance, key, value)
|
||||||
|
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
|
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,14 +12,11 @@ from pyvows import Vows, expect
|
||||||
|
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
class DjangoFailedContext(DjangoContext):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class ContextTest(Vows.Context):
|
class ContextTest(Vows.Context):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return DjangoFailedContext(self)
|
return DjangoContext._start_environment(None)
|
||||||
|
|
||||||
def should_be_an_error(self, topic):
|
def should_be_an_error(self, topic):
|
||||||
expect(topic).to_be_an_error()
|
expect(topic).to_be_an_error()
|
||||||
|
@ -28,4 +25,4 @@ class ContextTest(Vows.Context):
|
||||||
expect(topic).to_be_an_error_like(RuntimeError)
|
expect(topic).to_be_an_error_like(RuntimeError)
|
||||||
|
|
||||||
def should_have_nice_error_message(self, topic):
|
def should_have_nice_error_message(self, topic):
|
||||||
expect(topic).to_have_an_error_message_of('The context DjangoFailedContext needs a _get_settings method that returns the DJANGO_SETTINGS_MODULE environment variable value.')
|
expect(topic).to_have_an_error_message_of('The settings_path argument is required.')
|
||||||
|
|
29
vows/model_vows.py
Normal file
29
vows/model_vows.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/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()
|
||||||
|
|
||||||
|
|
|
@ -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
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
class StringModel(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
#!/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.
|
# Django settings for sandbox project.
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
@ -11,12 +21,10 @@ MANAGERS = ADMINS
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
|
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
|
||||||
'NAME': '', # Or path to database file if using sqlite3.
|
'NAME': 'django_pyvows', # Or path to database file if using sqlite3.
|
||||||
'USER': '', # Not used with sqlite3.
|
'USER': 'root', # Not used with sqlite3.
|
||||||
'PASSWORD': '', # 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.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +123,7 @@ INSTALLED_APPS = (
|
||||||
'django.contrib.sites',
|
'django.contrib.sites',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'sandbox.main',
|
||||||
# Uncomment the next line to enable the admin:
|
# Uncomment the next line to enable the admin:
|
||||||
# 'django.contrib.admin',
|
# 'django.contrib.admin',
|
||||||
# Uncomment the next line to enable admin documentation:
|
# Uncomment the next line to enable admin documentation:
|
||||||
|
|
|
@ -9,17 +9,16 @@
|
||||||
# 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
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
|
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(Vows.Context):
|
||||||
|
|
||||||
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('^$')
|
||||||
|
|
|
@ -9,18 +9,16 @@
|
||||||
# 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
|
from django_pyvows.context import DjangoContext
|
||||||
from django.http import HttpResponse
|
|
||||||
|
DjangoContext._start_environment('sandbox.settings')
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class ViewVows(DjangoContext):
|
class ViewVows(Vows.Context):
|
||||||
|
|
||||||
def _get_settings(self):
|
class Home(DjangoContext):
|
||||||
return 'sandbox.settings'
|
|
||||||
|
|
||||||
class Home(DjangoSubContext):
|
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return home(self._request())
|
return home(self._request())
|
||||||
|
|
Reference in a new issue