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 

26from numpy import array 

27import os 

28import unittest 

29 

30from lsst.daf.butler import ( 

31 Butler, 

32 CollectionType, 

33) 

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

35from lsst.daf.butler.cli.cmd import query_collections 

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

37from lsst.daf.butler.script import queryCollections 

38from lsst.daf.butler.tests import CliCmdTestBase, DatastoreMock 

39from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable 

40 

41 

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

43 

44 

45class QueryCollectionsCmdTest(CliCmdTestBase, unittest.TestCase): 

46 

47 @staticmethod 

48 def defaultExpected(): 

49 return dict(repo=None, 

50 collection_type=tuple(CollectionType.__members__.values()), 

51 chains="TABLE", 

52 glob=()) 

53 

54 @staticmethod 

55 def command(): 

56 return query_collections 

57 

58 def test_minimal(self): 

59 """Test only the required parameters, and omit the optional parameters. 

60 """ 

61 self.run_test(["query-collections", "here"], 

62 self.makeExpected(repo="here")) 

63 

64 def test_all(self): 

65 """Test all parameters""" 

66 self.run_test(["query-collections", "here", "foo*", 

67 "--collection-type", "TAGGED", 

68 "--collection-type", "RUN"], 

69 self.makeExpected(repo="here", 

70 glob=("foo*",), 

71 collection_type=(CollectionType.TAGGED, CollectionType.RUN), 

72 chains="TABLE")) 

73 

74 

75class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase): 

76 

77 def setUp(self): 

78 self.runner = LogCliRunner() 

79 

80 def testGetCollections(self): 

81 run = "ingest/run" 

82 tag = "tag" 

83 with self.runner.isolated_filesystem(): 

84 butlerCfg = Butler.makeRepo("here") 

85 # the purpose of this call is to create some collections 

86 Butler(butlerCfg, run=run, tags=[tag], collections=[tag], writeable=True) 

87 

88 # Verify collections that were created are found by 

89 # query-collections. 

90 result = self.runner.invoke(cli, ["query-collections", "here"]) 

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

92 expected = Table((("ingest/run", "tag"), ("RUN", "TAGGED")), 

93 names=("Name", "Type")) 

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

95 

96 # Verify that with a glob argument, that only collections whose 

97 # name matches with the specified pattern are returned. 

98 result = self.runner.invoke(cli, ["query-collections", "here", "t*"]) 

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

100 expected = Table((("tag",), ("TAGGED",)), 

101 names=("Name", "Type")) 

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

103 

104 # Verify that with a collection type argument, only collections of 

105 # that type are returned. 

106 result = self.runner.invoke(cli, ["query-collections", "here", "--collection-type", "RUN"]) 

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

108 expected = Table((("ingest/run",), ("RUN",)), 

109 names=("Name", "Type")) 

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

111 

112 

113class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase): 

114 

115 def setUp(self): 

116 self.runner = LogCliRunner() 

117 

118 def testChained(self): 

119 with self.runner.isolated_filesystem(): 

120 

121 # Create a butler and add some chained collections: 

122 butlerCfg = Butler.makeRepo("here") 

123 

124 butler1 = Butler(butlerCfg, writeable=True) 

125 

126 # Replace datastore functions with mocks: 

127 DatastoreMock.apply(butler1) 

128 

129 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "base.yaml")) 

130 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "datasets.yaml")) 

131 registry1 = butler1.registry 

132 registry1.registerRun("run1") 

133 registry1.registerCollection("tag1", CollectionType.TAGGED) 

134 registry1.registerCollection("calibration1", CollectionType.CALIBRATION) 

135 registry1.registerCollection("chain1", CollectionType.CHAINED) 

136 registry1.registerCollection("chain2", CollectionType.CHAINED) 

137 registry1.setCollectionChain("chain1", ["tag1", "run1", "chain2"]) 

138 registry1.setCollectionChain("chain2", ["calibration1", "run1"]) 

139 

140 # Use the script function to test the query-collections TREE 

141 # option, because the astropy.table.Table.read method, which we are 

142 # using for verification elsewhere in this file, seems to strip 

143 # leading whitespace from columns. This makes it impossible to test 

144 # the nested TREE output of the query-collections subcommand from 

145 # the command line interface. 

146 table = queryCollections("here", glob=(), collection_type=CollectionType.all(), chains="TREE") 

147 

148 # self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

149 expected = Table(array((("imported_g", "RUN"), 

150 ("imported_r", "RUN"), 

151 ("run1", "RUN"), 

152 ("tag1", "TAGGED"), 

153 ("calibration1", "CALIBRATION"), 

154 ("chain1", "CHAINED"), 

155 (" tag1", "TAGGED"), 

156 (" run1", "RUN"), 

157 (" chain2", "CHAINED"), 

158 (" calibration1", "CALIBRATION"), 

159 (" run1", "RUN"), 

160 ("chain2", "CHAINED"), 

161 (" calibration1", "CALIBRATION"), 

162 (" run1", "RUN"))), 

163 names=("Name", "Type")) 

164 self.assertAstropyTablesEqual(table, expected) 

165 

166 result = self.runner.invoke(cli, ["query-collections", "here"]) 

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

168 expected = Table(array(( 

169 ("imported_g", "RUN", ""), 

170 ("imported_r", "RUN", ""), 

171 ("run1", "RUN", ""), 

172 ("tag1", "TAGGED", ""), 

173 ("calibration1", "CALIBRATION", ""), 

174 ("chain1", "CHAINED", "[tag1, run1, chain2]"), 

175 ("chain2", "CHAINED", "[calibration1, run1]"))), 

176 names=("Name", "Type", "Definition")) 

177 table = readTable(result.output) 

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

179 

180 result = self.runner.invoke(cli, ["query-collections", "here", "--chains", "FLATTEN"]) 

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

182 expected = Table(array(( 

183 ("imported_g", "RUN"), 

184 ("imported_r", "RUN"), 

185 ("run1", "RUN"), 

186 ("tag1", "TAGGED"), 

187 ("calibration1", "CALIBRATION"), 

188 ("tag1", "TAGGED"), 

189 ("run1", "RUN"), 

190 ("calibration1", "CALIBRATION"), 

191 ("calibration1", "CALIBRATION"), 

192 ("run1", "RUN"))), 

193 names=("Name", "Type")) 

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

195 

196 

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

198 unittest.main()