From bb3137f8813427e19b340ebdfc89227284c71c19 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Thu, 25 Aug 2011 19:34:52 -0300 Subject: [PATCH] A great way to assert model fields. --- django_pyvows/assertions.py | 17 +++++++++++++++++ vows/model_vows.py | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/django_pyvows/assertions.py b/django_pyvows/assertions.py index b1cf932..5da842c 100644 --- a/django_pyvows/assertions.py +++ b/django_pyvows/assertions.py @@ -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" % \ (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 def to_be_cruddable(topic, defaults={}): import django.db.models.fields as fields diff --git a/vows/model_vows.py b/vows/model_vows.py index 5760e13..7d32b70 100644 --- a/vows/model_vows.py +++ b/vows/model_vows.py @@ -43,4 +43,13 @@ class ModelVows(DjangoContext): def should_be_an_instance_of_django_model_class(self, topic): 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) +