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

23""" 

24 

25from astropy.table import Table 

26from numpy import array 

27import os 

28import shutil 

29import tempfile 

30import unittest 

31 

32from lsst.daf.butler import Butler 

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

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

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

36 

37 

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

39 

40 

41class PruneCollectionsTest(unittest.TestCase): 

42 

43 def setUp(self): 

44 self.runner = LogCliRunner() 

45 

46 def testPruneCollections(self): 

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

48 butler prune-collection subcommand.""" 

49 with self.runner.isolated_filesystem(): 

50 repoName = "myRepo" 

51 runName = "myRun" 

52 taggedName = "taggedCollection" 

53 

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

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

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

57 # Use the butler initalizer to create the run and tagged 

58 # collection. 

59 Butler(repoName, run=runName, tags=[taggedName]) 

60 

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

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

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

64 self.assertIn(runName, result.output) 

65 self.assertIn(taggedName, result.output) 

66 

67 # Verify the tagged collection can be removed: 

68 result = self.runner.invoke(butlerCli, ["prune-collection", repoName, 

69 taggedName, 

70 "--unstore"]) 

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

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

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

74 self.assertIn(runName, result.output) 

75 self.assertNotIn(taggedName, result.output) 

76 

77 # Verify the run can be removed: 

78 result = self.runner.invoke(butlerCli, ["prune-collection", repoName, 

79 runName, 

80 "--purge", 

81 "--unstore"]) 

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

83 self.assertNotIn(runName, result.output) 

84 self.assertNotIn(taggedName, result.output) 

85 

86 

87class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper): 

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

89 verify collections can be pruned. 

90 """ 

91 

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

93 

94 def setUp(self): 

95 self.runner = LogCliRunner() 

96 

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

98 self.testRepo = MetricTestRepo(self.root, 

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

100 

101 def tearDown(self): 

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

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

104 

105 def testPruneRun(self): 

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

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

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

109 ("ingest", "TAGGED"))), 

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

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

112 

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

114 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run"]) 

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

116 

117 # Try pruning RUN without unstore, should fail. 

118 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

119 "--purge"]) 

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

121 

122 # Try pruning RUN without purge, should fail. 

123 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

124 "--unstore"]) 

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

126 

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

128 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

129 "--purge", "--unstore"]) 

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

131 

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

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

134 expected = Table((["ingest"], ["TAGGED"]), 

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

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

137 

138 def testPruneTagged(self): 

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

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

141 expected = Table(array((("ingest/run", "RUN"), 

142 ("ingest", "TAGGED"))), 

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

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

145 

146 # Try pruning TAGGED, should succeed. 

147 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest", "--unstore"]) 

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

149 

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

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

152 expected = Table((["ingest/run"], ["RUN"]), 

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

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

155 

156 

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

158 unittest.main()