Coverage for tests/test_doImport.py: 9%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-19 11:15 +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/>. 

21 

22import inspect 

23import unittest 

24 

25import lsst.utils.tests 

26from lsst.utils import doImport, doImportType 

27 

28 

29class ImportTestCase(lsst.utils.tests.TestCase): 

30 """Basic tests of doImport.""" 

31 

32 def testDoImport(self): 

33 c = doImport("lsst.utils.tests.TestCase") 

34 self.assertEqual(c, lsst.utils.tests.TestCase) 

35 

36 c = doImport("lsst.utils.tests.TestCase.assertFloatsAlmostEqual") 

37 self.assertEqual(c, lsst.utils.tests.TestCase.assertFloatsAlmostEqual) 

38 

39 c = doImport("lsst.utils.doImport") 

40 self.assertEqual(type(c), type(doImport)) 

41 self.assertTrue(inspect.isfunction(c)) 

42 

43 c = doImport("lsst.utils") 

44 self.assertTrue(inspect.ismodule(c)) 

45 

46 with self.assertRaises(ImportError): 

47 doImport("lsst.utils.tests.TestCase.xyprint") 

48 

49 with self.assertRaises(ImportError): 

50 doImport("lsst.utils.nothere") 

51 

52 with self.assertRaises(ModuleNotFoundError): 

53 doImport("missing module") 

54 

55 with self.assertRaises(ModuleNotFoundError): 

56 doImport("lsstdummy.import.fail") 

57 

58 with self.assertRaises(ImportError): 

59 doImport("lsst.import.fail") 

60 

61 with self.assertRaises(ImportError): 

62 doImport("lsst.utils.x") 

63 

64 with self.assertRaises(TypeError): 

65 doImport([]) 

66 

67 # Use a special test module 

68 with self.assertRaises(RuntimeError): 

69 doImport("import_test.two.three.runtime") 

70 

71 with self.assertRaises(ImportError): 

72 doImport("import_test.two.three.success.not_okay") 

73 

74 with self.assertRaises(ImportError): 

75 doImport("import_test.two.three.fail") 

76 

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)) 

82 

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") 

91 

92 def testDoImportType(self): 

93 with self.assertRaises(TypeError): 

94 doImportType("lsst.utils") 

95 

96 c = doImportType("lsst.utils.tests.TestCase") 

97 self.assertEqual(c, lsst.utils.tests.TestCase) 

98 

99 

100if __name__ == "__main__": 

101 unittest.main()