fastapi

Форк
0
/
test_get_request_body.py 
107 строк · 3.7 Кб
1
from fastapi import FastAPI
2
from fastapi.testclient import TestClient
3
from pydantic import BaseModel
4

5
app = FastAPI()
6

7

8
class Product(BaseModel):
9
    name: str
10
    description: str = None  # type: ignore
11
    price: float
12

13

14
@app.get("/product")
15
async def create_item(product: Product):
16
    return product
17

18

19
client = TestClient(app)
20

21

22
def test_get_with_body():
23
    body = {"name": "Foo", "description": "Some description", "price": 5.5}
24
    response = client.request("GET", "/product", json=body)
25
    assert response.json() == body
26

27

28
def test_openapi_schema():
29
    response = client.get("/openapi.json")
30
    assert response.status_code == 200, response.text
31
    assert response.json() == {
32
        "openapi": "3.1.0",
33
        "info": {"title": "FastAPI", "version": "0.1.0"},
34
        "paths": {
35
            "/product": {
36
                "get": {
37
                    "summary": "Create Item",
38
                    "operationId": "create_item_product_get",
39
                    "requestBody": {
40
                        "content": {
41
                            "application/json": {
42
                                "schema": {"$ref": "#/components/schemas/Product"}
43
                            }
44
                        },
45
                        "required": True,
46
                    },
47
                    "responses": {
48
                        "200": {
49
                            "description": "Successful Response",
50
                            "content": {"application/json": {"schema": {}}},
51
                        },
52
                        "422": {
53
                            "description": "Validation Error",
54
                            "content": {
55
                                "application/json": {
56
                                    "schema": {
57
                                        "$ref": "#/components/schemas/HTTPValidationError"
58
                                    }
59
                                }
60
                            },
61
                        },
62
                    },
63
                }
64
            }
65
        },
66
        "components": {
67
            "schemas": {
68
                "HTTPValidationError": {
69
                    "title": "HTTPValidationError",
70
                    "type": "object",
71
                    "properties": {
72
                        "detail": {
73
                            "title": "Detail",
74
                            "type": "array",
75
                            "items": {"$ref": "#/components/schemas/ValidationError"},
76
                        }
77
                    },
78
                },
79
                "Product": {
80
                    "title": "Product",
81
                    "required": ["name", "price"],
82
                    "type": "object",
83
                    "properties": {
84
                        "name": {"title": "Name", "type": "string"},
85
                        "description": {"title": "Description", "type": "string"},
86
                        "price": {"title": "Price", "type": "number"},
87
                    },
88
                },
89
                "ValidationError": {
90
                    "title": "ValidationError",
91
                    "required": ["loc", "msg", "type"],
92
                    "type": "object",
93
                    "properties": {
94
                        "loc": {
95
                            "title": "Location",
96
                            "type": "array",
97
                            "items": {
98
                                "anyOf": [{"type": "string"}, {"type": "integer"}]
99
                            },
100
                        },
101
                        "msg": {"title": "Message", "type": "string"},
102
                        "type": {"title": "Error Type", "type": "string"},
103
                    },
104
                },
105
            }
106
        },
107
    }
108

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

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

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

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