Skip to content

Commit 5b41fb8

Browse files
committed
refactoring
1 parent 2e38fdc commit 5b41fb8

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

services/storage/tests/conftest.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@
6363
from tenacity.retry import retry_if_exception_type
6464
from tenacity.stop import stop_after_delay
6565
from tenacity.wait import wait_fixed
66+
from tests.fixtures.data_models import FileIDDict
67+
from tests.helpers.utils_file_meta_data import assert_file_meta_data_in_db
6668
from types_aiobotocore_s3 import S3Client
6769
from yarl import URL
6870

69-
from tests.helpers.utils_file_meta_data import assert_file_meta_data_in_db
70-
7171
pytest_plugins = [
7272
"pytest_simcore.aws_s3_service",
7373
"pytest_simcore.aws_server",
@@ -290,9 +290,9 @@ async def _link_creator(
290290
location_id=f"{location_id}",
291291
file_id=file_id,
292292
).with_query(**query_kwargs, user_id=user_id)
293-
assert "file_size" in url.query, (
294-
"V2 call to upload file must contain file_size field!"
295-
)
293+
assert (
294+
"file_size" in url.query
295+
), "V2 call to upload file must contain file_size field!"
296296
response = await client.put(f"{url}")
297297
received_file_upload, error = assert_status(
298298
response, status.HTTP_200_OK, FileUploadSchema
@@ -641,14 +641,11 @@ async def with_random_project_with_files(
641641
Awaitable[
642642
tuple[
643643
ProjectAtDB,
644-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
644+
dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
645645
]
646646
],
647647
],
648-
) -> tuple[
649-
ProjectAtDB,
650-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
651-
]:
648+
) -> tuple[ProjectAtDB, dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],]:
652649
return await random_project_with_files(
653650
file_sizes=(
654651
TypeAdapter(ByteSize).validate_python("1Mib"),

services/storage/tests/fixtures/data_models.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from contextlib import asynccontextmanager
88
from pathlib import Path
99
from random import choice, randint
10-
from typing import Any, cast
10+
from typing import Any, TypedDict, cast
1111

1212
import pytest
1313
import sqlalchemy as sa
@@ -259,6 +259,11 @@ async def _creator(
259259
return _creator
260260

261261

262+
class FileIDDict(TypedDict):
263+
path: Path
264+
sha256_checksum: SHA256Str
265+
266+
262267
async def _upload_file_and_update_project(
263268
project_id: ProjectID,
264269
node_id: NodeID,
@@ -267,7 +272,7 @@ async def _upload_file_and_update_project(
267272
file_id: StorageFileID | None,
268273
file_sizes: tuple[ByteSize, ...],
269274
file_checksums: tuple[SHA256Str, ...],
270-
node_to_files_mapping: dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
275+
node_to_files_mapping: dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
271276
upload_file: Callable[..., Awaitable[tuple[Path, SimcoreS3FileID]]],
272277
create_simcore_file_id: Callable[
273278
[ProjectID, NodeID, str, Path | None], SimcoreS3FileID
@@ -304,9 +309,7 @@ async def random_project_with_files(
304309
faker: Faker,
305310
) -> Callable[
306311
[int, tuple[ByteSize, ...], tuple[SHA256Str, ...]],
307-
Awaitable[
308-
tuple[ProjectAtDB, dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]]]
309-
],
312+
Awaitable[tuple[ProjectAtDB, dict[NodeID, dict[SimcoreS3FileID, FileIDDict]]]],
310313
]:
311314
async def _creator(
312315
num_nodes: int = 12,
@@ -326,12 +329,10 @@ async def _creator(
326329
"488f3b57932803bbf644593bd46d95599b1d4da1d63bc020d7ebe6f1c255f7f3"
327330
),
328331
),
329-
) -> tuple[ProjectAtDB, dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]]]:
332+
) -> tuple[ProjectAtDB, dict[NodeID, dict[SimcoreS3FileID, FileIDDict]]]:
330333
assert len(file_sizes) == len(file_checksums)
331334
project = await create_project(name="random-project")
332-
node_to_files_mapping: dict[
333-
NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]
334-
] = {}
335+
node_to_files_mapping: dict[NodeID, dict[SimcoreS3FileID, FileIDDict]] = {}
335336
upload_tasks: deque[Awaitable] = deque()
336337
for _node_index in range(num_nodes):
337338
# Create a node with outputs (files and others)

services/storage/tests/unit/test_handlers_files.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@
6161
from tenacity.retry import retry_if_exception_type
6262
from tenacity.stop import stop_after_delay
6363
from tenacity.wait import wait_fixed
64+
from tests.fixtures.data_models import FileIDDict
65+
from tests.helpers.utils_file_meta_data import assert_file_meta_data_in_db
6466
from types_aiobotocore_s3 import S3Client
6567
from yarl import URL
6668

67-
from tests.helpers.utils_file_meta_data import assert_file_meta_data_in_db
68-
6969
pytest_simcore_core_services_selection = ["postgres"]
7070
pytest_simcore_ops_services_selection = ["adminer"]
7171

