Coverage for tests/test_testRepo.py: 21%

117 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-08 10:28 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Unit tests for `lsst.daf.butler.tests.testRepo`, a module for creating 

23test repositories or butlers. 

24""" 

25 

26import os 

27import shutil 

28import unittest 

29 

30import lsst.daf.butler 

31from lsst.daf.butler.tests import ( 

32 MetricsExample, 

33 addDataIdValue, 

34 addDatasetType, 

35 expandUniqueId, 

36 makeTestCollection, 

37 makeTestRepo, 

38 registerMetricsExample, 

39) 

40from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir, safeTestTempDir 

41 

42TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

43 

44 

45class ButlerTestRepoTestCase(unittest.TestCase): 

46 """Simpler test than below without setUpClass getting in the way.""" 

47 

48 def setUp(self): 

49 self.root = makeTestTempDir(TESTDIR) 

50 

51 def tearDown(self): 

52 removeTestTempDir(self.root) 

53 

54 def testMakeTestRepo(self): 

55 dataIds = { 

56 "instrument": ["DummyCam"], 

57 "physical_filter": ["d-r"], 

58 "exposure": [42, 43, 44], 

59 "visit": [42, 43, 44], 

60 } 

61 

62 butler = makeTestRepo(self.root, dataIds) 

63 

64 records = list(butler.registry.queryDimensionRecords("visit")) 

65 self.assertEqual(len(records), 3) 

66 

67 

68class ButlerUtilsTestSuite(unittest.TestCase): 

69 @classmethod 

70 def setUpClass(cls): 

71 # Repository should be re-created for each test case, but 

72 # this has a prohibitive run-time cost at present 

73 cls.root = makeTestTempDir(TESTDIR) 

74 

75 cls.creatorButler = makeTestRepo(cls.root) 

76 addDataIdValue(cls.creatorButler, "instrument", "notACam") 

77 addDataIdValue(cls.creatorButler, "instrument", "dummyCam") 

78 addDataIdValue(cls.creatorButler, "physical_filter", "k2020", band="k", instrument="notACam") 

79 addDataIdValue(cls.creatorButler, "physical_filter", "l2019", instrument="dummyCam") 

80 addDataIdValue(cls.creatorButler, "visit", 101, instrument="notACam", physical_filter="k2020") 

81 addDataIdValue(cls.creatorButler, "visit", 102, instrument="notACam", physical_filter="k2020") 

82 addDataIdValue(cls.creatorButler, "detector", 5) 

83 # Leave skymap/patch/tract undefined so that tests can assume 

84 # they're missing. 

85 

86 registerMetricsExample(cls.creatorButler) 

87 addDatasetType(cls.creatorButler, "DataType1", {"instrument"}, "StructuredDataNoComponents") 

88 addDatasetType(cls.creatorButler, "DataType2", {"instrument", "visit"}, "StructuredData") 

89 

90 @classmethod 

91 def tearDownClass(cls): 

92 # TODO: use addClassCleanup rather than tearDownClass in Python 3.8 

93 # to keep the addition and removal together and make it more robust 

94 removeTestTempDir(cls.root) 

95 

96 def setUp(self): 

97 # TestCase.id() is unique for each test method 

98 self.butler = makeTestCollection(self.creatorButler, uniqueId=self.id()) 

99 

100 def testButlerValid(self): 

101 self.butler.validateConfiguration() 

102 

103 def testButlerKwargs(self): 

104 # outfile has the most obvious effects of any Butler.makeRepo keyword 

105 with safeTestTempDir(TESTDIR) as temp: 

106 path = os.path.join(temp, "oddConfig.json") 

107 makeTestRepo(temp, {}, outfile=path) 

108 self.assertTrue(os.path.isfile(path)) 

109 

110 def _checkButlerDimension(self, dimensions, query, expected): 

111 result = list(self.butler.registry.queryDataIds(dimensions, where=query, check=False)) 

112 self.assertEqual(len(result), 1) 

113 self.assertIn(result[0].byName(), expected) 

114 

115 def testButlerDimensions(self): 

116 self._checkButlerDimension( 

117 {"instrument"}, "instrument='notACam'", [{"instrument": "notACam"}, {"instrument": "dummyCam"}] 

118 ) 

119 self._checkButlerDimension( 

120 {"visit", "instrument"}, 

121 "visit=101", 

122 [{"instrument": "notACam", "visit": 101}, {"instrument": "dummyCam", "visit": 101}], 

123 ) 

124 self._checkButlerDimension( 

125 {"visit", "instrument"}, 

126 "visit=102", 

127 [{"instrument": "notACam", "visit": 102}, {"instrument": "dummyCam", "visit": 102}], 

128 ) 

129 self._checkButlerDimension( 

130 {"detector", "instrument"}, 

131 "detector=5", 

132 [{"instrument": "notACam", "detector": 5}, {"instrument": "dummyCam", "detector": 5}], 

133 ) 

134 

135 def testAddDataIdValue(self): 

136 addDataIdValue(self.butler, "visit", 1, instrument="notACam", physical_filter="k2020") 

137 self._checkButlerDimension( 

138 {"visit", "instrument"}, "visit=1", [{"instrument": "notACam", "visit": 1}] 

139 ) 

140 addDataIdValue(self.butler, "visit", 2, instrument="dummyCam", physical_filter="l2019") 

141 self._checkButlerDimension( 

142 {"visit", "instrument"}, "visit=2", [{"instrument": "dummyCam", "visit": 2}] 

143 ) 

144 

145 with self.assertRaises(ValueError): 

146 addDataIdValue(self.butler, "NotADimension", 42) 

147 with self.assertRaises(ValueError): 

148 addDataIdValue(self.butler, "detector", "nonNumeric") 

149 with self.assertRaises(ValueError): 

150 addDataIdValue(self.butler, "detector", 101, nonsenseField="string") 

151 

152 # Keywords imply different instruments 

153 with self.assertRaises(RuntimeError): 

154 addDataIdValue(self.butler, "exposure", 101, instrument="dummyCam", physical_filter="k2020") 

155 

156 # No skymap defined 

157 with self.assertRaises(RuntimeError): 

158 addDataIdValue(self.butler, "tract", 42) 

159 # Didn't create skymap "map" first. 

160 with self.assertRaises(RuntimeError): 

161 addDataIdValue(self.butler, "tract", 43, skymap="map") 

162 

163 def testAddDatasetType(self): 

164 # 1 for StructuredDataNoComponents, 1 for StructuredData (components 

165 # not included). 

166 self.assertEqual(len(list(self.butler.registry.queryDatasetTypes(components=False))), 2) 

167 

168 # Testing the DatasetType objects is not practical, because all tests 

169 # need a DimensionUniverse. So just check that we have the dataset 

170 # types we expect. 

171 self.butler.registry.getDatasetType("DataType1") 

172 self.butler.registry.getDatasetType("DataType2") 

173 

174 with self.assertRaises(ValueError): 

175 addDatasetType(self.butler, "DataType3", {"4thDimension"}, "NumpyArray") 

176 with self.assertRaises(ValueError): 

177 addDatasetType(self.butler, "DataType3", {"instrument"}, "UnstorableType") 

178 

179 def testRegisterMetricsExample(self): 

180 id1 = {"instrument": "notACam"} 

181 id2 = expandUniqueId(self.butler, {"visit": 101}) 

182 data = MetricsExample(summary={"answer": 42, "question": "unknown"}) 

183 

184 self.butler.put(data, "DataType1", id1) 

185 self.assertEqual(self.butler.get("DataType1", id1), data) 

186 

187 self.butler.put(data, "DataType2", id2) 

188 self.assertEqual(self.butler.get("DataType2", id2), data) 

189 self.assertEqual(self.butler.get("DataType2.summary", id2), data.summary) 

190 

191 def testRegisterMetricsExampleChained(self): 

192 """Regression test for registerMetricsExample having no effect 

