Coverage for tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py: 100%
43 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from dirty_equals import IsDict 1ghijkl
2from fastapi.testclient import TestClient 1ghijkl
3from sqlmodel import create_engine 1ghijkl
4from sqlmodel.pool import StaticPool 1ghijkl
7def test_tutorial(clear_sqlmodel): 1ghijkl
8 from docs_src.tutorial.fastapi.delete import tutorial001 as mod 1fabcde
10 mod.sqlite_url = "sqlite://" 1fabcde
11 mod.engine = create_engine( 1fabcde
12 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
13 )
15 with TestClient(mod.app) as client: 1fabcde
16 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1fabcde
17 hero2_data = { 1abcde
18 "name": "Spider-Boy",
19 "secret_name": "Pedro Parqueador",
20 "id": 9000,
21 }
22 hero3_data = { 1abcde
23 "name": "Rusty-Man",
24 "secret_name": "Tommy Sharp",
25 "age": 48,
26 }
27 response = client.post("/heroes/", json=hero1_data) 1fabcde
28 assert response.status_code == 200, response.text 1fabcde
29 response = client.post("/heroes/", json=hero2_data) 1fabcde
30 assert response.status_code == 200, response.text 1fabcde
31 hero2 = response.json() 1fabcde
32 hero2_id = hero2["id"] 1fabcde
33 response = client.post("/heroes/", json=hero3_data) 1fabcde
34 assert response.status_code == 200, response.text 1fabcde
35 response = client.get(f"/heroes/{hero2_id}") 1fabcde
36 assert response.status_code == 200, response.text 1fabcde
37 response = client.get("/heroes/9000") 1fabcde
38 assert response.status_code == 404, response.text 1fabcde
39 response = client.get("/heroes/") 1fabcde
40 assert response.status_code == 200, response.text 1fabcde
41 data = response.json() 1fabcde
42 assert len(data) == 3 1fabcde
43 response = client.patch( 1fabcde
44 f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
45 )
46 assert response.status_code == 200, response.text 1fabcde
47 response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"}) 1fabcde
48 assert response.status_code == 404, response.text 1fabcde
50 response = client.delete(f"/heroes/{hero2_id}") 1fabcde
51 assert response.status_code == 200, response.text 1fabcde
52 response = client.get("/heroes/") 1fabcde
53 assert response.status_code == 200, response.text 1fabcde
54 data = response.json() 1fabcde
55 assert len(data) == 2 1fabcde
57 response = client.delete("/heroes/9000") 1fabcde
58 assert response.status_code == 404, response.text 1fabcde
60 response = client.get("/openapi.json") 1fabcde
61 assert response.status_code == 200, response.text 1fabcde
62 assert response.json() == { 1fabcde
63 "openapi": "3.1.0",
64 "info": {"title": "FastAPI", "version": "0.1.0"},
65 "paths": {
66 "/heroes/": {
67 "get": {
68 "summary": "Read Heroes",
69 "operationId": "read_heroes_heroes__get",
70 "parameters": [
71 {
72 "required": False,
73 "schema": {
74 "title": "Offset",
75 "type": "integer",
76 "default": 0,
77 },
78 "name": "offset",
79 "in": "query",
80 },
81 {
82 "required": False,
83 "schema": {
84 "title": "Limit",
85 "maximum": 100.0,
86 "type": "integer",
87 "default": 100,
88 },
89 "name": "limit",
90 "in": "query",
91 },
92 ],
93 "responses": {
94 "200": {
95 "description": "Successful Response",
96 "content": {
97 "application/json": {
98 "schema": {
99 "title": "Response Read Heroes Heroes Get",
100 "type": "array",
101 "items": {
102 "$ref": "#/components/schemas/HeroPublic"
103 },
104 }
105 }
106 },
107 },
108 "422": {
109 "description": "Validation Error",
110 "content": {
111 "application/json": {
112 "schema": {
113 "$ref": "#/components/schemas/HTTPValidationError"
114 }
115 }
116 },
117 },
118 },
119 },
120 "post": {
121 "summary": "Create Hero",
122 "operationId": "create_hero_heroes__post",
123 "requestBody": {
124 "content": {
125 "application/json": {
126 "schema": {
127 "$ref": "#/components/schemas/HeroCreate"
128 }
129 }
130 },
131 "required": True,
132 },
133 "responses": {
134 "200": {
135 "description": "Successful Response",
136 "content": {
137 "application/json": {
138 "schema": {
139 "$ref": "#/components/schemas/HeroPublic"
140 }
141 }
142 },
143 },
144 "422": {
145 "description": "Validation Error",
146 "content": {
147 "application/json": {
148 "schema": {
149 "$ref": "#/components/schemas/HTTPValidationError"
150 }
151 }
152 },
153 },
154 },
155 },
156 },
157 "/heroes/{hero_id}": {
158 "get": {
159 "summary": "Read Hero",
160 "operationId": "read_hero_heroes__hero_id__get",
161 "parameters": [
162 {
163 "required": True,
164 "schema": {"title": "Hero Id", "type": "integer"},
165 "name": "hero_id",
166 "in": "path",
167 }
168 ],
169 "responses": {
170 "200": {
171 "description": "Successful Response",
172 "content": {
173 "application/json": {
174 "schema": {
175 "$ref": "#/components/schemas/HeroPublic"
176 }
177 }
178 },
179 },
180 "422": {
181 "description": "Validation Error",
182 "content": {
183 "application/json": {
184 "schema": {
185 "$ref": "#/components/schemas/HTTPValidationError"
186 }
187 }
188 },
189 },
190 },
191 },
192 "delete": {
193 "summary": "Delete Hero",
194 "operationId": "delete_hero_heroes__hero_id__delete",
195 "parameters": [
196 {
197 "required": True,
198 "schema": {"title": "Hero Id", "type": "integer"},
199 "name": "hero_id",
200 "in": "path",
201 }
202 ],
203 "responses": {
204 "200": {
205 "description": "Successful Response",
206 "content": {"application/json": {"schema": {}}},
207 },
208 "422": {
209 "description": "Validation Error",
210 "content": {
211 "application/json": {
212 "schema": {
213 "$ref": "#/components/schemas/HTTPValidationError"
214 }
215 }
216 },
217 },
218 },
219 },
220 "patch": {
221 "summary": "Update Hero",
222 "operationId": "update_hero_heroes__hero_id__patch",
223 "parameters": [
224 {
225 "required": True,
226 "schema": {"title": "Hero Id", "type": "integer"},
227 "name": "hero_id",
228 "in": "path",
229 }
230 ],
231 "requestBody": {
232 "content": {
233 "application/json": {
234 "schema": {
235 "$ref": "#/components/schemas/HeroUpdate"
236 }
237 }
238 },
239 "required": True,
240 },
241 "responses": {
242 "200": {
243 "description": "Successful Response",
244 "content": {
245 "application/json": {
246 "schema": {
247 "$ref": "#/components/schemas/HeroPublic"
248 }
249 }
250 },
251 },
252 "422": {
253 "description": "Validation Error",
254 "content": {
255 "application/json": {
256 "schema": {
257 "$ref": "#/components/schemas/HTTPValidationError"
258 }
259 }
260 },
261 },
262 },
263 },
264 },
265 },
266 "components": {
267 "schemas": {
268 "HTTPValidationError": {
269 "title": "HTTPValidationError",
270 "type": "object",
271 "properties": {
272 "detail": {
273 "title": "Detail",
274 "type": "array",
275 "items": {
276 "$ref": "#/components/schemas/ValidationError"
277 },
278 }
279 },
280 },
281 "HeroCreate": {
282 "title": "HeroCreate",
283 "required": ["name", "secret_name"],
284 "type": "object",
285 "properties": {
286 "name": {"title": "Name", "type": "string"},
287 "secret_name": {"title": "Secret Name", "type": "string"},
288 "age": IsDict(
289 {
290 "title": "Age",
291 "anyOf": [{"type": "integer"}, {"type": "null"}],
292 }
293 )
294 | IsDict(
295 # TODO: remove when deprecating Pydantic v1
296 {"title": "Age", "type": "integer"}
297 ),
298 },
299 },
300 "HeroPublic": {
301 "title": "HeroPublic",
302 "required": ["name", "secret_name", "id"],
303 "type": "object",
304 "properties": {
305 "name": {"title": "Name", "type": "string"},
306 "secret_name": {"title": "Secret Name", "type": "string"},
307 "age": IsDict(
308 {
309 "title": "Age",
310 "anyOf": [{"type": "integer"}, {"type": "null"}],
311 }
312 )
313 | IsDict(
314 # TODO: remove when deprecating Pydantic v1
315 {"title": "Age", "type": "integer"}
316 ),
317 "id": {"title": "Id", "type": "integer"},
318 },
319 },
320 "HeroUpdate": {
321 "title": "HeroUpdate",
322 "type": "object",
323 "properties": {
324 "name": IsDict(
325 {
326 "title": "Name",
327 "anyOf": [{"type": "string"}, {"type": "null"}],
328 }
329 )
330 | IsDict(
331 # TODO: remove when deprecating Pydantic v1
332 {"title": "Name", "type": "string"}
333 ),
334 "secret_name": IsDict(
335 {
336 "title": "Secret Name",
337 "anyOf": [{"type": "string"}, {"type": "null"}],
338 }
339 )
340 | IsDict(
341 # TODO: remove when deprecating Pydantic v1
342 {"title": "Secret Name", "type": "string"}
343 ),
344 "age": IsDict(
345 {
346 "title": "Age",
347 "anyOf": [{"type": "integer"}, {"type": "null"}],
348 }
349 )
350 | IsDict(
351 # TODO: remove when deprecating Pydantic v1
352 {"title": "Age", "type": "integer"}
353 ),
354 },
355 },
356 "ValidationError": {
357 "title": "ValidationError",
358 "required": ["loc", "msg", "type"],
359 "type": "object",
360 "properties": {
361 "loc": {
362 "title": "Location",
363 "type": "array",
364 "items": {
365 "anyOf": [{"type": "string"}, {"type": "integer"}]
366 },
367 },
368 "msg": {"title": "Message", "type": "string"},
369 "type": {"title": "Error Type", "type": "string"},
370 },
371 },
372 }
373 },
374 }