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 testWhere(self): 

78 """Test with a WHERE constraint.""" 

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

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

81 expected = AstropyTable( 

82 array(( 

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

84 )), 

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

86 ) 

87 self.assertAstropyTablesEqual(res, expected) 

88 

89 def testDatasetsAndCollections(self): 

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

91 

92 # Add a dataset in a different collection 

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

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

95 "name": "fourtwentyfive", 

96 "physical_filter": "d-r", 

97 "visit_system": 1}) 

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

99 run="foo") 

100 

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

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

103 datasets="test_metric_comp") 

104 expected = AstropyTable( 

105 array(( 

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

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

108 )), 

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

110 ) 

111 self.assertAstropyTablesEqual(res, expected) 

112 

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

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

115 datasets="test_metric_comp") 

116 expected = AstropyTable( 

117 array(( 

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

119 )), 

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

121 ) 

122 self.assertAstropyTablesEqual(res, expected) 

123 

124 

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

126 unittest.main()