Home / File/ test_list_bytes_file_order_preserved_issue_14811.py — fastapi Source File

test_list_bytes_file_order_preserved_issue_14811.py — fastapi Source File

Architecture documentation for test_list_bytes_file_order_preserved_issue_14811.py, a python file in the fastapi codebase. 6 imports, 0 dependents.

File python FastAPI Routing 6 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  912ec46b_b44d_d220_086e_32e97ac7092e["test_list_bytes_file_order_preserved_issue_14811.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> 0dda2280_3359_8460_301c_e98c77e78185
  c56b4bb1_311b_6874_0f07_e8e8c69d3e0b["anyio"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> c56b4bb1_311b_6874_0f07_e8e8c69d3e0b
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> a7c04dee_ee23_5891_b185_47ff6bed036d
  92a32603_fcc6_3319_3df3_78c953234d9f["starlette.datastructures"]
  912ec46b_b44d_d220_086e_32e97ac7092e --> 92a32603_fcc6_3319_3df3_78c953234d9f
  style 912ec46b_b44d_d220_086e_32e97ac7092e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""
Regression test: preserve order when using list[bytes] + File()
See https://github.com/fastapi/fastapi/discussions/14811
Fixed in PR: https://github.com/fastapi/fastapi/pull/14884
"""

from typing import Annotated

import anyio
import pytest
from fastapi import FastAPI, File
from fastapi.testclient import TestClient
from starlette.datastructures import UploadFile as StarletteUploadFile


def test_list_bytes_file_preserves_order(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    app = FastAPI()

    @app.post("/upload")
    async def upload(files: Annotated[list[bytes], File()]):
        # return something that makes order obvious
        return [b[0] for b in files]

    original_read = StarletteUploadFile.read

    async def patched_read(self: StarletteUploadFile, size: int = -1) -> bytes:
        # Make the FIRST file slower *deterministically*
        if self.filename == "slow.txt":
            await anyio.sleep(0.05)
        return await original_read(self, size)

    monkeypatch.setattr(StarletteUploadFile, "read", patched_read)

    client = TestClient(app)

    files = [
        ("files", ("slow.txt", b"A" * 10, "text/plain")),
        ("files", ("fast.txt", b"B" * 10, "text/plain")),
    ]
    r = client.post("/upload", files=files)
    assert r.status_code == 200, r.text

    # Must preserve request order: slow first, fast second
    assert r.json() == [ord("A"), ord("B")]

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_list_bytes_file_order_preserved_issue_14811.py do?
test_list_bytes_file_order_preserved_issue_14811.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in test_list_bytes_file_order_preserved_issue_14811.py?
test_list_bytes_file_order_preserved_issue_14811.py defines 1 function(s): test_list_bytes_file_preserves_order.
What does test_list_bytes_file_order_preserved_issue_14811.py depend on?
test_list_bytes_file_order_preserved_issue_14811.py imports 6 module(s): __init__.py, anyio, pytest, starlette.datastructures, testclient.py, typing.
Where is test_list_bytes_file_order_preserved_issue_14811.py in the architecture?
test_list_bytes_file_order_preserved_issue_14811.py is located at tests/test_list_bytes_file_order_preserved_issue_14811.py (domain: FastAPI, subdomain: Routing, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free