fastapi

Форк
0
/
test_param_in_path_and_dependency.py 
93 строки · 3.1 Кб
1
from fastapi import Depends, FastAPI
2
from fastapi.testclient import TestClient
3

4
app = FastAPI()
5

6

7
async def user_exists(user_id: int):
8
    return True
9

10

11
@app.get("/users/{user_id}", dependencies=[Depends(user_exists)])
12
async def read_users(user_id: int):
13
    pass
14

15

16
client = TestClient(app)
17

18

19
def test_read_users():
20
    response = client.get("/users/42")
21
    assert response.status_code == 200, response.text
22

23

24
def test_openapi_schema():
25
    response = client.get("/openapi.json")
26
    data = response.json()
27
    assert data == {
28
        "openapi": "3.1.0",
29
        "info": {"title": "FastAPI", "version": "0.1.0"},
30
        "paths": {
31
            "/users/{user_id}": {
32
                "get": {
33
                    "summary": "Read Users",
34
                    "operationId": "read_users_users__user_id__get",
35
                    "parameters": [
36
                        {
37
                            "required": True,
38
                            "schema": {"title": "User Id", "type": "integer"},
39
                            "name": "user_id",
40
                            "in": "path",
41
                        },
42
                    ],
43
                    "responses": {
44
                        "200": {
45
                            "description": "Successful Response",
46
                            "content": {"application/json": {"schema": {}}},
47
                        },
48
                        "422": {
49
                            "description": "Validation Error",
50
                            "content": {
51
                                "application/json": {
52
                                    "schema": {
53
                                        "$ref": "#/components/schemas/HTTPValidationError"
54
                                    }
55
                                }
56
                            },
57
                        },
58
                    },
59
                }
60
            }
61
        },
62
        "components": {
63
            "schemas": {
64
                "HTTPValidationError": {
65
                    "title": "HTTPValidationError",
66
                    "type": "object",
67
                    "properties": {
68
                        "detail": {
69
                            "title": "Detail",
70
                            "type": "array",
71
                            "items": {"$ref": "#/components/schemas/ValidationError"},
72
                        }
73
                    },
74
                },
75
                "ValidationError": {
76
                    "title": "ValidationError",
77
                    "required": ["loc", "msg", "type"],
78
                    "type": "object",
79
                    "properties": {
80
                        "loc": {
81
                            "title": "Location",
82
                            "type": "array",
83
                            "items": {
84
                                "anyOf": [{"type": "string"}, {"type": "integer"}]
85
                            },
86
                        },
87
                        "msg": {"title": "Message", "type": "string"},
88
                        "type": {"title": "Error Type", "type": "string"},
89
                    },
90
                },
91
            }
92
        },
93
    }
94

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

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

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

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