Got EmbeddedDocumentFields working

Added validation on EmbeddedDocumentField, added tests for
EmbeddedDocumentFields.
This commit is contained in:
Harry Marr
2009-11-17 01:32:40 +00:00
parent 48418d5a60
commit 5a87534c22
5 changed files with 46 additions and 5 deletions

View File

@@ -65,6 +65,28 @@ class FieldTest(unittest.TestCase):
self.assertRaises(ValidationError, person.__setattr__, 'age', 120)
self.assertRaises(ValidationError, person.__setattr__, 'age', 'ten')
def test_embedded_document_validation(self):
"""Ensure that invalid embedded documents cannot be assigned to
embedded document fields.
"""
class Comment(EmbeddedDocument):
content = StringField()
class PersonPreferences(EmbeddedDocument):
food = StringField()
number = IntField()
class Person(Document):
name = StringField()
preferences = EmbeddedDocumentField(PersonPreferences)
person = Person(name='Test User')
self.assertRaises(ValidationError, person.__setattr__, 'preferences',
'My preferences')
self.assertRaises(ValidationError, person.__setattr__, 'preferences',
Comment(content='Nice blog post...'))
person.preferences = PersonPreferences(food='Cheese', number=47)
if __name__ == '__main__':
unittest.main()