-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
292 lines (241 loc) · 8.48 KB
/
conftest.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os
import logging
from pathlib import Path
import typing as ty
from datetime import datetime
import shutil
from tempfile import mkdtemp
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from pydra.engine.task import TaskBase
from frametree.core.frameset import FrameSet
from frametree.core.store import Store
from fileformats.text import Plain as PlainText
from frametree.testing.tasks import (
concatenate,
concatenate_reverse,
TEST_TASKS,
BASIC_TASKS,
)
from frametree.testing.blueprint import (
TestDatasetBlueprint,
FileSetEntryBlueprint as FileBP,
TEST_DATASET_BLUEPRINTS,
GOOD_DATASETS,
)
from frametree.testing import TestAxes, MockRemote, AlternateMockRemote
from frametree.common import FileSystem
# from pydra import set_input_validator
# set_input_validator(True)
# Set DEBUG logging for unittests
log_level = logging.WARNING
logger = logging.getLogger("frametree")
logger.setLevel(log_level)
sch = logging.StreamHandler()
sch.setLevel(log_level)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
sch.setFormatter(formatter)
logger.addHandler(sch)
PKG_DIR = Path(__file__).parent
@pytest.fixture
def work_dir() -> Path:
# work_dir = Path.home() / '.frametree-tests'
# work_dir.mkdir(exist_ok=True)
# return work_dir
work_dir = mkdtemp()
yield Path(work_dir)
# shutil.rmtree(work_dir)
@pytest.fixture(scope="session")
def build_cache_dir() -> Path:
# build_cache_dir = Path.home() / '.frametree-test-build-cache'
# if build_cache_dir.exists():
# shutil.rmtree(build_cache_dir)
# build_cache_dir.mkdir()
return Path(mkdtemp())
# return build_cache_dir
@pytest.fixture
def cli_runner(catch_cli_exceptions: bool) -> ty.Callable[..., ty.Any]:
def invoke(
*args: ty.Any, catch_exceptions: bool = catch_cli_exceptions, **kwargs: ty.Any
) -> ty.Any:
runner = CliRunner()
result = runner.invoke(*args, catch_exceptions=catch_exceptions, **kwargs) # type: ignore[misc]
return result
return invoke
@pytest.fixture(scope="session")
def pkg_dir() -> Path:
return PKG_DIR
@pytest.fixture(scope="session")
def run_prefix() -> str:
"A datetime string used to avoid stale data left over from previous tests"
return datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")
# For debugging in IDE's don't catch raised exceptions and let the IDE
# break at it
if os.getenv("_PYTEST_RAISE", "0") != "0":
@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call: pytest.CallInfo[ty.Any]) -> None:
if call.excinfo is not None:
raise call.excinfo.value
@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo: pytest.ExceptionInfo[BaseException]) -> None:
raise excinfo.value
CATCH_CLI_EXCEPTIONS = False
else:
CATCH_CLI_EXCEPTIONS = True
@pytest.fixture
def catch_cli_exceptions() -> bool:
return CATCH_CLI_EXCEPTIONS
@pytest.fixture(params=BASIC_TASKS)
def pydra_task_details(request: pytest.FixtureRequest) -> ty.Tuple[str, ty.Any]:
func_name = request.param
return ("frametree.analysis.tasks.tests.fixtures" + func_name,) + tuple(
TEST_TASKS[func_name][1:]
)
@pytest.fixture(params=BASIC_TASKS)
def pydra_task(request: pytest.FixtureRequest) -> TaskBase:
task, args, expected_out = TEST_TASKS[request.param]
task.test_args = args # stash args away in task object for future access
return task
# ------------------------------------
# Pytest fixtures and helper functions
# ------------------------------------
DATA_STORES = ["file_system", "mock_remote"]
@pytest.fixture(scope="session")
def frametree_home() -> ty.Generator[Path, None, None]:
frametree_home = Path(mkdtemp()) / "frametree-home"
with patch.dict(os.environ, {"FRAMETREE_HOME": str(frametree_home)}):
yield frametree_home
@pytest.fixture(params=DATA_STORES)
def data_store(
work_dir: Path, frametree_home: Path, request: ty.Any
) -> ty.Generator[Store, None, None]:
store: Store
if request.param == "file_system":
store = FileSystem()
elif request.param.endswith("mock_remote"):
cache_dir = work_dir / "mock-remote-store" / "cache"
cache_dir.mkdir(parents=True)
remote_dir = work_dir / "mock-remote-store" / "remote"
remote_dir.mkdir(parents=True)
klass = (
AlternateMockRemote if request.param == "alt_mock_remote" else MockRemote
)
store = klass(
server="http://a.server.com",
cache_dir=cache_dir,
user="admin",
password="admin",
remote_dir=remote_dir,
)
store.save("test_mock_store")
else:
assert False, f"Unrecognised store {request.param}"
yield store
@pytest.fixture
def delayed_mock_remote(
work_dir: Path,
frametree_home: Path, # So we save the store definition in the home dir, not ~/.frametree
) -> MockRemote:
cache_dir = work_dir / "mock-remote-store" / "cache"
cache_dir.mkdir(parents=True)
remote_dir = work_dir / "mock-remote-store" / "remote"
remote_dir.mkdir(parents=True)
store = MockRemote(
server="http://a.server.com",
cache_dir=cache_dir,
user="admin",
password="admin",
remote_dir=remote_dir,
mock_delay=0.01,
)
store_name = "delayed_mock_remote"
store.save(store_name)
return store
@pytest.fixture(params=GOOD_DATASETS)
def dataset(
work_dir: Path, data_store: Store, request: pytest.FixtureRequest
) -> ty.Generator[FrameSet, None, None]:
dataset_name = request.param
blueprint = TEST_DATASET_BLUEPRINTS[dataset_name]
dataset_path = work_dir / dataset_name
dataset_id = dataset_path if isinstance(data_store, FileSystem) else dataset_name
dataset = blueprint.make_dataset(data_store, dataset_id)
yield dataset
# shutil.rmtree(dataset.id)
@pytest.fixture
def simple_dataset_blueprint() -> TestDatasetBlueprint:
return TestDatasetBlueprint(
hierarchy=[
"abcd"
], # e.g. XNAT where session ID is unique in project but final layer is organised by visit
axes=TestAxes,
dim_lengths=[1, 1, 1, 1],
entries=[
FileBP(path="file1", datatype=PlainText, filenames=["file1.txt"]),
FileBP(path="file2", datatype=PlainText, filenames=["file2.txt"]),
],
)
@pytest.fixture
def saved_dataset(
data_store: Store, simple_dataset_blueprint: TestDatasetBlueprint, work_dir: Path
) -> FrameSet:
if isinstance(data_store, FileSystem):
dataset_id = work_dir / "saved-dataset"
else:
dataset_id = "saved_dataset"
return simple_dataset_blueprint.make_dataset(data_store, dataset_id, name="")
@pytest.fixture
def tmp_dir() -> ty.Generator[Path, None, None]:
tmp_dir = Path(mkdtemp())
yield tmp_dir
shutil.rmtree(tmp_dir)
@pytest.fixture(params=["forward", "reverse"])
def concatenate_task(request: pytest.FixtureRequest) -> TaskBase:
if request.param == "forward":
task = concatenate
# FIXME: Can be removed after https://github.com/nipype/pydra/pull/533 is merged
task.__name__ = "concatenate"
else:
task = concatenate_reverse
return task
@pytest.fixture(scope="session")
def command_spec() -> ty.Dict[str, ty.Any]:
return {
"task": "frametree.testing.tasks:concatenate",
"inputs": {
"first_file": {
"datatype": "text/text-file",
"field": "in_file1",
"column_defaults": {
"row_frequency": "common:Samples[sample]",
},
"help": "the first file to pass as an input",
},
"second_file": {
"datatype": "text/text-file",
"field": "in_file2",
"column_defaults": {
"row_frequency": "common:Samples[sample]",
},
"help": "the second file to pass as an input",
},
},
"outputs": {
"concatenated": {
"datatype": "text/text-file",
"field": "out_file",
"help": "an output file",
}
},
"parameters": {
"duplicates": {
"field": "duplicates",
"default": 2,
"datatype": "field/integer",
"required": True,
"help": "a parameter",
}
},
"row_frequency": "common:Samples[sample]",
}