Coverage for tests/test_testRepo.py: 21%

107 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-17 02:08 -0700

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, 4 for StructuredData 

142 self.assertEqual(len(list(self.butler.registry.queryDatasetTypes(components=True))), 5) 

143 

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

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

146 # types we expect. 

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

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

149 

150 with self.assertRaises(ValueError): 

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

152 with self.assertRaises(ValueError): 

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

154 

155 def testRegisterMetricsExample(self): 

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

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

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

159 

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

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

162 

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

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

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

166 

167 def testRegisterMetricsExampleChained(self): 

168 """Regression test for registerMetricsExample having no effect 

169 on ChainedDatastore. 

170 """ 

171 temp = makeTestTempDir(TESTDIR) 

172 try: 

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

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

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

176 { 

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

178 } 

179 ] 

180 

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

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

183 registerMetricsExample(butler) 

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

185 

186 data = MetricsExample(summary={}) 

187 # Should not raise 

188 butler.put(data, "DummyType") 

189 finally: 

190 shutil.rmtree(temp, ignore_errors=True) 

191 

192 def testUniqueButler(self): 

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

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

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

196 

197 newButler = makeTestCollection(self.creatorButler) 

198 with self.assertRaises(LookupError): 

199 newButler.datasetExists("DataType1", dataId) 

200 

201 def testExpandUniqueId(self): 

202 self.assertEqual( 

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

204 ) 

205 self.assertIn( 

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

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

208 ) 

209 self.assertIn( 

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

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

212 ) 

213 self.assertIn( 

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

215 [ 

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

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

218 ], 

219 ) 

220 with self.assertRaises(ValueError): 

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

222 

223 

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

225 unittest.main()