Debian Codegraph @

tests/test_dependency_class.py

raw
1 from collections.abc import AsyncGenerator, Generator
2
3 import pytest
4 from fastapi import Depends, FastAPI
5 from fastapi.testclient import TestClient
6
7 app = FastAPI()
8
9
10 class CallableDependency:
11 def __call__(self, value: str) -> str:
12 return value
13
14
15 class CallableGenDependency:
16 def __call__(self, value: str) -> Generator[str, None, None]:
17 yield value
18
19
20 class AsyncCallableDependency:
21 async def __call__(self, value: str) -> str:
22 return value
23
24
25 class AsyncCallableGenDependency:
26 async def __call__(self, value: str) -> AsyncGenerator[str, None]:
27 yield value
28
29
30 class MethodsDependency:
31 def synchronous(self, value: str) -> str:
32 return value
33
34 async def asynchronous(self, value: str) -> str:
35 return value
36
37 def synchronous_gen(self, value: str) -> Generator[str, None, None]:
38 yield value
39
40 async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]:
41 yield value
42
43
44 callable_dependency = CallableDependency()
45 callable_gen_dependency = CallableGenDependency()
46 async_callable_dependency = AsyncCallableDependency()
47 async_callable_gen_dependency = AsyncCallableGenDependency()
48 methods_dependency = MethodsDependency()
49
50
51 @app.get("/callable-dependency-class")
52 async def get_callable_dependency_class(
53 value: str, instance: CallableDependency = Depends()
54 ):
55 return instance(value)
56
57
58 @app.get("/callable-gen-dependency-class")
59 async def get_callable_gen_dependency_class(
60 value: str, instance: CallableGenDependency = Depends()
61 ):
62 return next(instance(value))
63
64
65 @app.get("/async-callable-dependency-class")
66 async def get_async_callable_dependency_class(
67 value: str, instance: AsyncCallableDependency = Depends()
68 ):
69 return await instance(value)
70
71
72 @app.get("/async-callable-gen-dependency-class")
73 async def get_async_callable_gen_dependency_class(
74 value: str, instance: AsyncCallableGenDependency = Depends()
75 ):
76 return await instance(value).__anext__()
77
78
79 @app.get("/callable-dependency")
80 async def get_callable_dependency(value: str = Depends(callable_dependency)):
81 return value
82
83
84 @app.get("/callable-gen-dependency")
85 async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)):
86 return value
87
88
89 @app.get("/async-callable-dependency")
90 async def get_async_callable_dependency(
91 value: str = Depends(async_callable_dependency),
92 ):
93 return value
94
95
96 @app.get("/async-callable-gen-dependency")
97 async def get_async_callable_gen_dependency(
98 value: str = Depends(async_callable_gen_dependency),
99 ):
100 return value
101
102
103 @app.get("/synchronous-method-dependency")
104 async def get_synchronous_method_dependency(
105 value: str = Depends(methods_dependency.synchronous),
106 ):
107 return value
108
109
110 @app.get("/synchronous-method-gen-dependency")
111 async def get_synchronous_method_gen_dependency(
112 value: str = Depends(methods_dependency.synchronous_gen),
113 ):
114 return value
115
116
117 @app.get("/asynchronous-method-dependency")
118 async def get_asynchronous_method_dependency(
119 value: str = Depends(methods_dependency.asynchronous),
120 ):
121 return value
122
123
124 @app.get("/asynchronous-method-gen-dependency")
125 async def get_asynchronous_method_gen_dependency(
126 value: str = Depends(methods_dependency.asynchronous_gen),
127 ):
128 return value
129
130
131 client = TestClient(app)
132
133
134 @pytest.mark.parametrize(
135 "route,value",
136 [
137 ("/callable-dependency", "callable-dependency"),
138 ("/callable-gen-dependency", "callable-gen-dependency"),
139 ("/async-callable-dependency", "async-callable-dependency"),
140 ("/async-callable-gen-dependency", "async-callable-gen-dependency"),
141 ("/synchronous-method-dependency", "synchronous-method-dependency"),
142 ("/synchronous-method-gen-dependency", "synchronous-method-gen-dependency"),
143 ("/asynchronous-method-dependency", "asynchronous-method-dependency"),
144 ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"),
145 ("/callable-dependency-class", "callable-dependency-class"),
146 ("/callable-gen-dependency-class", "callable-gen-dependency-class"),
147 ("/async-callable-dependency-class", "async-callable-dependency-class"),
148 ("/async-callable-gen-dependency-class", "async-callable-gen-dependency-class"),
149 ],
150 )
151 def test_class_dependency(route, value):
152 response = client.get(route, params={"value": value})
153 assert response.status_code == 200, response.text
154 assert response.json() == value