def test_to_son(self): field = EmbeddedDocumentField(db_field="test", embedded_document_type=User) u = User(name="test") expect(field.to_son(u)).to_be_like({"name": "test"})
def test_from_son(self): field = EmbeddedDocumentField(db_field="test", embedded_document_type=User) user = field.from_son({"name": "test2"}) expect(user).to_be_instance_of(User) expect(user.name).to_equal("test2")
class Post(Document): __collection__ = "posts" title = StringField(required=True) text = StringField(required=True, db_field="content") category = ReferenceField(reference_document_type=Category) comments = ListField(EmbeddedDocumentField(embedded_document_type=Comment))
class User(Document): email = StringField(required=True) first_name = StringField(db_field="whatever", max_length=50, default=lambda: "Bernardo") last_name = StringField(max_length=50, default="Heynemann") is_admin = BooleanField(default=True) website = URLField(default="http://google.com/") updated_at = DateTimeField(required=True, auto_now_on_insert=True, auto_now_on_update=True) embedded = EmbeddedDocumentField(EmbeddedDocument, db_field="embedded_document") nullable = EmbeddedDocumentField(EmbeddedDocument, db_field="nullable_embedded_document") numbers = ListField(IntField()) def __repr__(self): return "%s %s <%s>" % (self.first_name, self.last_name, self.email)
def test_cant_create_embedded_field_of_wrong_embedded_type(self): try: EmbeddedDocumentField(embedded_document_type=10).validate(None) except ValueError: err = sys.exc_info()[1] expect(err).to_have_an_error_message_of( "The field 'embedded_document_type' argument must be a subclass of Document, not '10'." ) else: assert False, "Should not have gotten this far"
class EmbeddedDocument(Document): test = StringField(db_field="other", required=True) embedded2 = EmbeddedDocumentField(EmbeddedDocument2)
class Post(Document): title = StringField(required=True) body = StringField(required=True) comments = ListField(EmbeddedDocumentField(Comment))
def test_create_embedded_document_field_with_string_type(self): field = EmbeddedDocumentField( embedded_document_type= "tests.fields.test_embedded_document_field.User") expect(field.embedded_type).to_equal(User)
def test_create_embedded_document_field(self): field = EmbeddedDocumentField(db_field="test", embedded_document_type=User) expect(field.db_field).to_equal("test") expect(field._embedded_document_type).to_equal(User)
class Test(Document): __collection__ = "TestEmbeddedParent" test = EmbeddedDocumentField(TestEmbedded)
class RawQueryDocument(Document): items = ListField(EmbeddedDocumentField(RawQueryEmbeddedDocument))
class ElemMatchEmbeddedParentDocument(Document): items = ListField(EmbeddedDocumentField(ElemMatchEmbeddedDocument))
class Doc(Document): embedded = EmbeddedDocumentField(embedded_document_type=Model, required=True)
class User(Document): name = EmbeddedDocumentField(embedded_document_type=Name) email = StringField(db_field="email_address") numbers = ListField(base_field=IntField())