fastapi

Форк
0
/
test_repeated_dependency_schema.py 
102 строки · 3.2 Кб
1
from fastapi import Depends, FastAPI, Header, status
2
from fastapi.testclient import TestClient
3

4
app = FastAPI()
5

6

7
def get_header(*, someheader: str = Header()):
8
    return someheader
9

10

11
def get_something_else(*, someheader: str = Depends(get_header)):
12
    return f"{someheader}123"
13

14

15
@app.get("/")
16
def get_deps(dep1: str = Depends(get_header), dep2: str = Depends(get_something_else)):
17
    return {"dep1": dep1, "dep2": dep2}
18

19

20
client = TestClient(app)
21

22
schema = {
23
    "components": {
24
        "schemas": {
25
            "HTTPValidationError": {
26
                "properties": {
27
                    "detail": {
28
                        "items": {"$ref": "#/components/schemas/ValidationError"},
29
                        "title": "Detail",
30
                        "type": "array",
31
                    }
32
                },
33
                "title": "HTTPValidationError",
34
                "type": "object",
35
            },
36
            "ValidationError": {
37
                "properties": {
38
                    "loc": {
39
                        "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
40
                        "title": "Location",
41
                        "type": "array",
42
                    },
43
                    "msg": {"title": "Message", "type": "string"},
44
                    "type": {"title": "Error " "Type", "type": "string"},
45
                },
46
                "required": ["loc", "msg", "type"],
47
                "title": "ValidationError",
48
                "type": "object",
49
            },
50
        }
51
    },
52
    "info": {"title": "FastAPI", "version": "0.1.0"},
53
    "openapi": "3.1.0",
54
    "paths": {
55
        "/": {
56
            "get": {
57
                "operationId": "get_deps__get",
58
                "parameters": [
59
                    {
60
                        "in": "header",
61
                        "name": "someheader",
62
                        "required": True,
63
                        "schema": {"title": "Someheader", "type": "string"},
64
                    }
65
                ],
66
                "responses": {
67
                    "200": {
68
                        "content": {"application/json": {"schema": {}}},
69
                        "description": "Successful " "Response",
70
                    },
71
                    "422": {
72
                        "content": {
73
                            "application/json": {
74
                                "schema": {
75
                                    "$ref": "#/components/schemas/HTTPValidationError"
76
                                }
77
                            }
78
                        },
79
                        "description": "Validation " "Error",
80
                    },
81
                },
82
                "summary": "Get Deps",
83
            }
84
        }
85
    },
86
}
87

88

89
def test_schema():
90
    response = client.get("/openapi.json")
91
    assert response.status_code == status.HTTP_200_OK
92
    actual_schema = response.json()
93
    assert actual_schema == schema
94
    assert (
95
        len(actual_schema["paths"]["/"]["get"]["parameters"]) == 1
96
    )  # primary goal of this test
97

98

99
def test_response():
100
    response = client.get("/", headers={"someheader": "hello"})
101
    assert response.status_code == status.HTTP_200_OK
102
    assert response.json() == {"dep1": "hello", "dep2": "hello123"}
103

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

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

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

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