Coverage for docs_src/dependency_testing/tutorial001.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 Union 1abcde
3from fastapi import Depends, FastAPI 1abcde
4from fastapi.testclient import TestClient 1abcde
6app = FastAPI() 1abcde
9async def common_parameters( 1abcde
10 q: Union[str, None] = None, skip: int = 0, limit: int = 100
11):
12 return {"q": q, "skip": skip, "limit": limit} 1abcde
15@app.get("/items/") 1abcde
16async def read_items(commons: dict = Depends(common_parameters)): 1abcde
17 return {"message": "Hello Items!", "params": commons} 1abcde
20@app.get("/users/") 1abcde
21async def read_users(commons: dict = Depends(common_parameters)): 1abcde
22 return {"message": "Hello Users!", "params": commons} 1abcde
25client = TestClient(app) 1abcde
28async def override_dependency(q: Union[str, None] = None): 1abcde
29 return {"q": q, "skip": 5, "limit": 10} 1abcde
32app.dependency_overrides[common_parameters] = override_dependency 1abcde
35def test_override_in_items(): 1abcde
36 response = client.get("/items/") 1abcde
37 assert response.status_code == 200 1abcde
38 assert response.json() == { 1abcde
39 "message": "Hello Items!",
40 "params": {"q": None, "skip": 5, "limit": 10},
41 }
44def test_override_in_items_with_q(): 1abcde
45 response = client.get("/items/?q=foo") 1abcde
46 assert response.status_code == 200 1abcde
47 assert response.json() == { 1abcde
48 "message": "Hello Items!",
49 "params": {"q": "foo", "skip": 5, "limit": 10},
50 }
53def test_override_in_items_with_params(): 1abcde
54 response = client.get("/items/?q=foo&skip=100&limit=200") 1abcde
55 assert response.status_code == 200 1abcde
56 assert response.json() == { 1abcde
57 "message": "Hello Items!",
58 "params": {"q": "foo", "skip": 5, "limit": 10},
59 }