fastapi

Форк
0
/
test_dependency_class.py 
122 строки · 3.3 Кб
1
from typing 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")
52
async def get_callable_dependency(value: str = Depends(callable_dependency)):
53
    return value
54

55

56
@app.get("/callable-gen-dependency")
57
async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)):
58
    return value
59

60

61
@app.get("/async-callable-dependency")
62
async def get_async_callable_dependency(
63
    value: str = Depends(async_callable_dependency),
64
):
65
    return value
66

67

68
@app.get("/async-callable-gen-dependency")
69
async def get_async_callable_gen_dependency(
70
    value: str = Depends(async_callable_gen_dependency),
71
):
72
    return value
73

74

75
@app.get("/synchronous-method-dependency")
76
async def get_synchronous_method_dependency(
77
    value: str = Depends(methods_dependency.synchronous),
78
):
79
    return value
80

81

82
@app.get("/synchronous-method-gen-dependency")
83
async def get_synchronous_method_gen_dependency(
84
    value: str = Depends(methods_dependency.synchronous_gen),
85
):
86
    return value
87

88

89
@app.get("/asynchronous-method-dependency")
90
async def get_asynchronous_method_dependency(
91
    value: str = Depends(methods_dependency.asynchronous),
92
):
93
    return value
94

95

96
@app.get("/asynchronous-method-gen-dependency")
97
async def get_asynchronous_method_gen_dependency(
98
    value: str = Depends(methods_dependency.asynchronous_gen),
99
):
100
    return value
101

102

103
client = TestClient(app)
104

105

106
@pytest.mark.parametrize(
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
)
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
123

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.