Coverage for tests/test_tutorial/test_sql_databases/test_sql_databases_middleware_py310.py: 100%
89 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 importlib 1deabc
2import os 1deabc
3from pathlib import Path 1deabc
5import pytest 1deabc
6from dirty_equals import IsDict 1deabc
7from fastapi.testclient import TestClient 1deabc
9from ...utils import needs_py310, needs_pydanticv1 1deabc
12@pytest.fixture(scope="module") 1deabc
13def client(tmp_path_factory: pytest.TempPathFactory): 1deabc
14 tmp_path = tmp_path_factory.mktemp("data") 1abc
15 cwd = os.getcwd() 1abc
16 os.chdir(tmp_path) 1abc
17 test_db = Path("./sql_app.db") 1abc
18 if test_db.is_file(): # pragma: nocover 1abc
19 test_db.unlink()
20 # Import while creating the client to create the DB after starting the test session
21 from docs_src.sql_databases.sql_app_py310 import alt_main 1abc
23 # Ensure import side effects are re-executed
24 importlib.reload(alt_main) 1abc
26 with TestClient(alt_main.app) as c: 1abc
27 yield c 1abc
28 if test_db.is_file(): # pragma: nocover 1abc
29 test_db.unlink() 1abc
30 os.chdir(cwd) 1abc
33@needs_py310 1deabc
34# TODO: pv2 add version with Pydantic v2
35@needs_pydanticv1 1deabc
36def test_create_user(client): 1deabc
37 test_user = {"email": "johndoe@example.com", "password": "secret"} 1abc
38 response = client.post("/users/", json=test_user) 1abc
39 assert response.status_code == 200, response.text 1abc
40 data = response.json() 1abc
41 assert test_user["email"] == data["email"] 1abc
42 assert "id" in data 1abc
43 response = client.post("/users/", json=test_user) 1abc
44 assert response.status_code == 400, response.text 1abc
47@needs_py310 1deabc
48# TODO: pv2 add version with Pydantic v2
49@needs_pydanticv1 1deabc
50def test_get_user(client): 1deabc
51 response = client.get("/users/1") 1abc
52 assert response.status_code == 200, response.text 1abc
53 data = response.json() 1abc
54 assert "email" in data 1abc
55 assert "id" in data 1abc
58@needs_py310 1deabc
59# TODO: pv2 add version with Pydantic v2
60@needs_pydanticv1 1deabc
61def test_nonexistent_user(client): 1deabc
62 response = client.get("/users/999") 1abc
63 assert response.status_code == 404, response.text 1abc
66@needs_py310 1deabc
67# TODO: pv2 add version with Pydantic v2
68@needs_pydanticv1 1deabc
69def test_get_users(client): 1deabc
70 response = client.get("/users/") 1abc
71 assert response.status_code == 200, response.text 1abc
72 data = response.json() 1abc
73 assert "email" in data[0] 1abc
74 assert "id" in data[0] 1abc
77@needs_py310 1deabc
78# TODO: pv2 add Pydantic v2 version
79@needs_pydanticv1 1deabc
80def test_create_item(client): 1deabc
81 item = {"title": "Foo", "description": "Something that fights"} 1abc
82 response = client.post("/users/1/items/", json=item) 1abc
83 assert response.status_code == 200, response.text 1abc
84 item_data = response.json() 1abc
85 assert item["title"] == item_data["title"] 1abc
86 assert item["description"] == item_data["description"] 1abc
87 assert "id" in item_data 1abc
88 assert "owner_id" in item_data 1abc
89 response = client.get("/users/1") 1abc
90 assert response.status_code == 200, response.text 1abc
91 user_data = response.json() 1abc
92 item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0] 1abc
93 assert item_to_check["title"] == item["title"] 1abc
94 assert item_to_check["description"] == item["description"] 1abc
95 response = client.get("/users/1") 1abc
96 assert response.status_code == 200, response.text 1abc
97 user_data = response.json() 1abc
98 item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0] 1abc
99 assert item_to_check["title"] == item["title"] 1abc
100 assert item_to_check["description"] == item["description"] 1abc
103@needs_py310 1deabc
104# TODO: pv2 add Pydantic v2 version
105@needs_pydanticv1 1deabc
106def test_read_items(client): 1deabc
107 response = client.get("/items/") 1abc
108 assert response.status_code == 200, response.text 1abc
109 data = response.json() 1abc
110 assert data 1abc
111 first_item = data[0] 1abc
112 assert "title" in first_item 1abc
113 assert "description" in first_item 1abc
116@needs_py310 1deabc
117# TODO: pv2 add version with Pydantic v2
118@needs_pydanticv1 1deabc
119def test_openapi_schema(client: TestClient): 1deabc
120 response = client.get("/openapi.json") 1abc
121 assert response.status_code == 200, response.text 1abc
122 assert response.json() == { 1abc
123 "openapi": "3.1.0",
124 "info": {"title": "FastAPI", "version": "0.1.0"},
125 "paths": {
126 "/users/": {
127 "get": {
128 "responses": {
129 "200": {
130 "description": "Successful Response",
131 "content": {
132 "application/json": {
133 "schema": {
134 "title": "Response Read Users Users Get",
135 "type": "array",
136 "items": {"$ref": "#/components/schemas/User"},
137 }
138 }
139 },
140 },
141 "422": {
142 "description": "Validation Error",
143 "content": {
144 "application/json": {
145 "schema": {
146 "$ref": "#/components/schemas/HTTPValidationError"
147 }
148 }
149 },
150 },
151 },
152 "summary": "Read Users",
153 "operationId": "read_users_users__get",
154 "parameters": [
155 {
156 "required": False,
157 "schema": {
158 "title": "Skip",
159 "type": "integer",
160 "default": 0,
161 },
162 "name": "skip",
163 "in": "query",
164 },
165 {
166 "required": False,
167 "schema": {
168 "title": "Limit",
169 "type": "integer",
170 "default": 100,
171 },
172 "name": "limit",
173 "in": "query",
174 },
175 ],
176 },
177 "post": {
178 "responses": {
179 "200": {
180 "description": "Successful Response",
181 "content": {
182 "application/json": {
183 "schema": {"$ref": "#/components/schemas/User"}
184 }
185 },
186 },
187 "422": {
188 "description": "Validation Error",
189 "content": {
190 "application/json": {
191 "schema": {
192 "$ref": "#/components/schemas/HTTPValidationError"
193 }
194 }
195 },
196 },
197 },
198 "summary": "Create User",
199 "operationId": "create_user_users__post",
200 "requestBody": {
201 "content": {
202 "application/json": {
203 "schema": {"$ref": "#/components/schemas/UserCreate"}
204 }
205 },
206 "required": True,
207 },
208 },
209 },
210 "/users/{user_id}": {
211 "get": {
212 "responses": {
213 "200": {
214 "description": "Successful Response",
215 "content": {
216 "application/json": {
217 "schema": {"$ref": "#/components/schemas/User"}
218 }
219 },
220 },
221 "422": {
222 "description": "Validation Error",
223 "content": {
224 "application/json": {
225 "schema": {
226 "$ref": "#/components/schemas/HTTPValidationError"
227 }
228 }
229 },
230 },
231 },
232 "summary": "Read User",
233 "operationId": "read_user_users__user_id__get",
234 "parameters": [
235 {
236 "required": True,
237 "schema": {"title": "User Id", "type": "integer"},
238 "name": "user_id",
239 "in": "path",
240 }
241 ],
242 }
243 },
244 "/users/{user_id}/items/": {
245 "post": {
246 "responses": {
247 "200": {
248 "description": "Successful Response",
249 "content": {
250 "application/json": {
251 "schema": {"$ref": "#/components/schemas/Item"}
252 }
253 },
254 },
255 "422": {
256 "description": "Validation Error",
257 "content": {
258 "application/json": {
259 "schema": {
260 "$ref": "#/components/schemas/HTTPValidationError"
261 }
262 }
263 },
264 },
265 },
266 "summary": "Create Item For User",
267 "operationId": "create_item_for_user_users__user_id__items__post",
268 "parameters": [
269 {
270 "required": True,
271 "schema": {"title": "User Id", "type": "integer"},
272 "name": "user_id",
273 "in": "path",
274 }
275 ],
276 "requestBody": {
277 "content": {
278 "application/json": {
279 "schema": {"$ref": "#/components/schemas/ItemCreate"}
280 }
281 },
282 "required": True,
283 },
284 }
285 },
286 "/items/": {
287 "get": {
288 "responses": {
289 "200": {
290 "description": "Successful Response",
291 "content": {
292 "application/json": {
293 "schema": {
294 "title": "Response Read Items Items Get",
295 "type": "array",
296 "items": {"$ref": "#/components/schemas/Item"},
297 }
298 }
299 },
300 },
301 "422": {
302 "description": "Validation Error",
303 "content": {
304 "application/json": {
305 "schema": {
306 "$ref": "#/components/schemas/HTTPValidationError"
307 }
308 }
309 },
310 },
311 },
312 "summary": "Read Items",
313 "operationId": "read_items_items__get",
314 "parameters": [
315 {
316 "required": False,
317 "schema": {
318 "title": "Skip",
319 "type": "integer",
320 "default": 0,
321 },
322 "name": "skip",
323 "in": "query",
324 },
325 {
326 "required": False,
327 "schema": {
328 "title": "Limit",
329 "type": "integer",
330 "default": 100,
331 },
332 "name": "limit",
333 "in": "query",
334 },
335 ],
336 }
337 },
338 },
339 "components": {
340 "schemas": {
341 "ItemCreate": {
342 "title": "ItemCreate",
343 "required": ["title"],
344 "type": "object",
345 "properties": {
346 "title": {"title": "Title", "type": "string"},
347 "description": IsDict(
348 {
349 "title": "Description",
350 "anyOf": [{"type": "string"}, {"type": "null"}],
351 }
352 )
353 | IsDict(
354 # TODO: remove when deprecating Pydantic v1
355 {"title": "Description", "type": "string"}
356 ),
357 },
358 },
359 "Item": {
360 "title": "Item",
361 "required": ["title", "id", "owner_id"],
362 "type": "object",
363 "properties": {
364 "title": {"title": "Title", "type": "string"},
365 "description": IsDict(
366 {
367 "title": "Description",
368 "anyOf": [{"type": "string"}, {"type": "null"}],
369 }
370 )
371 | IsDict(
372 # TODO: remove when deprecating Pydantic v1
373 {"title": "Description", "type": "string"},
374 ),
375 "id": {"title": "Id", "type": "integer"},
376 "owner_id": {"title": "Owner Id", "type": "integer"},
377 },
378 },
379 "User": {
380 "title": "User",
381 "required": ["email", "id", "is_active"],
382 "type": "object",
383 "properties": {
384 "email": {"title": "Email", "type": "string"},
385 "id": {"title": "Id", "type": "integer"},
386 "is_active": {"title": "Is Active", "type": "boolean"},
387 "items": {
388 "title": "Items",
389 "type": "array",
390 "items": {"$ref": "#/components/schemas/Item"},
391 "default": [],
392 },
393 },
394 },
395 "UserCreate": {
396 "title": "UserCreate",
397 "required": ["email", "password"],
398 "type": "object",
399 "properties": {
400 "email": {"title": "Email", "type": "string"},
401 "password": {"title": "Password", "type": "string"},
402 },
403 },
404 "ValidationError": {
405 "title": "ValidationError",
406 "required": ["loc", "msg", "type"],
407 "type": "object",
408 "properties": {
409 "loc": {
410 "title": "Location",
411 "type": "array",
412 "items": {
413 "anyOf": [{"type": "string"}, {"type": "integer"}]
414 },
415 },
416 "msg": {"title": "Message", "type": "string"},
417 "type": {"title": "Error Type", "type": "string"},
418 },
419 },
420 "HTTPValidationError": {
421 "title": "HTTPValidationError",
422 "type": "object",
423 "properties": {
424 "detail": {
425 "title": "Detail",
426 "type": "array",
427 "items": {"$ref": "#/components/schemas/ValidationError"},
428 }
429 },
430 },
431 }
432 },
433 }