|
1 | 1 | from ..pkg_info import cmp_pkg_version
|
| 2 | +import unittest |
| 3 | +from unittest import mock |
2 | 4 | import pytest
|
3 | 5 |
|
4 | 6 | 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"]), |
8 | 10 | # 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 | +] |
11 | 13 |
|
12 | 14 | 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")]), |
14 | 17 | # 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 | +] |
17 | 20 |
|
18 | 21 | ATTRIBUTE_SCHEDULE = [
|
19 |
| - ('5.0.0', [('nibabel.dataobj_images', 'DataobjImage', 'get_data')]), |
| 22 | + ("5.0.0", [("nibabel.dataobj_images", "DataobjImage", "get_data")]), |
20 | 23 | # 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] |
23 | 32 |
|
24 | 33 |
|
25 | 34 | 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 |
32 | 39 |
|
33 | 40 |
|
34 | 41 | 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,) |
43 | 48 |
|
44 | 49 |
|
45 | 50 | 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