Coverage for tests/test_dependency_cache.py: 100%

54 statements  

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

1from fastapi import Depends, FastAPI, Security 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4app = FastAPI() 1abcde

5 

6counter_holder = {"counter": 0} 1abcde

7 

8 

9async def dep_counter(): 1abcde

10 counter_holder["counter"] += 1 1abcde

11 return counter_holder["counter"] 1abcde

12 

13 

14async def super_dep(count: int = Depends(dep_counter)): 1abcde

15 return count 1abcde

16 

17 

18@app.get("/counter/") 1abcde

19async def get_counter(count: int = Depends(dep_counter)): 1abcde

20 return {"counter": count} 1abcde

21 

22 

23@app.get("/sub-counter/") 1abcde

24async def get_sub_counter( 1abcde

25 subcount: int = Depends(super_dep), count: int = Depends(dep_counter) 

26): 

27 return {"counter": count, "subcounter": subcount} 1abcde

28 

29 

30@app.get("/sub-counter-no-cache/") 1abcde

31async def get_sub_counter_no_cache( 1abcde

32 subcount: int = Depends(super_dep), 

33 count: int = Depends(dep_counter, use_cache=False), 

34): 

35 return {"counter": count, "subcounter": subcount} 1abcde

36 

37 

38@app.get("/scope-counter") 1abcde

39async def get_scope_counter( 1abcde

40 count: int = Security(dep_counter), 

41 scope_count_1: int = Security(dep_counter, scopes=["scope"]), 

42 scope_count_2: int = Security(dep_counter, scopes=["scope"]), 

43): 

44 return { 1abcde

45 "counter": count, 

46 "scope_counter_1": scope_count_1, 

47 "scope_counter_2": scope_count_2, 

48 } 

49 

50 

51client = TestClient(app) 1abcde

52 

53 

54def test_normal_counter(): 1abcde

55 counter_holder["counter"] = 0 1abcde

56 response = client.get("/counter/") 1abcde

57 assert response.status_code == 200, response.text 1abcde

58 assert response.json() == {"counter": 1} 1abcde

59 response = client.get("/counter/") 1abcde

60 assert response.status_code == 200, response.text 1abcde

61 assert response.json() == {"counter": 2} 1abcde

62 

63 

64def test_sub_counter(): 1abcde

65 counter_holder["counter"] = 0 1abcde

66 response = client.get("/sub-counter/") 1abcde

67 assert response.status_code == 200, response.text 1abcde

68 assert response.json() == {"counter": 1, "subcounter": 1} 1abcde

69 response = client.get("/sub-counter/") 1abcde

70 assert response.status_code == 200, response.text 1abcde

71 assert response.json() == {"counter": 2, "subcounter": 2} 1abcde

72 

73 

74def test_sub_counter_no_cache(): 1abcde

75 counter_holder["counter"] = 0 1abcde

76 response = client.get("/sub-counter-no-cache/") 1abcde

77 assert response.status_code == 200, response.text 1abcde

78 assert response.json() == {"counter": 2, "subcounter": 1} 1abcde

79 response = client.get("/sub-counter-no-cache/") 1abcde

80 assert response.status_code == 200, response.text 1abcde

81 assert response.json() == {"counter": 4, "subcounter": 3} 1abcde

82 

83 

84def test_security_cache(): 1abcde

85 counter_holder["counter"] = 0 1abcde

86 response = client.get("/scope-counter/") 1abcde

87 assert response.status_code == 200, response.text 1abcde

88 assert response.json() == {"counter": 1, "scope_counter_1": 2, "scope_counter_2": 2} 1abcde

89 response = client.get("/scope-counter/") 1abcde

90 assert response.status_code == 200, response.text 1abcde

91 assert response.json() == {"counter": 3, "scope_counter_1": 4, "scope_counter_2": 4} 1abcde