def test_patched_fields(self, db): instance1 = Instance(db) instance2 = Instance(db) class Embedded(EmbeddedDocument): simple = fields.IntField() Embedded1 = instance1.register(Embedded) Embedded2 = instance2.register(Embedded1) class ToRef(Document): pass ToRef1 = instance1.register(ToRef) ToRef2 = instance2.register(ToRef) class Doc(Document): embedded = fields.EmbeddedField(Embedded1) ref = fields.ReferenceField(ToRef1) Doc1 = instance1.register(Doc) Doc2 = instance2.register(Doc) assert issubclass(Doc.embedded.embedded_document, EmbeddedDocumentTemplate) assert issubclass(Doc1.schema.fields['embedded'].embedded_document, EmbeddedDocumentTemplate) assert issubclass(Doc2.schema.fields['embedded'].embedded_document, EmbeddedDocumentTemplate) assert issubclass(Doc1.schema.fields['embedded'].embedded_document_cls, EmbeddedDocumentImplementation) assert issubclass(Doc2.schema.fields['embedded'].embedded_document_cls, EmbeddedDocumentImplementation) assert issubclass(Doc.ref.document, DocumentTemplate) assert issubclass(Doc1.schema.fields['ref'].document, DocumentTemplate) assert issubclass(Doc2.schema.fields['ref'].document, DocumentTemplate) assert issubclass(Doc1.schema.fields['ref'].document_cls, DocumentImplementation) assert issubclass(Doc2.schema.fields['ref'].document_cls, DocumentImplementation) assert Embedded1.schema.fields['simple'].instance is instance1 assert Embedded1.opts.instance is instance1 assert Embedded2.schema.fields['simple'].instance is instance2 assert Embedded2.opts.instance is instance2 assert Doc1.schema.fields['embedded'].instance is instance1 assert Doc1.opts.instance is instance1 assert Doc2.schema.fields['embedded'].instance is instance2 assert Doc2.opts.instance is instance2
def test_register_other_implementation(self, db): instance1 = Instance.from_db(db) instance2 = Instance.from_db(db) class Doc(Document): pass doc_instance1_cls = instance1.register(Doc) doc_instance2_cls = instance2.register(doc_instance1_cls) assert issubclass(doc_instance2_cls, DocumentImplementation) with pytest.raises(AlreadyRegisteredDocumentError): instance2.register(Doc) class Embedded(EmbeddedDocument): pass embedded_instance1_cls = instance1.register(Embedded) embedded_instance2_cls = instance2.register(embedded_instance1_cls) assert issubclass(embedded_instance2_cls, EmbeddedDocumentImplementation) with pytest.raises(AlreadyRegisteredDocumentError): instance2.register(Embedded)
def test_register_other_implementation(self, db): instance1 = Instance(db) instance2 = Instance(db) class Doc(Document): pass doc_instance1_cls = instance1.register(Doc) doc_instance2_cls = instance2.register(doc_instance1_cls) assert issubclass(doc_instance2_cls, DocumentImplementation) with pytest.raises(AlreadyRegisteredDocumentError): instance2.register(Doc) class Embedded(EmbeddedDocument): pass embedded_instance1_cls = instance1.register(Embedded) embedded_instance2_cls = instance2.register(embedded_instance1_cls) assert issubclass(embedded_instance2_cls, EmbeddedDocumentImplementation) with pytest.raises(AlreadyRegisteredDocumentError): instance2.register(Embedded)
def test_multiple_instances(self, db): instance1 = Instance.from_db(db) instance2 = Instance.from_db(db) class Doc(Document): pass class Embedded(EmbeddedDocument): pass Doc1 = instance1.register(Doc) Doc2 = instance2.register(Doc) Embedded1 = instance1.register(Embedded) Embedded2 = instance2.register(Embedded) assert issubclass(Doc1, DocumentImplementation) assert issubclass(Doc2, DocumentImplementation) assert issubclass(Embedded1, EmbeddedDocumentImplementation) assert issubclass(Embedded2, EmbeddedDocumentImplementation) assert Doc1.opts.instance is instance1 assert Doc2.opts.instance is instance2 assert Embedded1.opts.instance is instance1 assert Embedded2.opts.instance is instance2
def test_multiple_instances(self, db): instance1 = Instance(db) instance2 = Instance(db) class Doc(Document): pass class Embedded(EmbeddedDocument): pass Doc1 = instance1.register(Doc) Doc2 = instance2.register(Doc) Embedded1 = instance1.register(Embedded) Embedded2 = instance2.register(Embedded) assert issubclass(Doc1, DocumentImplementation) assert issubclass(Doc2, DocumentImplementation) assert issubclass(Embedded1, EmbeddedDocumentImplementation) assert issubclass(Embedded2, EmbeddedDocumentImplementation) assert Doc1.opts.instance is instance1 assert Doc2.opts.instance is instance2 assert Embedded1.opts.instance is instance1 assert Embedded2.opts.instance is instance2
def instance(db): # `db` should be a fixture provided by the current framework's testbench return Instance(db)
def setup(self): self.instance = Instance(MockedDB('my_moked_db'))