source: orange/Orange/testing/unit/tests/test_refactoring.py @ 9862:000ff4cfbccb

Revision 9862:000ff4cfbccb, 2.1 KB checked in by ales_erjavec, 2 years ago (diff)

Added test for refactoring tool.

Line 
1"""
2Tests for orange2to25 fixers.
3
4"""
5import sys, os
6import unittest
7
8from Orange.fixes import fix_changed_names
9from Orange.fixes import fix_orange_imports
10 
11NAME_MAPPING = fix_changed_names.MAPPING
12IMPORT_MAPPING = fix_orange_imports.MAPPING
13
14def q_name_split(name):
15    """ Split the name into the module name and object name
16    within it using the same convention as fix_changed_names
17    tool.
18   
19    """
20    if ":" in name:
21        mod_name, obj_name = name.split(":")
22    else:
23        mod_name, obj_name = name.rsplit(".", 1)
24    return mod_name, obj_name
25   
26   
27def rhasattr(obj, name):
28    while "." in name:
29        first, name = name.split(".", 1)
30        if hasattr(obj, first):
31            obj = getattr(obj, first)
32        else:
33            return False
34    return hasattr(obj, name)
35
36def rgetattr(obj, name):
37    while "." in name:
38        first, name = name.split(".", 1)
39        if hasattr(obj, first):
40            obj = getattr(obj, first)
41        else:
42            return None
43       
44    return getattr(obj, name)
45
46def import_package(name):
47    mod = __import__(name)
48    if "." in name:
49        _, name = name.split(".", 1)
50        return rgetattr(mod, name)
51    else:
52        return mod
53   
54class TestMapping(unittest.TestCase):
55    """
56    """
57    def test_name_mapping(self):
58        """ Tests the existance of mapped named pairs.
59        """
60        for old, new in NAME_MAPPING.items():
61            old_mod, old_name = q_name_split(old)
62            new_mod, new_name = q_name_split(new)
63           
64            old_mod = import_package(old_mod)
65            new_mod = import_package(new_mod)
66           
67           
68            self.assertTrue(rhasattr(old_mod, old_name), "{0} is missing".format(old))
69            self.assertTrue(rhasattr(new_mod, new_name), "{0} is missing".format(new))
70           
71    def test_import_mapping(self):
72        for old_import, new_import in IMPORT_MAPPING.items():
73            __import__(old_import)
74            __import__(new_import)
75           
76   
77           
78           
79if __name__ == "__main__":
80    unittest.main()
81   
Note: See TracBrowser for help on using the repository browser.