Skip to content

Commit

Permalink
Add tests for fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed May 22, 2012
2 parents c8af592 + 9460a06 commit cfb6217
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 0 deletions.
1 change: 1 addition & 0 deletions testprj/testapp/tests/__init__.py
@@ -1 +1,2 @@
from fields import *
from regression import MongoformsRegressionTests
2 changes: 2 additions & 0 deletions testprj/testapp/tests/fields/__init__.py
@@ -0,0 +1,2 @@
from render import *
from validate import *
181 changes: 181 additions & 0 deletions testprj/testapp/tests/fields/render.py
@@ -0,0 +1,181 @@
from mongoengine import Document, EmbeddedDocument
from mongoengine.fields import *

from mongoforms.fields import MongoFormFieldGenerator

from testprj.tests import MongoengineTestCase


class _FieldRenderTestCase(MongoengineTestCase):
# mongoengine field instance to test
field_class = None
# widget rendering result (most common value)
rendered_widget = '<input type="text" name="test_field" />'
# hook for not implemented fields
is_not_implemented = False

def setUp(self):
self.generator = MongoFormFieldGenerator()

def get_field(self):

class TestDocument(Document):
test_field = self.field_class()

return TestDocument._fields['test_field']

def get_form_field(self):
return self.generator.generate('test_field', self.get_field())

def runTest(self):

if self.is_not_implemented:
self.assertRaises(NotImplementedError, self.get_form_field)
else:
self.assertMultiLineEqual(
self.rendered_widget,
self.get_form_field().widget.render('test_field', None))


class Test001StringFieldRender(_FieldRenderTestCase):
field_class = StringField
rendered_widget = \
'<textarea rows="10" cols="40" name="test_field"></textarea>'


class Test002IntFieldRender(_FieldRenderTestCase):
field_class = IntField


class Test003FloatFieldRender(_FieldRenderTestCase):
field_class = FloatField


class Test004BooleanFieldRender(_FieldRenderTestCase):
field_class = BooleanField
rendered_widget = \
'<input type="checkbox" name="test_field" />'


class Test005DateTimeFieldRender(_FieldRenderTestCase):
field_class = DateTimeField


class Test006EmbeddedDocumentFieldRender(_FieldRenderTestCase):
is_not_implemented = True

def get_field(self):

class TestEmbeddedDocument(EmbeddedDocument):
pass

class TestDocument(Document):
test_field = EmbeddedDocumentField(TestEmbeddedDocument)

return TestDocument._fields['test_field']


class Test007ListFieldRender(_FieldRenderTestCase):
field_class = ListField
is_not_implemented = True


class Test008DictFieldRender(_FieldRenderTestCase):
field_class = DictField
is_not_implemented = True


class Test009ObjectIdFieldRender(_FieldRenderTestCase):
field_class = ObjectIdField
is_not_implemented = True


class Test010ReferenceFieldRender(_FieldRenderTestCase):
rendered_widget = \
'<select name="test_field">\n</select>'

def get_field(self):

class TestDocument(Document):
test_field = ReferenceField('self')

return TestDocument._fields['test_field']


class Test011MapFieldRender(_FieldRenderTestCase):
is_not_implemented = True

def get_field(self):

class TestDocument(Document):
test_field = MapField(StringField())

return TestDocument._fields['test_field']


class Test012DecimalFieldRender(_FieldRenderTestCase):
field_class = DecimalField


class Test013ComplexDateTimeFieldRender(_FieldRenderTestCase):
field_class = ComplexDateTimeField
is_not_implemented = True


class Test014URLFieldRender(_FieldRenderTestCase):
field_class = URLField


class Test015GenericReferenceFieldRender(_FieldRenderTestCase):
field_class = GenericReferenceField
is_not_implemented = True


class Test016FileFieldRender(_FieldRenderTestCase):
field_class = FileField
is_not_implemented = True


class Test017BinaryFieldRender(_FieldRenderTestCase):
field_class = BinaryField
is_not_implemented = True


class Test018SortedListFieldRender(_FieldRenderTestCase):
is_not_implemented = True

def get_field(self):

class TestDocument(Document):
test_field = SortedListField(StringField)

return TestDocument._fields['test_field']


class Test019EmailFieldRender(_FieldRenderTestCase):
field_class = EmailField


class Test020GeoPointFieldRender(_FieldRenderTestCase):
field_class = GeoPointField
is_not_implemented = True


class Test021ImageFieldRender(_FieldRenderTestCase):
field_class = ImageField
is_not_implemented = True


class Test022SequenceFieldRender(_FieldRenderTestCase):
field_class = SequenceField
is_not_implemented = True


class Test023UUIDFieldRender(_FieldRenderTestCase):
field_class = UUIDField
is_not_implemented = True


class Test024GenericEmbeddedDocumentFieldRender(_FieldRenderTestCase):
field_class = GenericEmbeddedDocumentField
is_not_implemented = True
198 changes: 198 additions & 0 deletions testprj/testapp/tests/fields/validate.py
@@ -0,0 +1,198 @@
from decimal import Decimal

