Got EmbeddedDocumentFields working
Added validation on EmbeddedDocumentField, added tests for EmbeddedDocumentFields.
This commit is contained in:
parent
48418d5a60
commit
5a87534c22
@ -1,8 +1,9 @@
|
|||||||
from document import Document
|
import document
|
||||||
|
from document import *
|
||||||
import fields
|
import fields
|
||||||
from fields import *
|
from fields import *
|
||||||
|
|
||||||
__all__ = fields.__all__ + ['Document']
|
__all__ = document.__all__ + fields.__all__
|
||||||
|
|
||||||
__author__ = 'Harry Marr'
|
__author__ = 'Harry Marr'
|
||||||
__version__ = '0.1'
|
__version__ = '0.1'
|
||||||
|
@ -161,3 +161,8 @@ class BaseDocument(object):
|
|||||||
if name not in self._fields:
|
if name not in self._fields:
|
||||||
raise KeyError(name)
|
raise KeyError(name)
|
||||||
return setattr(self, name, value)
|
return setattr(self, name, value)
|
||||||
|
|
||||||
|
def _to_mongo(self):
|
||||||
|
"""Return data dictionary ready for use with MongoDB.
|
||||||
|
"""
|
||||||
|
return dict((k, v) for k, v in self._data.items() if v is not None)
|
||||||
|
@ -2,6 +2,10 @@ from base import DocumentMetaclass, TopLevelDocumentMetaclass, BaseDocument
|
|||||||
|
|
||||||
#import pymongo
|
#import pymongo
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['Document', 'EmbeddedDocument']
|
||||||
|
|
||||||
|
|
||||||
class EmbeddedDocument(BaseDocument):
|
class EmbeddedDocument(BaseDocument):
|
||||||
|
|
||||||
__metaclass__ = DocumentMetaclass
|
__metaclass__ = DocumentMetaclass
|
||||||
|
@ -4,7 +4,8 @@ from document import EmbeddedDocument
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['StringField', 'IntField', 'ValidationError']
|
__all__ = ['StringField', 'IntField', 'EmbeddedDocumentField',
|
||||||
|
'ValidationError']
|
||||||
|
|
||||||
|
|
||||||
class StringField(BaseField):
|
class StringField(BaseField):
|
||||||
@ -52,7 +53,15 @@ class EmbeddedDocumentField(BaseField):
|
|||||||
|
|
||||||
def __init__(self, document, **kwargs):
|
def __init__(self, document, **kwargs):
|
||||||
if not issubclass(document, EmbeddedDocument):
|
if not issubclass(document, EmbeddedDocument):
|
||||||
raise ValidationError('Invalid embedded document provided to an '
|
raise ValidationError('Invalid embedded document class provided '
|
||||||
'EmbeddedDocumentField')
|
'to an EmbeddedDocumentField')
|
||||||
self.document = document
|
self.document = document
|
||||||
super(EmbeddedDocumentField, self).__init__(**kwargs)
|
super(EmbeddedDocumentField, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def _to_python(self, value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
def _validate(self, value):
|
||||||
|
if not isinstance(value, self.document):
|
||||||
|
raise ValidationError('Invalid embedded document instance '
|
||||||
|
'provided to an EmbeddedDocumentField')
|
||||||
|
@ -65,6 +65,28 @@ class FieldTest(unittest.TestCase):
|
|||||||
self.assertRaises(ValidationError, person.__setattr__, 'age', 120)
|
self.assertRaises(ValidationError, person.__setattr__, 'age', 120)
|
||||||
self.assertRaises(ValidationError, person.__setattr__, 'age', 'ten')
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user