Coverage for docs_src/dependency_testing/tutorial001_an_py310.py: 100%
28 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Annotated 1abc
3from fastapi import Depends, FastAPI 1abc
4from fastapi.testclient import TestClient 1abc
6app = FastAPI() 1abc
9async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100): 1abc
10 return {"q": q, "skip": skip, "limit": limit} 1abc
13@app.get("/items/") 1abc
14async def read_items(commons: Annotated[dict, Depends(common_parameters)]): 1abc
15 return {"message": "Hello Items!", "params": commons} 1abc
18@app.get("/users/") 1abc
19async def read_users(commons: Annotated[dict, Depends(common_parameters)]): 1abc
20 return {"message": "Hello Users!", "params": commons} 1abc
23client = TestClient(app) 1abc
26async def override_dependency(q: str | None = None): 1abc
27 return {"q": q, "skip": 5, "limit": 10} 1abc
30app.dependency_overrides[common_parameters] = override_dependency 1abc
33def test_override_in_items(): 1abc
34 response = client.get("/items/") 1abc
35 assert response.status_code == 200 1abc
36 assert response.json() == { 1abc
37 "message": "Hello Items!",
38 "params": {"q": None, "skip": 5, "limit": 10},
39 }
42def test_override_in_items_with_q(): 1abc
43 response = client.get("/items/?q=foo") 1abc
44 assert response.status_code == 200 1abc
45 assert response.json() == { 1abc
46 "message": "Hello Items!",
47 "params": {"q": "foo", "skip": 5, "limit": 10},
48 }
51def test_override_in_items_with_params(): 1abc
52 response = client.get("/items/?q=foo&skip=100&limit=200") 1abc
53 assert response.status_code == 200 1abc
54 assert response.json() == { 1abc
55 "message": "Hello Items!",
56 "params": {"q": "foo", "skip": 5, "limit": 10},
57 }