Home / File/ test_file_and_form_order_issue_9116.py — fastapi Source File

test_file_and_form_order_issue_9116.py — fastapi Source File

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

File python FastAPI Responses 5 imports 8 functions

Entity Profile

Dependency Diagram

graph LR
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4["test_file_and_form_order_issue_9116.py"]
  b3e303a2_3f2d_638d_930c_3a5dcc2f5a42["pathlib"]
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 --> b3e303a2_3f2d_638d_930c_3a5dcc2f5a42
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 --> 0dda2280_3359_8460_301c_e98c77e78185
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 2f621e7e_cb9e_ad59_2637_4d2e1ff1bef4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""
Regression test, Error 422 if Form is declared before File
See https://github.com/tiangolo/fastapi/discussions/9116
"""

from pathlib import Path
from typing import Annotated

import pytest
from fastapi import FastAPI, File, Form
from fastapi.testclient import TestClient

app = FastAPI()


@app.post("/file_before_form")
def file_before_form(
    file: bytes = File(),
    city: str = Form(),
):
    return {"file_content": file, "city": city}


@app.post("/file_after_form")
def file_after_form(
    city: str = Form(),
    file: bytes = File(),
):
    return {"file_content": file, "city": city}


@app.post("/file_list_before_form")
def file_list_before_form(
    files: Annotated[list[bytes], File()],
    city: Annotated[str, Form()],
):
    return {"file_contents": files, "city": city}


@app.post("/file_list_after_form")
def file_list_after_form(
    city: Annotated[str, Form()],
    files: Annotated[list[bytes], File()],
):
    return {"file_contents": files, "city": city}


client = TestClient(app)


@pytest.fixture
def tmp_file_1(tmp_path: Path) -> Path:
    f = tmp_path / "example1.txt"
    f.write_text("foo")
    return f


@pytest.fixture
def tmp_file_2(tmp_path: Path) -> Path:
    f = tmp_path / "example2.txt"
    f.write_text("bar")
    return f


@pytest.mark.parametrize("endpoint_path", ("/file_before_form", "/file_after_form"))
def test_file_form_order(endpoint_path: str, tmp_file_1: Path):
    response = client.post(
        url=endpoint_path,
        data={"city": "Thimphou"},
        files={"file": (tmp_file_1.name, tmp_file_1.read_bytes())},
    )
    assert response.status_code == 200, response.text
    assert response.json() == {"file_content": "foo", "city": "Thimphou"}


@pytest.mark.parametrize(
    "endpoint_path", ("/file_list_before_form", "/file_list_after_form")
)
def test_file_list_form_order(endpoint_path: str, tmp_file_1: Path, tmp_file_2: Path):
    response = client.post(
        url=endpoint_path,
        data={"city": "Thimphou"},
        files=(
            ("files", (tmp_file_1.name, tmp_file_1.read_bytes())),
            ("files", (tmp_file_2.name, tmp_file_2.read_bytes())),
        ),
    )
    assert response.status_code == 200, response.text
    assert response.json() == {"file_contents": ["foo", "bar"], "city": "Thimphou"}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_file_and_form_order_issue_9116.py do?
test_file_and_form_order_issue_9116.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_file_and_form_order_issue_9116.py?
test_file_and_form_order_issue_9116.py defines 8 function(s): file_after_form, file_before_form, file_list_after_form, file_list_before_form, test_file_form_order, test_file_list_form_order, tmp_file_1, tmp_file_2.
What does test_file_and_form_order_issue_9116.py depend on?
test_file_and_form_order_issue_9116.py imports 5 module(s): __init__.py, pathlib, pytest, testclient.py, typing.
Where is test_file_and_form_order_issue_9116.py in the architecture?
test_file_and_form_order_issue_9116.py is located at tests/test_file_and_form_order_issue_9116.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

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

Try Supermodel Free