|
| 1 | +from pathlib import Path |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from murfey.client.destinations import determine_default_destination |
| 7 | + |
| 8 | +source_list = [ |
| 9 | + ["X:/DoseFractions/cm12345-6/Supervisor", "Supervisor", True, "extra_name"], |
| 10 | + ["X:/DoseFractions/Supervisor/Images-Disc1", "Supervisor", False, ""], |
| 11 | + [ |
| 12 | + "X:/DoseFractions/DATA/Supervisor/Sample1", |
| 13 | + "Supervisor_Sample1", |
| 14 | + False, |
| 15 | + "extra_name", |
| 16 | + ], |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +@mock.patch("murfey.client.destinations.capture_get") |
| 21 | +@mock.patch("murfey.client.destinations.capture_post") |
| 22 | +@pytest.mark.parametrize("sources", source_list) |
| 23 | +def test_determine_default_destinations_suggested_path(mock_post, mock_get, sources): |
| 24 | + mock_environment = mock.Mock() |
| 25 | + mock_environment.murfey_session = 2 |
| 26 | + mock_environment.instrument_name = "m01" |
| 27 | + mock_environment.destination_registry = {} |
| 28 | + |
| 29 | + source, source_name, touch, extra_directory = sources |
| 30 | + |
| 31 | + mock_get().json.return_value = { |
| 32 | + "data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"] |
| 33 | + } |
| 34 | + mock_post().json.return_value = {"suggested_path": "/base_path/2025/cm12345-6/raw"} |
| 35 | + |
| 36 | + destination = determine_default_destination( |
| 37 | + visit="cm12345-6", |
| 38 | + source=Path(source), |
| 39 | + destination="2025", |
| 40 | + environment=mock_environment, |
| 41 | + token="token", |
| 42 | + touch=touch, |
| 43 | + extra_directory=extra_directory, |
| 44 | + use_suggested_path=True, |
| 45 | + ) |
| 46 | + mock_get.assert_any_call( |
| 47 | + base_url=str(mock_environment.url.geturl()), |
| 48 | + router_name="session_control.router", |
| 49 | + function_name="machine_info_by_instrument", |
| 50 | + token="token", |
| 51 | + instrument_name="m01", |
| 52 | + ) |
| 53 | + mock_post.assert_any_call( |
| 54 | + base_url=str(mock_environment.url.geturl()), |
| 55 | + router_name="file_io_instrument.router", |
| 56 | + function_name="suggest_path", |
| 57 | + token="token", |
| 58 | + visit_name="cm12345-6", |
| 59 | + session_id=2, |
| 60 | + data={ |
| 61 | + "base_path": "2025/cm12345-6/raw", |
| 62 | + "touch": touch, |
| 63 | + "extra_directory": extra_directory, |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + assert destination == f"/base_path/2025/cm12345-6/raw/{extra_directory}" |
| 68 | + assert ( |
| 69 | + mock_environment.destination_registry.get(source_name) |
| 70 | + == "/base_path/2025/cm12345-6/raw" |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +@mock.patch("murfey.client.destinations.capture_get") |
| 75 | +@pytest.mark.parametrize("sources", source_list) |
| 76 | +def test_determine_default_destinations_skip_suggested(mock_get, sources): |
| 77 | + mock_environment = mock.Mock() |
| 78 | + mock_environment.murfey_session = 2 |
| 79 | + mock_environment.instrument_name = "m01" |
| 80 | + mock_environment.destination_registry = {} |
| 81 | + |
| 82 | + source, source_name, touch, extra_directory = sources |
| 83 | + |
| 84 | + mock_get().json.return_value = { |
| 85 | + "data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"] |
| 86 | + } |
| 87 | + |
| 88 | + destination = determine_default_destination( |
| 89 | + visit="cm12345-6", |
| 90 | + source=Path(source), |
| 91 | + destination="2025", |
| 92 | + environment=mock_environment, |
| 93 | + token="token", |
| 94 | + touch=touch, |
| 95 | + extra_directory=extra_directory, |
| 96 | + use_suggested_path=False, |
| 97 | + ) |
| 98 | + mock_get.assert_any_call( |
| 99 | + base_url=str(mock_environment.url.geturl()), |
| 100 | + router_name="session_control.router", |
| 101 | + function_name="machine_info_by_instrument", |
| 102 | + token="token", |
| 103 | + instrument_name="m01", |
| 104 | + ) |
| 105 | + |
| 106 | + assert destination == f"2025/cm12345-6/{Path(source).name}/{extra_directory}" |
| 107 | + |
| 108 | + |
| 109 | +parameter_list_fail_cases = [ |
| 110 | + ["X:/DoseFractions/cm12345-6", "", "2025", "X:/DoseFractions"], |
| 111 | + ["X:/DoseFractions/cm12345-6", "cm12345-6", "", "X:/DoseFractions"], |
| 112 | + ["X:/DoseFractions", "cm12345-6", "2025", "X:/DoseFractions"], |
| 113 | + ["X:/cm12345-6", "cm12345-6", "2025", "X:/DoseFractions"], |
| 114 | +] |
| 115 | + |
| 116 | + |
| 117 | +@mock.patch("murfey.client.destinations.capture_get") |
| 118 | +@pytest.mark.parametrize("destination_params", parameter_list_fail_cases) |
| 119 | +def test_determine_default_destinations_failures(mock_get, destination_params): |
| 120 | + """ |
| 121 | + Test failure of the following cases: |
| 122 | + No visit, no destination, source = default, source not in default |
| 123 | + """ |
| 124 | + mock_get().json.return_value = { |
| 125 | + "data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"] |
| 126 | + } |
| 127 | + source, visit, destination, default_dests = destination_params |
| 128 | + mock_environment = mock.Mock() |
| 129 | + with pytest.raises(ValueError): |
| 130 | + determine_default_destination( |
| 131 | + visit=visit, |
| 132 | + source=Path(source), |
| 133 | + destination=destination, |
| 134 | + environment=mock_environment, |
| 135 | + token="token", |
| 136 | + ) |
0 commit comments