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

1from typing import Annotated 1abc

2 

3from fastapi import Depends, FastAPI 1abc

4from fastapi.testclient import TestClient 1abc

5 

6app = FastAPI() 1abc

7 

8 

9async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100): 1abc

10 return {"q": q, "skip": skip, "limit": limit} 1abc

11 

12 

13@app.get("/items/") 1abc

14async def read_items(commons: Annotated[dict, Depends(common_parameters)]): 1abc

15 return {"message": "Hello Items!", "params": commons} 1abc

16 

17 

18@app.get("/users/") 1abc

19async def read_users(commons: Annotated[dict, Depends(common_parameters)]): 1abc

20 return {"message": "Hello Users!", "params": commons} 1abc

21 

22 

23client = TestClient(app) 1abc

24 

25 

26async def override_dependency(q: str | None = None): 1abc

27 return {"q": q, "skip": 5, "limit": 10} 1abc

28 

29 

30app.dependency_overrides[common_parameters] = override_dependency 1abc

31 

32 

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 } 

40 

41 

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 } 

49 

50 

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 }