Coverage for tests / test_tutorial / test_body_multiple_params / test_tutorial004.py: 100%
34 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial004_py310", marks=needs_py310),
14 pytest.param("tutorial004_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_put_all(client: TestClient): 1abdc
25 response = client.put( 1efg
26 "/items/5",
27 json={
28 "importance": 2,
29 "item": {"name": "Foo", "price": 50.5},
30 "user": {"username": "Dave"},
31 },
32 params={"q": "somequery"},
33 )
34 assert response.status_code == 200 1efg
35 assert response.json() == { 1efg
36 "item_id": 5,
37 "importance": 2,
38 "item": {
39 "name": "Foo",
40 "price": 50.5,
41 "description": None,
42 "tax": None,
43 },
44 "user": {"username": "Dave", "full_name": None},
45 "q": "somequery",
46 }
49def test_put_only_required(client: TestClient): 1abdc
50 response = client.put( 1hij
51 "/items/5",
52 json={
53 "importance": 2,
54 "item": {"name": "Foo", "price": 50.5},
55 "user": {"username": "Dave"},
56 },
57 )
58 assert response.status_code == 200 1hij
59 assert response.json() == { 1hij
60 "item_id": 5,
61 "importance": 2,
62 "item": {
63 "name": "Foo",
64 "price": 50.5,
65 "description": None,
66 "tax": None,
67 },
68 "user": {"username": "Dave", "full_name": None},
69 }
72def test_put_missing_body(client: TestClient): 1abdc
73 response = client.put("/items/5") 1klm
74 assert response.status_code == 422 1klm
75 assert response.json() == { 1klm
76 "detail": [
77 {
78 "input": None,
79 "loc": [
80 "body",
81 "item",
82 ],
83 "msg": "Field required",
84 "type": "missing",
85 },
86 {
87 "input": None,
88 "loc": [
89 "body",
90 "user",
91 ],
92 "msg": "Field required",
93 "type": "missing",
94 },
95 {
96 "input": None,
97 "loc": [
98 "body",
99 "importance",
100 ],
101 "msg": "Field required",
102 "type": "missing",
103 },
104 ],
105 }
108def test_put_empty_body(client: TestClient): 1abdc
109 response = client.put("/items/5", json={}) 1nop
110 assert response.status_code == 422 1nop
111 assert response.json() == { 1nop
112 "detail": [
113 {
114 "type": "missing",
115 "loc": ["body", "item"],
116 "msg": "Field required",
117 "input": None,
118 },
119 {
120 "type": "missing",
121 "loc": ["body", "user"],
122 "msg": "Field required",
123 "input": None,
124 },
125 {
126 "type": "missing",
127 "loc": ["body", "importance"],
128 "msg": "Field required",
129 "input": None,
130 },
131 ]
132 }
135def test_put_invalid_importance(client: TestClient): 1abdc
136 response = client.put( 1qrs
137 "/items/5",
138 json={
139 "importance": 0,
140 "item": {"name": "Foo", "price": 50.5},
141 "user": {"username": "Dave"},
142 },
143 )
144 assert response.status_code == 422 1qrs
145 assert response.json() == { 1qrs
146 "detail": [
147 {
148 "loc": ["body", "importance"],
149 "msg": "Input should be greater than 0",
150 "type": "greater_than",
151 "input": 0,
152 "ctx": {"gt": 0},
153 },
154 ],
155 }
158def test_openapi_schema(client: TestClient): 1abdc
159 response = client.get("/openapi.json") 1tuv
160 assert response.status_code == 200, response.text 1tuv
161 assert response.json() == snapshot( 1tuv
162 {
163 "openapi": "3.1.0",
164 "info": {"title": "FastAPI", "version": "0.1.0"},
165 "paths": {
166 "/items/{item_id}": {
167 "put": {
168 "responses": {
169 "200": {
170 "description": "Successful Response",
171 "content": {"application/json": {"schema": {}}},
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": "Update Item",
185 "operationId": "update_item_items__item_id__put",
186 "parameters": [
187 {
188 "required": True,
189 "schema": {"title": "Item Id", "type": "integer"},
190 "name": "item_id",
191 "in": "path",
192 },
193 {
194 "required": False,
195 "schema": {
196 "anyOf": [{"type": "string"}, {"type": "null"}],
197 "title": "Q",
198 },
199 "name": "q",
200 "in": "query",
201 },
202 ],
203 "requestBody": {
204 "content": {
205 "application/json": {
206 "schema": {
207 "$ref": "#/components/schemas/Body_update_item_items__item_id__put"
208 }
209 }
210 },
211 "required": True,
212 },
213 }
214 }
215 },
216 "components": {
217 "schemas": {
218 "Item": {
219 "title": "Item",
220 "required": ["name", "price"],
221 "type": "object",
222 "properties": {
223 "name": {"title": "Name", "type": "string"},
224 "description": {
225 "title": "Description",
226 "anyOf": [{"type": "string"}, {"type": "null"}],
227 },
228 "price": {"title": "Price", "type": "number"},
229 "tax": {
230 "title": "Tax",
231 "anyOf": [{"type": "number"}, {"type": "null"}],
232 },
233 },
234 },
235 "User": {
236 "title": "User",
237 "required": ["username"],
238 "type": "object",
239 "properties": {
240 "username": {"title": "Username", "type": "string"},
241 "full_name": {
242 "title": "Full Name",
243 "anyOf": [{"type": "string"}, {"type": "null"}],
244 },
245 },
246 },
247 "Body_update_item_items__item_id__put": {
248 "title": "Body_update_item_items__item_id__put",
249 "required": ["item", "user", "importance"],
250 "type": "object",
251 "properties": {
252 "item": {"$ref": "#/components/schemas/Item"},
253 "user": {"$ref": "#/components/schemas/User"},
254 "importance": {
255 "title": "Importance",
256 "type": "integer",
257 "exclusiveMinimum": 0.0,
258 },
259 },
260 },
261 "ValidationError": {
262 "title": "ValidationError",
263 "required": ["loc", "msg", "type"],
264 "type": "object",
265 "properties": {
266 "loc": {
267 "title": "Location",
268 "type": "array",
269 "items": {
270 "anyOf": [{"type": "string"}, {"type": "integer"}]
271 },
272 },
273 "msg": {"title": "Message", "type": "string"},
274 "type": {"title": "Error Type", "type": "string"},
275 "input": {"title": "Input"},
276 "ctx": {"title": "Context", "type": "object"},
277 },
278 },
279 "HTTPValidationError": {
280 "title": "HTTPValidationError",
281 "type": "object",
282 "properties": {
283 "detail": {
284 "title": "Detail",
285 "type": "array",
286 "items": {
287 "$ref": "#/components/schemas/ValidationError"
288 },
289 }
290 },
291 },
292 }
293 },
294 }
295 )