def test_from_son(self): field = ReferenceField(db_field="test", reference_document_type=User) data = ObjectId("123456789012123456789012") result = field.from_son(data) expect(result).to_equal(ObjectId("123456789012123456789012"))
def test_to_son(self): field = ReferenceField(db_field="test", reference_document_type=User) u = User(name="test") u._id = ObjectId("123456789012123456789012") result = field.to_son(u) expect(str(result)).to_equal(str(u._id))
class User(Document): __collection__ = 'users' index = IntField(required=True) 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()) posts = ListField(ReferenceField(reference_document_type=Post)) def __repr__(self): return "%s %s <%s>" % (self.first_name, self.last_name, self.email)
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))
def test_cant_create_reference_field_of_the_wrong_type(self): try: ReferenceField(reference_document_type=10).validate(None) except ValueError: err = sys.exc_info()[1] expected = "The field 'reference_document_type' argument must be a subclass of Document, not '10'." expect(err).to_have_an_error_message_of(expected) else: assert False, "Should not have gotten this far"
class CommentNotLazy(Document): __lazy__ = False text = StringField(required=True) user = ReferenceField(User, required=True)
class Comment(Document): text = StringField(required=True) user = ReferenceField(User, required=True)
class Parent(Document): __collection__ = "EmbeddedIsNullTestParent" child = ReferenceField(Child)
class Test(Document): __collection__ = "EmbeddedExistsTestParent" test = ReferenceField(Test2)
class ReferenceFieldClass(Document): __collection__ = "TestFindAllReferenceField" ref1 = ReferenceField(User) num = IntField(default=10)
class ReferenceFieldClass(Document): ref1 = ReferenceField(User) ref2 = ReferenceField(User) ref3 = ReferenceField(User)
class Base(Document): __collection__ = 'base' __lazy__ = False list_val = ListField(ReferenceField(reference_document_type=Ref))
def test_create_reference_field_with_string_class(self): field = ReferenceField( db_field="test", reference_document_type= "tests.aiomotorengine.fields.test_reference_field.User") expect(field.reference_type).to_equal(User)
def test_create_reference_field(self): field = ReferenceField(db_field="test", reference_document_type=User) expect(field.db_field).to_equal("test") expect(field.reference_type).to_equal(User)