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