Coverage for tests/test_introspection.py: 20%

71 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-21 09:53 +0000

1# This file is part of utils. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12import unittest 

13from collections import Counter 

14 

15# Classes and functions to use in tests. 

16import lsst.utils 

17from lsst.utils import doImport 

18from lsst.utils._packaging import getPackageDir 

19from lsst.utils.introspection import ( 

20 find_outside_stacklevel, 

21 get_caller_name, 

22 get_class_of, 

23 get_full_type_name, 

24 get_instance_of, 

25) 

26 

27 

28class GetCallerNameTestCase(unittest.TestCase): 

29 """Test get_caller_name 

30 

31 Warning: due to the different ways this can be run 

32 (e.g. directly or py.test), the module name can be one of two different 

33 things. 

34 """ 

35 

36 def test_free_function(self): 

37 def test_func(): 

38 return get_caller_name(1) 

39 

40 result = test_func() 

41 self.assertEqual(result, f"{__name__}.test_func") 

42 

43 def test_instance_method(self): 

44 class TestClass: 

45 def run(self): 

46 return get_caller_name(1) 

47 

48 tc = TestClass() 

49 result = tc.run() 

50 self.assertEqual(result, f"{__name__}.TestClass.run") 

51 

52 def test_class_method(self): 

53 class TestClass: 

54 @classmethod 

55 def run(cls): 

56 return get_caller_name(1) 

57 

58 tc = TestClass() 

59 result = tc.run() 

60 self.assertEqual(result, f"{__name__}.TestClass.run") 

61 

62 def test_skip(self): 

63 def test_func(stacklevel): 

64 return get_caller_name(stacklevel) 

65 

66 result = test_func(2) 

67 self.assertEqual(result, f"{__name__}.GetCallerNameTestCase.test_skip") 

68 

69 result = test_func(2000000) # use a large number to avoid details of how the test is run 

70 self.assertEqual(result, "") 

71 

72 

73class TestInstropection(unittest.TestCase): 

74 def testTypeNames(self): 

75 # Check types and also an object 

76 tests = [ 

77 (getPackageDir, "lsst.utils.getPackageDir"), # underscore filtered out 

78 (int, "int"), 

79 (0, "int"), 

80 ("", "str"), 

81 (doImport, "lsst.utils.doImport.doImport"), # no underscore 

82 (Counter, "collections.Counter"), 

83 (Counter(), "collections.Counter"), 

84 (lsst.utils, "lsst.utils"), 

85 ] 

86 

87 for item, typeName in tests: 

88 self.assertEqual(get_full_type_name(item), typeName) 

89 

90 def testUnderscores(self): 

91 # Underscores are filtered out unless they can't be, either 

92 # because __init__.py did not import it or there is a clash with 

93 # the non-underscore version. 

94 for test_name in ( 

95 "import_test.two._four.simple.Simple", 

96 "import_test.two._four.clash.Simple", 

97 "import_test.two.clash.Simple", 

98 ): 

99 test_cls = get_class_of(test_name) 

100 self.assertTrue(test_cls.true()) 

101 full = get_full_type_name(test_cls) 

102 self.assertEqual(full, test_name) 

103 

104 def testGetClassOf(self): 

105 tests = [(doImport, "lsst.utils.doImport"), (Counter, "collections.Counter")] 

106 

107 for test in tests: 

108 ref_type = test[0] 

109 for t in test: 

110 c = get_class_of(t) 

111 self.assertIs(c, ref_type) 

112 

113 def testGetInstanceOf(self): 

114 c = get_instance_of("collections.Counter", "abcdeab") 

115 self.assertIsInstance(c, Counter) 

116 self.assertEqual(c["a"], 2) 

117 with self.assertRaises(TypeError) as cm: 

118 get_instance_of(lsst.utils) 

119 self.assertIn("lsst.utils", str(cm.exception)) 

120 

121 def test_stacklevel(self): 

122 level = find_outside_stacklevel("lsst.utils") 

123 self.assertEqual(level, 1) 

124 

125 c = doImport("import_test.two.three.success.Container") 

126 with self.assertWarns(Warning) as cm: 

127 level = c.level() 

128 self.assertTrue(cm.filename.endswith("test_introspection.py")) 

129 self.assertEqual(level, 2) 

130 with self.assertWarns(Warning) as cm: 

131 level = c.indirect_level() 

132 self.assertTrue(cm.filename.endswith("test_introspection.py")) 

133 self.assertEqual(level, 3) 

134 

135 

136if __name__ == "__main__": 

137 unittest.main()