Skip to content

Commit 1880079

Browse files
committed
refactoring tests
1 parent e655a5a commit 1880079

File tree

4 files changed

+84
-71
lines changed

4 files changed

+84
-71
lines changed

nipype/interfaces/utility/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
77
Requires Packages to be installed
88
"""
9-
from __future__ import print_function, division, unicode_literals, absolute_import
10-
__docformat__ = 'restructuredtext'
119

1210
from .base import (IdentityInterface, Rename, Select, Split, Merge,
1311
AssertEqual)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
from __future__ import print_function, unicode_literals
5+
import os
6+
import pytest
7+
8+
from nipype.interfaces import utility
9+
import nipype.pipeline.engine as pe
10+
11+
12+
def test_rename(tmpdir):
13+
os.chdir(str(tmpdir))
14+
15+
# Test very simple rename
16+
_ = open("file.txt", "w").close()
17+
rn = utility.Rename(in_file="file.txt", format_string="test_file1.txt")
18+
res = rn.run()
19+
outfile = str(tmpdir.join("test_file1.txt"))
20+
assert res.outputs.out_file == outfile
21+
assert os.path.exists(outfile)
22+
23+
# Now a string-formatting version
24+
rn = utility.Rename(in_file="file.txt", format_string="%(field1)s_file%(field2)d", keep_ext=True)
25+
# Test .input field creation
26+
assert hasattr(rn.inputs, "field1")
27+
assert hasattr(rn.inputs, "field2")
28+
29+
# Set the inputs
30+
rn.inputs.field1 = "test"
31+
rn.inputs.field2 = 2
32+
res = rn.run()
33+
outfile = str(tmpdir.join("test_file2.txt"))
34+
assert res.outputs.out_file == outfile
35+
assert os.path.exists(outfile)
36+
37+
38+
@pytest.mark.parametrize("args, expected", [
39+
({} , ([0], [1,2,3])),
40+
({"squeeze" : True}, (0 , [1,2,3]))
41+
])
42+
def test_split(tmpdir, args, expected):
43+
os.chdir(str(tmpdir))
44+
45+
node = pe.Node(utility.Split(inlist=list(range(4)),
46+
splits=[1, 3],
47+
**args),
48+
name='split_squeeze')
49+
res = node.run()
50+
assert res.outputs.out1 == expected[0]
51+
assert res.outputs.out2 == expected[1]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
from __future__ import print_function, unicode_literals
5+
6+
from nipype.interfaces import utility
7+
8+
9+
def test_csvReader(tmpdir):
10+
header = "files,labels,erosion\n"
11+
lines = ["foo,hello,300.1\n",
12+
"bar,world,5\n",
13+
"baz,goodbye,0.3\n"]
14+
for x in range(2):
15+
name = str(tmpdir.join("testfile.csv"))
16+
with open(name, 'w') as fid:
17+
reader = utility.CSVReader()
18+
if x % 2 == 0:
19+
fid.write(header)
20+
reader.inputs.header = True
21+
fid.writelines(lines)
22+
fid.flush()
23+
reader.inputs.in_file = name
24+
out = reader.run()
25+
if x % 2 == 0:
26+
assert out.outputs.files == ['foo', 'bar', 'baz']
27+
assert out.outputs.labels == ['hello', 'world', 'goodbye']
28+
assert out.outputs.erosion == ['300.1', '5', '0.3']
29+
else:
30+
assert out.outputs.column_0 == ['foo', 'bar', 'baz']
31+
assert out.outputs.column_1 == ['hello', 'world', 'goodbye']
32+
assert out.outputs.column_2 == ['300.1', '5', '0.3']

nipype/interfaces/tests/test_utility.py renamed to nipype/interfaces/utility/tests/test_wrappers.py

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function, unicode_literals
32
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
43
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
from __future__ import print_function, unicode_literals
55
import os
66
import pytest
77

88
from nipype.interfaces import utility
99
import nipype.pipeline.engine as pe
1010

1111

12-
def test_rename(tmpdir):
13-
os.chdir(str(tmpdir))
14-
15-
# Test very simple rename
16-
_ = open("file.txt", "w").close()
17-
rn = utility.Rename(in_file="file.txt", format_string="test_file1.txt")
18-
res = rn.run()
19-
outfile = str(tmpdir.join("test_file1.txt"))
20-
assert res.outputs.out_file == outfile
21-
assert os.path.exists(outfile)
22-
23-
# Now a string-formatting version
24-
rn = utility.Rename(in_file="file.txt", format_string="%(field1)s_file%(field2)d", keep_ext=True)
25-
# Test .input field creation
26-
assert hasattr(rn.inputs, "field1")
27-
assert hasattr(rn.inputs, "field2")
28-
29-
# Set the inputs
30-
rn.inputs.field1 = "test"
31-
rn.inputs.field2 = 2
32-
res = rn.run()
33-
outfile = str(tmpdir.join("test_file2.txt"))
34-
assert res.outputs.out_file == outfile
35-
assert os.path.exists(outfile)
36-
37-
3812
def test_function(tmpdir):
3913
os.chdir(str(tmpdir))
4014

@@ -89,48 +63,6 @@ def test_function_with_imports(tmpdir):
8963
node.run()
9064

9165

92-
@pytest.mark.parametrize("args, expected", [
93-
({} , ([0], [1,2,3])),
94-
({"squeeze" : True}, (0 , [1,2,3]))
95-
])
96-
def test_split(tmpdir, args, expected):
97-
os.chdir(str(tmpdir))
98-
99-
node = pe.Node(utility.Split(inlist=list(range(4)),
100-
splits=[1, 3],
101-
**args),
102-
name='split_squeeze')
103-
res = node.run()
104-
assert res.outputs.out1 == expected[0]
105-
assert res.outputs.out2 == expected[1]
106-
107-
108-
def test_csvReader(tmpdir):
109-
header = "files,labels,erosion\n"
110-
lines = ["foo,hello,300.1\n",
111-
"bar,world,5\n",
112-
"baz,goodbye,0.3\n"]
113-
for x in range(2):
114-
name = str(tmpdir.join("testfile.csv"))
115-
with open(name, 'w') as fid:
116-
reader = utility.CSVReader()
117-
if x % 2 == 0:
118-
fid.write(header)
119-
reader.inputs.header = True
120-
fid.writelines(lines)
121-
fid.flush()
122-
reader.inputs.in_file = name
123-
out = reader.run()
124-
if x % 2 == 0:
125-
assert out.outputs.files == ['foo', 'bar', 'baz']
126-
assert out.outputs.labels == ['hello', 'world', 'goodbye']
127-
assert out.outputs.erosion == ['300.1', '5', '0.3']
128-
else:
129-
assert out.outputs.column_0 == ['foo', 'bar', 'baz']
130-
assert out.outputs.column_1 == ['hello', 'world', 'goodbye']
131-
assert out.outputs.column_2 == ['300.1', '5', '0.3']
132-
133-
13466
def test_aux_connect_function(tmpdir):
13567
""" This tests excution nodes with multiple inputs and auxiliary
13668
function inside the Workflow connect function.

0 commit comments

Comments
 (0)