Coverage for tests/test_dependency_class.py: 100%
61 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 AsyncGenerator, Generator 1abcde
3import pytest 1abcde
4from fastapi import Depends, FastAPI 1abcde
5from fastapi.testclient import TestClient 1abcde
7app = FastAPI() 1abcde
10class CallableDependency: 1abcde
11 def __call__(self, value: str) -> str: 1abcde
12 return value 1abcde
15class CallableGenDependency: 1abcde
16 def __call__(self, value: str) -> Generator[str, None, None]: 1abcde
17 yield value 1abcde
20class AsyncCallableDependency: 1abcde
21 async def __call__(self, value: str) -> str: 1abcde
22 return value 1abcde
25class AsyncCallableGenDependency: 1abcde
26 async def __call__(self, value: str) -> AsyncGenerator[str, None]: 1abcde
27 yield value 1abcde
30class MethodsDependency: 1abcde
31 def synchronous(self, value: str) -> str: 1abcde
32 return value 1abcde
34 async def asynchronous(self, value: str) -> str: 1abcde
35 return value 1abcde
37 def synchronous_gen(self, value: str) -> Generator[str, None, None]: 1abcde
38 yield value 1abcde
40 async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]: 1abcde
41 yield value 1abcde
44callable_dependency = CallableDependency() 1abcde
45callable_gen_dependency = CallableGenDependency() 1abcde
46async_callable_dependency = AsyncCallableDependency() 1abcde
47async_callable_gen_dependency = AsyncCallableGenDependency() 1abcde
48methods_dependency = MethodsDependency() 1abcde
51@app.get("/callable-dependency") 1abcde
52async def get_callable_dependency(value: str = Depends(callable_dependency)): 1abcde
53 return value 1abcde
56@app.get("/callable-gen-dependency") 1abcde
57async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)): 1abcde
58 return value 1abcde
61@app.get("/async-callable-dependency") 1abcde
62async def get_async_callable_dependency( 1abcde
63 value: str = Depends(async_callable_dependency),
64):
65 return value 1abcde
68@app.get("/async-callable-gen-dependency") 1abcde
69async def get_async_callable_gen_dependency( 1abcde
70 value: str = Depends(async_callable_gen_dependency),
71):
72 return value 1abcde
75@app.get("/synchronous-method-dependency") 1abcde
76async def get_synchronous_method_dependency( 1abcde
77 value: str = Depends(methods_dependency.synchronous),
78):
79 return value 1abcde
82@app.get("/synchronous-method-gen-dependency") 1abcde
83async def get_synchronous_method_gen_dependency( 1abcde
84 value: str = Depends(methods_dependency.synchronous_gen),
85):
86 return value 1abcde
89@app.get("/asynchronous-method-dependency") 1abcde
90async def get_asynchronous_method_dependency( 1abcde
91 value: str = Depends(methods_dependency.asynchronous),
92):
93 return value 1abcde
96@app.get("/asynchronous-method-gen-dependency") 1abcde
97async def get_asynchronous_method_gen_dependency( 1abcde
98 value: str = Depends(methods_dependency.asynchronous_gen),
99):
100 return value 1abcde
103client = TestClient(app) 1abcde
106@pytest.mark.parametrize( 1abcde
107 "route,value",
108 [
109 ("/callable-dependency", "callable-dependency"),
110 ("/callable-gen-dependency", "callable-gen-dependency"),
111 ("/async-callable-dependency", "async-callable-dependency"),
112 ("/async-callable-gen-dependency", "async-callable-gen-dependency"),
113 ("/synchronous-method-dependency", "synchronous-method-dependency"),
114 ("/synchronous-method-gen-dependency", "synchronous-method-gen-dependency"),
115 ("/asynchronous-method-dependency", "asynchronous-method-dependency"),
116 ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"),
117 ],
118)
119def test_class_dependency(route, value): 1abcde
120 response = client.get(route, params={"value": value}) 1abcde
121 assert response.status_code == 200, response.text 1abcde
122 assert response.json() == value 1abcde