Skip to content

Commit fe41b99

Browse files
committed
debugging unittests after syntax changes
1 parent 6bb494a commit fe41b99

File tree

2 files changed

+100
-152
lines changed

2 files changed

+100
-152
lines changed

pydra/engine/tests/test_helpers.py

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import typing as ty
77
import pytest
88
import cloudpickle as cp
9-
from unittest.mock import Mock
109
from pydra.engine.submitter import Submitter
1110
from pydra.engine.specs import Result
1211
from pydra.engine.core import Task
12+
from pydra.design import workflow
1313
from fileformats.generic import Directory, File
14-
from fileformats.core import FileSet
1514
from .utils import Multiply, RaiseXeq1
1615
from ..helpers import (
1716
get_available_cpus,
@@ -179,47 +178,39 @@ def test_get_available_cpus():
179178
def test_load_and_run(tmpdir):
180179
"""testing load_and_run for pickled task"""
181180
task_pkl = Path(tmpdir.join("task_main.pkl"))
182-
183-
task = Multiply(name="mult", y=10).split(x=[1, 2])
184-
task.state.prepare_states(inputs=task.inputs)
185-
task.state.prepare_inputs()
181+
# Note that tasks now don't have state arrays and indices, just a single resolved
182+
# set of parameters that are ready to run
183+
task = Task(name="mult", definition=Multiply(x=2, y=10), submitter=Submitter())
186184
with task_pkl.open("wb") as fp:
187185
cp.dump(task, fp)
188-
189-
resultfile_0 = load_and_run(task_pkl=task_pkl, ind=0)
190-
resultfile_1 = load_and_run(task_pkl=task_pkl, ind=1)
186+
resultfile = load_and_run(task_pkl=task_pkl)
191187
# checking the result files
192-
result_0 = cp.loads(resultfile_0.read_bytes())
193-
result_1 = cp.loads(resultfile_1.read_bytes())
194-
assert result_0.output.out == 10
195-
assert result_1.output.out == 20
196-
197-
198-
def test_load_and_run_exception_load(tmpdir):
199-
"""testing raising exception and saving info in crashfile when when load_and_run"""
200-
task_pkl = Path(tmpdir.join("task_main.pkl"))
201-
RaiseXeq1(name="raise").split("x", x=[1, 2])
202-
with pytest.raises(FileNotFoundError):
203-
load_and_run(task_pkl=task_pkl, ind=0)
188+
result = cp.loads(resultfile.read_bytes())
189+
assert result.outputs.out == 20
204190

205191

206192
def test_load_and_run_exception_run(tmpdir):
207193
"""testing raising exception and saving info in crashfile when when load_and_run"""
208194
task_pkl = Path(tmpdir.join("task_main.pkl"))
195+
cache_root = Path(tmpdir.join("cache"))
196+
cache_root.mkdir()
209197

210-
task = RaiseXeq1(name="raise").split("x", x=[1, 2])
211-
task.state.prepare_states(inputs=task.inputs)
212-
task.state.prepare_inputs()
198+
task = Task(
199+
definition=RaiseXeq1(x=1),
200+
name="raise",
201+
submitter=Submitter(worker="cf", cache_dir=cache_root),
202+
)
213203

214204
with task_pkl.open("wb") as fp:
215205
cp.dump(task, fp)
216206

217207
with pytest.raises(Exception) as excinfo:
218-
load_and_run(task_pkl=task_pkl, ind=0)
219-
assert "i'm raising an exception!" in str(excinfo.value)
208+
load_and_run(task_pkl=task_pkl)
209+
exc_msg = excinfo.value.args[0]
210+
assert "i'm raising an exception!" in exc_msg
220211
# checking if the crashfile has been created
221-
assert "crash" in str(excinfo.value)
222-
errorfile = Path(str(excinfo.value).split("here: ")[1][:-2])
212+
assert "crash" in exc_msg
213+
errorfile = Path(exc_msg.split("here: ")[1][:-2])
223214
assert errorfile.exists()
224215

225216
resultfile = errorfile.parent / "_result.pklz"
@@ -228,37 +219,35 @@ def test_load_and_run_exception_run(tmpdir):
228219
result_exception = cp.loads(resultfile.read_bytes())
229220
assert result_exception.errored is True
230221

222+
task = Task(definition=RaiseXeq1(x=2), name="wont_raise", submitter=Submitter())
223+
224+
with task_pkl.open("wb") as fp:
225+
cp.dump(task, fp)
226+
231227
# the second task should be fine
232-
resultfile = load_and_run(task_pkl=task_pkl, ind=1)
228+
resultfile = load_and_run(task_pkl=task_pkl)
233229
result_1 = cp.loads(resultfile.read_bytes())
234-
assert result_1.output.out == 2
230+
assert result_1.outputs.out == 2
235231

236232

237233
def test_load_and_run_wf(tmpdir):
238234
"""testing load_and_run for pickled task"""
239235
wf_pkl = Path(tmpdir.join("wf_main.pkl"))
240236

241-
wf = Workflow(name="wf", input_spec=["x", "y"], y=10)
242-
wf.add(Multiply(name="mult", x=wf.lzin.x, y=wf.lzin.y))
243-
wf.split("x", x=[1, 2])
244-
245-
wf.set_output([("out", wf.mult.lzout.out)])
237+
@workflow.define
238+
def Workflow(x, y=10):
239+
multiply = workflow.add(Multiply(x=x, y=y))
240+
return multiply.out
246241

247-
# task = multiply(name="mult", x=[1, 2], y=10).split("x")
248-
wf.state.prepare_states(inputs=wf.inputs)
249-
wf.state.prepare_inputs()
250-
wf.plugin = "cf"
242+
task = Task(name="mult", definition=Workflow(x=2), submitter=Submitter(worker="cf"))
251243

252244
with wf_pkl.open("wb") as fp:
253-
cp.dump(wf, fp)
245+
cp.dump(task, fp)
254246

255-
resultfile_0 = load_and_run(ind=0, task_pkl=wf_pkl)
256-
resultfile_1 = load_and_run(ind=1, task_pkl=wf_pkl)
247+
resultfile = load_and_run(task_pkl=wf_pkl)
257248
# checking the result files
258-
result_0 = cp.loads(resultfile_0.read_bytes())
259-
result_1 = cp.loads(resultfile_1.read_bytes())
260-
assert result_0.output.out == 10
261-
assert result_1.output.out == 20
249+
result = cp.loads(resultfile.read_bytes())
250+
assert result.outputs.out == 20
262251

263252

264253
@pytest.mark.parametrize(
@@ -276,45 +265,6 @@ def test_position_sort(pos_args):
276265
assert final_args == ["a", "b", "c"]
277266

278267

279-
def test_parse_copyfile():
280-
Mode = FileSet.CopyMode
281-
Collation = FileSet.CopyCollation
282-
283-
def mock_field(copyfile):
284-
mock = Mock(["metadata"])
285-
mock.metadata = {"copyfile": copyfile}
286-
return mock
287-
288-
assert parse_copyfile(mock_field((Mode.any, Collation.any))) == (
289-
Mode.any,
290-
Collation.any,
291-
)
292-
assert parse_copyfile(mock_field("copy"), default_collation=Collation.siblings) == (
293-
Mode.copy,
294-
Collation.siblings,
295-
)
296-
assert parse_copyfile(mock_field("link,adjacent")) == (
297-
Mode.link,
298-
Collation.adjacent,
299-
)
300-
assert parse_copyfile(mock_field(True)) == (
301-
Mode.copy,
302-
Collation.any,
303-
)
304-
assert parse_copyfile(mock_field(False)) == (
305-
Mode.link,
306-
Collation.any,
307-
)
308-
assert parse_copyfile(mock_field(None)) == (
309-
Mode.any,
310-
Collation.any,
311-
)
312-
with pytest.raises(TypeError, match="Unrecognised type for mode copyfile"):
313-
parse_copyfile(mock_field((1, 2)))
314-
with pytest.raises(TypeError, match="Unrecognised type for collation copyfile"):
315-
parse_copyfile(mock_field((Mode.copy, 2)))
316-
317-
318268
def test_parse_format_string1():
319269
assert parse_format_string("{a}") == {"a"}
320270

0 commit comments

Comments
 (0)