Coverage for tests/test_tutorial/test_handling_errors/test_tutorial004.py: 100%
20 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
1from fastapi.testclient import TestClient 1abcde
3from docs_src.handling_errors.tutorial004 import app 1abcde
5client = TestClient(app) 1abcde
8def test_get_validation_error(): 1abcde
9 response = client.get("/items/foo") 1abcde
10 assert response.status_code == 400, response.text 1abcde
11 # TODO: remove when deprecating Pydantic v1
12 assert ( 1abcde
13 # TODO: remove when deprecating Pydantic v1
14 "path -> item_id" in response.text
15 or "'loc': ('path', 'item_id')" in response.text
16 )
17 assert ( 1abcde
18 # TODO: remove when deprecating Pydantic v1
19 "value is not a valid integer" in response.text
20 or "Input should be a valid integer, unable to parse string as an integer"
21 in response.text
22 )
25def test_get_http_error(): 1abcde
26 response = client.get("/items/3") 1abcde
27 assert response.status_code == 418, response.text 1abcde
28 assert response.content == b"Nope! I don't like 3." 1abcde
31def test_get(): 1abcde
32 response = client.get("/items/2") 1abcde
33 assert response.status_code == 200, response.text 1abcde
34 assert response.json() == {"item_id": 2} 1abcde
37def test_openapi_schema(): 1abcde
38 response = client.get("/openapi.json") 1abcde
39 assert response.status_code == 200, response.text 1abcde
40 assert response.json() == { 1abcde
41 "openapi": "3.1.0",
42 "info": {"title": "FastAPI", "version": "0.1.0"},
43 "paths": {
44 "/items/{item_id}": {
45 "get": {
46 "responses": {
47 "200": {
48 "description": "Successful Response",
49 "content": {"application/json": {"schema": {}}},
50 },
51 "422": {
52 "description": "Validation Error",
53 "content": {
54 "application/json": {
55 "schema": {
56 "$ref": "#/components/schemas/HTTPValidationError"
57 }
58 }
59 },
60 },
61 },
62 "summary": "Read Item",
63 "operationId": "read_item_items__item_id__get",
64 "parameters": [
65 {
66 "required": True,
67 "schema": {"title": "Item Id", "type": "integer"},
68 "name": "item_id",
69 "in": "path",
70 }
71 ],
72 }
73 }
74 },
75 "components": {
76 "schemas": {
77 "ValidationError": {
78 "title": "ValidationError",
79 "required": ["loc", "msg", "type"],
80 "type": "object",
81 "properties": {
82 "loc": {
83 "title": "Location",
84 "type": "array",
85 "items": {
86 "anyOf": [{"type": "string"}, {"type": "integer"}]
87 },
88 },
89 "msg": {"title": "Message", "type": "string"},
90 "type": {"title": "Error Type", "type": "string"},
91 },
92 },
93 "HTTPValidationError": {
94 "title": "HTTPValidationError",
95 "type": "object",
96 "properties": {
97 "detail": {
98 "title": "Detail",
99 "type": "array",
100 "items": {"$ref": "#/components/schemas/ValidationError"},
101 }
102 },
103 },
104 }
105 },
106 }