async def test_save_not_found(document_not_inserted): document_not_inserted.id = PydanticObjectId() await document_not_inserted.save() assert (hasattr(document_not_inserted, "id") and document_not_inserted.id is not None) from_db = await DocumentTestModel.get(document_not_inserted.id) assert from_db == document_not_inserted
async def test_find_one(documents): inserted_one = await documents(1, "kipasa") await documents(10, "smthe else") expected_doc_id = PydanticObjectId(inserted_one[0]) new_document = await DocumentTestModel.find_one({"test_str": "kipasa"}) assert new_document.id == expected_doc_id
async def test_replace_many_not_all_the_docs_found(documents): await documents(10, "foo") created_documents = await DocumentTestModel.find_many({ "test_str": "foo" }).to_list() to_replace = [] created_documents[0].id = PydanticObjectId() for document in created_documents[:5]: document.test_str = "REPLACED_VALUE" to_replace.append(document) with pytest.raises(ReplaceError): await DocumentTestModel.replace_many(to_replace)
async def create(self, session: ClientSession = None) -> "Document": """ Create the document in the database :return: Document """ if self.id is not None: raise DocumentAlreadyCreated result = await self.get_motor_collection().insert_one( self.dict(by_alias=True, exclude={"id"}), session=session ) self.id = PydanticObjectId(result.inserted_id) return self
async def test_replace_not_found(document_not_inserted): document_not_inserted.id = PydanticObjectId() with pytest.raises(DocumentNotFound): await document_not_inserted.replace()
def test_pydantic_object_id_bytes_input(): p = PydanticObjectId() m = M(p=str(p).encode("utf-8")) assert m.p == p with pytest.raises(ValidationError): M(p=b"test")
async def test_get_not_found(document): new_document = await DocumentTestModel.get(PydanticObjectId()) assert new_document is None