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 StorageClassFactory 

33from lsst.daf.butler import Butler 

34from lsst.daf.butler.cli.butler import cli as butlerCli 

35from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner 

36from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, readTable 

37 

38 

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

40 

41 

42class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper): 

43 

44 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords" 

45 

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

47 storageClassFactory = StorageClassFactory() 

48 

49 expectedColumnNames = ("instrument", "id", "physical_filter", "visit_system", "name", "day_obs", 

50 "exposure_time", "target_name", "observation_reason", "science_program", 

51 "zenith_angle", "region", "timespan [2]") 

52 

53 def setUp(self): 

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

55 self.testRepo = MetricTestRepo(self.root, 

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

57 self.runner = LogCliRunner() 

58 

59 def tearDown(self): 

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

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

62 

63 def testBasic(self): 

64 result = self.runner.invoke(butlerCli, ["query-dimension-records", 

65 self.root, 

66 "visit"]) 

67 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

68 rows = array(( 

69 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None", 

70 "None", "None", "None .. None"), 

71 ("DummyCamComp", "424", "d-r", "1", "fourtwentyfour", "None", "None", "None", "None", "None", 

72 "None", "None", "None .. None") 

73 )) 

74 expected = AstropyTable(rows, names=self.expectedColumnNames) 

75 self.assertAstropyTablesEqual(readTable(result.output), expected) 

76 

77 def testWhere(self): 

78 result = self.runner.invoke(butlerCli, ["query-dimension-records", 

79 self.root, 

80 "visit", 

81 "--where", 

82 "instrument='DummyCamComp' AND visit.name='fourtwentythree'"]) 

83 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

84 rows = array(( 

85 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None", 

86 "None", "None", "None .. None"), 

87 )) 

88 expected = AstropyTable(rows, names=self.expectedColumnNames) 

89 self.assertAstropyTablesEqual(readTable(result.output), expected) 

90 

91 def testCollection(self): 

92 

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

94 

95 # try replacing the testRepo's butler with the one with the "foo" run. 

96 self.testRepo.butler = butler 

97 

98 self.testRepo.butler.registry.insertDimensionData("visit", {"instrument": "DummyCamComp", "id": 425, 

99 "name": "fourtwentyfive", 

100 "physical_filter": "d-r", 

101 "visit_system": 1}) 

102 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, 

103 run="foo") 

104 

105 # verify getting records from the "ingest/run" collection 

106 result = self.runner.invoke(butlerCli, ["query-dimension-records", 

107 self.root, 

108 "visit", 

109 "--collections", "ingest/run", 

110 "--datasets", "test_metric_comp" 

111 ]) 

112 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

113 rows = array(( 

114 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None", 

115 "None", "None", "None .. None"), 

116 ("DummyCamComp", "424", "d-r", "1", "fourtwentyfour", "None", "None", "None", "None", "None", 

117 "None", "None", "None .. None") 

118 )) 

119 expected = AstropyTable(rows, names=self.expectedColumnNames) 

120 self.assertAstropyTablesEqual(readTable(result.output), expected) 

121 

122 # verify getting records from the "foo" collection 

123 result = self.runner.invoke(butlerCli, ["query-dimension-records", 

124 self.root, 

125 "visit", 

126 "--collections", "foo", 

127 "--datasets", "test_metric_comp" 

128 ]) 

129 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

130 rows = array(( 

131 ("DummyCamComp", "425", "d-r", "1", "fourtwentyfive", "None", "None", "None", "None", "None", 

132 "None", "None", "None .. None"), 

133 )) 

134 expected = AstropyTable(rows, names=self.expectedColumnNames) 

135 self.assertAstropyTablesEqual(readTable(result.output), 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()