Skip to content

Commit ee4b703

Browse files
authored
Merge pull request #878 from effigies/test/cleanups2
TEST: Style cleanups, simpler fixtures
2 parents fe61612 + 92c758a commit ee4b703

7 files changed

+37
-81
lines changed

nibabel/optpkg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
""" Routines to support optional packages """
2-
import pkgutil
32
from distutils.version import LooseVersion
43
from .tripwire import TripWire
54

nibabel/tests/test_arrayproxy.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_init():
7373
assert ap.shape == shape
7474
# shape should be read only
7575
with pytest.raises(AttributeError):
76-
setattr(ap, 'shape', shape)
76+
ap.shape = shape
7777
# Get the data
7878
assert_array_equal(np.asarray(ap), arr)
7979
# Check we can modify the original header without changing the ap version
@@ -323,10 +323,8 @@ def check_mmap(hdr, offset, proxy_class,
323323
assert not unscaled_is_mmap
324324
assert not back_is_mmap
325325
else:
326-
assert (unscaled_is_mmap ==
327-
(viral_memmap or unscaled_really_mmap))
328-
assert (back_is_mmap ==
329-
(viral_memmap or scaled_really_mmap))
326+
assert unscaled_is_mmap == (viral_memmap or unscaled_really_mmap)
327+
assert back_is_mmap == (viral_memmap or scaled_really_mmap)
330328
if scaled_really_mmap:
331329
assert back_data.mode == expected_mode
332330
del prox, back_data

nibabel/tests/test_arraywriters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
from ..casting import int_abs, type_info, shared_range, on_powerpc
1515
from ..volumeutils import array_from_file, apply_read_scaling, _dt_min_max
1616

17-
from numpy.testing import (assert_array_almost_equal,
18-
assert_array_equal)
17+
from numpy.testing import assert_array_almost_equal, assert_array_equal
1918
import pytest
2019
from ..testing_pytest import (assert_allclose_safely, suppress_warnings,
21-
error_warnings)
20+
error_warnings)
2221

2322

2423
FLOAT_TYPES = np.sctypes['float']
@@ -532,7 +531,7 @@ def test_nan2zero():
532531
# Deprecation warning for nan2zero as argument to `to_fileobj`
533532
with error_warnings():
534533
with pytest.raises(DeprecationWarning):
535-
aw.to_fileobj(BytesIO(), 'F', False)
534+
aw.to_fileobj(BytesIO(), 'F', False)
536535
with pytest.raises(DeprecationWarning):
537536
aw.to_fileobj(BytesIO(), 'F', nan2zero=False)
538537
# Error if nan2zero is not the value set at initialization

nibabel/tests/test_batteryrunners.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,43 +159,19 @@ def test_logging():
159159
def test_checks():
160160
battrun = BatteryRunner((chk1,))
161161
reports = battrun.check_only({})
162-
assert (reports[0] ==
163-
Report(KeyError,
164-
20,
165-
'no "testkey"',
166-
''))
162+
assert reports[0] == Report(KeyError, 20, 'no "testkey"', '')
167163
obj, reports = battrun.check_fix({})
168-
assert (reports[0] ==
169-
Report(KeyError,
170-
20,
171-
'no "testkey"',
172-
'added "testkey"'))
164+
assert reports[0] == Report(KeyError, 20, 'no "testkey"', 'added "testkey"')
173165
assert obj == {'testkey': 1}
174166
battrun = BatteryRunner((chk1, chk2))
175167
reports = battrun.check_only({})
176-
assert (reports[0] ==
177-
Report(KeyError,
178-
20,
179-
'no "testkey"',
180-
''))
181-
assert (reports[1] ==
182-
Report(KeyError,
183-
20,
184-
'no "testkey"',
185-
''))
168+
assert reports[0] == Report(KeyError, 20, 'no "testkey"', '')
169+
assert reports[1] == Report(KeyError, 20, 'no "testkey"', '')
186170
obj, reports = battrun.check_fix({})
187171
# In the case of fix, the previous fix exposes a different error
188172
# Note, because obj is mutable, first and second point to modified
189173
# (and final) dictionary
190174
output_obj = {'testkey': 0}
191-
assert (reports[0] ==
192-
Report(KeyError,
193-
20,
194-
'no "testkey"',
195-
'added "testkey"'))
196-
assert (reports[1] ==
197-
Report(ValueError,
198-
10,
199-
'"testkey" != 0',
200-
'set "testkey" to 0'))
175+
assert reports[0] == Report(KeyError, 20, 'no "testkey"', 'added "testkey"')
176+
assert reports[1] == Report(ValueError, 10, '"testkey" != 0', 'set "testkey" to 0')
201177
assert obj == output_obj

nibabel/tests/test_casting.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ def test_shared_range():
4343
if imax_roundtrip == imax:
4444
thresh_overflow = True
4545
if thresh_overflow:
46-
assert np.all(
47-
(bit_bigger == casted_mx) |
48-
(bit_bigger == imax))
46+
assert np.all((bit_bigger == casted_mx) | (bit_bigger == imax))
4947
else:
5048
assert np.all((bit_bigger <= casted_mx))
5149
if it in np.sctypes['uint']:
@@ -71,9 +69,7 @@ def test_shared_range():
7169
if imin_roundtrip == imin:
7270
thresh_overflow = True
7371
if thresh_overflow:
74-
assert np.all(
75-
(bit_smaller == casted_mn) |
76-
(bit_smaller == imin))
72+
assert np.all((bit_smaller == casted_mn) | (bit_smaller == imin))
7773
else:
7874
assert np.all((bit_smaller >= casted_mn))
7975

