|
2 | 2 | from unittest import mock |
3 | 3 |
|
4 | 4 | import pytest |
| 5 | +from pytest_mock import MockerFixture |
| 6 | + |
| 7 | +from murfey.client.destinations import ( |
| 8 | + determine_default_destination, |
| 9 | + find_longest_data_directory, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "test_params", |
| 15 | + ( |
| 16 | + # File to match | Use Path? | Expected result |
| 17 | + ( |
| 18 | + "X:/cm12345-6/Supervisor/Images-Disc1/file", |
| 19 | + True, |
| 20 | + ("X:/", "cm12345-6/Supervisor/Images-Disc1"), |
| 21 | + ), |
| 22 | + ( |
| 23 | + "X:/DATA/cm12345-6/Supervisor/Images-Disc1/file", |
| 24 | + False, |
| 25 | + ("X:/DATA", "cm12345-6/Supervisor/Images-Disc1"), |
| 26 | + ), |
| 27 | + ( |
| 28 | + "X:/DoseFractions/cm12345-6/Supervisor/Images-Disc1/file", |
| 29 | + True, |
| 30 | + ("X:/DoseFractions", "cm12345-6/Supervisor/Images-Disc1"), |
| 31 | + ), |
| 32 | + ( |
| 33 | + "X:/DoseFractions/DATA/cm12345-6/Supervisor/Images-Disc1/file", |
| 34 | + False, |
| 35 | + ("X:/DoseFractions/DATA", "cm12345-6/Supervisor/Images-Disc1"), |
| 36 | + ), |
| 37 | + ), |
| 38 | +) |
| 39 | +def test_find_longest_data_directory( |
| 40 | + mocker: MockerFixture, test_params: tuple[str, bool, tuple[str, str]] |
| 41 | +): |
| 42 | + # Unpack test params |
| 43 | + match_path, use_path, (expected_base_dir, expected_mid_dir) = test_params |
| 44 | + |
| 45 | + # Construct data directories using strings or Paths as needed |
| 46 | + data_directories: list[str] | list[Path] = [ |
| 47 | + "X:", |
| 48 | + "X:/DATA", |
| 49 | + "X:/DoseFractions", |
| 50 | + "X:/DoseFractions/DATA", |
| 51 | + ] |
| 52 | + if use_path: |
| 53 | + data_directories = [Path(dd) for dd in data_directories] |
| 54 | + # Patch Pathlib's 'absolute()' function, since we are simulating Windows on Linux |
| 55 | + mocker.patch("murfey.client.destinations.Path.absolute", lambda self: self) |
| 56 | + |
| 57 | + base_dir, mid_dir = find_longest_data_directory( |
| 58 | + Path(match_path), |
| 59 | + data_directories, |
| 60 | + ) |
| 61 | + assert base_dir == Path(expected_base_dir) |
| 62 | + assert mid_dir == Path(expected_mid_dir) |
5 | 63 |
|
6 | | -from murfey.client.destinations import determine_default_destination |
7 | 64 |
|
8 | 65 | source_list = [ |
9 | 66 | ["X:/DoseFractions/cm12345-6/Supervisor", "Supervisor", True, "extra_name"], |
@@ -71,6 +128,56 @@ def test_determine_default_destinations_suggested_path(mock_post, mock_get, sour |
71 | 128 | ) |
72 | 129 |
|
73 | 130 |
|
| 131 | +@mock.patch("murfey.client.destinations.capture_get") |
| 132 | +@mock.patch("murfey.client.destinations.capture_post") |
| 133 | +def test_determine_default_destinations_short_path(mock_post, mock_get): |
| 134 | + """Test for the case where the data directory is a drive""" |
| 135 | + mock_environment = mock.Mock() |
| 136 | + mock_environment.murfey_session = 2 |
| 137 | + mock_environment.instrument_name = "m01" |
| 138 | + mock_environment.destination_registry = {} |
| 139 | + |
| 140 | + mock_get().json.return_value = {"data_directories": ["X:/"]} |
| 141 | + mock_post().json.return_value = {"suggested_path": "/base_path/2025/cm12345-6/raw"} |
| 142 | + |
| 143 | + destination = determine_default_destination( |
| 144 | + visit="cm12345-6", |
| 145 | + source=Path("X:/cm12345-6/Supervisor"), |
| 146 | + destination="2025", |
| 147 | + environment=mock_environment, |
| 148 | + token="token", |
| 149 | + touch=True, |
| 150 | + extra_directory="", |
| 151 | + use_suggested_path=True, |
| 152 | + ) |
| 153 | + mock_get.assert_any_call( |
| 154 | + base_url=str(mock_environment.url.geturl()), |
| 155 | + router_name="session_control.router", |
| 156 | + function_name="machine_info_by_instrument", |
| 157 | + token="token", |
| 158 | + instrument_name="m01", |
| 159 | + ) |
| 160 | + mock_post.assert_any_call( |
| 161 | + base_url=str(mock_environment.url.geturl()), |
| 162 | + router_name="file_io_instrument.router", |
| 163 | + function_name="suggest_path", |
| 164 | + token="token", |
| 165 | + visit_name="cm12345-6", |
| 166 | + session_id=2, |
| 167 | + data={ |
| 168 | + "base_path": "2025/cm12345-6/raw", |
| 169 | + "touch": True, |
| 170 | + "extra_directory": "", |
| 171 | + }, |
| 172 | + ) |
| 173 | + |
| 174 | + assert destination == "/base_path/2025/cm12345-6/raw/" |
| 175 | + assert ( |
| 176 | + mock_environment.destination_registry.get("Supervisor") |
| 177 | + == "/base_path/2025/cm12345-6/raw" |
| 178 | + ) |
| 179 | + |
| 180 | + |
74 | 181 | @mock.patch("murfey.client.destinations.capture_get") |
75 | 182 | @pytest.mark.parametrize("sources", source_list) |
76 | 183 | def test_determine_default_destinations_skip_suggested(mock_get, sources): |
|
0 commit comments