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 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryCollections" 

48 

49 @staticmethod 

50 def defaultExpected(): 

51 return dict(repo=None, 

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

53 chains="TABLE", 

54 glob=()) 

55 

56 @staticmethod 

57 def command(): 

58 return query_collections 

59 

60 def test_minimal(self): 

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

62 """ 

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

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

65 

66 def test_all(self): 

67 """Test all parameters""" 

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

69 "--collection-type", "TAGGED", 

70 "--collection-type", "RUN"], 

71 self.makeExpected(repo="here", 

72 glob=("foo*",), 

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

74 chains="TABLE")) 

75 

76 

77class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase): 

78 

79 def setUp(self): 

80 self.runner = LogCliRunner() 

81 

82 def testGetCollections(self): 

83 run = "ingest/run" 

84 tag = "tag" 

85 with self.runner.isolated_filesystem(): 

86 butlerCfg = Butler.makeRepo("here") 

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

88 butler = Butler(butlerCfg, run=run, collections=[tag], writeable=True) 

89 butler.registry.registerCollection(tag, CollectionType.TAGGED) 

90 

91 # Verify collections that were created are found by 

92 # query-collections. 

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

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

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

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

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

98 

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

100 # name matches with the specified pattern are returned. 

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

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

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

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

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

106 

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

108 # that type are returned. 

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

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

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

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

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

114 

115 

116class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase): 

117 

118 def setUp(self): 

119 self.runner = LogCliRunner() 

120 

121 def testChained(self): 

122 with self.runner.isolated_filesystem(): 

123 

124 # Create a butler and add some chained collections: 

125 butlerCfg = Butler.makeRepo("here") 

126 

127 butler1 = Butler(butlerCfg, writeable=True) 

128 

129 # Replace datastore functions with mocks: 

130 DatastoreMock.apply(butler1) 

131 

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

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

134 registry1 = butler1.registry 

135 registry1.registerRun("run1") 

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

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

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

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

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

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

142 

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

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

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

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

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

148 # the command line interface. 

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

150 

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

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

153 ("imported_r", "RUN"), 

154 ("run1", "RUN"), 

155 ("tag1", "TAGGED"), 

156 ("calibration1", "CALIBRATION"), 

157 ("chain1", "CHAINED"), 

158 (" tag1", "TAGGED"), 

159 (" run1", "RUN"), 

160 (" chain2", "CHAINED"), 

161 (" calibration1", "CALIBRATION"), 

162 (" run1", "RUN"), 

163 ("chain2", "CHAINED"), 

164 (" calibration1", "CALIBRATION"), 

165 (" run1", "RUN"))), 

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

167 self.assertAstropyTablesEqual(table, expected) 

168 

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

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

171 expected = Table(array(( 

172 ("imported_g", "RUN", ""), 

173 ("imported_r", "RUN", ""), 

174 ("run1", "RUN", ""), 

175 ("tag1", "TAGGED", ""), 

176 ("calibration1", "CALIBRATION", ""), 

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

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

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

180 table = readTable(result.output) 

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

182 

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

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

185 expected = Table(array(( 

186 ("imported_g", "RUN"), 

187 ("imported_r", "RUN"), 

188 ("run1", "RUN"), 

189 ("tag1", "TAGGED"), 

190 ("calibration1", "CALIBRATION"), 

191 ("tag1", "TAGGED"), 

192 ("run1", "RUN"), 

193 ("calibration1", "CALIBRATION"), 

194 ("calibration1", "CALIBRATION"), 

195 ("run1", "RUN"))), 

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

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

198 

199 

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

201 unittest.main()