-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_transfer_special_arguments.py
More file actions
227 lines (186 loc) · 6.91 KB
/
Copy pathtest_transfer_special_arguments.py
File metadata and controls
227 lines (186 loc) · 6.91 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
""" """
import fnmatch
import shutil
from pathlib import Path
import pytest
from ... import test_utils
from ..base_transfer import BaseTransfer
PARAM_SUBS = [
["all"],
["all_sub"],
["all_non_sub"],
["sub-001"],
["sub-003_date-20231201"],
["sub-002", "all_non_sub"],
]
PARAM_SES = [
["all"],
["all_non_ses"],
["all_ses"],
["ses-001"],
["ses-002_random-key"],
["all_non_ses", "ses-001"],
]
PARAM_DATATYPE = [
["all"],
["all_non_datatype"],
["all_datatype"],
["behav"],
["ephys"],
["anat"],
["funcimg"],
["anat", "behav", "all_non_datatype"],
]
class TestFileTransfer(BaseTransfer):
# ----------------------------------------------------------------------------------
# Test File Transfer - All Options
# ----------------------------------------------------------------------------------
@pytest.mark.parametrize("sub_names", PARAM_SUBS)
@pytest.mark.parametrize("ses_names", PARAM_SES)
@pytest.mark.parametrize("datatype", PARAM_DATATYPE)
@pytest.mark.parametrize("upload_or_download", ["upload", "download"])
def test_combinations_filesystem_transfer(
self,
pathtable_and_project,
sub_names,
ses_names,
datatype,
upload_or_download,
):
"""
Test many combinations of possible file transfer commands.
"""
pathtable, project = pathtable_and_project
paths_to_transferred_files = self.perform_transfer(
project, upload_or_download, sub_names, ses_names, datatype
)
expected_transferred_paths = self.get_expected_transferred_paths(
pathtable, sub_names, ses_names, datatype
)
assert sorted(paths_to_transferred_files) == sorted(
expected_transferred_paths
)
# Test Wildcards
# ----------------------------------------------------------------------------------
# It is very difficult to test wildcards using the original machinery
# for testing keywords such as "all", "all_sub" etc as used in test_combinations_filesystem_transfer().
# Therefore, test a few specific cases here.
@pytest.mark.parametrize("upload_or_download", ["upload", "download"])
def test_local_filesystem_wildcards_1(
self, pathtable_and_project, upload_or_download
):
"""Test a single custom transfer that combines different special keywords."""
pathtable, project = pathtable_and_project
sub_names = ["@*@date@*@"]
ses_names = ["all_ses"]
datatype = ["funcimg"]
paths_to_transferred_files = self.perform_transfer(
project, upload_or_download, sub_names, ses_names, datatype
)
pathtable = pathtable[
pathtable["parent_sub"]
.fillna("")
.apply(lambda x: fnmatch.fnmatch(x, "*date*"))
]
pathtable = pathtable[
pathtable["parent_datatype"].apply(lambda x: x == "funcimg")
]
expected_transferred_paths = pathtable["path"]
assert sorted(paths_to_transferred_files) == sorted(
expected_transferred_paths
)
@pytest.mark.parametrize("upload_or_download", ["upload", "download"])
def test_local_filesystem_wildcards_2(
self, pathtable_and_project, upload_or_download
):
"""Test a single custom transfer that combines different special keywords."""
pathtable, project = pathtable_and_project
sub_names = ["all_sub"]
ses_names = ["ses-003@*@"]
datatype = ["all_non_datatype"]
paths_to_transferred_files = self.perform_transfer(
project, upload_or_download, sub_names, ses_names, datatype
)
pathtable = pathtable[
pathtable["parent_ses"]
.fillna("")
.apply(lambda x: fnmatch.fnmatch(x, "ses-003*"))
]
pathtable = pathtable[pathtable["parent_datatype"].isna()]
expected_transferred_paths = pathtable["path"]
assert sorted(paths_to_transferred_files) == sorted(
expected_transferred_paths
)
@pytest.mark.parametrize("upload_or_download", ["upload", "download"])
def test_local_filesystem_wildcards_3(
self, pathtable_and_project, upload_or_download
):
"""Test a single custom transfer that combines different special keywords."""
pathtable, project = pathtable_and_project
sub_names = ["sub-002@TO@003_@*@"]
ses_names = ["ses-001"]
datatype = ["all"]
paths_to_transferred_files = self.perform_transfer(
project, upload_or_download, sub_names, ses_names, datatype
)
pathtable = pathtable[
pathtable["parent_sub"]
.fillna("")
.apply(
lambda x: (
fnmatch.fnmatch(x, "sub-002*")
or fnmatch.fnmatch(x, "sub-003*")
)
)
]
pathtable = pathtable[
pathtable["parent_ses"]
.fillna("")
.apply(lambda x: fnmatch.fnmatch(x, "ses-001"))
]
expected_transferred_paths = pathtable["path"]
assert sorted(paths_to_transferred_files) == sorted(
expected_transferred_paths
)
def perform_transfer(
self, project, upload_or_download, sub_names, ses_names, datatype
):
"""Transfer the data, swapping the paths to move a subset of
files from the already set up directory to a new directory
using upload or download.
The entire test project is created in the original `local_path`
and subset of it is uploaded and tested against. To test
upload vs. download, the `local_path` and `central_path`
locations are swapped.
"""
transfer_function = test_utils.handle_upload_or_download(
project,
upload_or_download,
transfer_method="custom",
swap_last_folder_only=False,
)[0]
transfer_function(
"rawdata", sub_names, ses_names, datatype, init_log=False
)
if upload_or_download == "download":
test_utils.swap_local_and_central_paths(
project, swap_last_folder_only=False
)
# Check what paths were actually moved
# (through the local filesystem), and test
path_to_search = (
self.central_from_local(project.cfg["local_path"]) / "rawdata"
)
all_transferred = path_to_search.glob("**/*")
paths_to_transferred_files = list(
filter(Path.is_file, all_transferred)
)
paths_to_transferred_files = self.remove_path_before_rawdata(
paths_to_transferred_files
)
# Teardown here, because we have session scope.
try:
shutil.rmtree(self.central_from_local(project.cfg["local_path"]))
except FileNotFoundError:
pass
return paths_to_transferred_files