Coverage for tests/test_testRepo.py: 20%

107 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-15 01:59 -0800

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 ButlerUtilsTestSuite(unittest.TestCase): 

46 @classmethod 

47 def setUpClass(cls): 

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

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

50 cls.root = makeTestTempDir(TESTDIR) 

51 

52 cls.creatorButler = makeTestRepo(cls.root) 

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

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

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

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

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

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

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

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

61 # they're missing. 

62 

63 registerMetricsExample(cls.creatorButler) 

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

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

66 

67 @classmethod 

68 def tearDownClass(cls): 

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

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

71 removeTestTempDir(cls.root) 

72 

73 def setUp(self): 

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

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

76 

77 def testButlerValid(self): 

78 self.butler.validateConfiguration() 

79 

80 def testButlerKwargs(self): 

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

82 with safeTestTempDir(TESTDIR) as temp: 

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

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

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

86 

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

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

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

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

91 

92 def testButlerDimensions(self): 

93 self._checkButlerDimension( 

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

95 ) 

96 self._checkButlerDimension( 

97 {"visit", "instrument"}, 

98 "visit=101", 

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

100 ) 

101 self._checkButlerDimension( 

102 {"visit", "instrument"}, 

103 "visit=102", 

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

105 ) 

106 self._checkButlerDimension( 

107 {"detector", "instrument"}, 

108 "detector=5", 

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

110 ) 

111 

112 def testAddDataIdValue(self): 

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

114 self._checkButlerDimension( 

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

116 ) 

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

118 self._checkButlerDimension( 

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

120 ) 

121 

122 with self.assertRaises(ValueError): 

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

124 with self.assertRaises(ValueError): 

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

126 with self.assertRaises(ValueError): 

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

128 

129 # Keywords imply different instruments 

130 with self.assertRaises(RuntimeError): 

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

132 

133 # No skymap defined 

134 with self.assertRaises(RuntimeError): 

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

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

137 with self.assertRaises(RuntimeError): 

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

139 

140 def testAddDatasetType(self): 

141 # 1 for StructuredDataNoComponents, 1 for StructuredData (components 

142 # not included). 

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

144 

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

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

147 # types we expect. 

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

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

150 

151 with self.assertRaises(ValueError): 

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

153 with self.assertRaises(ValueError): 

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

155 

156 def testRegisterMetricsExample(self): 

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

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

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

160 

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

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

163 

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

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

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

167 

168 def testRegisterMetricsExampleChained(self): 

169 """Regression test for registerMetricsExample having no effect 

170 on ChainedDatastore. 

171 """ 

172 temp = makeTestTempDir(TESTDIR) 

173 try: 

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

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

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

177 { 

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

179 } 

180 ] 

181 

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

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

184 registerMetricsExample(butler) 

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

186 

187 data = MetricsExample(summary={}) 

188 # Should not raise 

189 butler.put(data, "DummyType") 

190 finally: 

191 shutil.rmtree(temp, ignore_errors=True) 

192 

193 def testUniqueButler(self): 

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

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

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

197 

198 newButler = makeTestCollection(self.creatorButler) 

199 with self.assertRaises(LookupError): 

200 newButler.datasetExists("DataType1", dataId) 

201 

202 def testExpandUniqueId(self): 

203 self.assertEqual( 

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

205 ) 

206 self.assertIn( 

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

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

209 ) 

210 self.assertIn( 

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

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

213 ) 

214 self.assertIn( 

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

216 [ 

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

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

219 ], 

220 ) 

221 with self.assertRaises(ValueError): 

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

223 

224 

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

226 unittest.main()