source: orange/Orange/testing/unit/tests/test_refactoring.py @ 9910:f3496f4c2145

Revision 9910:f3496f4c2145, 2.2 KB checked in by ales_erjavec, 2 years ago (diff)

Added some docstrings.

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