Coverage for tests/test_introspection.py: 20%
71 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 09:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 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.
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 (
20 find_outside_stacklevel,
21 get_caller_name,
22 get_class_of,
23 get_full_type_name,
24 get_instance_of,
25)
28class GetCallerNameTestCase(unittest.TestCase):
29 """Test get_caller_name
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 """
36 def test_free_function(self):
37 def test_func():
38 return get_caller_name(1)
40 result = test_func()
41 self.assertEqual(result, f"{__name__}.test_func")
43 def test_instance_method(self):
44 class TestClass:
45 def run(self):
46 return get_caller_name(1)
48 tc = TestClass()
49 result = tc.run()
50 self.assertEqual(result, f"{__name__}.TestClass.run")
52 def test_class_method(self):
53 class TestClass:
54 @classmethod
55 def run(cls):
56 return get_caller_name(1)
58 tc = TestClass()
59 result = tc.run()
60 self.assertEqual(result, f"{__name__}.TestClass.run")
62 def test_skip(self):
63 def test_func(stacklevel):
64 return get_caller_name(stacklevel)
66 result = test_func(2)
67 self.assertEqual(result, f"{__name__}.GetCallerNameTestCase.test_skip")
69 result = test_func(2000000) # use a large number to avoid details of how the test is run
70 self.assertEqual(result, "")
73class TestInstropection(unittest.TestCase):
74 """Tests for lsst.utils.introspection."""
76 def testTypeNames(self):
77 # Check types and also an object
78 tests = [
79 (getPackageDir, "lsst.utils.getPackageDir"), # underscore filtered out
80 (int, "int"),
81 (0, "int"),
82 ("", "str"),
83 (doImport, "lsst.utils.doImport.doImport"), # no underscore
84 (Counter, "collections.Counter"),
85 (Counter(), "collections.Counter"),
86 (lsst.utils, "lsst.utils"),
87 ]
89 for item, typeName in tests:
90 self.assertEqual(get_full_type_name(item), typeName)
92 def testUnderscores(self):
93 # Underscores are filtered out unless they can't be, either
94 # because __init__.py did not import it or there is a clash with
95 # the non-underscore version.
96 for test_name in (
97 "import_test.two._four.simple.Simple",
98 "import_test.two._four.clash.Simple",
99 "import_test.two.clash.Simple",
100 ):
101 test_cls = get_class_of(test_name)
102 self.assertTrue(test_cls.true())
103 full = get_full_type_name(test_cls)
104 self.assertEqual(full, test_name)
106 def testGetClassOf(self):
107 tests = [(doImport, "lsst.utils.doImport"), (Counter, "collections.Counter")]
109 for test in tests:
110 ref_type = test[0]
111 for t in test:
112 c = get_class_of(t)
113 self.assertIs(c, ref_type)
115 def testGetInstanceOf(self):
116 c = get_instance_of("collections.Counter", "abcdeab")
117 self.assertIsInstance(c, Counter)
118 self.assertEqual(c["a"], 2)
119 with self.assertRaises(TypeError) as cm:
120 get_instance_of(lsst.utils)
121 self.assertIn("lsst.utils", str(cm.exception))
123 def test_stacklevel(self):
124 level = find_outside_stacklevel("lsst.utils")
125 self.assertEqual(level, 1)
127 c = doImport("import_test.two.three.success.Container")
128 with self.assertWarns(Warning) as cm:
129 level = c.level()
130 self.assertTrue(cm.filename.endswith("test_introspection.py"))
131 self.assertEqual(level, 2)
132 with self.assertWarns(Warning) as cm:
133 level = c.indirect_level()
134 self.assertTrue(cm.filename.endswith("test_introspection.py"))
135 self.assertEqual(level, 3)
138if __name__ == "__main__":
139 unittest.main()