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