Coverage for tests/test_doImport.py: 9%
52 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-17 07:53 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-17 07: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# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import inspect
23import unittest
25import lsst.utils.tests
26from lsst.utils import doImport, doImportType
29class ImportTestCase(lsst.utils.tests.TestCase):
30 """Basic tests of doImport."""
32 def testDoImport(self):
33 c = doImport("lsst.utils.tests.TestCase")
34 self.assertEqual(c, lsst.utils.tests.TestCase)
36 c = doImport("lsst.utils.tests.TestCase.assertFloatsAlmostEqual")
37 self.assertEqual(c, lsst.utils.tests.TestCase.assertFloatsAlmostEqual)
39 c = doImport("lsst.utils.doImport")
40 self.assertEqual(type(c), type(doImport))
41 self.assertTrue(inspect.isfunction(c))
43 c = doImport("lsst.utils")
44 self.assertTrue(inspect.ismodule(c))
46 with self.assertRaises(ImportError):
47 doImport("lsst.utils.tests.TestCase.xyprint")
49 with self.assertRaises(ImportError):
50 doImport("lsst.utils.nothere")
52 with self.assertRaises(ModuleNotFoundError):
53 doImport("missing module")
55 with self.assertRaises(ModuleNotFoundError):
56 doImport("lsstdummy.import.fail")
58 with self.assertRaises(ImportError):
59 doImport("lsst.import.fail")
61 with self.assertRaises(ImportError):
62 doImport("lsst.utils.x")
64 with self.assertRaises(TypeError):
65 doImport([])
67 # Use a special test module
68 with self.assertRaises(RuntimeError):
69 doImport("import_test.two.three.runtime")
71 with self.assertRaises(ImportError):
72 doImport("import_test.two.three.success.not_okay")
74 with self.assertRaises(ImportError):
75 doImport("import_test.two.three.fail")
77 # Check that the error message reports the notthere failure
78 try:
79 doImport("import_test.two.three.fail.myfunc")
80 except ImportError as e:
81 self.assertIn("notthere", str(e))
83 c = doImport("import_test.two.three.success")
84 self.assertTrue(c.okay())
85 c = doImport("import_test.two.three.success.okay")
86 self.assertTrue(c())
87 c = doImport("import_test.two.three.success.Container")
88 self.assertEqual(c.inside(), "1")
89 c = doImport("import_test.two.three.success.Container.inside")
90 self.assertEqual(c(), "1")
92 def testDoImportType(self):
93 with self.assertRaises(TypeError):
94 doImportType("lsst.utils")
96 c = doImportType("lsst.utils.tests.TestCase")
97 self.assertEqual(c, lsst.utils.tests.TestCase)
100if __name__ == "__main__":
101 unittest.main()