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