1212from memory_profiler import memory_usage
1313from typing import Final , List , Callable
1414from pydantic import ByteSize
15+ from _utils import skip_if_osparc_version
16+ from packaging .version import Version
1517
1618_KB : ByteSize = ByteSize (1024 ) # in bytes
1719_MB : ByteSize = ByteSize (_KB * 1024 ) # in bytes
@@ -33,7 +35,30 @@ def _hash_file(file: Path) -> str:
3335def test_upload_file (
3436 create_tmp_file : Callable [[ByteSize ], Path ], api_client : osparc .ApiClient
3537) -> None :
36- """Test that we can upload a file via the multipart upload and download it again. Also check RAM usage of upload/download fcns"""
38+ """Test that we can upload a file via the multipart upload and download it again."""
39+ tmp_file = create_tmp_file (ByteSize (1 * _GB ))
40+ tmp_path : Path = tmp_file .parent
41+ files_api : osparc .FilesApi = osparc .FilesApi (api_client = api_client )
42+ try :
43+ uploaded_file1 : osparc .File = files_api .upload_file (tmp_file )
44+ uploaded_file2 : osparc .File = files_api .upload_file (tmp_file )
45+ assert (
46+ uploaded_file1 .id == uploaded_file2 .id
47+ ), "could not detect that file was already on server"
48+ downloaded_file = files_api .download_file (
49+ uploaded_file1 .id , destination_folder = tmp_path , retval = True
50+ )
51+ assert Path (downloaded_file ).parent == tmp_path
52+ assert _hash_file (Path (downloaded_file )) == _hash_file (tmp_file )
53+ finally :
54+ files_api .delete_file (uploaded_file1 .id )
55+
56+
57+ @skip_if_osparc_version (at_least = Version ("0.8.3.post0.dev12" ))
58+ def test_upload_download_file_ram_usage (
59+ create_tmp_file : Callable [[ByteSize ], Path ], api_client : osparc .ApiClient
60+ ) -> None :
61+ """Check RAM usage of upload/download fcns"""
3762 _allowed_ram_usage_in_mb : Final [int ] = 300 # 300MB
3863 tmp_file = create_tmp_file (ByteSize (1 * _GB ))
3964 assert (
@@ -53,10 +78,6 @@ def max_diff(data: List[int]) -> int:
5378 assert (
5479 max_diff (upload_ram_usage_in_mb ) < _allowed_ram_usage_in_mb
5580 ), f"Used more than { _allowed_ram_usage_in_mb = } to upload file of size { tmp_file .stat ().st_size = } "
56- uploaded_file2 : osparc .File = files_api .upload_file (tmp_file )
57- assert (
58- uploaded_file1 .id == uploaded_file2 .id
59- ), "could not detect that file was already on server"
6081 download_ram_usage_in_mb , downloaded_file = memory_usage (
6182 (
6283 files_api .download_file ,
@@ -67,8 +88,7 @@ def max_diff(data: List[int]) -> int:
6788 )
6889 assert (
6990 max_diff (download_ram_usage_in_mb ) < _allowed_ram_usage_in_mb
70- ), f"Used more than { _allowed_ram_usage_in_mb = } to down file of size { Path (downloaded_file ).stat ().st_size = } "
71- assert Path (downloaded_file ).parent == tmp_path
91+ ), f"Used more than { _allowed_ram_usage_in_mb = } to download file of size { Path (downloaded_file ).stat ().st_size = } "
7292 assert _hash_file (Path (downloaded_file )) == _hash_file (tmp_file )
7393 finally :
7494 files_api .delete_file (uploaded_file1 .id )
0 commit comments