Skip to content

Commit 1bec68c

Browse files
tcloseeffigies
authored andcommitted
fixed up unittests
1 parent 0eed44a commit 1bec68c

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

pydra/engine/tests/test_specs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ def test_lazy_getvale():
124124
lf = LazyIn(task=tn)
125125
with pytest.raises(Exception) as excinfo:
126126
lf.inp_c
127-
assert str(excinfo.value) == "Task tn has no input attribute inp_c"
127+
assert (
128+
str(excinfo.value)
129+
== "Task 'tn' has no input attribute 'inp_c', available: 'inp_a', 'inp_b'"
130+
)
128131

129132

130133
def test_input_file_hash_1(tmp_path):

pydra/engine/tests/test_workflow.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from ..core import Workflow
3838
from ... import mark
3939
from ..specs import SpecInfo, BaseSpec, ShellSpec
40+
from pydra.utils import exc_info_matches
4041

4142

4243
def test_wf_no_input_spec():
@@ -102,13 +103,15 @@ def test_wf_dict_input_and_output_spec():
102103
wf.inputs.a = "any-string"
103104
wf.inputs.b = {"foo": 1, "bar": False}
104105

105-
with pytest.raises(TypeError, match="Cannot coerce 1.0 into <class 'str'>"):
106+
with pytest.raises(TypeError) as exc_info:
106107
wf.inputs.a = 1.0
107-
with pytest.raises(
108-
TypeError,
109-
match=("Could not coerce object, 'bad-value', to any of the union types "),
110-
):
108+
assert exc_info_matches(exc_info, "Cannot coerce 1.0 into <class 'str'>")
109+
110+
with pytest.raises(TypeError) as exc_info:
111111
wf.inputs.b = {"foo": 1, "bar": "bad-value"}
112+
assert exc_info_matches(
113+
exc_info, "Could not coerce object, 'bad-value', to any of the union types"
114+
)
112115

113116
result = wf()
114117
assert result.output.a == "any-string"
@@ -5002,14 +5005,13 @@ def test_wf_input_output_typing():
50025005
output_spec={"alpha": int, "beta": ty.List[int]},
50035006
)
50045007

5005-
with pytest.raises(
5006-
TypeError, match="Cannot coerce <class 'list'> into <class 'int'>"
5007-
):
5008+
with pytest.raises(TypeError) as exc_info:
50085009
list_mult_sum(
50095010
scalar=wf.lzin.y,
50105011
in_list=wf.lzin.y,
50115012
name="A",
50125013
)
5014+
exc_info_matches(exc_info, "Cannot coerce <class 'list'> into <class 'int'>")
50135015

50145016
wf.add( # Split over workflow input "x" on "scalar" input
50155017
list_mult_sum(

pydra/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .misc import user_cache_dir, add_exc_note # noqa: F401
1+
from .misc import user_cache_dir, add_exc_note, exc_info_matches # noqa: F401

pydra/utils/misc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
import re
23
import platformdirs
34
from pydra._version import __version__
45

@@ -31,3 +32,14 @@ def add_exc_note(e: Exception, note: str) -> Exception:
3132
else:
3233
e.args = (e.args[0] + "\n" + note,)
3334
return e
35+
36+
37+
def exc_info_matches(exc_info, match, regex=False):
38+
if exc_info.value.__cause__ is not None:
39+
msg = str(exc_info.value.__cause__)
40+
else:
41+
msg = str(exc_info.value)
42+
if regex:
43+
return re.match(".*" + match, msg)
44+
else:
45+
return match in msg

pydra/utils/tests/test_typing.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import itertools
33
import sys
4-
import re
54
import typing as ty
65
from pathlib import Path
76
import tempfile
@@ -22,24 +21,14 @@
2221
MyOtherFormatX,
2322
MyHeader,
2423
)
24+
from pydra.utils import exc_info_matches
2525

2626

2727
def lz(tp: ty.Type):
2828
"""convenience method for creating a LazyField of type 'tp'"""
2929
return LazyOutField(name="foo", field="boo", type=tp)
3030

3131

32-
def exc_info_matches(exc_info, match, regex=False):
33-
if exc_info.value.__cause__ is not None:
34-
msg = str(exc_info.value.__cause__)
35-
else:
36-
msg = str(exc_info.value)
37-
if regex:
38-
return re.match(".*" + match, msg)
39-
else:
40-
return match in msg
41-
42-
4332
PathTypes = ty.Union[str, os.PathLike]
4433

4534

@@ -154,7 +143,8 @@ def test_type_check_basic16():
154143
@pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10")
155144
def test_type_check_basic16a():
156145
with pytest.raises(
157-
TypeError, match="Cannot coerce <class 'float'> to any of the union types"
146+
TypeError,
147+
match="Incorrect type for lazy field: <class 'float'> is not a subclass of",
158148
):
159149
TypeParser(Path | File | bool | int)(lz(float))
160150

@@ -234,7 +224,7 @@ def test_type_check_fail2():
234224

235225
@pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10")
236226
def test_type_check_fail2a():
237-
with pytest.raises(TypeError, match="to any of the union types"):
227+
with pytest.raises(TypeError, match="Incorrect type for lazy field: <class 'int'>"):
238228
TypeParser(Path | File)(lz(int))
239229

240230

@@ -249,7 +239,10 @@ def test_type_check_fail3():
249239
def test_type_check_fail4():
250240
with pytest.raises(TypeError) as exc_info:
251241
TypeParser(ty.Sequence)(lz(ty.Dict[str, int]))
252-
assert exc_info_matches(exc_info, "Cannot coerce .*(d|D)ict.* into")
242+
assert exc_info_matches(
243+
exc_info,
244+
"Cannot coerce typing.Dict[str, int] into <class 'collections.abc.Sequence'>",
245+
)
253246

254247

255248
def test_type_check_fail5():
@@ -366,13 +359,13 @@ def test_type_coercion_basic12():
366359

367360
@pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10")
368361
def test_type_coercion_basic12a():
369-
with pytest.raises(TypeError, match="explicitly excluded"):
362+
with pytest.raises(TypeError) as exc_info:
370363
TypeParser(
371364
list,
372365
coercible=[(ty.Sequence, ty.Sequence)],
373366
not_coercible=[(str, ty.Sequence)],
374367
)("a-string")
375-
368+
assert exc_info_matches(exc_info, "explicitly excluded")
376369
assert TypeParser(Path | File | int, coercible=[(ty.Any, ty.Any)])(1.0) == 1
377370

378371

@@ -480,8 +473,9 @@ def test_type_coercion_fail2():
480473

481474
@pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10")
482475
def test_type_coercion_fail2a():
483-
with pytest.raises(TypeError, match="to any of the union types"):
476+
with pytest.raises(TypeError) as exc_info:
484477
TypeParser(Path | File, coercible=[(ty.Any, ty.Any)])(1)
478+
assert exc_info_matches(exc_info, "to any of the union types")
485479

486480

487481
def test_type_coercion_fail3():

0 commit comments

Comments
 (0)