Home / File/ test_datastructures.py — fastapi Source File

test_datastructures.py — fastapi Source File

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

File python FastAPI Applications 7 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf["test_datastructures.py"]
  d6678c5f_f1ec_ef20_539f_26d6a74adb08["io"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> d6678c5f_f1ec_ef20_539f_26d6a74adb08
  b3e303a2_3f2d_638d_930c_3a5dcc2f5a42["pathlib"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> b3e303a2_3f2d_638d_930c_3a5dcc2f5a42
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  dc4a1804_f7b4_848b_3280_30523680d7b9["datastructures.py"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> dc4a1804_f7b4_848b_3280_30523680d7b9
  28261a63_2ba9_f1ff_1d1b_475348a45a65["Default"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> 28261a63_2ba9_f1ff_1d1b_475348a45a65
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  03dbbf97_8dc4_0b91_12c2_0804fcc355cf --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 03dbbf97_8dc4_0b91_12c2_0804fcc355cf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import io
from pathlib import Path

import pytest
from fastapi import FastAPI, UploadFile
from fastapi.datastructures import Default
from fastapi.testclient import TestClient


def test_upload_file_invalid_pydantic_v2():
    with pytest.raises(ValueError):
        UploadFile._validate("not a Starlette UploadFile", {})


def test_default_placeholder_equals():
    placeholder_1 = Default("a")
    placeholder_2 = Default("a")
    assert placeholder_1 == placeholder_2
    assert placeholder_1.value == placeholder_2.value


def test_default_placeholder_bool():
    placeholder_a = Default("a")
    placeholder_b = Default("")
    assert placeholder_a
    assert not placeholder_b


def test_upload_file_is_closed(tmp_path: Path):
    path = tmp_path / "test.txt"
    path.write_bytes(b"<file content>")
    app = FastAPI()

    testing_file_store: list[UploadFile] = []

    @app.post("/uploadfile/")
    def create_upload_file(file: UploadFile):
        testing_file_store.append(file)
        return {"filename": file.filename}

    client = TestClient(app)
    with path.open("rb") as file:
        response = client.post("/uploadfile/", files={"file": file})
    assert response.status_code == 200, response.text
    assert response.json() == {"filename": "test.txt"}

    assert testing_file_store
    assert testing_file_store[0].file.closed


# For UploadFile coverage, segments copied from Starlette tests


@pytest.mark.anyio
async def test_upload_file():
    stream = io.BytesIO(b"data")
    file = UploadFile(filename="file", file=stream, size=4)
    assert await file.read() == b"data"
    assert file.size == 4
    await file.write(b" and more data!")
    assert await file.read() == b""
    assert file.size == 19
    await file.seek(0)
    assert await file.read() == b"data and more data!"
    await file.close()

Domain

Subdomains

Frequently Asked Questions

What does test_datastructures.py do?
test_datastructures.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Applications subdomain.
What functions are defined in test_datastructures.py?
test_datastructures.py defines 5 function(s): test_default_placeholder_bool, test_default_placeholder_equals, test_upload_file, test_upload_file_invalid_pydantic_v2, test_upload_file_is_closed.
What does test_datastructures.py depend on?
test_datastructures.py imports 7 module(s): Default, __init__.py, datastructures.py, io, pathlib, pytest, testclient.py.
Where is test_datastructures.py in the architecture?
test_datastructures.py is located at tests/test_datastructures.py (domain: FastAPI, subdomain: Applications, directory: tests).

Analyze Your Own Codebase

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

Try Supermodel Free