Coverage for tests/test_repositoryArgs.py: 38%

89 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-23 02:30 -0700

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 lsst.daf.persistence as dp 

26import lsst.utils.tests 

27import os 

28import shutil 

29import unittest 

30import urllib.parse 

31import tempfile 

32 

33 

34def setup_module(module): 

35 lsst.utils.tests.init() 

36 

37 

38class MapperTest(dp.Mapper): 

39 pass 

40 

41 

42# Define the root of the tests relative to this file 

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

44 

45 

46class DefaultMapper(unittest.TestCase): 

47 """Tests for finding the default mapper for a repository given different inputs. 

48 

49 Butler should allow class objects, class instances , and importable strings to be passed in, and treat 

50 them as equivalent. 

51 

52 Butler will find a default mapper only if all the inputs to the butler use the same mapper. If there are 

53 inputs with different mappers then the butler will not assume a default mapper and _getDefaultMapper 

54 will return None.""" 

55 

56 def setUp(self): 

57 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="DefaultMapper-") 

58 

59 def tearDown(self): 

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

61 shutil.rmtree(self.testDir) 

62 

63 def _setup(self, mapper1, mapper2): 

64 repo1Root = os.path.join(self.testDir, 'repo1') 

65 repo2Root = os.path.join(self.testDir, 'repo2') 

66 butler = dp.Butler(outputs=(dp.RepositoryArgs(root=repo1Root, mapper=mapper1))) 

67 del butler 

68 butler = dp.Butler(outputs=(dp.RepositoryArgs(root=repo2Root, mapper=mapper2))) 

69 del butler 

70 butler = dp.Butler(inputs=(repo1Root, repo2Root)) 

71 return butler 

72 

73 def testClassObjAndMatchingString(self): 

74 """Pass a class object and a string that evaluates to the same object, and verify a default mapper 

75 can be found.""" 

76 butler = self._setup(dp.Mapper, 'lsst.daf.persistence.Mapper') 

77 self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

78 

79 def testClassObjAndNotMatchingString(self): 

80 """Pass a class object and a non-matching string, and verify a default mapper can not be found.""" 

81 butler = self._setup(MapperTest, 'lsst.daf.persistence.Mapper') 

82 self.assertIsNone(butler._getDefaultMapper()) 

83 

84 def testInstanceAndMatchingString(self): 

85 """Pass a class instance and a string that evaluates to the same object, and verify a default mapper 

86 can be found.""" 

87 butler = self._setup(dp.Mapper, 'lsst.daf.persistence.Mapper') 

88 self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

89 

90 def testInstanceAndNotMatchingString(self): 

91 """Pass a class instance and a non-matching string, and verify a default mapper can not be found.""" 

92 butler = self._setup(MapperTest, 'lsst.daf.persistence.Mapper') 

93 self.assertIsNone(butler._getDefaultMapper()) 

94 

95 def testClassObjAndMatchingInstance(self): 

96 """Pass a class object and a class instance of the same type, and verify a default mapper can be 

97 found.""" 

98 butler = self._setup(dp.Mapper, dp.Mapper) 

99 self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

100 

101 def testClassObjAndNotMatchingInstance(self): 

102 """Pass a class object and a class instance of a different type, and verify a default mapper can not 

103 be found.""" 

104 butler = self._setup(MapperTest, dp.Mapper) 

105 self.assertIsNone(butler._getDefaultMapper()) 

106 

107 

108class ParseRootURI(unittest.TestCase): 

109 """Verify that supported root URI schemas are identified and build the correct Storage. 

110 """ 

111 def setUp(self): 

112 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="ParseRootURI-") 

113 

114 def tearDown(self): 

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

116 shutil.rmtree(self.testDir) 

117 

118 def testAbsoluteFilePathWithSchema(self): 

119 """Test writing & reading an absolute path that begins with 'file://""" 

120 uri = urllib.parse.urljoin('file://', self.testDir) 

121 args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

122 butler = dp.Butler(outputs=args) 

123 self.assertIsInstance(butler, dp.Butler) 

124 self.assertTrue(os.path.exists(os.path.join(self.testDir, 'repositoryCfg.yaml'))) 

125 

126 try: 

127 butler2 = dp.Butler(inputs=uri) 

128 self.assertIsInstance(butler2, dp.Butler) 

129 except RuntimeError: 

130 self.fail("Butler init raised a runtime error loading input %s" % uri) 

131 

132 def testAbsoluteFilePathWithoutSchema(self): 

133 """Test writing and reading an absolute path that begins with '/' (not 'file://')""" 

134 uri = self.testDir 

135 args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

136 butler = dp.Butler(outputs=args) 

137 self.assertIsInstance(butler, dp.Butler) 

138 self.assertTrue(os.path.exists(os.path.join(uri, 'repositoryCfg.yaml'))) 

139 

140 try: 

141 butler2 = dp.Butler(inputs=uri) 

142 self.assertIsInstance(butler2, dp.Butler) 

143 except RuntimeError: 

144 self.fail("Butler init raised a runtime error loading input %s" % uri) 

145 

146 def testRelativeFilePath(self): 

147 """Test writing & reading a relative filepath. 

148 

149 Relative filepaths can not start with 'file://' so there will be no relative file path test starting 

150 with the 'file' schema.""" 

151 uri = os.path.relpath(self.testDir) 

152 args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

153 butler = dp.Butler(outputs=args) 

154 self.assertIsInstance(butler, dp.Butler) 

155 self.assertTrue(self.testDir, 'repositoryCfg.yaml') 

156 

157 try: 

158 butler2 = dp.Butler(inputs=uri) 

159 self.assertIsInstance(butler2, dp.Butler) 

160 except RuntimeError: 

161 self.fail("Butler init raised a runtime error loading input %s" % uri) 

162 

163 

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

165 pass 

166 

167 

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

169 lsst.utils.tests.init() 

170 unittest.main()