Coverage for tests/test_cliCmdPruneCollection.py: 28%

Shortcuts 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

83 statements  

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 prune-collections subcommand. 

23""" 

24 

25import os 

26import unittest 

27 

28from astropy.table import Table 

29from lsst.daf.butler import Butler, CollectionType 

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

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

32from lsst.daf.butler.tests.utils import ( 

33 ButlerTestHelper, 

34 MetricTestRepo, 

35 makeTestTempDir, 

36 readTable, 

37 removeTestTempDir, 

38) 

39from numpy import array 

40 

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

42 

43 

44class PruneCollectionsTest(unittest.TestCase): 

45 def setUp(self): 

46 self.runner = LogCliRunner() 

47 

48 def testPruneCollections(self): 

49 """Test removing a collection and run from a repository using the 

50 butler prune-collection subcommand.""" 

51 with self.runner.isolated_filesystem(): 

52 repoName = "myRepo" 

53 runName = "myRun" 

54 taggedName = "taggedCollection" 

55 

56 # Add the run and the tagged collection to the repo: 

57 result = self.runner.invoke(butlerCli, ["create", repoName]) 

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

59 # Use the butler initalizer to create the run, then create a tagged 

60 # collection. 

61 butler = Butler(repoName, run=runName) 

62 butler.registry.registerCollection(taggedName, CollectionType.TAGGED) 

63 

64 # Verify the run and tag show up in query-collections: 

65 result = self.runner.invoke(butlerCli, ["query-collections", repoName]) 

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

67 self.assertIn(runName, result.output) 

68 self.assertIn(taggedName, result.output) 

69 

70 # Verify the tagged collection can be removed: 

71 result = self.runner.invoke( 

72 butlerCli, 

73 ["prune-collection", repoName, taggedName, "--unstore"], 

74 input="yes", 

75 ) 

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

77 result = self.runner.invoke(butlerCli, ["query-collections", repoName]) 

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

79 self.assertIn(runName, result.output) 

80 self.assertNotIn(taggedName, result.output) 

81 

82 # Verify the run can be removed: 

83 result = self.runner.invoke( 

84 butlerCli, 

85 ["prune-collection", repoName, runName, "--purge", "--unstore"], 

86 input="yes", 

87 ) 

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

89 result = self.runner.invoke(butlerCli, ["query-collections", repoName]) 

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

91 self.assertNotIn(runName, result.output) 

92 self.assertNotIn(taggedName, result.output) 

93 

94 

95class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper): 

96 """Test executing a small number of basic prune-collections commands to 

97 verify collections can be pruned. 

98 """ 

99 

100 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.pruneCollection" 

101 

102 def setUp(self): 

103 self.runner = LogCliRunner() 

104 

105 self.root = makeTestTempDir(TESTDIR) 

106 self.testRepo = MetricTestRepo( 

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

108 ) 

109 

110 def tearDown(self): 

111 removeTestTempDir(self.root) 

112 

113 def testPruneRun(self): 

114 def confirm_initial_tables(): 

115 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

117 expected = Table(array((("ingest/run", "RUN"), ("ingest", "TAGGED"))), names=("Name", "Type")) 

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

119 

120 confirm_initial_tables() 

121 

122 # Try pruning RUN without purge or unstore, should fail. 

123 result = self.runner.invoke( 

124 butlerCli, 

125 ["prune-collection", self.root, "ingest/run"], 

126 input="yes", 

127 ) 

128 self.assertEqual(result.exit_code, 1, clickResultMsg(result)) 

129 

130 # Try pruning RUN without unstore, should fail. 

131 result = self.runner.invoke( 

132 butlerCli, 

133 ["prune-collection", self.root, "ingest/run", "--purge"], 

134 input="yes", 

135 ) 

136 self.assertEqual(result.exit_code, 1, clickResultMsg(result)) 

137 

138 # Try pruning RUN without purge, should fail. 

139 result = self.runner.invoke( 

140 butlerCli, 

141 ["prune-collection", self.root, "ingest/run", "--unstore"], 

142 input="yes", 

143 ) 

144 self.assertEqual(result.exit_code, 1, clickResultMsg(result)) 

145 

146 # Try pruning RUN with purge and unstore but say "no" for confirmation, 

147 # should succeed but not change datasets. 

148 result = self.runner.invoke( 

149 butlerCli, 

150 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore"], 

151 input="no", 

152 ) 

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

154 

155 confirm_initial_tables() 

156 

157 # Try pruning RUN with purge and unstore, should succeed. 

158 result = self.runner.invoke( 

159 butlerCli, 

160 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore"], 

161 input="no", 

162 ) 

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

164 

165 # Try pruning RUN with purge and unstore, and use --no-confirm instead 

166 # of confirm dialog, should succeed. 

167 result = self.runner.invoke( 

168 butlerCli, 

169 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore", "--no-confirm"], 

170 ) 

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

172 

173 result = self.runner.invoke( 

174 butlerCli, 

175 ["query-collections", self.root], 

176 ) 

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

178 expected = Table((["ingest"], ["TAGGED"]), names=("Name", "Type")) 

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

180 

181 def testPruneTagged(self): 

182 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

184 expected = Table(array((("ingest/run", "RUN"), ("ingest", "TAGGED"))), names=("Name", "Type")) 

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

186 

187 # Try pruning TAGGED, should succeed. 

188 result = self.runner.invoke( 

189 butlerCli, 

190 ["prune-collection", self.root, "ingest", "--unstore"], 

191 input="yes", 

192 ) 

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

194 

195 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

197 expected = Table((["ingest/run"], ["RUN"]), names=("Name", "Type")) 

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

199 

200 

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

202 unittest.main()