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 shutil 

29import tempfile 

30import unittest 

31 

32from lsst.daf.butler import Butler 

33from lsst.daf.butler import script 

34from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo 

35 

36 

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

38 

39 

40class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper): 

41 

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

43 

44 @staticmethod 

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

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

47 values.""" 

48 return script.queryDataIds(repo=repo, 

49 dimensions=dimensions, 

50 collections=collections, 

51 datasets=datasets, 

52 where=where) 

53 

54 def setUp(self): 

55 self.root = tempfile.mkdtemp(dir=TESTDIR) 

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

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

58 

59 def tearDown(self): 

60 if os.path.exists(self.root): 

61 shutil.rmtree(self.root, ignore_errors=True) 

62 

63 def testDimensions(self): 

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

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

66 expected = AstropyTable( 

67 array(( 

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

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

70 )), 

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

72 ) 

73 self.assertAstropyTablesEqual(res, expected) 

74 

75 def testNull(self): 

76 "Test asking for nothing." 

77 res = self._queryDataIds(self.root) 

78 self.assertEqual(res, None) 

79 

80 def testDatasets(self): 

81 """Test getting datasets.""" 

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

83 expected = AstropyTable( 

84 array(( 

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

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

87 )), 

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

89 ) 

90 self.assertAstropyTablesEqual(res, expected) 

91 

92 def testWhere(self): 

93 """Test getting datasets.""" 

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

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

96 expected = AstropyTable( 

97 array(( 

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

99 )), 

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

101 ) 

102 self.assertAstropyTablesEqual(res, expected) 

103 

104 def testCollections(self): 

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

106 

107 # Add a dataset in a different collection 

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

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

110 "name": "fourtwentyfive", 

111 "physical_filter": "d-r", 

112 "visit_system": 1}) 

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

114 run="foo") 

115 

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

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

118 datasets="test_metric_comp") 

119 expected = AstropyTable( 

120 array(( 

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

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

123 )), 

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

125 ) 

126 self.assertAstropyTablesEqual(res, expected) 

127 

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

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

130 datasets="test_metric_comp") 

131 expected = AstropyTable( 

132 array(( 

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

134 )), 

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

136 ) 

137 self.assertAstropyTablesEqual(res, expected) 

138 

139 

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

141 unittest.main()