Debian Codegraph @

tests/test_request_params/test_file/test_optional_list.py

raw
1 from typing import Annotated
2
3 import pytest
4 from fastapi import FastAPI, File, UploadFile
5 from fastapi.testclient import TestClient
6
7 from .utils import get_body_model_name
8
9 app = FastAPI()
10
11 # =====================================================================================
12 # Without aliases
13
14
15 @app.post("/optional-list-bytes")
16 async def read_optional_list_bytes(p: Annotated[list[bytes] | None, File()] = None):
17 return {"file_size": [len(file) for file in p] if p else None}
18
19
20 @app.post("/optional-list-uploadfile")
21 async def read_optional_list_uploadfile(
22 p: Annotated[list[UploadFile] | None, File()] = None,
23 ):
24 return {"file_size": [file.size for file in p] if p else None}
25
26
27 @pytest.mark.parametrize(
28 "path",
29 [
30 "/optional-list-bytes",
31 "/optional-list-uploadfile",
32 ],
33 )
34 def test_optional_list_schema(path: str):
35 openapi = app.openapi()
36 body_model_name = get_body_model_name(openapi, path)
37
38 assert app.openapi()["components"]["schemas"][body_model_name] == {
39 "properties": {
40 "p": {
41 "anyOf": [
42 {
43 "type": "array",
44 "items": {
45 "type": "string",
46 "contentMediaType": "application/octet-stream",
47 },
48 },
49 {"type": "null"},
50 ],
51 "title": "P",
52 }
53 },
54 "title": body_model_name,
55 "type": "object",
56 }
57
58
59 @pytest.mark.parametrize(
60 "path",
61 [
62 "/optional-list-bytes",
63 "/optional-list-uploadfile",
64 ],
65 )
66 def test_optional_list_missing(path: str):
67 client = TestClient(app)
68 response = client.post(path)
69 assert response.status_code == 200, response.text
70 assert response.json() == {"file_size": None}
71
72
73 @pytest.mark.parametrize(
74 "path",
75 [
76 "/optional-list-bytes",
77 "/optional-list-uploadfile",
78 ],
79 )
80 def test_optional_list(path: str):
81 client = TestClient(app)
82 response = client.post(path, files=[("p", b"hello"), ("p", b"world")])
83 assert response.status_code == 200
84 assert response.json() == {"file_size": [5, 5]}
85
86
87 # =====================================================================================
88 # Alias
89
90
91 @app.post("/optional-list-bytes-alias")
92 async def read_optional_list_bytes_alias(
93 p: Annotated[list[bytes] | None, File(alias="p_alias")] = None,
94 ):
95 return {"file_size": [len(file) for file in p] if p else None}
96
97
98 @app.post("/optional-list-uploadfile-alias")
99 async def read_optional_list_uploadfile_alias(
100 p: Annotated[list[UploadFile] | None, File(alias="p_alias")] = None,
101 ):
102 return {"file_size": [file.size for file in p] if p else None}
103
104
105 @pytest.mark.parametrize(
106 "path",
107 [
108 "/optional-list-bytes-alias",
109 "/optional-list-uploadfile-alias",
110 ],
111 )
112 def test_optional_list_alias_schema(path: str):
113 openapi = app.openapi()
114 body_model_name = get_body_model_name(openapi, path)
115
116 assert app.openapi()["components"]["schemas"][body_model_name] == {
117 "properties": {
118 "p_alias": {
119 "anyOf": [
120 {
121 "type": "array",
122 "items": {
123 "type": "string",
124 "contentMediaType": "application/octet-stream",
125 },
126 },
127 {"type": "null"},
128 ],
129 "title": "P Alias",
130 }
131 },
132 "title": body_model_name,
133 "type": "object",
134 }
135
136
137 @pytest.mark.parametrize(
138 "path",
139 [
140 "/optional-list-bytes-alias",
141 "/optional-list-uploadfile-alias",
142 ],
143 )
144 def test_optional_list_alias_missing(path: str):
145 client = TestClient(app)
146 response = client.post(path)
147 assert response.status_code == 200
148 assert response.json() == {"file_size": None}
149
150
151 @pytest.mark.parametrize(
152 "path",
153 [
154 "/optional-list-bytes-alias",
155 "/optional-list-uploadfile-alias",
156 ],
157 )
158 def test_optional_list_alias_by_name(path: str):
159 client = TestClient(app)
160 response = client.post(path, files=[("p", b"hello"), ("p", b"world")])
161 assert response.status_code == 200, response.text
162 assert response.json() == {"file_size": None}
163
164
165 @pytest.mark.parametrize(
166 "path",
167 [
168 "/optional-list-bytes-alias",
169 "/optional-list-uploadfile-alias",
170 ],
171 )
172 def test_optional_list_alias_by_alias(path: str):
173 client = TestClient(app)
174 response = client.post(path, files=[("p_alias", b"hello"), ("p_alias", b"world")])
175 assert response.status_code == 200, response.text
176 assert response.json() == {"file_size": [5, 5]}
177
178
179 # =====================================================================================
180 # Validation alias
181
182
183 @app.post("/optional-list-bytes-validation-alias")
184 def read_optional_list_bytes_validation_alias(
185 p: Annotated[list[bytes] | None, File(validation_alias="p_val_alias")] = None,
186 ):
187 return {"file_size": [len(file) for file in p] if p else None}
188
189
190 @app.post("/optional-list-uploadfile-validation-alias")
191 def read_optional_list_uploadfile_validation_alias(
192 p: Annotated[list[UploadFile] | None, File(validation_alias="p_val_alias")] = None,
193 ):
194 return {"file_size": [file.size for file in p] if p else None}
195
196
197 @pytest.mark.parametrize(
198 "path",
199 [
200 "/optional-list-bytes-validation-alias",
201 "/optional-list-uploadfile-validation-alias",
202 ],
203 )
204 def test_optional_validation_alias_schema(path: str):
205 openapi = app.openapi()
206 body_model_name = get_body_model_name(openapi, path)
207
208 assert app.openapi()["components"]["schemas"][body_model_name] == {
209 "properties": {
210 "p_val_alias": {
211 "anyOf": [
212 {
213 "type": "array",
214 "items": {
215 "type": "string",
216 "contentMediaType": "application/octet-stream",
217 },
218 },
219 {"type": "null"},
220 ],
221 "title": "P Val Alias",
222 }
223 },
224 "title": body_model_name,
225 "type": "object",
226 }
227
228
229 @pytest.mark.parametrize(
230 "path",
231 [
232 "/optional-list-bytes-validation-alias",
233 "/optional-list-uploadfile-validation-alias",
234 ],
235 )
236 def test_optional_validation_alias_missing(path: str):
237 client = TestClient(app)
238 response = client.post(path)
239 assert response.status_code == 200
240 assert response.json() == {"file_size": None}
241
242
243 @pytest.mark.parametrize(
244 "path",
245 [
246 "/optional-list-bytes-validation-alias",
247 "/optional-list-uploadfile-validation-alias",
248 ],
249 )
250 def test_optional_validation_alias_by_name(path: str):
251 client = TestClient(app)
252 response = client.post(path, files=[("p", b"hello"), ("p", b"world")])
253 assert response.status_code == 200, response.text
254 assert response.json() == {"file_size": None}
255
256
257 @pytest.mark.parametrize(
258 "path",
259 [
260 "/optional-list-bytes-validation-alias",
261 "/optional-list-uploadfile-validation-alias",
262 ],
263 )
264 def test_optional_validation_alias_by_validation_alias(path: str):
265 client = TestClient(app)
266 response = client.post(
267 path, files=[("p_val_alias", b"hello"), ("p_val_alias", b"world")]
268 )
269 assert response.status_code == 200, response.text
270 assert response.json() == {"file_size": [5, 5]}
271
272
273 # =====================================================================================
274 # Alias and validation alias
275
276
277 @app.post("/optional-list-bytes-alias-and-validation-alias")
278 def read_optional_list_bytes_alias_and_validation_alias(
279 p: Annotated[
280 list[bytes] | None, File(alias="p_alias", validation_alias="p_val_alias")
281 ] = None,
282 ):
283 return {"file_size": [len(file) for file in p] if p else None}
284
285
286 @app.post("/optional-list-uploadfile-alias-and-validation-alias")
287 def read_optional_list_uploadfile_alias_and_validation_alias(
288 p: Annotated[
289 list[UploadFile] | None,
290 File(alias="p_alias", validation_alias="p_val_alias"),
291 ] = None,
292 ):
293 return {"file_size": [file.size for file in p] if p else None}
294
295
296 @pytest.mark.parametrize(
297 "path",
298 [
299 "/optional-list-bytes-alias-and-validation-alias",
300 "/optional-list-uploadfile-alias-and-validation-alias",
301 ],
302 )
303 def test_optional_list_alias_and_validation_alias_schema(path: str):
304 openapi = app.openapi()
305 body_model_name = get_body_model_name(openapi, path)
306
307 assert app.openapi()["components"]["schemas"][body_model_name] == {
308 "properties": {
309 "p_val_alias": {
310 "anyOf": [
311 {
312 "type": "array",
313 "items": {
314 "type": "string",
315 "contentMediaType": "application/octet-stream",
316 },
317 },
318 {"type": "null"},
319 ],
320 "title": "P Val Alias",
321 }
322 },
323 "title": body_model_name,
324 "type": "object",
325 }
326
327
328 @pytest.mark.parametrize(
329 "path",
330 [
331 "/optional-list-bytes-alias-and-validation-alias",
332 "/optional-list-uploadfile-alias-and-validation-alias",
333 ],
334 )
335 def test_optional_list_alias_and_validation_alias_missing(path: str):
336 client = TestClient(app)
337 response = client.post(path)
338 assert response.status_code == 200
339 assert response.json() == {"file_size": None}
340
341
342 @pytest.mark.parametrize(
343 "path",
344 [
345 "/optional-list-bytes-alias-and-validation-alias",
346 "/optional-list-uploadfile-alias-and-validation-alias",
347 ],
348 )
349 def test_optional_list_alias_and_validation_alias_by_name(path: str):
350 client = TestClient(app)
351 response = client.post(path, files={"p": "hello"})
352 assert response.status_code == 200
353 assert response.json() == {"file_size": None}
354
355
356 @pytest.mark.parametrize(
357 "path",
358 [
359 "/optional-list-bytes-alias-and-validation-alias",
360 "/optional-list-uploadfile-alias-and-validation-alias",
361 ],
362 )
363 def test_optional_list_alias_and_validation_alias_by_alias(path: str):
364 client = TestClient(app)
365 response = client.post(path, files=[("p_alias", b"hello"), ("p_alias", b"world")])
366 assert response.status_code == 200, response.text
367 assert response.json() == {"file_size": None}
368
369
370 @pytest.mark.parametrize(
371 "path",
372 [
373 "/optional-list-bytes-alias-and-validation-alias",
374 "/optional-list-uploadfile-alias-and-validation-alias",
375 ],
376 )
377 def test_optional_list_alias_and_validation_alias_by_validation_alias(path: str):
378 client = TestClient(app)
379 response = client.post(
380 path, files=[("p_val_alias", b"hello"), ("p_val_alias", b"world")]
381 )
382 assert response.status_code == 200, response.text
383 assert response.json() == {"file_size": [5, 5]}