Coverage for tests/test_cliCmdQueryDataIds.py: 27%

62 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-01 02:05 -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 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 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDataIds" 

38 

39 @staticmethod 

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

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

42 values.""" 

43 return script.queryDataIds( 

44 repo=repo, 

45 dimensions=dimensions, 

46 collections=collections, 

47 datasets=datasets, 

48 where=where, 

49 order_by=None, 

50 limit=0, 

51 offset=0, 

52 ) 

53 

54 def setUp(self): 

55 self.root = makeTestTempDir(TESTDIR) 

56 self.repo = MetricTestRepo( 

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

58 ) 

59 

60 def tearDown(self): 

61 removeTestTempDir(self.root) 

62 

63 def testDimensions(self): 

64 """Test getting a dimension.""" 

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

66 expected = AstropyTable( 

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

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

69 ) 

70 self.assertFalse(msg) 

71 self.assertAstropyTablesEqual(res, expected) 

72 

73 def testNull(self): 

74 "Test asking for nothing." 

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

76 self.assertIsNone(res, msg) 

77 self.assertEqual(msg, "") 

78 

79 def testWhere(self): 

80 """Test with a WHERE constraint.""" 

81 res, msg = self._queryDataIds( 

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

83 ) 

84 expected = AstropyTable( 

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

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

87 ) 

88 self.assertAstropyTablesEqual(res, expected) 

89 self.assertIsNone(msg) 

90 

91 def testDatasetsAndCollections(self): 

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

93 

94 # Add a dataset in a different collection 

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

96 self.repo.butler.registry.insertDimensionData( 

97 "visit", 

98 { 

99 "instrument": "DummyCamComp", 

100 "id": 425, 

101 "name": "fourtwentyfive", 

102 "physical_filter": "d-r", 

103 }, 

104 ) 

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

106 

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

108 res, msg = self._queryDataIds( 

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

110 ) 

111 expected = AstropyTable( 

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

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

114 ) 

115 self.assertAstropyTablesEqual(res, expected) 

116 self.assertIsNone(msg) 

117 

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

119 res, msg = self._queryDataIds( 

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

121 ) 

122 expected = AstropyTable( 

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

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

125 ) 

126 self.assertAstropyTablesEqual(res, expected) 

127 self.assertIsNone(msg) 

128 

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

130 # dimensions are determined automatically. 

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

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

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

134 expected = AstropyTable( 

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

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

137 ) 

138 self.assertAstropyTablesEqual(res, expected) 

139 self.assertIsNone(msg) 

140 

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

142 new_dataset_type = DatasetType( 

143 "test_metric_dimensionless", 

144 (), 

145 "StructuredDataDict", 

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

147 ) 

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

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

150 self.assertIsNone(res) 

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

152 

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

154 with self.assertWarns(FutureWarning): 

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__": 

171 unittest.main()