1
from typing import AsyncGenerator, Generator
4
from fastapi import Depends, FastAPI
5
from fastapi.testclient import TestClient
10
class CallableDependency:
11
def __call__(self, value: str) -> str:
15
class CallableGenDependency:
16
def __call__(self, value: str) -> Generator[str, None, None]:
20
class AsyncCallableDependency:
21
async def __call__(self, value: str) -> str:
25
class AsyncCallableGenDependency:
26
async def __call__(self, value: str) -> AsyncGenerator[str, None]:
30
class MethodsDependency:
31
def synchronous(self, value: str) -> str:
34
async def asynchronous(self, value: str) -> str:
37
def synchronous_gen(self, value: str) -> Generator[str, None, None]:
40
async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]:
44
callable_dependency = CallableDependency()
45
callable_gen_dependency = CallableGenDependency()
46
async_callable_dependency = AsyncCallableDependency()
47
async_callable_gen_dependency = AsyncCallableGenDependency()
48
methods_dependency = MethodsDependency()
51
@app.get("/callable-dependency")
52
async def get_callable_dependency(value: str = Depends(callable_dependency)):
56
@app.get("/callable-gen-dependency")
57
async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)):
61
@app.get("/async-callable-dependency")
62
async def get_async_callable_dependency(
63
value: str = Depends(async_callable_dependency),
68
@app.get("/async-callable-gen-dependency")
69
async def get_async_callable_gen_dependency(
70
value: str = Depends(async_callable_gen_dependency),
75
@app.get("/synchronous-method-dependency")
76
async def get_synchronous_method_dependency(
77
value: str = Depends(methods_dependency.synchronous),
82
@app.get("/synchronous-method-gen-dependency")
83
async def get_synchronous_method_gen_dependency(
84
value: str = Depends(methods_dependency.synchronous_gen),
89
@app.get("/asynchronous-method-dependency")
90
async def get_asynchronous_method_dependency(
91
value: str = Depends(methods_dependency.asynchronous),
96
@app.get("/asynchronous-method-gen-dependency")
97
async def get_asynchronous_method_gen_dependency(
98
value: str = Depends(methods_dependency.asynchronous_gen),
103
client = TestClient(app)
106
@pytest.mark.parametrize(
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"),
119
def test_class_dependency(route, value):
120
response = client.get(route, params={"value": value})
121
assert response.status_code == 200, response.text
122
assert response.json() == value