Coverage for tests/test_cliCmdQueryDataIds.py: 32%

63 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 12:13 +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 daf_butler CLI query-collections command. 

23""" 

24 

25import os 

26import unittest 

27 

28from astropy.table import Table as AstropyTable 

29from lsst.daf.butler import Butler, DatasetType, script 

30from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir 

31from numpy import array 

32 

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

34 

35 

36class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper): 

37 

38 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDataIds" 

39 

40 @staticmethod 

41 def _queryDataIds(repo, dimensions=(), collections=(), datasets=None, where=None): 

42 """Helper to populate the call to script.queryDataIds with default 

43 values.""" 

44 return script.queryDataIds( 

45 repo=repo, 

46 dimensions=dimensions, 

47 collections=collections, 

48 datasets=datasets, 

49 where=where, 

50 order_by=None, 

51 limit=0, 

52 offset=0, 

53 ) 

54 

55 def setUp(self): 

56 self.root = makeTestTempDir(TESTDIR) 

57 self.repo = MetricTestRepo( 

58 root=self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml") 

59 ) 

60 

61 def tearDown(self): 

62 removeTestTempDir(self.root) 

63 

64 def testDimensions(self): 

65 """Test getting a dimension.""" 

66 res, msg = self._queryDataIds(self.root, dimensions=("visit",)) 

67 expected = AstropyTable( 

68 array((("R", "DummyCamComp", "d-r", 423), ("R", "DummyCamComp", "d-r", 424))), 

69 names=("band", "instrument", "physical_filter", "visit"), 

70 ) 

71 self.assertFalse(msg) 

72 self.assertAstropyTablesEqual(res, expected) 

73 

74 def testNull(self): 

75 "Test asking for nothing." 

76 res, msg = self._queryDataIds(self.root) 

77 self.assertIsNone(res, msg) 

78 self.assertEqual(msg, "") 

79 

80 def testWhere(self): 

81 """Test with a WHERE constraint.""" 

82 res, msg = self._queryDataIds( 

83 self.root, dimensions=("visit",), where="instrument='DummyCamComp' AND visit=423" 

84 ) 

85 expected = AstropyTable( 

86 array((("R", "DummyCamComp", "d-r", 423),)), 

87 names=("band", "instrument", "physical_filter", "visit"), 

88 ) 

89 self.assertAstropyTablesEqual(res, expected) 

90 self.assertIsNone(msg) 

91 

92 def testDatasetsAndCollections(self): 

93 """Test constraining via datasets and collections.""" 

94 

95 # Add a dataset in a different collection 

96 self.butler = Butler(self.root, run="foo") 

97 self.repo.butler.registry.insertDimensionData( 

98 "visit", 

99 { 

100 "instrument": "DummyCamComp", 

101 "id": 425, 

102 "name": "fourtwentyfive", 

103 "physical_filter": "d-r", 

104 }, 

105 ) 

106 self.repo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo") 

107 

108 # Verify the new dataset is not found in the "ingest/run" collection. 

109 res, msg = self._queryDataIds( 

110 repo=self.root, dimensions=("visit",), collections=("ingest/run",), datasets="test_metric_comp" 

111 ) 

112 expected = AstropyTable( 

113 array((("R", "DummyCamComp", "d-r", 423), ("R", "DummyCamComp", "d-r", 424))), 

114 names=("band", "instrument", "physical_filter", "visit"), 

115 ) 

116 self.assertAstropyTablesEqual(res, expected) 

117 self.assertIsNone(msg) 

118 

119 # Verify the new dataset is found in the "foo" collection. 

120 res, msg = self._queryDataIds( 

121 repo=self.root, dimensions=("visit",), collections=("foo",), datasets="test_metric_comp" 

122 ) 

123 expected = AstropyTable( 

124 array((("R", "DummyCamComp", "d-r", 425),)), 

125 names=("band", "instrument", "physical_filter", "visit"), 

126 ) 

127 self.assertAstropyTablesEqual(res, expected) 

128 self.assertIsNone(msg) 

129 

130 # Verify the new dataset is found in the "foo" collection and the 

131 # dimensions are determined automatically. 

132 with self.assertLogs("lsst.daf.butler.script.queryDataIds", "INFO") as cm: 

133 res, msg = self._queryDataIds(repo=self.root, collections=("foo",), datasets="test_metric_comp") 

134 self.assertIn("Determined dimensions", "\n".join(cm.output)) 

135 expected = AstropyTable( 

136 array((("R", "DummyCamComp", "d-r", 425),)), 

137 names=("band", "instrument", "physical_filter", "visit"), 

138 ) 

139 self.assertAstropyTablesEqual(res, expected) 

140 self.assertIsNone(msg) 

141 

142 # Check that we get a reason if no dimensions can be inferred. 

143 new_dataset_type = DatasetType( 

144 "test_metric_dimensionless", 

145 (), 

146 "StructuredDataDict", 

147 universe=self.repo.butler.registry.dimensions, 

148 ) 

149 self.repo.butler.registry.registerDatasetType(new_dataset_type) 

150 res, msg = self._queryDataIds(repo=self.root, collections=("foo",), datasets=...) 

151 self.assertIsNone(res) 

152 self.assertIn("No dimensions in common", msg) 

153 

154 # Check that we get a reason returned if no dataset type is found. 

155 res, msg = self._queryDataIds( 

156 repo=self.root, dimensions=("visit",), collections=("foo",), datasets="raw" 

157 ) 

158 self.assertIsNone(res) 

159 self.assertEqual(msg, "Dataset type raw is not registered.") 

160 

161 # Check that we get a reason returned if no dataset is found in 

162 # collection. 

163 res, msg = self._queryDataIds( 

164 repo=self.root, dimensions=("visit",), collections=("ingest",), datasets="test_metric_comp" 

165 ) 

166 self.assertIsNone(res) 

167 self.assertIn("No datasets of type test_metric_comp", msg) 

168 

169 

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

171 unittest.main()