Coverage for tests/test_butler.py: 35%

67 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 01:29 -0800

1# -*- coding: UTF-8 -*- 

2 

3# 

4# LSST Data Management System 

5# Copyright 2016 LSST Corporation. 

6# 

7# This product includes software developed by the 

8# LSST Project (http://www.lsst.org/). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the LSST License Statement and 

21# the GNU General Public License along with this program. If not, 

22# see <http://www.lsstcorp.org/LegalNotices/>. 

23# 

24 

25import unittest 

26import lsst.daf.persistence as dp 

27import lsst.daf.persistence.test as dpTest 

28import lsst.utils.tests 

29import os 

30import shutil 

31import tempfile 

32 

33ROOT = os.path.abspath(os.path.dirname(__file__)) 

34 

35 

36def setup_module(module): 

37 lsst.utils.tests.init() 

38 

39 

40class ButlerTest(unittest.TestCase): 

41 """Test case for basic Butler operations.""" 

42 

43 def setUp(self): 

44 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='ButlerTest-') 

45 

46 def tearDown(self): 

47 if os.path.exists(self.testDir): 

48 shutil.rmtree(self.testDir) 

49 

50 def testV1withV2InitApiRaises(self): 

51 """Test that a RuntimeError is raised when Butler v1 init api (root, 

52 mapper, mapperArgs**) is used with Butler v2 init api 

53 (inputs, outputs).""" 

54 foobar = os.path.join(self.testDir, "bar") 

55 foobaz = os.path.join(self.testDir, "baz") 

56 with self.assertRaises(RuntimeError): 

57 dp.Butler(root=foobar, inputs=foobar) 

58 with self.assertRaises(RuntimeError): 

59 dp.Butler(mapper='lsst.obs.base.CameraMapper', inputs=foobar) 

60 with self.assertRaises(RuntimeError): 

61 dp.Butler(inputs=foobar, calibRoot=foobaz) 

62 with self.assertRaises(RuntimeError): 

63 dp.Butler(root=foobar, outputs=foobar) 

64 with self.assertRaises(RuntimeError): 

65 dp.Butler(mapper='lsst.obs.base.CameraMapper', outputs=foobar) 

66 with self.assertRaises(RuntimeError): 

67 dp.Butler(inputs=foobar, outputs=foobaz) 

68 

69 def testV1RepoWithRootOnly(self): 

70 repoDir = os.path.join(self.testDir, 'repo') 

71 os.makedirs(repoDir) 

72 with open(os.path.join(repoDir, '_mapper'), 'w') as f: 

73 f.write('lsst.daf.persistence.test.EmptyTestMapper') 

74 butler = dp.Butler(repoDir) 

75 self.assertIsInstance(butler, dp.Butler) 

76 

77 def testMapperCanBeString(self): 

78 # should not raise 

79 butler = dp.Butler(outputs={'root': self.testDir, 

80 'mapper': 'lsst.daf.persistence.test.EmptyTestMapper'}) 

81 self.assertIsInstance(butler, dp.Butler) 

82 

83 def testMapperCanBeStringV1Api(self): 

84 # should not raise 

85 butler = dp.Butler(root=self.testDir, mapper='lsst.daf.persistence.test.EmptyTestMapper') 

86 self.assertIsInstance(butler, dp.Butler) 

87 

88 def testMapperCanBeClassObject(self): 

89 # should not raise 

90 butler = dp.Butler(outputs={'root': self.testDir, 

91 'mapper': dpTest.EmptyTestMapper}) 

92 self.assertIsInstance(butler, dp.Butler) 

93 

94 def testMapperCanBeClassObjectV1Api(self): 

95 # should not raise 

96 butler = dp.Butler(root=self.testDir, mapper=dpTest.EmptyTestMapper) 

97 self.assertIsInstance(butler, dp.Butler) 

98 

99 def testMapperCanBeClassInstance(self): 

100 # should warn but not raise 

101 butler = dp.Butler(outputs={'root': self.testDir, 

102 'mapper': dpTest.EmptyTestMapper()}) 

103 self.assertIsInstance(butler, dp.Butler) 

104 

105 def testMapperCanBeClassInstanceV1Api(self): 

106 # should warn but not raise 

107 butler = dp.Butler(root=self.testDir, mapper=dpTest.EmptyTestMapper()) 

108 self.assertIsInstance(butler, dp.Butler) 

109 

110 def testWarning(self): 

111 with self.assertWarns(FutureWarning): 

112 current = lsst.daf.persistence.deprecation.always_warn 

113 lsst.daf.persistence.deprecation.always_warn = True 

114 dp.Butler(outputs={'root': self.testDir, 

115 'mapper': 'lsst.daf.persistence.test.EmptyTestMapper'}) 

116 lsst.daf.persistence.deprecation.always_warn = current 

117 

118 

119class MemoryTester(lsst.utils.tests.MemoryTestCase): 

120 pass 

121 

122 

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

124 lsst.utils.tests.init() 

125 unittest.main()