Coverage for tests/test_cliCmdPruneCollection.py: 21%

92 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-09 09:43 +0000

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 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

72 result = self.runner.invoke( 

73 butlerCli, 

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

75 input="yes", 

76 ) 

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

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

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

80 self.assertIn(runName, result.output) 

81 self.assertNotIn(taggedName, result.output) 

82 

83 # Verify the run can be removed: 

84 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

85 result = self.runner.invoke( 

86 butlerCli, 

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

88 input="yes", 

89 ) 

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

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

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

93 self.assertNotIn(runName, result.output) 

94 self.assertNotIn(taggedName, result.output) 

95 

96 

97class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper): 

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

99 verify collections can be pruned. 

100 """ 

101 

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

103 

104 def setUp(self): 

105 self.runner = LogCliRunner() 

106 

107 self.root = makeTestTempDir(TESTDIR) 

108 self.testRepo = MetricTestRepo( 

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

110 ) 

111 

112 def tearDown(self): 

113 removeTestTempDir(self.root) 

114 

115 def testPruneRun(self): 

116 def confirm_initial_tables(): 

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

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

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

120 self.assertAstropyTablesEqual(readTable(result.output), expected, unorderedRows=True) 

121 

122 confirm_initial_tables() 

123 

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

125 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

126 result = self.runner.invoke( 

127 butlerCli, 

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

129 input="yes", 

130 ) 

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

132 

133 # Try pruning RUN without unstore, should fail. 

134 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

135 result = self.runner.invoke( 

136 butlerCli, 

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

138 input="yes", 

139 ) 

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

141 

142 # Try pruning RUN without purge, should fail. 

143 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

144 result = self.runner.invoke( 

145 butlerCli, 

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

147 input="yes", 

148 ) 

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

150 

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

152 # should succeed but not change datasets. 

153 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

154 result = self.runner.invoke( 

155 butlerCli, 

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

157 input="no", 

158 ) 

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

160 

161 confirm_initial_tables() 

162 

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

164 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

165 result = self.runner.invoke( 

166 butlerCli, 

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

168 input="no", 

169 ) 

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

171 

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

173 # of confirm dialog, should succeed. 

174 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

175 result = self.runner.invoke( 

176 butlerCli, 

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

178 ) 

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

180 

181 result = self.runner.invoke( 

182 butlerCli, 

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

184 ) 

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

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

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

188 

189 def testPruneTagged(self): 

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

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

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

193 self.assertAstropyTablesEqual(readTable(result.output), expected, unorderedRows=True) 

194 

195 # Try pruning TAGGED, should succeed. 

196 with self.assertWarns(FutureWarning): # Capture the deprecation warning 

197 result = self.runner.invoke( 

198 butlerCli, 

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

200 input="yes", 

201 ) 

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

203 

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

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

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

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

208 

209 

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

211 unittest.main()