Coverage for tests/test_doImport.py: 16%

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

54 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 

13import inspect 

14 

15import lsst.utils.tests 

16 

17from lsst.utils import doImport, doImportType 

18 

19 

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

21 """Basic tests of doImport.""" 

22 

23 def testDoImport(self): 

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

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

26 

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

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

29 

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

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

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

33 

34 c = doImport("lsst.utils") 

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

36 

37 with self.assertRaises(ImportError): 

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

39 

40 with self.assertRaises(ImportError): 

41 doImport("lsst.utils.nothere") 

42 

43 with self.assertRaises(ModuleNotFoundError): 

44 doImport("missing module") 

45 

46 with self.assertRaises(ModuleNotFoundError): 

47 doImport("lsstdummy.import.fail") 

48 

49 with self.assertRaises(ImportError): 

50 doImport("lsst.import.fail") 

51 

52 with self.assertRaises(ImportError): 

53 doImport("lsst.utils.x") 

54 

55 with self.assertRaises(TypeError): 

56 doImport([]) 

57 

58 # Use a special test module 

59 with self.assertRaises(RuntimeError): 

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

61 

62 with self.assertRaises(ImportError): 

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

64 

65 with self.assertRaises(ImportError): 

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

67 

68 # Check that the error message reports the notthere failure 

69 try: 

70 doImport("import_test.two.three.fail.myfunc") 

71 except ImportError as e: 

72 self.assertIn("notthere", str(e)) 

73 

74 c = doImport("import_test.two.three.success") 

75 self.assertTrue(c.okay()) 

76 c = doImport("import_test.two.three.success.okay") 

77 self.assertTrue(c()) 

78 c = doImport("import_test.two.three.success.Container") 

79 self.assertEqual(c.inside(), "1") 

80 c = doImport("import_test.two.three.success.Container.inside") 

81 self.assertEqual(c(), "1") 

82 

83 def testDoImportType(self): 

84 with self.assertRaises(TypeError): 

85 doImportType("lsst.utils") 

86 

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

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

89 

90 

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

92 unittest.main()