Coverage for tests/test_doImport.py: 9%

52 statements  

« 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. 

11 

12import inspect 

13import unittest 

14 

15import lsst.utils.tests 

16from lsst.utils import doImport, doImportType 

17 

18 

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

20 """Basic tests of doImport.""" 

21 

22 def testDoImport(self): 

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

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

25 

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

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

28 

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

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

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

32 

33 c = doImport("lsst.utils") 

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

35 

36 with self.assertRaises(ImportError): 

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

38 

39 with self.assertRaises(ImportError): 

40 doImport("lsst.utils.nothere") 

41 

42 with self.assertRaises(ModuleNotFoundError): 

43 doImport("missing module") 

44 

45 with self.assertRaises(ModuleNotFoundError): 

46 doImport("lsstdummy.import.fail") 

47 

48 with self.assertRaises(ImportError): 

49 doImport("lsst.import.fail") 

50 

51 with self.assertRaises(ImportError): 

52 doImport("lsst.utils.x") 

53 

54 with self.assertRaises(TypeError): 

55 doImport([]) 

56 

57 # Use a special test module 

58 with self.assertRaises(RuntimeError): 

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

60 

61 with self.assertRaises(ImportError): 

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

63 

64 with self.assertRaises(ImportError): 

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

66 

67 # Check that the error message reports the notthere failure 

68 try: 

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

70 except ImportError as e: 

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

72 

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

74 self.assertTrue(c.okay()) 

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

76 self.assertTrue(c()) 

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

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

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

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

81 

82 def testDoImportType(self): 

83 with self.assertRaises(TypeError): 

84 doImportType("lsst.utils") 

85 

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

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

88 

89 

90if __name__ == "__main__": 

91 unittest.main()