193 on ChainedDatastore. 

194 """ 

195 temp = makeTestTempDir(TESTDIR) 

196 try: 

197 config = lsst.daf.butler.Config() 

198 config["datastore", "cls"] = "lsst.daf.butler.datastores.chainedDatastore.ChainedDatastore" 

199 config["datastore", "datastores"] = [ 

200 { 

201 "cls": "lsst.daf.butler.datastores.fileDatastore.FileDatastore", 

202 } 

203 ] 

204 

205 repo = lsst.daf.butler.Butler.makeRepo(temp, config=config) 

206 butler = lsst.daf.butler.Butler(repo, run="chainedExample") 

207 registerMetricsExample(butler) 

208 addDatasetType(butler, "DummyType", {}, "StructuredDataNoComponents") 

209 

210 data = MetricsExample(summary={}) 

211 # Should not raise 

212 butler.put(data, "DummyType") 

213 finally: 

214 shutil.rmtree(temp, ignore_errors=True) 

215 

216 def testUniqueButler(self): 

217 dataId = {"instrument": "notACam"} 

218 self.butler.put(MetricsExample({"answer": 42, "question": "unknown"}), "DataType1", dataId) 

219 self.assertTrue(self.butler.datasetExists("DataType1", dataId)) 

220 

221 newButler = makeTestCollection(self.creatorButler) 

222 with self.assertRaises(LookupError): 

223 newButler.datasetExists("DataType1", dataId) 

224 

225 def testExpandUniqueId(self): 

226 self.assertEqual( 

227 dict(expandUniqueId(self.butler, {"instrument": "notACam"})), {"instrument": "notACam"} 

228 ) 

229 self.assertIn( 

230 dict(expandUniqueId(self.butler, {"visit": 101})), 

231 [{"instrument": "notACam", "visit": 101}, {"instrument": "dummyCam", "visit": 101}], 

232 ) 

233 self.assertIn( 

234 dict(expandUniqueId(self.butler, {"detector": 5})), 

235 [{"instrument": "notACam", "detector": 5}, {"instrument": "dummyCam", "detector": 5}], 

236 ) 

237 self.assertIn( 

238 dict(expandUniqueId(self.butler, {"physical_filter": "k2020"})), 

239 [ 

240 {"instrument": "notACam", "physical_filter": "k2020"}, 

241 {"instrument": "notACam", "physical_filter": "k2020"}, 

242 ], 

243 ) 

244 with self.assertRaises(ValueError): 

245 expandUniqueId(self.butler, {"tract": 42}) 

246 

247 

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

249 unittest.main()