Coverage for docs_src/dependency_testing/tutorial001_py310.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import Depends, FastAPI 1abc

2from fastapi.testclient import TestClient 1abc

3 

4app = FastAPI() 1abc

5 

6 

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

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

9 

10 

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

12async def read_items(commons: dict = Depends(common_parameters)): 1abc

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

14 

15 

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

17async def read_users(commons: dict = Depends(common_parameters)): 1abc

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

19 

20 

21client = TestClient(app) 1abc

22 

23 

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

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

26 

27 

28app.dependency_overrides[common_parameters] = override_dependency 1abc

29 

30 

31def test_override_in_items(): 1abc

32 response = client.get("/items/") 1abc

33 assert response.status_code == 200 1abc

34 assert response.json() == { 1abc

35 "message": "Hello Items!", 

36 "params": {"q": None, "skip": 5, "limit": 10}, 

37 } 

38 

39 

40def test_override_in_items_with_q(): 1abc

41 response = client.get("/items/?q=foo") 1abc

42 assert response.status_code == 200 1abc

43 assert response.json() == { 1abc

44 "message": "Hello Items!", 

45 "params": {"q": "foo", "skip": 5, "limit": 10}, 

46 } 

47 

48 

49def test_override_in_items_with_params(): 1abc

50 response = client.get("/items/?q=foo&skip=100&limit=200") 1abc

51 assert response.status_code == 200 1abc

52 assert response.json() == { 1abc

53 "message": "Hello Items!", 

54 "params": {"q": "foo", "skip": 5, "limit": 10}, 

55 }