|
| 1 | +from pathlib import Path |
| 2 | + |
1 | 3 | import pytest
|
2 |
| -from simcore_service_storage.utils.simcore_s3_dsm_utils import compute_file_id_prefix |
| 4 | +from aws_library.s3._models import S3ObjectKey |
| 5 | +from simcore_service_storage.utils.simcore_s3_dsm_utils import ( |
| 6 | + UserSelectionStr, |
| 7 | + _base_path_parent, |
| 8 | + compute_file_id_prefix, |
| 9 | + ensure_user_selection_from_same_base_directory, |
| 10 | +) |
3 | 11 |
|
4 | 12 |
|
5 | 13 | @pytest.mark.parametrize(
|
|
19 | 27 | )
|
20 | 28 | def test_compute_file_id_prefix(file_id, levels, expected):
|
21 | 29 | assert compute_file_id_prefix(file_id, levels) == expected
|
| 30 | + |
| 31 | + |
| 32 | +_FOLDERS_PATH = Path("nested/folders/path") |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize( |
| 36 | + "selection, s3_object, expected", |
| 37 | + [ |
| 38 | + ("single_file", "single_file", "single_file"), |
| 39 | + ("single_folder", "single_folder", "single_folder"), |
| 40 | + ("a/b/c", "a/b/c/d/e/f/g", "c/d/e/f/g"), |
| 41 | + (_FOLDERS_PATH / "folder", _FOLDERS_PATH / "folder", "folder"), |
| 42 | + (_FOLDERS_PATH / "a_file.txt", _FOLDERS_PATH / "a_file.txt", "a_file.txt"), |
| 43 | + (_FOLDERS_PATH, _FOLDERS_PATH / "with/some/content", "path/with/some/content"), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test__base_path_parent(selection: Path | str, s3_object: Path, expected: str): |
| 47 | + assert ( |
| 48 | + _base_path_parent(UserSelectionStr(f"{selection}"), S3ObjectKey(f"{s3_object}")) |
| 49 | + == expected |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "user_selection, expected", |
| 55 | + [ |
| 56 | + ([], True), |
| 57 | + (["folder"], True), |
| 58 | + (["folder", "folder"], True), |
| 59 | + (["", ""], True), |
| 60 | + ([""], True), |
| 61 | + ([_FOLDERS_PATH / "a", _FOLDERS_PATH / "b"], True), |
| 62 | + (["a.txt", "b.txt"], True), |
| 63 | + (["a/a.txt"], True), |
| 64 | + # not same parent |
| 65 | + (["firsta/file", "second/file"], False), |
| 66 | + (["a/a.txt", "a.txt", "c.txt", "a/d.txt"], False), |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_ensure_user_selection_from_same_base_directory( |
| 70 | + user_selection: list[S3ObjectKey | Path], expected: bool |
| 71 | +): |
| 72 | + assert ( |
| 73 | + ensure_user_selection_from_same_base_directory([f"{x}" for x in user_selection]) |
| 74 | + == expected |
| 75 | + ) |
0 commit comments