Coverage for tests / test_datastructures.py: 100%
47 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import io 1efgh
2from pathlib import Path 1efgh
4import pytest 1efgh
5from fastapi import FastAPI, UploadFile 1efgh
6from fastapi.datastructures import Default 1efgh
7from fastapi.testclient import TestClient 1efgh
10def test_upload_file_invalid_pydantic_v2(): 1efgh
11 with pytest.raises(ValueError): 1yzAB
12 UploadFile._validate("not a Starlette UploadFile", {}) 1yzAB
15def test_default_placeholder_equals(): 1efgh
16 placeholder_1 = Default("a") 1qrst
17 placeholder_2 = Default("a") 1qrst
18 assert placeholder_1 == placeholder_2 1qrst
19 assert placeholder_1.value == placeholder_2.value 1qrst
22def test_default_placeholder_bool(): 1efgh
23 placeholder_a = Default("a") 1uvwx
24 placeholder_b = Default("") 1uvwx
25 assert placeholder_a 1uvwx
26 assert not placeholder_b 1uvwx
29def test_upload_file_is_closed(tmp_path: Path): 1efgh
30 path = tmp_path / "test.txt" 1abcd
31 path.write_bytes(b"<file content>") 1abcd
32 app = FastAPI() 1abcd
34 testing_file_store: list[UploadFile] = [] 1abcd
36 @app.post("/uploadfile/") 1abcd
37 def create_upload_file(file: UploadFile): 1abcd
38 testing_file_store.append(file) 1abcd
39 return {"filename": file.filename} 1abcd
41 client = TestClient(app) 1abcd
42 with path.open("rb") as file: 1abcd
43 response = client.post("/uploadfile/", files={"file": file}) 1abcd
44 assert response.status_code == 200, response.text 1abcd
45 assert response.json() == {"filename": "test.txt"} 1abcd
47 assert testing_file_store 1abcd
48 assert testing_file_store[0].file.closed 1abcd
51# For UploadFile coverage, segments copied from Starlette tests
54@pytest.mark.anyio 1efgh
55async def test_upload_file(): 1efgh
56 stream = io.BytesIO(b"data") 1ijklmnop
57 file = UploadFile(filename="file", file=stream, size=4) 1ijklmnop
58 assert await file.read() == b"data" 1ijklmnop
59 assert file.size == 4 1ijklmnop
60 await file.write(b" and more data!") 1ijklmnop
61 assert await file.read() == b"" 1ijklmnop
62 assert file.size == 19 1ijklmnop
63 await file.seek(0) 1ijklmnop
64 assert await file.read() == b"data and more data!" 1ijklmnop
65 await file.close() 1ijklmnop