@@ -91,14 +91,14 @@ async def assert_multipart_uploads_in_progress(
9191
tuple[UploadID, S3ObjectKey]
9292
] = await storage_s3_client.list_ongoing_multipart_uploads(bucket=storage_s3_bucket)
9393
if expected_upload_ids is None:
94-
assert not list_uploads, (
95-
f"expected NO multipart uploads in progress, got {list_uploads}"
96-
)
94+
assert (
95+
not list_uploads
96+
), f"expected NO multipart uploads in progress, got {list_uploads}"
9797
else:
9898
for upload_id, _ in list_uploads:
99-
assert upload_id in expected_upload_ids, (
100-
f"{upload_id=} is in progress but was not expected!"
101-
)
99+
assert (
100+
upload_id in expected_upload_ids
101+
), f"{upload_id=} is in progress but was not expected!"
102102

103103

104104
@dataclass
@@ -209,9 +209,9 @@ async def _link_creator(file_id: SimcoreS3FileID, **query_kwargs) -> PresignedLi
209209
location_id=f"{location_id}",
210210
file_id=file_id,
211211
).with_query(**query_kwargs, user_id=user_id)
212-
assert "file_size" not in url.query, (
213-
"v1 call to upload_file MUST NOT contain file_size field, this is reserved for v2 call"
214-
)
212+
assert (
213+
"file_size" not in url.query
214+
), "v1 call to upload_file MUST NOT contain file_size field, this is reserved for v2 call"
215215
response = await client.put(f"{url}")
216216
received_file_upload_link, error = assert_status(
217217
response, status.HTTP_200_OK, PresignedLink
@@ -390,9 +390,9 @@ async def test_create_upload_file_presigned_with_file_size_returns_multipart_lin
390390
file_size=f"{test_param.file_size}",
391391
)
392392
# number of links
393-
assert len(received_file_upload.urls) == test_param.expected_num_links, (
394-
f"{len(received_file_upload.urls)} vs {test_param.expected_num_links=}"
395-
)
393+
assert (
394+
len(received_file_upload.urls) == test_param.expected_num_links
395+
), f"{len(received_file_upload.urls)} vs {test_param.expected_num_links=}"
396396
# all links are unique
397397
assert len(set(received_file_upload.urls)) == len(received_file_upload.urls)
398398
assert received_file_upload.chunk_size == test_param.expected_chunk_size
@@ -547,9 +547,9 @@ async def test_upload_same_file_uuid_aborts_previous_upload(
547547
expected_sha256_checksum=None,
548548
)
549549
if expect_upload_id and link_type == LinkType.PRESIGNED:
550-
assert upload_id != new_upload_id, (
551-
"There shall be a new upload id after a new call to create_upload_file"
552-
)
550+
assert (
551+
upload_id != new_upload_id
552+
), "There shall be a new upload id after a new call to create_upload_file"
553553
elif expect_upload_id and link_type == LinkType.S3:
554554
assert upload_id == new_upload_id
555555
assert upload_id == S3_UNDEFINED_OR_EXTERNAL_MULTIPART_ID
@@ -1483,7 +1483,7 @@ async def test_listing_with_project_id_filter(
14831483
Awaitable[
14841484
tuple[
14851485
ProjectAtDB,
1486-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
1486+
dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
14871487
]
14881488
],
14891489
],

services/storage/tests/unit/test_handlers_paths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pytest_simcore.helpers.fastapi import url_from_operation_id
2020
from pytest_simcore.helpers.httpx_assert_checks import assert_status
2121
from servicelib.aiohttp import status
22+
from tests.fixtures.data_models import FileIDDict
2223

2324
pytest_simcore_core_services_selection = ["postgres"]
2425
pytest_simcore_ops_services_selection = ["adminer"]
@@ -55,7 +56,7 @@ async def test_list_paths_root_folder(
5556
user_id: UserID,
5657
with_random_project_with_files: tuple[
5758
ProjectAtDB,
58-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
59+
dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
5960
],
6061
):
6162
project_in_db, list_of_files = with_random_project_with_files

services/storage/tests/unit/test_handlers_simcore_s3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from simcore_service_storage.models import SearchFilesQueryParams
3838
from simcore_service_storage.simcore_s3_dsm import SimcoreS3DataManager
3939
from sqlalchemy.ext.asyncio import AsyncEngine
40+
from tests.fixtures.data_models import FileIDDict
4041
from tests.helpers.utils_file_meta_data import assert_file_meta_data_in_db
4142
from tests.helpers.utils_project import clone_project_data
4243
from yarl import URL
@@ -213,7 +214,7 @@ async def test_copy_folders_from_valid_project_with_one_large_file(
213214
Awaitable[
214215
tuple[
215216
ProjectAtDB,
216-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | str]]],
217+
dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
217218
]
218219
],
219220
],
@@ -283,7 +284,7 @@ async def test_copy_folders_from_valid_project(
283284
Awaitable[
284285
tuple[
285286
ProjectAtDB,
286-
dict[NodeID, dict[SimcoreS3FileID, dict[str, Path | SHA256Str]]],
287+
dict[NodeID, dict[SimcoreS3FileID, FileIDDict]],
287288
]
288289
],
289290
],

0 commit comments

Comments
 (0)