Coverage for tests/test_introspection.py: 28%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

61 statements  

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 

13 

14from lsst.utils.introspection import ( 

15 get_full_type_name, 

16 get_class_of, 

17 get_instance_of, 

18 get_caller_name, 

19) 

20 

21# Classes and functions to use in tests. 

22import lsst.utils 

23from lsst.utils._packaging import getPackageDir 

24from lsst.utils import doImport 

25from collections import Counter 

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 

75 def testTypeNames(self): 

76 # Check types and also an object 

77 tests = [(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 ("import_test.two._four.simple.Simple", 

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

96 "import_test.two.clash.Simple", 

97 ): 

98 test_cls = get_class_of(test_name) 

99 self.assertTrue(test_cls.true()) 

100 full = get_full_type_name(test_cls) 

101 self.assertEqual(full, test_name) 

102 

103 def testGetClassOf(self): 

104 tests = [(doImport, "lsst.utils.doImport"), 

105 (Counter, "collections.Counter") 

106 ] 

107 

108 for test in tests: 

109 ref_type = test[0] 

110 for t in test: 

111 c = get_class_of(t) 

112 self.assertIs(c, ref_type) 

113 

114 def testGetInstanceOf(self): 

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

116 self.assertIsInstance(c, Counter) 

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

118 with self.assertRaises(TypeError) as cm: 

119 get_instance_of(lsst.utils) 

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

121 

122 

123if __name__ == "__main__": 123 ↛ 124line 123 didn't jump to line 124, because the condition on line 123 was never true

124 unittest.main()