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)
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")
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")
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({ '__module__': 'tests.fields.test_embedded_document_field', '__class__': 'User', 'name': 'test' })
def test_from_son(self): field = EmbeddedDocumentField(db_field="test", embedded_document_type=User) user = field.from_son({ '__module__': 'tests.fields.test_embedded_document_field', '__class__': 'User', 'name': 'test2' }) expect(user).to_be_instance_of(User) expect(user.name).to_equal("test2")
def test_validate_enforces_embedded_document_object(self): class Doc(Document): name = StringField() field = EmbeddedDocumentField(embedded_document_type=User) user = User(name='test') doc = Doc(name='test') expect(field.validate(user)).to_be_true() expect(field.validate(doc)).to_be_false() expect(field.validate(None)).to_be_true()
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 FakeUserDocument(Document): __collection__ = "fakeUser" fake_id = StringField() info = EmbeddedDocumentField(FakeUserInfoDocument) critic = BaseField() cluster = StringField() def get_user_name(self) -> str: return self.info.name
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 Lesson(Document): id = IntField() name = StringField(required=True) audience = StringField() teacher_name = StringField() lesson_type = StringField() start_time = StringField() color = StringField() comments = ListField(EmbeddedDocumentField(embedded_document_type=Comment)) day = ReferenceField('DayTimetable')
def test_validate(self): field = EmbeddedDocumentField(User) expect(field.validate(None)).to_be_true() expect(field.validate("String")).to_be_false() expect(field.validate(User())).to_be_true() expect(field.from_son(None)).to_be_null()
class User(Document): def __init__(self, **kwargs): super(User, self).__init__(**kwargs) id = IntField(unique=True) email = EmailField() _salt = BinaryField(12) # 64 is the length of the SHA-256 encoded string length _password = StringField(64) group = ReferenceField('scheduler.model.Group') timetable = EmbeddedDocumentField('scheduler.model.Schedule')
class EmbeddedDocument(Document): test = StringField(db_field="other", required=True) embedded2 = EmbeddedDocumentField(EmbeddedDocument2)
class University(Document): id = IntField() name = StringField(required=True) groups = ListField(EmbeddedDocumentField(Group()))
class Group(Document): id = IntField() name = StringField(required=True) users = ListField(ReferenceField('User')) university = ReferenceField('University') timetable = EmbeddedDocumentField(Schedule())
class Schedule(Document): days = ListField(EmbeddedDocumentField(DayTimetable())) user = ReferenceField(reference_document_type=User) group = ReferenceField('scheduler.model.Group') pass
class DayTimetable(Document): name = StringField(required=True) lessons = ListField(EmbeddedDocumentField(Lesson())) schedule = ReferenceField('scheduler.model.Schedule')
class Doc(Document): embedded = EmbeddedDocumentField( embedded_document_type=Model, required=False )
class User(Document): name = EmbeddedDocumentField(embedded_document_type=Name) email = StringField(db_field='email_address') numbers = ListField(base_field=IntField())
class Test(Document): __collection__ = "TestEmbeddedParent" test = EmbeddedDocumentField(TestEmbedded)
class Post(Document): title = StringField(required=True) body = StringField(required=True) comments = ListField(EmbeddedDocumentField(Comment))
class Action(Document): __collection__ = "actions" action_type = IntField(required=True) payload = EmbeddedDocumentField(embedded_document_type=Document)
class RawQueryDocument(Document): items = ListField(EmbeddedDocumentField(RawQueryEmbeddedDocument))
class ElemMatchEmbeddedParentDocument(Document): items = ListField(EmbeddedDocumentField(ElemMatchEmbeddedDocument))
def test_item_embedded_type(self): class OtherType(Document): pass field = ListField(EmbeddedDocumentField(OtherType)) expect(field.item_type).to_equal(OtherType)
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 EditPatientAction(Document): __collection__ = "edit_patient_actions" patientUid = UUIDField() new_first_name = StringField() new_last_name = StringField() contact_info = EmbeddedDocumentField(embedded_document_type=Document)
def test_validate(self): field = EmbeddedDocumentField(User) expect(field.validate(None)).to_be_true() expect(field.validate("String")).to_be_false() expect(field.validate(User())).to_be_true()
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)