| 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("/list-bytes", operation_id="list_bytes") |
| 16 |
async def read_list_bytes(p: Annotated[list[bytes], File()]): |
| 17 |
return {"file_size": [len(file) for file in p]} |
| 18 |
|
| 19 |
|
| 20 |
@app.post("/list-uploadfile", operation_id="list_uploadfile") |
| 21 |
async def read_list_uploadfile(p: Annotated[list[UploadFile], File()]): |
| 22 |
return {"file_size": [file.size for file in p]} |
| 23 |
|
| 24 |
|
| 25 |
@pytest.mark.parametrize( |
| 26 |
"path", |
| 27 |
[ |
| 28 |
"/list-bytes", |
| 29 |
"/list-uploadfile", |
| 30 |
], |
| 31 |
) |
| 32 |
def test_list_schema(path: str): |
| 33 |
openapi = app.openapi() |
| 34 |
body_model_name = get_body_model_name(openapi, path) |
| 35 |
|
| 36 |
assert app.openapi()["components"]["schemas"][body_model_name] == { |
| 37 |
"properties": { |
| 38 |
"p": { |
| 39 |
"type": "array", |
| 40 |
"items": { |
| 41 |
"type": "string", |
| 42 |
"contentMediaType": "application/octet-stream", |
| 43 |
}, |
| 44 |
"title": "P", |
| 45 |
}, |
| 46 |
}, |
| 47 |
"required": ["p"], |
| 48 |
"title": body_model_name, |
| 49 |
"type": "object", |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
"path", |
| 55 |
[ |
| 56 |
"/list-bytes", |
| 57 |
"/list-uploadfile", |
| 58 |
], |
| 59 |
) |
| 60 |
def test_list_missing(path: str): |
| 61 |
client = TestClient(app) |
| 62 |
response = client.post(path) |
| 63 |
assert response.status_code == 422 |
| 64 |
assert response.json() == { |
| 65 |
"detail": [ |
| 66 |
{ |
| 67 |
"type": "missing", |
| 68 |
"loc": ["body", "p"], |
| 69 |
"msg": "Field required", |
| 70 |
"input": None, |
| 71 |
} |
| 72 |
] |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize( |
| 77 |
"path", |
| 78 |
[ |
| 79 |
"/list-bytes", |
| 80 |
"/list-uploadfile", |
| 81 |
], |
| 82 |
) |
| 83 |
def test_list(path: str): |
| 84 |
client = TestClient(app) |
| 85 |
response = client.post(path, files=[("p", b"hello"), ("p", b"world")]) |
| 86 |
assert response.status_code == 200 |
| 87 |
assert response.json() == {"file_size": [5, 5]} |
| 88 |
|
| 89 |
|
| 90 |
# ===================================================================================== |
| 91 |
# Alias |
| 92 |
|
| 93 |
|
| 94 |
@app.post("/list-bytes-alias", operation_id="list_bytes_alias") |
| 95 |
async def read_list_bytes_alias(p: Annotated[list[bytes], File(alias="p_alias")]): |
| 96 |
return {"file_size": [len(file) for file in p]} |
| 97 |
|
| 98 |
|
| 99 |
@app.post("/list-uploadfile-alias", operation_id="list_uploadfile_alias") |
| 100 |
async def read_list_uploadfile_alias( |
| 101 |
p: Annotated[list[UploadFile], File(alias="p_alias")], |
| 102 |
): |
| 103 |
return {"file_size": [file.size for file in p]} |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.parametrize( |
| 107 |
"path", |
| 108 |
[ |
| 109 |
"/list-bytes-alias", |
| 110 |
"/list-uploadfile-alias", |
| 111 |
], |
| 112 |
) |
| 113 |
def test_list_alias_schema(path: str): |
| 114 |
openapi = app.openapi() |
| 115 |
body_model_name = get_body_model_name(openapi, path) |
| 116 |
|
| 117 |
assert app.openapi()["components"]["schemas"][body_model_name] == { |
| 118 |
"properties": { |
| 119 |
"p_alias": { |
| 120 |
"type": "array", |
| 121 |
"items": { |
| 122 |
"type": "string", |
| 123 |
"contentMediaType": "application/octet-stream", |
| 124 |
}, |
| 125 |
"title": "P Alias", |
| 126 |
}, |
| 127 |
}, |
| 128 |
"required": ["p_alias"], |
| 129 |
"title": body_model_name, |
| 130 |
"type": "object", |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize( |
| 135 |
"path", |
| 136 |
[ |
| 137 |
"/list-bytes-alias", |
| 138 |
"/list-uploadfile-alias", |
| 139 |
], |
| 140 |
) |
| 141 |
def test_list_alias_missing(path: str): |
| 142 |
client = TestClient(app) |
| 143 |
response = client.post(path) |
| 144 |
assert response.status_code == 422 |
| 145 |
assert response.json() == { |
| 146 |
"detail": [ |
| 147 |
{ |
| 148 |
"type": "missing", |
| 149 |
"loc": ["body", "p_alias"], |
| 150 |
"msg": "Field required", |
| 151 |
"input": None, |
| 152 |
} |
| 153 |
] |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.parametrize( |
| 158 |
"path", |
| 159 |
[ |
| 160 |
"/list-bytes-alias", |
| 161 |
"/list-uploadfile-alias", |
| 162 |
], |
| 163 |
) |
| 164 |
def test_list_alias_by_name(path: str): |
| 165 |
client = TestClient(app) |
| 166 |
response = client.post(path, files=[("p", b"hello"), ("p", b"world")]) |
| 167 |
assert response.status_code == 422 |
| 168 |
assert response.json() == { |
| 169 |
"detail": [ |
| 170 |
{ |
| 171 |
"type": "missing", |
| 172 |
"loc": ["body", "p_alias"], |
| 173 |
"msg": "Field required", |
| 174 |
"input": None, |
| 175 |
} |
| 176 |
] |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
"path", |
| 182 |
[ |
| 183 |
"/list-bytes-alias", |
| 184 |
"/list-uploadfile-alias", |
| 185 |
], |
| 186 |
) |
| 187 |
def test_list_alias_by_alias(path: str): |
| 188 |
client = TestClient(app) |
| 189 |
response = client.post(path, files=[("p_alias", b"hello"), ("p_alias", b"world")]) |
| 190 |
assert response.status_code == 200, response.text |
| 191 |
assert response.json() == {"file_size": [5, 5]} |
| 192 |
|
| 193 |
|
| 194 |
# ===================================================================================== |
| 195 |
# Validation alias |
| 196 |
|
| 197 |
|
| 198 |
@app.post("/list-bytes-validation-alias", operation_id="list_bytes_validation_alias") |
| 199 |
def read_list_bytes_validation_alias( |
| 200 |
p: Annotated[list[bytes], File(validation_alias="p_val_alias")], |
| 201 |
): |
| 202 |
return {"file_size": [len(file) for file in p]} |
| 203 |
|
| 204 |
|
| 205 |
@app.post( |
| 206 |
"/list-uploadfile-validation-alias", |
| 207 |
operation_id="list_uploadfile_validation_alias", |
| 208 |
) |
| 209 |
def read_list_uploadfile_validation_alias( |
| 210 |
p: Annotated[list[UploadFile], File(validation_alias="p_val_alias")], |
| 211 |
): |
| 212 |
return {"file_size": [file.size for file in p]} |
| 213 |
|
| 214 |
|
| 215 |
@pytest.mark.parametrize( |
| 216 |
"path", |
| 217 |
[ |
| 218 |
"/list-bytes-validation-alias", |
| 219 |
"/list-uploadfile-validation-alias", |
| 220 |
], |
| 221 |
) |
| 222 |
def test_list_validation_alias_schema(path: str): |
| 223 |
openapi = app.openapi() |
| 224 |
body_model_name = get_body_model_name(openapi, path) |
| 225 |
|
| 226 |
assert app.openapi()["components"]["schemas"][body_model_name] == { |
| 227 |
"properties": { |
| 228 |
"p_val_alias": { |
| 229 |
"type": "array", |
| 230 |
"items": { |
| 231 |
"type": "string", |
| 232 |
"contentMediaType": "application/octet-stream", |
| 233 |
}, |
| 234 |
"title": "P Val Alias", |
| 235 |
}, |
| 236 |
}, |
| 237 |
"required": ["p_val_alias"], |
| 238 |
"title": body_model_name, |
| 239 |
"type": "object", |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.parametrize( |
| 244 |
"path", |
| 245 |
[ |
| 246 |
"/list-bytes-validation-alias", |
| 247 |
"/list-uploadfile-validation-alias", |
| 248 |
], |
| 249 |
) |
| 250 |
def test_list_validation_alias_missing(path: str): |
| 251 |
client = TestClient(app) |
| 252 |
response = client.post(path) |
| 253 |
assert response.status_code == 422 |
| 254 |
assert response.json() == { |
| 255 |
"detail": [ |
| 256 |
{ |
| 257 |
"type": "missing", |
| 258 |
"loc": [ |
| 259 |
"body", |
| 260 |
"p_val_alias", |
| 261 |
], |
| 262 |
"msg": "Field required", |
| 263 |
"input": None, |
| 264 |
} |
| 265 |
] |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
@pytest.mark.parametrize( |
| 270 |
"path", |
| 271 |
[ |
| 272 |
"/list-bytes-validation-alias", |
| 273 |
"/list-uploadfile-validation-alias", |
| 274 |
], |
| 275 |
) |
| 276 |
def test_list_validation_alias_by_name(path: str): |
| 277 |
client = TestClient(app) |
| 278 |
response = client.post(path, files=[("p", b"hello"), ("p", b"world")]) |
| 279 |
assert response.status_code == 422, response.text |
| 280 |
|
| 281 |
assert response.json() == { |
| 282 |
"detail": [ |
| 283 |
{ |
| 284 |
"type": "missing", |
| 285 |
"loc": ["body", "p_val_alias"], |
| 286 |
"msg": "Field required", |
| 287 |
"input": None, |
| 288 |
} |
| 289 |
] |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
@pytest.mark.parametrize( |
| 294 |
"path", |
| 295 |
[ |
| 296 |
"/list-bytes-validation-alias", |
| 297 |
"/list-uploadfile-validation-alias", |
| 298 |
], |
| 299 |
) |
| 300 |
def test_list_validation_alias_by_validation_alias(path: str): |
| 301 |
client = TestClient(app) |
| 302 |
response = client.post( |
| 303 |
path, files=[("p_val_alias", b"hello"), ("p_val_alias", b"world")] |
| 304 |
) |
| 305 |
assert response.status_code == 200, response.text |
| 306 |
assert response.json() == {"file_size": [5, 5]} |
| 307 |
|
| 308 |
|
| 309 |
# ===================================================================================== |
| 310 |
# Alias and validation alias |
| 311 |
|
| 312 |
|
| 313 |
@app.post( |
| 314 |
"/list-bytes-alias-and-validation-alias", |
| 315 |
operation_id="list_bytes_alias_and_validation_alias", |
| 316 |
) |
| 317 |
def read_list_bytes_alias_and_validation_alias( |
| 318 |
p: Annotated[list[bytes], File(alias="p_alias", validation_alias="p_val_alias")], |
| 319 |
): |
| 320 |
return {"file_size": [len(file) for file in p]} |
| 321 |
|
| 322 |
|
| 323 |
@app.post( |
| 324 |
"/list-uploadfile-alias-and-validation-alias", |
| 325 |
operation_id="list_uploadfile_alias_and_validation_alias", |
| 326 |
) |
| 327 |
def read_list_uploadfile_alias_and_validation_alias( |
| 328 |
p: Annotated[ |
| 329 |
list[UploadFile], File(alias="p_alias", validation_alias="p_val_alias") |
| 330 |
], |
| 331 |
): |
| 332 |
return {"file_size": [file.size for file in p]} |
| 333 |
|
| 334 |
|
| 335 |
@pytest.mark.parametrize( |
| 336 |
"path", |
| 337 |
[ |
| 338 |
"/list-bytes-alias-and-validation-alias", |
| 339 |
"/list-uploadfile-alias-and-validation-alias", |
| 340 |
], |
| 341 |
) |
| 342 |
def test_list_alias_and_validation_alias_schema(path: str): |
| 343 |
openapi = app.openapi() |
| 344 |
body_model_name = get_body_model_name(openapi, path) |
| 345 |
|
| 346 |
assert app.openapi()["components"]["schemas"][body_model_name] == { |
| 347 |
"properties": { |
| 348 |
"p_val_alias": { |
| 349 |
"type": "array", |
| 350 |
"items": { |
| 351 |
"type": "string", |
| 352 |
"contentMediaType": "application/octet-stream", |
| 353 |
}, |
| 354 |
"title": "P Val Alias", |
| 355 |
}, |
| 356 |
}, |
| 357 |
"required": ["p_val_alias"], |
| 358 |
"title": body_model_name, |
| 359 |
"type": "object", |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
@pytest.mark.parametrize( |
| 364 |
"path", |
| 365 |
[ |
| 366 |
"/list-bytes-alias-and-validation-alias", |
| 367 |
"/list-uploadfile-alias-and-validation-alias", |
| 368 |
], |
| 369 |
) |
| 370 |
def test_list_alias_and_validation_alias_missing(path: str): |
| 371 |
client = TestClient(app) |
| 372 |
response = client.post(path) |
| 373 |
assert response.status_code == 422 |
| 374 |
assert response.json() == { |
| 375 |
"detail": [ |
| 376 |
{ |
| 377 |
"type": "missing", |
| 378 |
"loc": [ |
| 379 |
"body", |
| 380 |
"p_val_alias", |
| 381 |
], |
| 382 |
"msg": "Field required", |
| 383 |
"input": None, |
| 384 |
} |
| 385 |
] |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
@pytest.mark.parametrize( |
| 390 |
"path", |
| 391 |
[ |
| 392 |
"/list-bytes-alias-and-validation-alias", |
| 393 |
"/list-uploadfile-alias-and-validation-alias", |
| 394 |
], |
| 395 |
) |
| 396 |
def test_list_alias_and_validation_alias_by_name(path: str): |
| 397 |
client = TestClient(app) |
| 398 |
response = client.post(path, files=[("p", "hello"), ("p", "world")]) |
| 399 |
assert response.status_code == 422 |
| 400 |
|
| 401 |
assert response.json() == { |
| 402 |
"detail": [ |
| 403 |
{ |
| 404 |
"type": "missing", |
| 405 |
"loc": [ |
| 406 |
"body", |
| 407 |
"p_val_alias", |
| 408 |
], |
| 409 |
"msg": "Field required", |
| 410 |
"input": None, |
| 411 |
} |
| 412 |
] |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
@pytest.mark.parametrize( |
| 417 |
"path", |
| 418 |
[ |
| 419 |
"/list-bytes-alias-and-validation-alias", |
| 420 |
"/list-uploadfile-alias-and-validation-alias", |
| 421 |
], |
| 422 |
) |
| 423 |
def test_list_alias_and_validation_alias_by_alias(path: str): |
| 424 |
client = TestClient(app) |
| 425 |
response = client.post(path, files=[("p_alias", b"hello"), ("p_alias", b"world")]) |
| 426 |
assert response.status_code == 422, response.text |
| 427 |
|
| 428 |
assert response.json() == { |
| 429 |
"detail": [ |
| 430 |
{ |
| 431 |
"type": "missing", |
| 432 |
"loc": ["body", "p_val_alias"], |
| 433 |
"msg": "Field required", |
| 434 |
"input": None, |
| 435 |
} |
| 436 |
] |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
@pytest.mark.parametrize( |
| 441 |
"path", |
| 442 |
[ |
| 443 |
"/list-bytes-alias-and-validation-alias", |
| 444 |
"/list-uploadfile-alias-and-validation-alias", |
| 445 |
], |
| 446 |
) |
| 447 |
def test_list_alias_and_validation_alias_by_validation_alias(path: str): |
| 448 |
client = TestClient(app) |
| 449 |
response = client.post( |
| 450 |
path, files=[("p_val_alias", b"hello"), ("p_val_alias", b"world")] |
| 451 |
) |
| 452 |
assert response.status_code == 200, response.text |
| 453 |
assert response.json() == {"file_size": [5, 5]} |