async def test_find_document_field_not_set_with_default(engine: AIOEngine): class M(Model): field: Optional[str] = None await engine.get_collection(M).insert_one({"_id": ObjectId()}) gathered = await engine.find_one(M) assert gathered is not None assert gathered.field is None
async def test_find_document_field_not_set_with_default_factory_disabled( engine: AIOEngine, ): class M(Model): field: str = Field(default_factory=lambda: "hello") # pragma: no cover await engine.get_collection(M).insert_one({"_id": ObjectId()}) with pytest.raises(DocumentParsingError, match="key not found in document"): await engine.find_one(M)
async def test_find_document_field_not_set_with_default_field_descriptor( engine: AIOEngine, ): class M(Model): field: str = Field(default="hello world") await engine.get_collection(M).insert_one({"_id": ObjectId()}) gathered = await engine.find_one(M) assert gathered is not None assert gathered.field == "hello world"
async def test_save_multiple_time_same_document(engine: AIOEngine): fixed_id = ObjectId() instance = PersonModel(first_name="Jean-Pierre", last_name="Pernaud", id=fixed_id) await engine.save(instance) instance = PersonModel(first_name="Jean-Pierre", last_name="Pernaud", id=fixed_id) await engine.save(instance) assert await engine.count(PersonModel, PersonModel.id == fixed_id) == 1
async def test_find_document_field_not_set_with_default_factory_enabled( engine: AIOEngine, ): class M(Model): field: str = Field(default_factory=lambda: "hello") class Config: parse_doc_with_default_factories = True await engine.get_collection(M).insert_one({"_id": ObjectId()}) instance = await engine.find_one(M) assert instance is not None assert instance.field == "hello"
async def test_find_document_field_not_set_with_no_default(engine: AIOEngine): class M(Model): field: str await engine.get_collection(M).insert_one({"_id": ObjectId()}) with pytest.raises(DocumentParsingError, match="key not found in document") as exc_info: await engine.find_one(M) assert ( "1 validation error for M\n" "field\n" " key not found in document " "(type=value_error.keynotfoundindocument; key_name='field')") in str( exc_info.value)
async def test_get_audio(): data = AudioFactory().build() await data.save() request_data = json.loads(data.json()) async with AsyncClient(app=app, base_url="http://test") as ac: path = f"/{data.audioFileType}/{data.audioFileMetadata.id}" response = await ac.get(path) assert response.status_code == 200 audio_id = ObjectId(response.json()['id']) created_audio = await Audio.find_by_id(data.audioFileType, audio_id) assert created_audio created_audio_json = json.loads(created_audio.json()) for key in request_data.keys(): assert request_data[key] == created_audio_json[key]
async def test_create_audio(): data = AudioFactory().build() request_data = json.loads(data.json()) async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.post("/", json=request_data) assert response.status_code == 200 audio_id = ObjectId(response.json()['id']) # import pudb; pudb.set_trace() created_audio = await Audio.find_by_id(data.audioFileType, audio_id) created_audio_json = json.loads(created_audio.json()) assert created_audio for key in request_data.keys(): if key == 'id': continue assert request_data[key] == created_audio_json[key]
async def test_reference_not_set_in_database(engine: AIOEngine): class R(Model): field: int class M(Model): r: R = Reference() await engine.get_collection(M).insert_one({"_id": ObjectId()}) with pytest.raises(DocumentParsingError) as exc_info: await engine.find_one(M) assert ( "1 validation error for M\n" "r\n" " referenced document not found " "(type=value_error.referenceddocumentnotfound; foreign_key_name='r')" ) in str(exc_info.value)
from datetime import datetime from odmantic.bson import BSON_TYPES_ENCODERS, BaseBSONModel, ObjectId class M(BaseBSONModel): id: ObjectId date: datetime class Config: json_encoders = { **BSON_TYPES_ENCODERS, datetime: lambda dt: dt.year, } print(M(id=ObjectId(), date=datetime.utcnow()).json()) #> {"id": "5fa3378c8fde3766574d874d", "date": 2020}