def test_fastapi_dependency_custom_uri(fastapi_app: FastAPI, test_client: TestClient): EngineD = AIOEngineDependency("mongodb://*****:*****@fastapi_app.get("/") async def get(engine: AIOEngine = EngineD): assert isinstance(engine, AIOEngine) class M(Model): ... await engine.find(M) response = test_client.get("/") assert response.status_code == 200
def test_fastapi_dependency_with_fastapi(fastapi_app: FastAPI, test_client: TestClient): EngineD = AIOEngineDependency() @fastapi_app.get("/") async def get(engine: AIOEngine = EngineD): assert isinstance(engine, AIOEngine) class M(Model): ... await engine.find(M) response = test_client.get("/") assert response.status_code == 200
def test_fastapi_dependency_deprecation_warning(): with pytest.warns(DeprecationWarning, match="the AIOEngineDependency object is deprecated"): AIOEngineDependency()
async def test_fastapi_dependency_cache_logic(): my_dep = AIOEngineDependency(TEST_MONGO_URI) engine1 = await my_dep() engine2 = await my_dep() assert engine1 is engine2
async def test_fastapi_dependency_custom_database_name(): my_dep = AIOEngineDependency(TEST_MONGO_URI, database="mydb") engine = await my_dep() assert engine.database_name == "mydb"
async def test_fastapi_dependency_return_value(): my_dep = AIOEngineDependency() engine = await my_dep() assert isinstance(engine, AIOEngine)
import uvicorn from fastapi import FastAPI, HTTPException from odmantic import AIOEngine, Model, ObjectId from odmantic.fastapi import AIOEngineDependency class Tree(Model): name: str average_size: float discovery_year: int app = FastAPI() EngineD = AIOEngineDependency() @app.put("/trees/", response_model=Tree) async def create_tree(tree: Tree, engine: AIOEngine = EngineD): await engine.save(tree) return tree @app.get("/trees/", response_model=List[Tree]) async def get_trees(engine: AIOEngine = EngineD): trees = await engine.find(Tree) return trees @app.get("/trees/count", response_model=int)