1
from fastapi import FastAPI
2
from fastapi.testclient import TestClient
3
from pydantic import BaseModel
8
class Product(BaseModel):
10
description: str = None # type: ignore
15
async def create_item(product: Product):
19
client = TestClient(app)
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
28
def test_openapi_schema():
29
response = client.get("/openapi.json")
30
assert response.status_code == 200, response.text
31
assert response.json() == {
33
"info": {"title": "FastAPI", "version": "0.1.0"},
37
"summary": "Create Item",
38
"operationId": "create_item_product_get",
42
"schema": {"$ref": "#/components/schemas/Product"}
49
"description": "Successful Response",
50
"content": {"application/json": {"schema": {}}},
53
"description": "Validation Error",
57
"$ref": "#/components/schemas/HTTPValidationError"
68
"HTTPValidationError": {
69
"title": "HTTPValidationError",
75
"items": {"$ref": "#/components/schemas/ValidationError"},
81
"required": ["name", "price"],
84
"name": {"title": "Name", "type": "string"},
85
"description": {"title": "Description", "type": "string"},
86
"price": {"title": "Price", "type": "number"},
90
"title": "ValidationError",
91
"required": ["loc", "msg", "type"],
98
"anyOf": [{"type": "string"}, {"type": "integer"}]
101
"msg": {"title": "Message", "type": "string"},
102
"type": {"title": "Error Type", "type": "string"},