Coverage for tests/test_optional_file_list.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from typing import List, Optional 1abcdefg

2 

3from fastapi import FastAPI, File 1abcdefg

4from fastapi.testclient import TestClient 1abcdefg

5 

6app = FastAPI() 1abcdefg

7 

8 

9@app.post("/files") 1abcdefg

10async def upload_files(files: Optional[List[bytes]] = File(None)): 1abcdefg

11 if files is None: 1hijklmnopqrstu

12 return {"files_count": 0} 1ikmoqsu

13 return {"files_count": len(files), "sizes": [len(f) for f in files]} 1hjlnprt

14 

15 

16def test_optional_bytes_list(): 1abcdefg

17 client = TestClient(app) 1hjlnprt

18 response = client.post( 1hjlnprt

19 "/files", 

20 files=[("files", b"content1"), ("files", b"content2")], 

21 ) 

22 assert response.status_code == 200 1hjlnprt

23 assert response.json() == {"files_count": 2, "sizes": [8, 8]} 1hjlnprt

24 

25 

26def test_optional_bytes_list_no_files(): 1abcdefg

27 client = TestClient(app) 1ikmoqsu

28 response = client.post("/files") 1ikmoqsu

29 assert response.status_code == 200 1ikmoqsu

30 assert response.json() == {"files_count": 0} 1ikmoqsu