Skip to content

Commit 757a6f4

Browse files
committed
add some tests
1 parent de91a1b commit 757a6f4

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import pytest
2+
from collections import namedtuple
3+
from ...base import traits, TraitedSpec, BaseInterfaceInputSpec
4+
from ..base import (convert_to_traits_type, create_interface_specs,
5+
dipy_to_nipype_interface, DipyBaseInterface)
6+
from dipy.workflows.workflow import Workflow
7+
8+
9+
def test_convert_to_traits_type():
10+
Params = namedtuple("Params", "traits_type is_file")
11+
Res = namedtuple("Res", "traits_type is_mandatory")
12+
l_entries = [Params('variable string', False),
13+
Params('variable int', False),
14+
Params('variable float', False),
15+
Params('variable bool', False),
16+
Params('variable complex', False),
17+
Params('variable int, optional', False),
18+
Params('variable string, optional', False),
19+
Params('variable float, optional', False),
20+
Params('variable bool, optional', False),
21+
Params('variable complex, optional', False),
22+
Params('string', False), Params('int', False),
23+
Params('string', True), Params('float', False),
24+
Params('bool', False), Params('complex', False),
25+
Params('string, optional', False),
26+
Params('int, optional', False),
27+
Params('string, optional', True),
28+
Params('float, optional', False),
29+
Params('bool, optional', False),
30+
Params('complex, optional', False),
31+
]
32+
l_expected = [Res(traits.ListStr, True), Res(traits.ListInt, True),
33+
Res(traits.ListFloat, True), Res(traits.ListBool, True),
34+
Res(traits.ListComplex, True), Res(traits.ListInt, False),
35+
Res(traits.ListStr, False), Res(traits.ListFloat, False),
36+
Res(traits.ListBool, False), Res(traits.ListComplex, False),
37+
Res(traits.Str, True), Res(traits.Int, True),
38+
Res(traits.File, True), Res(traits.Float, True),
39+
Res(traits.Bool, True), Res(traits.Complex, True),
40+
Res(traits.Str, False), Res(traits.Int, False),
41+
Res(traits.File, False), Res(traits.Float, False),
42+
Res(traits.Bool, False), Res(traits.Complex, False),
43+
]
44+
45+
for entry, res in zip(l_entries, l_expected):
46+
traits_type, is_mandatory = convert_to_traits_type(entry.traits_type,
47+
entry.is_file)
48+
assert traits_type == res.traits_type
49+
assert is_mandatory == res.is_mandatory
50+
51+
with pytest.raises(IOError):
52+
convert_to_traits_type("file, optional")
53+
54+
55+
def test_create_interface_specs():
56+
new_interface = create_interface_specs("MyInterface")
57+
58+
assert new_interface.__base__ == TraitedSpec
59+
assert isinstance(new_interface(), TraitedSpec)
60+
assert new_interface.__name__ == "MyInterface"
61+
assert not new_interface().get()
62+
63+
new_interface = create_interface_specs("MyInterface",
64+
BaseClass=BaseInterfaceInputSpec)
65+
assert new_interface.__base__ == BaseInterfaceInputSpec
66+
assert isinstance(new_interface(), BaseInterfaceInputSpec)
67+
assert new_interface.__name__ == "MyInterface"
68+
assert not new_interface().get()
69+
70+
params = [("params1", "string", ["my description"]), ("params2_files", "string", ["my description @"]),
71+
("params3", "int, optional", ["useful option"]), ("out_params", "string", ["my out description"])]
72+
73+
new_interface = create_interface_specs("MyInterface", params=params,
74+
BaseClass=BaseInterfaceInputSpec)
75+
76+
assert new_interface.__base__ == BaseInterfaceInputSpec
77+
assert isinstance(new_interface(), BaseInterfaceInputSpec)
78+
assert new_interface.__name__ == "MyInterface"
79+
current_params = new_interface().get()
80+
assert len(current_params) == 4
81+
for key, expected in zip(current_params.keys(), params):
82+
assert key == expected[0]
83+
84+
85+
class DummyWorkflow(Workflow):
86+
87+
@classmethod
88+
def get_short_name(cls):
89+
return 'dwf1'
90+
91+
def run(self, in_files, param1=1, out_dir='', out_ref='out1.txt'):
92+
"""Workflow used to test basic workflows.
93+
94+
Parameters
95+
----------
96+
in_files : string
97+
fake input string param
98+
param1 : int, optional
99+
fake positional param (default 1)
100+
out_dir : string, optional
101+
fake output directory (default '')
102+
out_ref : string, optional
103+
fake out file (default out1.txt)
104+
105+
References
106+
-----------
107+
dummy references
108+
109+
"""
110+
return param1
111+
112+
113+
def test_dipy_to_nipype_interface():
114+
115+
new_specs = dipy_to_nipype_interface("MyModelSpec", DummyWorkflow)
116+
assert new_specs.__base__ == DipyBaseInterface
117+
assert isinstance(new_specs(), DipyBaseInterface)
118+
assert new_specs.__name__ == "MyModelSpec"
119+
assert hasattr(new_specs, 'input_spec')
120+
assert new_specs().input_spec.__base__ == BaseInterfaceInputSpec
121+
assert hasattr(new_specs, 'output_spec')
122+
assert new_specs().output_spec.__base__ == TraitedSpec
123+
assert hasattr(new_specs, '_run_interface')
124+
assert hasattr(new_specs, '_list_outputs')
125+
params_in = new_specs().inputs.get()
126+
params_out = new_specs()._outputs().get()
127+
assert len(params_in) == 4
128+
assert 'in_files' in params_in.keys()
129+
assert 'param1' in params_in.keys()
130+
assert 'out_dir' in params_out.keys()
131+
assert 'out_ref' in params_out.keys()
132+
133+
with pytest.raises(ValueError):
134+
new_specs().run()
135+
136+
137+
# test_convert_to_traits_type()
138+
# test_create_interface_specs()
139+
# test_dipy_to_nipype_interface()

0 commit comments

Comments
 (0)