Skip to content

Commit f8db80a

Browse files
committed
TEST/RF: Reduce redundancy in test_removalschedule, test failure case
1 parent 3b6896e commit f8db80a

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

nibabel/tests/test_removalschedule.py

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,85 @@
11
from ..pkg_info import cmp_pkg_version
2+
import unittest
3+
from unittest import mock
24
import pytest
35

46
MODULE_SCHEDULE = [
5-
('5.0.0', ['nibabel.keywordonly']),
6-
('4.0.0', ['nibabel.trackvis']),
7-
('3.0.0', ['nibabel.minc', 'nibabel.checkwarns']),
7+
("5.0.0", ["nibabel.keywordonly"]),
8+
("4.0.0", ["nibabel.trackvis"]),
9+
("3.0.0", ["nibabel.minc", "nibabel.checkwarns"]),
810
# Verify that the test will be quiet if the schedule outlives the modules
9-
('1.0.0', ['nibabel.neverexisted']),
10-
]
11+
("1.0.0", ["nibabel.nosuchmod"]),
12+
]
1113

1214
OBJECT_SCHEDULE = [
13-
('3.0.0', [('nibabel.testing', 'catch_warn_reset')]),
15+
("5.0.0", [("nibabel.pydicom_compat", "dicom_test")]),
16+
("3.0.0", [("nibabel.testing", "catch_warn_reset")]),
1417
# Verify that the test will be quiet if the schedule outlives the modules
15-
('1.0.0', [('nibabel', 'neverexisted')]),
16-
]
18+
("1.0.0", [("nibabel.nosuchmod", "anyobj"), ("nibabel.nifti1", "nosuchobj")]),
19+
]
1720

1821
ATTRIBUTE_SCHEDULE = [
19-
('5.0.0', [('nibabel.dataobj_images', 'DataobjImage', 'get_data')]),
22+
("5.0.0", [("nibabel.dataobj_images", "DataobjImage", "get_data")]),
2023
# Verify that the test will be quiet if the schedule outlives the modules
21-
('1.0.0', [('nibabel', 'Nifti1Image', 'neverexisted')]),
22-
]
24+
("1.0.0", [("nibabel.nosuchmod", "anyobj", "anyattr"),
25+
("nibabel.nifti1", "nosuchobj", "anyattr"),
26+
("nibabel.nifti1", "Nifti1Image", "nosuchattr")]),
27+
]
28+
29+
30+
def _filter(schedule):
31+
return [entry for ver, entries in schedule if cmp_pkg_version(ver) < 1 for entry in entries]
2332

2433

2534
def test_module_removal():
26-
for version, to_remove in MODULE_SCHEDULE:
27-
if cmp_pkg_version(version) < 1:
28-
for module in to_remove:
29-
with pytest.raises(ImportError):
30-
__import__(module)
31-
pytest.fail("Time to remove " + module)
35+
for module in _filter(MODULE_SCHEDULE):
36+
with pytest.raises(ImportError):
37+
__import__(module)
38+
assert False, "Time to remove %s" % module
3239

3340

3441
def test_object_removal():
35-
for version, to_remove in OBJECT_SCHEDULE:
36-
if cmp_pkg_version(version) < 1:
37-
for module_name, obj in to_remove:
38-
try:
39-
module = __import__(module_name)
40-
except ImportError:
41-
continue
42-
assert not hasattr(module, obj), "Time to remove %s.%s" % (module_name, obj)
42+
for module_name, obj in _filter(OBJECT_SCHEDULE):
43+
try:
44+
module = __import__(module_name)
45+
except ImportError:
46+
continue
47+
assert not hasattr(module, obj), "Time to remove %s.%s" % (module_name, obj,)
4348

4449

4550
def test_attribute_removal():
46-
for version, to_remove in ATTRIBUTE_SCHEDULE:
47-
if cmp_pkg_version(version) < 1:
48-
for module_name, cls, attr in to_remove:
49-
try:
50-
module = __import__(module_name)
51-
except ImportError:
52-
continue
53-
try:
54-
klass = getattr(module, cls)
55-
except AttributeError:
56-
continue
57-
assert not hasattr(klass, attr), "Time to remove %s.%s.%s" % (module_name, cls, attr)
51+
for module_name, cls, attr in _filter(ATTRIBUTE_SCHEDULE):
52+
try:
53+
module = __import__(module_name)
54+
except ImportError:
55+
continue
56+
try:
57+
klass = getattr(module, cls)
58+
except AttributeError:
59+
continue
60+
assert not hasattr(klass, attr), "Time to remove %s.%s.%s" % (module_name, cls, attr,)
61+
62+
63+
#
64+
# Test the tests, making sure that we will get errors when the time comes
65+
#
66+
67+
_sched = "nibabel.tests.test_removalschedule.{}_SCHEDULE".format
68+
69+
70+
@mock.patch(_sched("MODULE"), [("3.0.0", ["nibabel.nifti1"])])
71+
def test_unremoved_module():
72+
with pytest.raises(AssertionError):
73+
test_module_removal()
74+
75+
76+
@mock.patch(_sched("OBJECT"), [("3.0.0", [("nibabel.nifti1", "Nifti1Image")])])
77+
def test_unremoved_object():
78+
with pytest.raises(AssertionError):
79+
test_object_removal()
80+
81+
82+
@mock.patch(_sched("ATTRIBUTE"), [("3.0.0", [("nibabel.nifti1", "Nifti1Image", "affine")])])
83+
def test_unremoved_attr():
84+
with pytest.raises(AssertionError):
85+
test_attribute_removal()

0 commit comments

Comments
 (0)