12
12
from memory_profiler import memory_usage
13
13
from typing import Final , List , Callable
14
14
from pydantic import ByteSize
15
+ from _utils import skip_if_osparc_version
16
+ from packaging .version import Version
15
17
16
18
_KB : ByteSize = ByteSize (1024 ) # in bytes
17
19
_MB : ByteSize = ByteSize (_KB * 1024 ) # in bytes
@@ -33,7 +35,30 @@ def _hash_file(file: Path) -> str:
33
35
def test_upload_file (
34
36
create_tmp_file : Callable [[ByteSize ], Path ], api_client : osparc .ApiClient
35
37
) -> 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"""
37
62
_allowed_ram_usage_in_mb : Final [int ] = 300 # 300MB
38
63
tmp_file = create_tmp_file (ByteSize (1 * _GB ))
39
64
assert (
@@ -53,10 +78,6 @@ def max_diff(data: List[int]) -> int:
53
78
assert (
54
79
max_diff (upload_ram_usage_in_mb ) < _allowed_ram_usage_in_mb
55
80
), 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"
60
81
download_ram_usage_in_mb , downloaded_file = memory_usage (
61
82
(
62
83
files_api .download_file ,
@@ -67,8 +88,7 @@ def max_diff(data: List[int]) -> int:
67
88
)
68
89
assert (
69
90
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 = } "
72
92
assert _hash_file (Path (downloaded_file )) == _hash_file (tmp_file )
73
93
finally :
74
94
files_api .delete_file (uploaded_file1 .id )
0 commit comments