A great way to assert model fields.

This commit is contained in:
Rafael Caricio 2011-08-25 19:34:52 -03:00
parent aab2930c4e
commit bb3137f881
2 changed files with 26 additions and 0 deletions

View file

@ -106,6 +106,23 @@ def not_to_contain(topic, selector):
assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \ assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \
(unicode(topic), selector) (unicode(topic), selector)
@Vows.assertion
def to_have_field(topic, field_name, field_class=None, **kwargs):
from django.db import models
if isinstance(topic, models.Model):
topic = topic.__class__
if not field_class:
field_class = models.Field
field = topic._meta.get_field(field_name)
assert isinstance(field, field_class), "The '%s.%s' is not an instance of '%s'" % (topic.__name__, field_name, field_class.__name__)
if kwargs:
for attr, value in kwargs.items():
field_value = getattr(field, attr)
assert field_value == value, "The field option '%s' should be equal to '%s', but it's equal to '%s'" % (attr, value, field_value)
@Vows.assertion @Vows.assertion
def to_be_cruddable(topic, defaults={}): def to_be_cruddable(topic, defaults={}):
import django.db.models.fields as fields import django.db.models.fields as fields

View file

@ -43,4 +43,13 @@ class ModelVows(DjangoContext):
def should_be_an_instance_of_django_model_class(self, topic): def should_be_an_instance_of_django_model_class(self, topic):
expect(isinstance(topic, models.Model)).to_be_true() expect(isinstance(topic, models.Model)).to_be_true()
def should_have_a_name_field(self, topic):
expect(topic).to_have_field('name')
def should_have_a_name_field_as_charfield(self, topic):
expect(topic).to_have_field('name', models.CharField)
def should_have_a_name_field_as_charfield_and_max_length_100(self, topic):
expect(topic).to_have_field('name', models.CharField, max_length=100)