Coverage for tests/test_tutorial/test_handling_errors/test_tutorial005.py: 100%
17 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 dirty_equals import IsDict 1abcde
2from fastapi.testclient import TestClient 1abcde
4from docs_src.handling_errors.tutorial005 import app 1abcde
6client = TestClient(app) 1abcde
9def test_post_validation_error(): 1abcde
10 response = client.post("/items/", json={"title": "towel", "size": "XL"}) 1abcde
11 assert response.status_code == 422, response.text 1abcde
12 assert response.json() == IsDict( 1abcde
13 {
14 "detail": [
15 {
16 "type": "int_parsing",
17 "loc": ["body", "size"],
18 "msg": "Input should be a valid integer, unable to parse string as an integer",
19 "input": "XL",
20 }
21 ],
22 "body": {"title": "towel", "size": "XL"},
23 }
24 ) | IsDict(
25 # TODO: remove when deprecating Pydantic v1
26 {
27 "detail": [
28 {
29 "loc": ["body", "size"],
30 "msg": "value is not a valid integer",
31 "type": "type_error.integer",
32 }
33 ],
34 "body": {"title": "towel", "size": "XL"},
35 }
36 )
39def test_post(): 1abcde
40 data = {"title": "towel", "size": 5} 1abcde
41 response = client.post("/items/", json=data) 1abcde
42 assert response.status_code == 200, response.text 1abcde
43 assert response.json() == data 1abcde
46def test_openapi_schema(): 1abcde
47 response = client.get("/openapi.json") 1abcde
48 assert response.status_code == 200, response.text 1abcde
49 assert response.json() == { 1abcde
50 "openapi": "3.1.0",
51 "info": {"title": "FastAPI", "version": "0.1.0"},
52 "paths": {
53 "/items/": {
54 "post": {
55 "summary": "Create Item",
56 "operationId": "create_item_items__post",
57 "requestBody": {
58 "content": {
59 "application/json": {
60 "schema": {"$ref": "#/components/schemas/Item"}
61 }
62 },
63 "required": True,
64 },
65 "responses": {
66 "200": {
67 "description": "Successful Response",
68 "content": {"application/json": {"schema": {}}},
69 },
70 "422": {
71 "description": "Validation Error",
72 "content": {
73 "application/json": {
74 "schema": {
75 "$ref": "#/components/schemas/HTTPValidationError"
76 }
77 }
78 },
79 },
80 },
81 }
82 }
83 },
84 "components": {
85 "schemas": {
86 "HTTPValidationError": {
87 "title": "HTTPValidationError",
88 "type": "object",
89 "properties": {
90 "detail": {
91 "title": "Detail",
92 "type": "array",
93 "items": {"$ref": "#/components/schemas/ValidationError"},
94 }
95 },
96 },
97 "Item": {
98 "title": "Item",
99 "required": ["title", "size"],
100 "type": "object",
101 "properties": {
102 "title": {"title": "Title", "type": "string"},
103 "size": {"title": "Size", "type": "integer"},
104 },
105 },
106 "ValidationError": {
107 "title": "ValidationError",
108 "required": ["loc", "msg", "type"],
109 "type": "object",
110 "properties": {
111 "loc": {
112 "title": "Location",
113 "type": "array",
114 "items": {
115 "anyOf": [{"type": "string"}, {"type": "integer"}]
116 },
117 },
118 "msg": {"title": "Message", "type": "string"},
119 "type": {"title": "Error Type", "type": "string"},
120 },
121 },
122 }
123 },
124 }