Updated Unit Tests for Field Choices of Documents

- Added Unit Test with Invalid EmbeddedDocument Choice.
- Updated Broken Link in Author's File
This commit is contained in:
Matthew Ellison 2015-01-12 10:11:42 -05:00
parent 1a24d599b3
commit 213a0a18a5
2 changed files with 36 additions and 6 deletions

View File

@ -215,4 +215,4 @@ that much better:
* André Ericson https://github.com/aericson) * André Ericson https://github.com/aericson)
* Mikhail Moshnogorsky (https://github.com/mikhailmoshnogorsky) * Mikhail Moshnogorsky (https://github.com/mikhailmoshnogorsky)
* Diego Berrocal (https://github.com/cestdiego) * Diego Berrocal (https://github.com/cestdiego)
* Matthew Ellison (https://github.com/mmelliso) * Matthew Ellison (https://github.com/seglberg)

View File

@ -2448,8 +2448,7 @@ class FieldTest(unittest.TestCase):
def test_choices_validation_documents(self): def test_choices_validation_documents(self):
""" """
Ensure that a field with choices which are documents are Ensure fields with document choices validate given a valid choice.
validated correctly.
""" """
class UserComments(EmbeddedDocument): class UserComments(EmbeddedDocument):
author = StringField() author = StringField()
@ -2460,20 +2459,50 @@ class FieldTest(unittest.TestCase):
GenericEmbeddedDocumentField(choices=(UserComments,)) GenericEmbeddedDocumentField(choices=(UserComments,))
) )
# Ensure Validation Passes
BlogPost(comments=[ BlogPost(comments=[
UserComments(author='user2', message='message2'), UserComments(author='user2', message='message2'),
]).save() ]).save()
def test_choices_validation_documents_invalid(self):
"""
Ensure fields with document choices validate given an invalid choice.
This should throw a ValidationError exception.
"""
class UserComments(EmbeddedDocument):
author = StringField()
message = StringField()
class ModeratorComments(EmbeddedDocument):
author = StringField()
message = StringField()
class BlogPost(Document):
comments = ListField(
GenericEmbeddedDocumentField(choices=(UserComments,))
)
# Single Entry Failure
post = BlogPost(comments=[
ModeratorComments(author='mod1', message='message1'),
])
self.assertRaises(ValidationError, post.save)
# Mixed Entry Failure
post = BlogPost(comments=[
ModeratorComments(author='mod1', message='message1'),
UserComments(author='user2', message='message2'),
])
self.assertRaises(ValidationError, post.save)
def test_choices_validation_documents_inheritance(self): def test_choices_validation_documents_inheritance(self):
""" """
Ensure that a field with choices which are documents are Ensure fields with document choices validate given subclass of choice.
validated correctly when a subclass of a valid choice is used.
""" """
class Comments(EmbeddedDocument): class Comments(EmbeddedDocument):
meta = { meta = {
'abstract': True 'abstract': True
} }
author = StringField() author = StringField()
message = StringField() message = StringField()
@ -2485,6 +2514,7 @@ class FieldTest(unittest.TestCase):
GenericEmbeddedDocumentField(choices=(Comments,)) GenericEmbeddedDocumentField(choices=(Comments,))
) )
# Save Valid EmbeddedDocument Type
BlogPost(comments=[ BlogPost(comments=[
UserComments(author='user2', message='message2'), UserComments(author='user2', message='message2'),
]).save() ]).save()