Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

25from astropy.table import Table as AstropyTable 

26from numpy import array 

27import os 

28import unittest 

29 

30from lsst.daf.butler import Butler 

31from lsst.daf.butler import script 

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

33 

34 

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

36 

37 

38class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper): 

39 

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

41 

42 @staticmethod 

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

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

45 values.""" 

46 return script.queryDataIds(repo=repo, 

47 dimensions=dimensions, 

48 collections=collections, 

49 datasets=datasets, 

50 where=where) 

51 

52 def setUp(self): 

53 self.root = makeTestTempDir(TESTDIR) 

54 self.repo = MetricTestRepo(root=self.root, 

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

56 

57 def tearDown(self): 

58 removeTestTempDir(self.root) 

59 

60 def testDimensions(self): 

61 """Test getting a dimension.""" 

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

63 expected = AstropyTable( 

64 array(( 

65 ("R", "DummyCamComp", "d-r", 1, 423), 

66 ("R", "DummyCamComp", "d-r", 1, 424) 

67 )), 

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

69 ) 

70 self.assertAstropyTablesEqual(res, expected) 

71 

72 def testNull(self): 

73 "Test asking for nothing." 

74 res = self._queryDataIds(self.root) 

75 self.assertEqual(res, None) 

76 

77 def testDatasets(self): 

78 """Test getting datasets.""" 

79 res = self._queryDataIds(self.root, datasets="test_metric_comp") 

80 expected = AstropyTable( 

81 array(( 

82 ("R", "DummyCamComp", "d-r", 1, 423), 

83 ("R", "DummyCamComp", "d-r", 1, 424) 

84 )), 

85 names=("band", "instrument", "physical_filter", "visit_system", "visit") 

86 ) 

87 self.assertAstropyTablesEqual(res, expected) 

88 

89 def testWhere(self): 

90 """Test getting datasets.""" 

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

92 where="instrument='DummyCamComp' AND visit=423") 

93 expected = AstropyTable( 

94 array(( 

95 ("R", "DummyCamComp", "d-r", 1, 423), 

96 )), 

97 names=("band", "instrument", "physical_filter", "visit_system", "visit") 

98 ) 

99 self.assertAstropyTablesEqual(res, expected) 

100 

101 def testCollections(self): 

102 """Test getting datasets using the collections option.""" 

103 

104 # Add a dataset in a different collection 

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

106 self.repo.butler.registry.insertDimensionData("visit", {"instrument": "DummyCamComp", "id": 425, 

107 "name": "fourtwentyfive", 

108 "physical_filter": "d-r", 

109 "visit_system": 1}) 

110 self.repo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, 

111 run="foo") 

112 

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

114 res = self._queryDataIds(repo=self.root, dimensions=("visit",), collections=("ingest/run",), 

115 datasets="test_metric_comp") 

116 expected = AstropyTable( 

117 array(( 

118 ("R", "DummyCamComp", "d-r", 1, 423), 

119 ("R", "DummyCamComp", "d-r", 1, 424) 

120 )), 

121 names=("band", "instrument", "physical_filter", "visit_system", "visit") 

122 ) 

123 self.assertAstropyTablesEqual(res, expected) 

124 

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

126 res = self._queryDataIds(repo=self.root, dimensions=("visit",), collections=("foo",), 

127 datasets="test_metric_comp") 

128 expected = AstropyTable( 

129 array(( 

130 ("R", "DummyCamComp", "d-r", 1, 425), 

131 )), 

132 names=("band", "instrument", "physical_filter", "visit_system", "visit") 

133 ) 

134 self.assertAstropyTablesEqual(res, expected) 

135 

136 

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

138 unittest.main()