| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
from ...utils import needs_py310 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture( |
| 11 |
name="client", |
| 12 |
params=[ |
| 13 |
pytest.param("tutorial001_py310", marks=needs_py310), |
| 14 |
], |
| 15 |
) |
| 16 |
def get_client(request: pytest.FixtureRequest): |
| 17 |
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") |
| 18 |
|
| 19 |
client = TestClient(mod.app) |
| 20 |
return client |
| 21 |
|
| 22 |
|
| 23 |
def test_post_body_example(client: TestClient): |
| 24 |
response = client.put( |
| 25 |
"/items/5", |
| 26 |
json={ |
| 27 |
"name": "Foo", |
| 28 |
"description": "A very nice Item", |
| 29 |
"price": 35.4, |
| 30 |
"tax": 3.2, |
| 31 |
}, |
| 32 |
) |
| 33 |
assert response.status_code == 200 |
| 34 |
|
| 35 |
|
| 36 |
def test_openapi_schema(client: TestClient): |
| 37 |
response = client.get("/openapi.json") |
| 38 |
assert response.status_code == 200, response.text |
| 39 |
assert response.json() == snapshot( |
| 40 |
{ |
| 41 |
"openapi": "3.1.0", |
| 42 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 43 |
"paths": { |
| 44 |
"/items/{item_id}": { |
| 45 |
"put": { |
| 46 |
"summary": "Update Item", |
| 47 |
"operationId": "update_item_items__item_id__put", |
| 48 |
"parameters": [ |
| 49 |
{ |
| 50 |
"name": "item_id", |
| 51 |
"in": "path", |
| 52 |
"required": True, |
| 53 |
"schema": {"type": "integer", "title": "Item Id"}, |
| 54 |
} |
| 55 |
], |
| 56 |
"requestBody": { |
| 57 |
"required": True, |
| 58 |
"content": { |
| 59 |
"application/json": { |
| 60 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 61 |
} |
| 62 |
}, |
| 63 |
}, |
| 64 |
"responses": { |
| 65 |
"200": { |
| 66 |
"description": "Successful Response", |
| 67 |
"content": {"application/json": {"schema": {}}}, |
| 68 |
}, |
| 69 |
"422": { |
| 70 |
"description": "Validation Error", |
| 71 |
"content": { |
| 72 |
"application/json": { |
| 73 |
"schema": { |
| 74 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 75 |
} |
| 76 |
} |
| 77 |
}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
} |
| 81 |
} |
| 82 |
}, |
| 83 |
"components": { |
| 84 |
"schemas": { |
| 85 |
"HTTPValidationError": { |
| 86 |
"properties": { |
| 87 |
"detail": { |
| 88 |
"items": { |
| 89 |
"$ref": "#/components/schemas/ValidationError" |
| 90 |
}, |
| 91 |
"type": "array", |
| 92 |
"title": "Detail", |
| 93 |
} |
| 94 |
}, |
| 95 |
"type": "object", |
| 96 |
"title": "HTTPValidationError", |
| 97 |
}, |
| 98 |
"Item": { |
| 99 |
"properties": { |
| 100 |
"name": {"type": "string", "title": "Name"}, |
| 101 |
"description": { |
| 102 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 103 |
"title": "Description", |
| 104 |
}, |
| 105 |
"price": {"type": "number", "title": "Price"}, |
| 106 |
"tax": { |
| 107 |
"anyOf": [{"type": "number"}, {"type": "null"}], |
| 108 |
"title": "Tax", |
| 109 |
}, |
| 110 |
}, |
| 111 |
"type": "object", |
| 112 |
"required": ["name", "price"], |
| 113 |
"title": "Item", |
| 114 |
"examples": [ |
| 115 |
{ |
| 116 |
"description": "A very nice Item", |
| 117 |
"name": "Foo", |
| 118 |
"price": 35.4, |
| 119 |
"tax": 3.2, |
| 120 |
} |
| 121 |
], |
| 122 |
}, |
| 123 |
"ValidationError": { |
| 124 |
"properties": { |
| 125 |
"ctx": {"title": "Context", "type": "object"}, |
| 126 |
"input": {"title": "Input"}, |
| 127 |
"loc": { |
| 128 |
"items": { |
| 129 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 130 |
}, |
| 131 |
"type": "array", |
| 132 |
"title": "Location", |
| 133 |
}, |
| 134 |
"msg": {"type": "string", "title": "Message"}, |
| 135 |
"type": {"type": "string", "title": "Error Type"}, |
| 136 |
}, |
| 137 |
"type": "object", |
| 138 |
"required": ["loc", "msg", "type"], |
| 139 |
"title": "ValidationError", |
| 140 |
}, |
| 141 |
} |
| 142 |
}, |
| 143 |
} |
| 144 |
) |