Skip to content

Commit

Permalink
fixed up unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose authored and effigies committed May 29, 2024
1 parent 0eed44a commit 1bec68c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
5 changes: 4 additions & 1 deletion pydra/engine/tests/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ def test_lazy_getvale():
lf = LazyIn(task=tn)
with pytest.raises(Exception) as excinfo:
lf.inp_c
assert str(excinfo.value) == "Task tn has no input attribute inp_c"
assert (
str(excinfo.value)
== "Task 'tn' has no input attribute 'inp_c', available: 'inp_a', 'inp_b'"
)


def test_input_file_hash_1(tmp_path):
Expand Down
18 changes: 10 additions & 8 deletions pydra/engine/tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ..core import Workflow
from ... import mark
from ..specs import SpecInfo, BaseSpec, ShellSpec
from pydra.utils import exc_info_matches


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

with pytest.raises(TypeError, match="Cannot coerce 1.0 into <class 'str'>"):
with pytest.raises(TypeError) as exc_info:
wf.inputs.a = 1.0
with pytest.raises(
TypeError,
match=("Could not coerce object, 'bad-value', to any of the union types "),
):
assert exc_info_matches(exc_info, "Cannot coerce 1.0 into <class 'str'>")

with pytest.raises(TypeError) as exc_info:
wf.inputs.b = {"foo": 1, "bar": "bad-value"}
assert exc_info_matches(
exc_info, "Could not coerce object, 'bad-value', to any of the union types"
)

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

with pytest.raises(
TypeError, match="Cannot coerce <class 'list'> into <class 'int'>"
):
with pytest.raises(TypeError) as exc_info:
list_mult_sum(
scalar=wf.lzin.y,
in_list=wf.lzin.y,
name="A",
)
exc_info_matches(exc_info, "Cannot coerce <class 'list'> into <class 'int'>")

wf.add( # Split over workflow input "x" on "scalar" input
list_mult_sum(
Expand Down
2 changes: 1 addition & 1 deletion pydra/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .misc import user_cache_dir, add_exc_note # noqa: F401
from .misc import user_cache_dir, add_exc_note, exc_info_matches # noqa: F401
12 changes: 12 additions & 0 deletions pydra/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
import re
import platformdirs
from pydra._version import __version__

Expand Down Expand Up @@ -31,3 +32,14 @@ def add_exc_note(e: Exception, note: str) -> Exception:
else:
e.args = (e.args[0] + "\n" + note,)
return e


def exc_info_matches(exc_info, match, regex=False):
if exc_info.value.__cause__ is not None:
msg = str(exc_info.value.__cause__)
else:
msg = str(exc_info.value)

Check warning on line 41 in pydra/utils/misc.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/misc.py#L41

Added line #L41 was not covered by tests
if regex:
return re.match(".*" + match, msg)

Check warning on line 43 in pydra/utils/misc.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/misc.py#L43

Added line #L43 was not covered by tests
else:
return match in msg
30 changes: 12 additions & 18 deletions pydra/utils/tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import itertools
import sys
import re
import typing as ty
from pathlib import Path
import tempfile
Expand All @@ -22,24 +21,14 @@
MyOtherFormatX,
MyHeader,
)
from pydra.utils import exc_info_matches

Check warning on line 24 in pydra/utils/tests/test_typing.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/tests/test_typing.py#L24

Added line #L24 was not covered by tests


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


def exc_info_matches(exc_info, match, regex=False):
if exc_info.value.__cause__ is not None:
msg = str(exc_info.value.__cause__)
else:
msg = str(exc_info.value)
if regex:
return re.match(".*" + match, msg)
else:
return match in msg


PathTypes = ty.Union[str, os.PathLike]


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

Expand Down Expand Up @@ -234,7 +224,7 @@ def test_type_check_fail2():

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


Expand All @@ -249,7 +239,10 @@ def test_type_check_fail3():
def test_type_check_fail4():
with pytest.raises(TypeError) as exc_info:
TypeParser(ty.Sequence)(lz(ty.Dict[str, int]))
assert exc_info_matches(exc_info, "Cannot coerce .*(d|D)ict.* into")
assert exc_info_matches(

Check warning on line 242 in pydra/utils/tests/test_typing.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/tests/test_typing.py#L242

Added line #L242 was not covered by tests
exc_info,
"Cannot coerce typing.Dict[str, int] into <class 'collections.abc.Sequence'>",
)


def test_type_check_fail5():
Expand Down Expand Up @@ -366,13 +359,13 @@ def test_type_coercion_basic12():

@pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10")
def test_type_coercion_basic12a():
with pytest.raises(TypeError, match="explicitly excluded"):
with pytest.raises(TypeError) as exc_info:
TypeParser(
list,
coercible=[(ty.Sequence, ty.Sequence)],
not_coercible=[(str, ty.Sequence)],
)("a-string")

assert exc_info_matches(exc_info, "explicitly excluded")

Check warning on line 368 in pydra/utils/tests/test_typing.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/tests/test_typing.py#L368

Added line #L368 was not covered by tests
assert TypeParser(Path | File | int, coercible=[(ty.Any, ty.Any)])(1.0) == 1


Expand Down Expand Up @@ -480,8 +473,9 @@ def test_type_coercion_fail2():

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

Check warning on line 478 in pydra/utils/tests/test_typing.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/tests/test_typing.py#L478

Added line #L478 was not covered by tests


def test_type_coercion_fail3():
Expand Down

0 comments on commit 1bec68c

Please sign in to comment.