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 @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 = tempfile.mkdtemp(dir=TESTDIR) 

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

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

56 

57 def tearDown(self): 

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

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

60 

61 def testDimensions(self): 

62 """Test getting a dimension.""" 

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

64 expected = AstropyTable( 

65 array(( 

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

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

68 )), 

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

70 ) 

71 self.assertAstropyTablesEqual(res, expected) 

72 

73 def testNull(self): 

74 "Test asking for nothing." 

75 res = self._queryDataIds(self.root) 

76 self.assertEqual(res, None) 

77 

78 def testDatasets(self): 

79 """Test getting datasets.""" 

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

81 expected = AstropyTable( 

82 array(( 

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

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

85 )), 

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

87 ) 

88 self.assertAstropyTablesEqual(res, expected) 

89 

90 def testWhere(self): 

91 """Test getting datasets.""" 

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

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

94 expected = AstropyTable( 

95 array(( 

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

97 )), 

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

99 ) 

100 self.assertAstropyTablesEqual(res, expected) 

101 

102 def testCollections(self): 

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

104 

105 # Add a dataset in a different collection 

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

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

108 "name": "fourtwentyfive", 

109 "physical_filter": "d-r", 

110 "visit_system": 1}) 

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

112 run="foo") 

113 

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

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

116 datasets="test_metric_comp") 

117 expected = AstropyTable( 

118 array(( 

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

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

121 )), 

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

123 ) 

124 self.assertAstropyTablesEqual(res, expected) 

125 

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

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

128 datasets="test_metric_comp") 

129 expected = AstropyTable( 

130 array(( 

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

132 )), 

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

134 ) 

135 self.assertAstropyTablesEqual(res, expected) 

136 

137 

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

139 unittest.main()