nibabel/tests/test_data.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@
1919

2020
import pytest
2121

22-
from .test_environment import (with_environment,
23-
DATA_KEY,
24-
USER_KEY)
22+
from .test_environment import with_environment, DATA_KEY, USER_KEY
2523

2624

27-
@pytest.fixture()
25+
@pytest.fixture
2826
def with_nimd_env(request, with_environment):
2927
DATA_FUNCS = {}
3028
DATA_FUNCS['home_dir_func'] = nibd.get_nipy_user_dir
3129
DATA_FUNCS['sys_dir_func'] = nibd.get_nipy_system_dir
3230
DATA_FUNCS['path_func'] = nibd.get_data_path
33-
34-
def teardown_data_env():
35-
nibd.get_nipy_user_dir = DATA_FUNCS['home_dir_func']
36-
nibd.get_nipy_system_dir = DATA_FUNCS['sys_dir_func']
37-
nibd.get_data_path = DATA_FUNCS['path_func']
38-
39-
request.addfinalizer(teardown_data_env)
31+
yield
32+
nibd.get_nipy_user_dir = DATA_FUNCS['home_dir_func']
33+
nibd.get_nipy_system_dir = DATA_FUNCS['sys_dir_func']
34+
nibd.get_data_path = DATA_FUNCS['path_func']
4035

4136

4237
def test_datasource():
@@ -162,8 +157,7 @@ def test_data_path(with_nimd_env):
162157
with open(tmpfile, 'wt') as fobj:
163158
fobj.write('[DATA]\n')
164159
fobj.write('path = %s\n' % '/path/two')
165-
assert (get_data_path() ==
166-
tst_list + ['/path/two'] + old_pth)
160+
assert get_data_path() == tst_list + ['/path/two'] + old_pth
167161

168162

169163
def test_find_data_dir():
@@ -206,10 +200,10 @@ def test_make_datasource(with_nimd_env):
206200
assert ds.version == '0.1'
207201

208202

203+
@pytest.mark.xfail(raises=DataError)
209204
def test_bomber():
210-
with pytest.raises(DataError):
211-
b = Bomber('bomber example', 'a message')
212-
b.any_attribute # no error
205+
b = Bomber('bomber example', 'a message')
206+
b.any_attribute # no error
213207

214208

215209
def test_bomber_inspect():
@@ -218,13 +212,12 @@ def test_bomber_inspect():
218212

219213

220214
def test_datasource_or_bomber(with_nimd_env):
221-
pkg_def = dict(
222-
relpath='pkg')
215+
pkg_def = dict(relpath='pkg')
223216
with TemporaryDirectory() as tmpdir:
224217
nibd.get_data_path = lambda: [tmpdir]
225218
ds = datasource_or_bomber(pkg_def)
226219
with pytest.raises(DataError):
227-
getattr(ds, 'get_filename')
220+
ds.get_filename('some_file.txt')
228221
pkg_dir = pjoin(tmpdir, 'pkg')
229222
os.mkdir(pkg_dir)
230223
tmpfile = pjoin(pkg_dir, 'config.ini')
@@ -240,4 +233,4 @@ def test_datasource_or_bomber(with_nimd_env):
240233
pkg_def['min version'] = '0.3'
241234
ds = datasource_or_bomber(pkg_def) # not OK
242235
with pytest.raises(DataError):
243-
getattr(ds, 'get_filename')
236+
ds.get_filename('some_file.txt')

nibabel/tests/test_environment.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
USER_KEY = 'NIPY_USER_DIR'
1515

1616

17-
@pytest.fixture()
17+
@pytest.fixture
1818
def with_environment(request):
1919
"""Setup test environment for some functions that are tested
2020
in this module. In particular this functions stores attributes
@@ -24,20 +24,15 @@ def with_environment(request):
2424
"""
2525
GIVEN_ENV = {}
2626
GIVEN_ENV['env'] = env.copy()
27-
28-
29-
def teardown_environment():
30-
"""Restore things that were remembered by the setup_environment function
31-
"""
32-
orig_env = GIVEN_ENV['env']
33-
# Pull keys out into list to avoid altering dictionary during iteration,
34-
# causing python 3 error
35-
for key in list(env.keys()):
36-
if key not in orig_env:
37-
del env[key]
38-
env.update(orig_env)
39-
40-
request.addfinalizer(teardown_environment)
27+
yield
28+
"""Restore things that were remembered by the setup_environment function """
29+
orig_env = GIVEN_ENV['env']
30+
# Pull keys out into list to avoid altering dictionary during iteration,
31+
# causing python 3 error
32+
for key in list(env.keys()):
33+
if key not in orig_env:
34+
del env[key]
35+
env.update(orig_env)
4136

4237

4338
def test_nipy_home():

0 commit comments

Comments
 (0)