Coverage for tests/test_introspection.py: 25%
61 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-06 01:34 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-06 01:34 -0800
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.
12import unittest
13from collections import Counter
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 get_caller_name, get_class_of, get_full_type_name, get_instance_of
22class GetCallerNameTestCase(unittest.TestCase):
23 """Test get_caller_name
25 Warning: due to the different ways this can be run
26 (e.g. directly or py.test), the module name can be one of two different
27 things.
28 """
30 def test_free_function(self):
31 def test_func():
32 return get_caller_name(1)
34 result = test_func()
35 self.assertEqual(result, f"{__name__}.test_func")
37 def test_instance_method(self):
38 class TestClass:
39 def run(self):
40 return get_caller_name(1)
42 tc = TestClass()
43 result = tc.run()
44 self.assertEqual(result, f"{__name__}.TestClass.run")
46 def test_class_method(self):
47 class TestClass:
48 @classmethod
49 def run(cls):
50 return get_caller_name(1)
52 tc = TestClass()
53 result = tc.run()
54 self.assertEqual(result, f"{__name__}.TestClass.run")
56 def test_skip(self):
57 def test_func(stacklevel):
58 return get_caller_name(stacklevel)
60 result = test_func(2)
61 self.assertEqual(result, f"{__name__}.GetCallerNameTestCase.test_skip")
63 result = test_func(2000000) # use a large number to avoid details of how the test is run
64 self.assertEqual(result, "")
67class TestInstropection(unittest.TestCase):
68 def testTypeNames(self):
69 # Check types and also an object
70 tests = [
71 (getPackageDir, "lsst.utils.getPackageDir"), # underscore filtered out
72 (int, "int"),
73 (0, "int"),
74 ("", "str"),
75 (doImport, "lsst.utils.doImport.doImport"), # no underscore
76 (Counter, "collections.Counter"),
77 (Counter(), "collections.Counter"),
78 (lsst.utils, "lsst.utils"),
79 ]
81 for item, typeName in tests:
82 self.assertEqual(get_full_type_name(item), typeName)
84 def testUnderscores(self):
85 # Underscores are filtered out unless they can't be, either
86 # because __init__.py did not import it or there is a clash with
87 # the non-underscore version.
88 for test_name in (
89 "import_test.two._four.simple.Simple",
90 "import_test.two._four.clash.Simple",
91 "import_test.two.clash.Simple",
92 ):
93 test_cls = get_class_of(test_name)
94 self.assertTrue(test_cls.true())
95 full = get_full_type_name(test_cls)
96 self.assertEqual(full, test_name)
98 def testGetClassOf(self):
99 tests = [(doImport, "lsst.utils.doImport"), (Counter, "collections.Counter")]
101 for test in tests:
102 ref_type = test[0]
103 for t in test:
104 c = get_class_of(t)
105 self.assertIs(c, ref_type)
107 def testGetInstanceOf(self):
108 c = get_instance_of("collections.Counter", "abcdeab")
109 self.assertIsInstance(c, Counter)
110 self.assertEqual(c["a"], 2)
111 with self.assertRaises(TypeError) as cm:
112 get_instance_of(lsst.utils)
113 self.assertIn("lsst.utils", str(cm.exception))
116if __name__ == "__main__": 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true
117 unittest.main()