Coverage for tests/test_tutorial/test_body_updates/test_tutorial001.py: 100%
26 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from fastapi.testclient import TestClient 1abcdef
6from ...utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2 1abcdef
9@pytest.fixture( 1abcdef
10 name="client",
11 params=[
12 "tutorial001",
13 pytest.param("tutorial001_py310", marks=needs_py310),
14 pytest.param("tutorial001_py39", marks=needs_py39),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.body_updates.{request.param}") 1abcdef
20 client = TestClient(mod.app) 1abcdef
21 return client 1abcdef
24def test_get(client: TestClient): 1abcdef
25 response = client.get("/items/baz") 1ghijkl
26 assert response.status_code == 200, response.text 1ghijkl
27 assert response.json() == { 1ghijkl
28 "name": "Baz",
29 "description": None,
30 "price": 50.2,
31 "tax": 10.5,
32 "tags": [],
33 }
36def test_put(client: TestClient): 1abcdef
37 response = client.put( 1yzABCD
38 "/items/bar", json={"name": "Barz", "price": 3, "description": None}
39 )
40 assert response.json() == { 1yzABCD
41 "name": "Barz",
42 "description": None,
43 "price": 3,
44 "tax": 10.5,
45 "tags": [],
46 }
49@needs_pydanticv2 1abcdef
50def test_openapi_schema(client: TestClient): 1abcdef
51 response = client.get("/openapi.json") 1mnopqr
52 assert response.status_code == 200, response.text 1mnopqr
53 assert response.json() == { 1mnopqr
54 "openapi": "3.1.0",
55 "info": {"title": "FastAPI", "version": "0.1.0"},
56 "paths": {
57 "/items/{item_id}": {
58 "get": {
59 "responses": {
60 "200": {
61 "description": "Successful Response",
62 "content": {
63 "application/json": {
64 "schema": {"$ref": "#/components/schemas/Item"}
65 }
66 },
67 },
68 "422": {
69 "description": "Validation Error",
70 "content": {
71 "application/json": {
72 "schema": {
73 "$ref": "#/components/schemas/HTTPValidationError"
74 }
75 }
76 },
77 },
78 },
79 "summary": "Read Item",
80 "operationId": "read_item_items__item_id__get",
81 "parameters": [
82 {
83 "required": True,
84 "schema": {"title": "Item Id", "type": "string"},
85 "name": "item_id",
86 "in": "path",
87 }
88 ],
89 },
90 "put": {
91 "responses": {
92 "200": {
93 "description": "Successful Response",
94 "content": {
95 "application/json": {
96 "schema": {"$ref": "#/components/schemas/Item"}
97 }
98 },
99 },
100 "422": {
101 "description": "Validation Error",
102 "content": {
103 "application/json": {
104 "schema": {
105 "$ref": "#/components/schemas/HTTPValidationError"
106 }
107 }
108 },
109 },
110 },
111 "summary": "Update Item",
112 "operationId": "update_item_items__item_id__put",
113 "parameters": [
114 {
115 "required": True,
116 "schema": {"title": "Item Id", "type": "string"},
117 "name": "item_id",
118 "in": "path",
119 }
120 ],
121 "requestBody": {
122 "content": {
123 "application/json": {
124 "schema": {"$ref": "#/components/schemas/Item"}
125 }
126 },
127 "required": True,
128 },
129 },
130 }
131 },
132 "components": {
133 "schemas": {
134 "Item": {
135 "type": "object",
136 "title": "Item",
137 "properties": {
138 "name": {
139 "anyOf": [{"type": "string"}, {"type": "null"}],
140 "title": "Name",
141 },
142 "description": {
143 "anyOf": [{"type": "string"}, {"type": "null"}],
144 "title": "Description",
145 },
146 "price": {
147 "anyOf": [{"type": "number"}, {"type": "null"}],
148 "title": "Price",
149 },
150 "tax": {"title": "Tax", "type": "number", "default": 10.5},
151 "tags": {
152 "title": "Tags",
153 "type": "array",
154 "items": {"type": "string"},
155 "default": [],
156 },
157 },
158 },
159 "ValidationError": {
160 "title": "ValidationError",
161 "required": ["loc", "msg", "type"],
162 "type": "object",
163 "properties": {
164 "loc": {
165 "title": "Location",
166 "type": "array",
167 "items": {
168 "anyOf": [{"type": "string"}, {"type": "integer"}]
169 },
170 },
171 "msg": {"title": "Message", "type": "string"},
172 "type": {"title": "Error Type", "type": "string"},
173 },
174 },
175 "HTTPValidationError": {
176 "title": "HTTPValidationError",
177 "type": "object",
178 "properties": {
179 "detail": {
180 "title": "Detail",
181 "type": "array",
182 "items": {"$ref": "#/components/schemas/ValidationError"},
183 }
184 },
185 },
186 }
187 },
188 }
191# TODO: remove when deprecating Pydantic v1
192@needs_pydanticv1 1abcdef
193def test_openapi_schema_pv1(client: TestClient): 1abcdef
194 response = client.get("/openapi.json") 1stuvwx
195 assert response.status_code == 200, response.text 1stuvwx
196 assert response.json() == { 1stuvwx
197 "openapi": "3.1.0",
198 "info": {"title": "FastAPI", "version": "0.1.0"},
199 "paths": {
200 "/items/{item_id}": {
201 "get": {
202 "responses": {
203 "200": {
204 "description": "Successful Response",
205 "content": {
206 "application/json": {
207 "schema": {"$ref": "#/components/schemas/Item"}
208 }
209 },
210 },
211 "422": {
212 "description": "Validation Error",
213 "content": {
214 "application/json": {
215 "schema": {
216 "$ref": "#/components/schemas/HTTPValidationError"
217 }
218 }
219 },
220 },
221 },
222 "summary": "Read Item",
223 "operationId": "read_item_items__item_id__get",
224 "parameters": [
225 {
226 "required": True,
227 "schema": {"title": "Item Id", "type": "string"},
228 "name": "item_id",
229 "in": "path",
230 }
231 ],
232 },
233 "put": {
234 "responses": {
235 "200": {
236 "description": "Successful Response",
237 "content": {
238 "application/json": {
239 "schema": {"$ref": "#/components/schemas/Item"}
240 }
241 },
242 },
243 "422": {
244 "description": "Validation Error",
245 "content": {
246 "application/json": {
247 "schema": {
248 "$ref": "#/components/schemas/HTTPValidationError"
249 }
250 }
251 },
252 },
253 },
254 "summary": "Update Item",
255 "operationId": "update_item_items__item_id__put",
256 "parameters": [
257 {
258 "required": True,
259 "schema": {"title": "Item Id", "type": "string"},
260 "name": "item_id",
261 "in": "path",
262 }
263 ],
264 "requestBody": {
265 "content": {
266 "application/json": {
267 "schema": {"$ref": "#/components/schemas/Item"}
268 }
269 },
270 "required": True,
271 },
272 },
273 }
274 },
275 "components": {
276 "schemas": {
277 "Item": {
278 "title": "Item",
279 "type": "object",
280 "properties": {
281 "name": {"title": "Name", "type": "string"},
282 "description": {"title": "Description", "type": "string"},
283 "price": {"title": "Price", "type": "number"},
284 "tax": {"title": "Tax", "type": "number", "default": 10.5},
285 "tags": {
286 "title": "Tags",
287 "type": "array",
288 "items": {"type": "string"},
289 "default": [],
290 },
291 },
292 },
293 "ValidationError": {
294 "title": "ValidationError",
295 "required": ["loc", "msg", "type"],
296 "type": "object",
297 "properties": {
298 "loc": {
299 "title": "Location",
300 "type": "array",
301 "items": {
302 "anyOf": [{"type": "string"}, {"type": "integer"}]
303 },
304 },
305 "msg": {"title": "Message", "type": "string"},
306 "type": {"title": "Error Type", "type": "string"},
307 },
308 },
309 "HTTPValidationError": {
310 "title": "HTTPValidationError",
311 "type": "object",
312 "properties": {
313 "detail": {
314 "title": "Detail",
315 "type": "array",
316 "items": {"$ref": "#/components/schemas/ValidationError"},
317 }
318 },
319 },
320 }
321 },
322 }