from mongoengine import Document, EmbeddedDocument
from mongoengine.fields import *

from mongoforms.fields import MongoFormFieldGenerator

from testprj.tests import MongoengineTestCase


class _FieldValidateTestCase(MongoengineTestCase):

# mongoengine field instance to test
field_class = None
# list of correct sample field values before and after clean
correct_samples = ()
# list of incorrect sample field values before clean
incorrect_samples = ()
# hook for not implemented fields
is_not_implemented = False

def setUp(self):
self.generator = MongoFormFieldGenerator()

def get_field(self):

class TestDocument(Document):
test_field = self.field_class()

return TestDocument._fields['test_field']

def get_form_field(self):
return self.generator.generate('test_field', self.get_field())

def runTest(self):

# skip test as we have already tested this in render tests
if self.is_not_implemented:
return

# test for correct samples
for dirty_value, clean_value in self.correct_samples:
self.assertEqual(
clean_value,
self.get_form_field().validate(dirty_value))

# test for incorrect samples
for value in self.incorrect_samples:
self.assertRaises(
ValidationError,
lambda: self.get_form_field().validate(value))


class Test001StringFieldValidate(_FieldValidateTestCase):
field_class = StringField
correct_samples = [('test value', None)]


class Test002IntFieldValidate(_FieldValidateTestCase):
field_class = IntField
correct_samples = [('42', None)]


class Test003FloatFieldValidate(_FieldValidateTestCase):
field_class = FloatField
correct_samples = [('3.14', None)]


class Test004BooleanFieldValidate(_FieldValidateTestCase):
field_class = BooleanField
correct_samples = [('1', None), ('0', None)]


class Test005DateTimeFieldValidate(_FieldValidateTestCase):
field_class = DateTimeField
correct_samples = [('1970-01-02 03:04:05.678901', None)]


class Test006EmbeddedDocumentFieldValidate(_FieldValidateTestCase):
is_not_implemented = True

def get_field(self):

class TestEmbeddedDocument(EmbeddedDocument):
pass

class TestDocument(Document):
test_field = EmbeddedDocumentField(TestEmbeddedDocument)

return TestDocument._fields['test_field']


class Test007ListFieldValidate(_FieldValidateTestCase):
field_class = ListField
is_not_implemented = True


class Test008DictFieldValidate(_FieldValidateTestCase):
field_class = DictField
is_not_implemented = True


class Test009ObjectIdFieldValidate(_FieldValidateTestCase):
field_class = ObjectIdField
is_not_implemented = True


class Test010ReferenceFieldValidate(_FieldValidateTestCase):
correct_samples = []

def get_field(self):

class TestDocument(Document):
test_field = ReferenceField('self')

return TestDocument._fields['test_field']


class Test011MapFieldValidate(_FieldValidateTestCase):
is_not_implemented = True

def get_field(self):

class TestDocument(Document):
test_field = MapField(StringField())

return TestDocument._fields['test_field']


class Test012DecimalFieldValidate(_FieldValidateTestCase):
field_class = DecimalField
correct_samples = [(Decimal('3.14'), Decimal('3.14'))]


class Test013ComplexDateTimeFieldValidate(_FieldValidateTestCase):
field_class = ComplexDateTimeField
is_not_implemented = True


class Test014URLFieldValidate(_FieldValidateTestCase):
field_class = URLField
correct_samples = [('http://www.example.com/', None)]


class Test015GenericReferenceFieldValidate(_FieldValidateTestCase):
field_class = GenericReferenceField
is_not_implemented = True


class Test016FileFieldValidate(_FieldValidateTestCase):
field_class = FileField
is_not_implemented = True


class Test017BinaryFieldValidate(_FieldValidateTestCase):
field_class = BinaryField
is_not_implemented = True


class Test018SortedListFieldValidate(_FieldValidateTestCase):
is_not_implemented = True

def get_field(self):

class TestDocument(Document):
test_field = SortedListField(StringField)

return TestDocument._fields['test_field']


class Test019EmailFieldValidate(_FieldValidateTestCase):
field_class = EmailField
correct_samples = [('user@example.com', None)]


class Test020GeoPointFieldValidate(_FieldValidateTestCase):
field_class = GeoPointField
is_not_implemented = True


class Test021ImageFieldValidate(_FieldValidateTestCase):
field_class = ImageField
is_not_implemented = True


class Test022SequenceFieldValidate(_FieldValidateTestCase):
field_class = SequenceField
is_not_implemented = True


class Test023UUIDFieldValidate(_FieldValidateTestCase):
field_class = UUIDField
is_not_implemented = True


class Test024GenericEmbeddedDocumentFieldValidate(_FieldValidateTestCase):
field_class = GenericEmbeddedDocumentField
is_not_implemented = True

0 comments on commit cfb6217

Please sign in to comment.