From 5adb86a59bd3d0b9d1ae4d74fc807ed276491331 Mon Sep 17 00:00:00 2001 From: Laurent LAPORTE Date: Mon, 4 Sep 2023 10:35:13 +0200 Subject: [PATCH] test(api): update stream file download tests for compatibility with Requests and Httpx --- tests/storage/integration/test_exporter.py | 28 ++++++++++++++++++---- tests/storage/web/test_studies_bp.py | 22 +++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/tests/storage/integration/test_exporter.py b/tests/storage/integration/test_exporter.py index b888869a42..3e4e5666f3 100644 --- a/tests/storage/integration/test_exporter.py +++ b/tests/storage/integration/test_exporter.py @@ -1,3 +1,5 @@ +import io +import json import zipfile from pathlib import Path from typing import List, Optional @@ -52,12 +54,28 @@ def assert_url_content(url: str, tmp_dir: Path, sta_mini_zip_path: Path) -> byte metadata_repository=repo, config=config, ) + + # Simulate the download of data using a streamed request client = TestClient(app) - res = client.get(url, stream=True) - download_filepath = ftm.fetch_download( - FileDownloadTaskDTO.parse_obj(res.json()).file.id, - RequestParameters(user=DEFAULT_ADMIN_USER), - ).path + if client.stream is False: + # `TestClient` is based on `Requests` (old way before AntaREST-v2.15) + # noinspection PyArgumentList + res = client.get(url, stream=True) + res.raise_for_status() + result = res.json() + else: + # `TestClient` is based on `httpx` (new way since AntaREST-v2.15) + data = io.BytesIO() + # noinspection PyCallingNonCallable + with client.stream("GET", url) as res: + for chunk in res.iter_bytes(): + data.write(chunk) + res.raise_for_status() + result = json.loads(data.getvalue()) + + download_task = FileDownloadTaskDTO(**result) + parameters = RequestParameters(user=DEFAULT_ADMIN_USER) + download_filepath = ftm.fetch_download(download_task.file.id, parameters).path with open(download_filepath, "rb") as fh: return fh.read() diff --git a/tests/storage/web/test_studies_bp.py b/tests/storage/web/test_studies_bp.py index a513872529..0a87e81a6e 100644 --- a/tests/storage/web/test_studies_bp.py +++ b/tests/storage/web/test_studies_bp.py @@ -1,4 +1,5 @@ import io +import json import shutil import uuid from datetime import datetime @@ -347,10 +348,27 @@ def test_export_files(tmp_path: Path) -> None: user_service=Mock(), matrix_service=Mock(spec=MatrixService), ) + + # Simulate the download of data using a streamed request client = TestClient(app) - result = client.get("/v1/studies/name/export", stream=True) + if client.stream is False: + # `TestClient` is based on `Requests` (old way before AntaREST-v2.15) + # noinspection PyArgumentList + res = client.get("/v1/studies/name/export", stream=True) + res.raise_for_status() + result = res.json() + else: + # `TestClient` is based on `httpx` (new way since AntaREST-v2.15) + data = io.BytesIO() + # noinspection PyCallingNonCallable + with client.stream("GET", "/v1/studies/name/export") as res: + for chunk in res.iter_bytes(): + data.write(chunk) + res.raise_for_status() + result = json.loads(data.getvalue()) + + assert FileDownloadTaskDTO(**result).json() == expected.json() - assert FileDownloadTaskDTO.parse_obj(result.json()).json() == expected.json() mock_storage_service.export_study.assert_called_once_with("name", PARAMS, True)