Coverage for tests/test_tutorial/test_path_operation_configurations/test_tutorial005_py39.py: 100%
25 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 pytest 1eabcd
2from fastapi.testclient import TestClient 1eabcd
4from ...utils import needs_py39, needs_pydanticv1, needs_pydanticv2 1eabcd
7@pytest.fixture(name="client") 1eabcd
8def get_client(): 1eabcd
9 from docs_src.path_operation_configuration.tutorial005_py39 import app 1abcd
11 client = TestClient(app) 1abcd
12 return client 1abcd
15@needs_py39 1eabcd
16def test_query_params_str_validations(client: TestClient): 1eabcd
17 response = client.post("/items/", json={"name": "Foo", "price": 42}) 1abcd
18 assert response.status_code == 200, response.text 1abcd
19 assert response.json() == { 1abcd
20 "name": "Foo",
21 "price": 42,
22 "description": None,
23 "tax": None,
24 "tags": [],
25 }
28@needs_py39 1eabcd
29@needs_pydanticv2 1eabcd
30def test_openapi_schema(client: TestClient): 1eabcd
31 response = client.get("/openapi.json") 1abcd
32 assert response.status_code == 200, response.text 1abcd
33 assert response.json() == { 1abcd
34 "openapi": "3.1.0",
35 "info": {"title": "FastAPI", "version": "0.1.0"},
36 "paths": {
37 "/items/": {
38 "post": {
39 "responses": {
40 "200": {
41 "description": "The created item",
42 "content": {
43 "application/json": {
44 "schema": {"$ref": "#/components/schemas/Item"}
45 }
46 },
47 },
48 "422": {
49 "description": "Validation Error",
50 "content": {
51 "application/json": {
52 "schema": {
53 "$ref": "#/components/schemas/HTTPValidationError"
54 }
55 }
56 },
57 },
58 },
59 "summary": "Create an item",
60 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item",
61 "operationId": "create_item_items__post",
62 "requestBody": {
63 "content": {
64 "application/json": {
65 "schema": {"$ref": "#/components/schemas/Item"}
66 }
67 },
68 "required": True,
69 },
70 }
71 }
72 },
73 "components": {
74 "schemas": {
75 "Item": {
76 "title": "Item",
77 "required": ["name", "price"],
78 "type": "object",
79 "properties": {
80 "name": {"title": "Name", "type": "string"},
81 "description": {
82 "title": "Description",
83 "anyOf": [{"type": "string"}, {"type": "null"}],
84 },
85 "price": {"title": "Price", "type": "number"},
86 "tax": {
87 "title": "Tax",
88 "anyOf": [{"type": "number"}, {"type": "null"}],
89 },
90 "tags": {
91 "title": "Tags",
92 "uniqueItems": True,
93 "type": "array",
94 "items": {"type": "string"},
95 "default": [],
96 },
97 },
98 },
99 "ValidationError": {
100 "title": "ValidationError",
101 "required": ["loc", "msg", "type"],
102 "type": "object",
103 "properties": {
104 "loc": {
105 "title": "Location",
106 "type": "array",
107 "items": {
108 "anyOf": [{"type": "string"}, {"type": "integer"}]
109 },
110 },
111 "msg": {"title": "Message", "type": "string"},
112 "type": {"title": "Error Type", "type": "string"},
113 },
114 },
115 "HTTPValidationError": {
116 "title": "HTTPValidationError",
117 "type": "object",
118 "properties": {
119 "detail": {
120 "title": "Detail",
121 "type": "array",
122 "items": {"$ref": "#/components/schemas/ValidationError"},
123 }
124 },
125 },
126 }
127 },
128 }
131# TODO: remove when deprecating Pydantic v1
132@needs_py39 1eabcd
133@needs_pydanticv1 1eabcd
134def test_openapi_schema_pv1(client: TestClient): 1eabcd
135 response = client.get("/openapi.json") 1abcd
136 assert response.status_code == 200, response.text 1abcd
137 assert response.json() == { 1abcd
138 "openapi": "3.1.0",
139 "info": {"title": "FastAPI", "version": "0.1.0"},
140 "paths": {
141 "/items/": {
142 "post": {
143 "responses": {
144 "200": {
145 "description": "The created item",
146 "content": {
147 "application/json": {
148 "schema": {"$ref": "#/components/schemas/Item"}
149 }
150 },
151 },
152 "422": {
153 "description": "Validation Error",
154 "content": {
155 "application/json": {
156 "schema": {
157 "$ref": "#/components/schemas/HTTPValidationError"
158 }
159 }
160 },
161 },
162 },
163 "summary": "Create an item",
164 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item",
165 "operationId": "create_item_items__post",
166 "requestBody": {
167 "content": {
168 "application/json": {
169 "schema": {"$ref": "#/components/schemas/Item"}
170 }
171 },
172 "required": True,
173 },
174 }
175 }
176 },
177 "components": {
178 "schemas": {
179 "Item": {
180 "title": "Item",
181 "required": ["name", "price"],
182 "type": "object",
183 "properties": {
184 "name": {"title": "Name", "type": "string"},
185 "description": {"title": "Description", "type": "string"},
186 "price": {"title": "Price", "type": "number"},
187 "tax": {"title": "Tax", "type": "number"},
188 "tags": {
189 "title": "Tags",
190 "uniqueItems": True,
191 "type": "array",
192 "items": {"type": "string"},
193 "default": [],
194 },
195 },
196 },
197 "ValidationError": {
198 "title": "ValidationError",
199 "required": ["loc", "msg", "type"],
200 "type": "object",
201 "properties": {
202 "loc": {
203 "title": "Location",
204 "type": "array",
205 "items": {
206 "anyOf": [{"type": "string"}, {"type": "integer"}]
207 },
208 },
209 "msg": {"title": "Message", "type": "string"},
210 "type": {"title": "Error Type", "type": "string"},
211 },
212 },
213 "HTTPValidationError": {
214 "title": "HTTPValidationError",
215 "type": "object",
216 "properties": {
217 "detail": {
218 "title": "Detail",
219 "type": "array",
220 "items": {"$ref": "#/components/schemas/ValidationError"},
221 }
222 },
223 },
224 }
225 },
226 }