-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_destinations.py
More file actions
243 lines (214 loc) · 7.79 KB
/
test_destinations.py
File metadata and controls
243 lines (214 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from pathlib import Path
from unittest import mock
import pytest
from pytest_mock import MockerFixture
from murfey.client.destinations import (
determine_default_destination,
find_longest_data_directory,
)
@pytest.mark.parametrize(
"test_params",
(
# File to match | Use Path? | Expected result
(
"X:/cm12345-6/Supervisor/Images-Disc1/file",
True,
("X:/", "cm12345-6/Supervisor/Images-Disc1"),
),
(
"X:/DATA/cm12345-6/Supervisor/Images-Disc1/file",
False,
("X:/DATA", "cm12345-6/Supervisor/Images-Disc1"),
),
(
"X:/DoseFractions/cm12345-6/Supervisor/Images-Disc1/file",
True,
("X:/DoseFractions", "cm12345-6/Supervisor/Images-Disc1"),
),
(
"X:/DoseFractions/DATA/cm12345-6/Supervisor/Images-Disc1/file",
False,
("X:/DoseFractions/DATA", "cm12345-6/Supervisor/Images-Disc1"),
),
),
)
def test_find_longest_data_directory(
mocker: MockerFixture, test_params: tuple[str, bool, tuple[str, str]]
):
# Unpack test params
match_path, use_path, (expected_base_dir, expected_mid_dir) = test_params
# Construct data directories using strings or Paths as needed
data_directories: list[str] | list[Path] = [
"X:",
"X:/DATA",
"X:/DoseFractions",
"X:/DoseFractions/DATA",
]
if use_path:
data_directories = [Path(dd) for dd in data_directories]
# Patch Pathlib's 'absolute()' function, since we are simulating Windows on Linux
mocker.patch("murfey.client.destinations.Path.absolute", lambda self: self)
base_dir, mid_dir = find_longest_data_directory(
Path(match_path),
data_directories,
)
assert base_dir == Path(expected_base_dir)
assert mid_dir == Path(expected_mid_dir)
source_list = [
["X:/DoseFractions/cm12345-6/Supervisor", "Supervisor", True, "extra_name"],
["X:/DoseFractions/Supervisor/Images-Disc1", "Supervisor", False, ""],
[
"X:/DoseFractions/DATA/Supervisor/Sample1",
"Supervisor_Sample1",
False,
"extra_name",
],
]
@mock.patch("murfey.client.destinations.capture_get")
@mock.patch("murfey.client.destinations.capture_post")
@pytest.mark.parametrize("sources", source_list)
def test_determine_default_destinations_suggested_path(mock_post, mock_get, sources):
mock_environment = mock.Mock()
mock_environment.murfey_session = 2
mock_environment.instrument_name = "m01"
mock_environment.destination_registry = {}
source, source_name, touch, extra_directory = sources
mock_get().json.return_value = {
"data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"]
}
mock_post().json.return_value = {"suggested_path": "/base_path/2025/cm12345-6/raw"}
destination = determine_default_destination(
visit="cm12345-6",
source=Path(source),
destination="2025",
environment=mock_environment,
token="token",
touch=touch,
extra_directory=extra_directory,
use_suggested_path=True,
)
mock_get.assert_any_call(
base_url=str(mock_environment.url.geturl()),
router_name="session_control.router",
function_name="machine_info_by_instrument",
token="token",
instrument_name="m01",
)
mock_post.assert_any_call(
base_url=str(mock_environment.url.geturl()),
router_name="file_io_instrument.router",
function_name="suggest_path",
token="token",
visit_name="cm12345-6",
session_id=2,
data={
"base_path": "2025/cm12345-6/raw",
"touch": touch,
"extra_directory": extra_directory,
},
)
assert destination == f"/base_path/2025/cm12345-6/raw/{extra_directory}"
assert (
mock_environment.destination_registry.get(source_name)
== "/base_path/2025/cm12345-6/raw"
)
@mock.patch("murfey.client.destinations.capture_get")
@mock.patch("murfey.client.destinations.capture_post")
def test_determine_default_destinations_short_path(mock_post, mock_get):
"""Test for the case where the data directory is a drive"""
mock_environment = mock.Mock()
mock_environment.murfey_session = 2
mock_environment.instrument_name = "m01"
mock_environment.destination_registry = {}
mock_get().json.return_value = {"data_directories": ["X:/"]}
mock_post().json.return_value = {"suggested_path": "/base_path/2025/cm12345-6/raw"}
destination = determine_default_destination(
visit="cm12345-6",
source=Path("X:/cm12345-6/Supervisor"),
destination="2025",
environment=mock_environment,
token="token",
touch=True,
extra_directory="",
use_suggested_path=True,
)
mock_get.assert_any_call(
base_url=str(mock_environment.url.geturl()),
router_name="session_control.router",
function_name="machine_info_by_instrument",
token="token",
instrument_name="m01",
)
mock_post.assert_any_call(
base_url=str(mock_environment.url.geturl()),
router_name="file_io_instrument.router",
function_name="suggest_path",
token="token",
visit_name="cm12345-6",
session_id=2,
data={
"base_path": "2025/cm12345-6/raw",
"touch": True,
"extra_directory": "",
},
)
assert destination == "/base_path/2025/cm12345-6/raw/"
assert (
mock_environment.destination_registry.get("Supervisor")
== "/base_path/2025/cm12345-6/raw"
)
@mock.patch("murfey.client.destinations.capture_get")
@pytest.mark.parametrize("sources", source_list)
def test_determine_default_destinations_skip_suggested(mock_get, sources):
mock_environment = mock.Mock()
mock_environment.murfey_session = 2
mock_environment.instrument_name = "m01"
mock_environment.destination_registry = {}
source, source_name, touch, extra_directory = sources
mock_get().json.return_value = {
"data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"]
}
destination = determine_default_destination(
visit="cm12345-6",
source=Path(source),
destination="2025",
environment=mock_environment,
token="token",
touch=touch,
extra_directory=extra_directory,
use_suggested_path=False,
)
mock_get.assert_any_call(
base_url=str(mock_environment.url.geturl()),
router_name="session_control.router",
function_name="machine_info_by_instrument",
token="token",
instrument_name="m01",
)
assert destination == f"2025/cm12345-6/{Path(source).name}/{extra_directory}"
parameter_list_fail_cases = [
["X:/DoseFractions/cm12345-6", "", "2025", "X:/DoseFractions"],
["X:/DoseFractions/cm12345-6", "cm12345-6", "", "X:/DoseFractions"],
["X:/DoseFractions", "cm12345-6", "2025", "X:/DoseFractions"],
["X:/cm12345-6", "cm12345-6", "2025", "X:/DoseFractions"],
]
@mock.patch("murfey.client.destinations.capture_get")
@pytest.mark.parametrize("destination_params", parameter_list_fail_cases)
def test_determine_default_destinations_failures(mock_get, destination_params):
"""
Test failure of the following cases:
No visit, no destination, source = default, source not in default
"""
mock_get().json.return_value = {
"data_directories": ["X:/DoseFractions", "X:/DoseFractions/DATA"]
}
source, visit, destination, default_dests = destination_params
mock_environment = mock.Mock()
with pytest.raises(ValueError):
determine_default_destination(
visit=visit,
source=Path(source),
destination=destination,
environment=mock_environment,
token="token",
)