I am trying to test fastapi router that get List of files. In html request using JS its working but I need to test it. I am using TestClient from fastapi, when I am trying so send list I get status code 422 so I go which documentry and try dict but than I get list of only 1 file.
@router.post('/uploadone')
async def upload_file(response: Response,files:List = File(...)):
try:
properties = json.loads(files[len(files)-1])
check_file_type(files[:len(files)-1])
def test_uploadone(self):
with open('upload_data/system_test/properties.json', 'rb') as file1:
json_file = json.load(file1)
with open('upload_data/system_test/heatmap1.csv', 'rb') as file:
body = file.read()
response = self.client.post('/actions/uploadone',
files={'files':('design_matrix1.csv', body),'json':
('prop.json', json.dumps(json_file))})
self.assertTrue(response.status_code == 200)
thanks for helping
I had problems testing uploading a list of files, I achieved it using a list format instead of a dict
files = [
("parameter", ("file1.txt", BytesIO(b"aaa"))),
("parameter", ("file2.txt", BytesIO(b"bbb"))),
]
response = self.test_client.post("/api/upload", files=files)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With