Coverage for tests / test_tutorial / test_schema_extra_example / test_tutorial005.py: 100%
17 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial005_py310", marks=needs_py310),
14 pytest.param("tutorial005_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_post_body_example(client: TestClient): 1abdc
25 response = client.put( 1hij
26 "/items/5",
27 json={
28 "name": "Foo",
29 "description": "A very nice Item",
30 "price": 35.4,
31 "tax": 3.2,
32 },
33 )
34 assert response.status_code == 200 1hij
37def test_openapi_schema(client: TestClient) -> None: 1abdc
38 response = client.get("/openapi.json") 1efg
39 assert response.status_code == 200, response.text 1efg
40 assert response.json() == snapshot( 1efg
41 {
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/items/{item_id}": {
46 "put": {
47 "summary": "Update Item",
48 "operationId": "update_item_items__item_id__put",
49 "parameters": [
50 {
51 "required": True,
52 "schema": {"title": "Item Id", "type": "integer"},
53 "name": "item_id",
54 "in": "path",
55 }
56 ],
57 "requestBody": {
58 "content": {
59 "application/json": {
60 "schema": {"$ref": "#/components/schemas/Item"},
61 "examples": {
62 "normal": {
63 "summary": "A normal example",
64 "description": "A **normal** item works correctly.",
65 "value": {
66 "name": "Foo",
67 "description": "A very nice Item",
68 "price": 35.4,
69 "tax": 3.2,
70 },
71 },
72 "converted": {
73 "summary": "An example with converted data",
74 "description": "FastAPI can convert price `strings` to actual `numbers` automatically",
75 "value": {"name": "Bar", "price": "35.4"},
76 },
77 "invalid": {
78 "summary": "Invalid data is rejected with an error",
79 "value": {
80 "name": "Baz",
81 "price": "thirty five point four",
82 },
83 },
84 },
85 }
86 },
87 "required": True,
88 },
89 "responses": {
90 "200": {
91 "description": "Successful Response",
92 "content": {"application/json": {"schema": {}}},
93 },
94 "422": {
95 "description": "Validation Error",
96 "content": {
97 "application/json": {
98 "schema": {
99 "$ref": "#/components/schemas/HTTPValidationError"
100 }
101 }
102 },
103 },
104 },
105 }
106 }
107 },
108 "components": {
109 "schemas": {
110 "HTTPValidationError": {
111 "title": "HTTPValidationError",
112 "type": "object",
113 "properties": {
114 "detail": {
115 "title": "Detail",
116 "type": "array",
117 "items": {
118 "$ref": "#/components/schemas/ValidationError"
119 },
120 }
121 },
122 },
123 "Item": {
124 "title": "Item",
125 "required": ["name", "price"],
126 "type": "object",
127 "properties": {
128 "name": {"title": "Name", "type": "string"},
129 "description": {
130 "title": "Description",
131 "anyOf": [{"type": "string"}, {"type": "null"}],
132 },
133 "price": {"title": "Price", "type": "number"},
134 "tax": {
135 "title": "Tax",
136 "anyOf": [{"type": "number"}, {"type": "null"}],
137 },
138 },
139 },
140 "ValidationError": {
141 "title": "ValidationError",
142 "required": ["loc", "msg", "type"],
143 "type": "object",
144 "properties": {
145 "loc": {
146 "title": "Location",
147 "type": "array",
148 "items": {
149 "anyOf": [{"type": "string"}, {"type": "integer"}]
150 },
151 },
152 "msg": {"title": "Message", "type": "string"},
153 "type": {"title": "Error Type", "type": "string"},
154 "input": {"title": "Input"},
155 "ctx": {"title": "Context", "type": "object"},
156 },
157 },
158 }
159 },
160 }
161 )