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 unittest 

29 

30from lsst.daf.butler import Butler, CollectionType 

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

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

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

34 ButlerTestHelper, 

35 makeTestTempDir, 

36 MetricTestRepo, 

37 readTable, 

38 removeTestTempDir, 

39) 

40 

41 

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

43 

44 

45class PruneCollectionsTest(unittest.TestCase): 

46 

47 def setUp(self): 

48 self.runner = LogCliRunner() 

49 

50 def testPruneCollections(self): 

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

52 butler prune-collection subcommand.""" 

53 with self.runner.isolated_filesystem(): 

54 repoName = "myRepo" 

55 runName = "myRun" 

56 taggedName = "taggedCollection" 

57 

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

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

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

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

62 # collection. 

63 butler = Butler(repoName, run=runName) 

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

65 

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

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

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

69 self.assertIn(runName, result.output) 

70 self.assertIn(taggedName, result.output) 

71 

72 # Verify the tagged collection can be removed: 

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

74 taggedName, 

75 "--unstore"]) 

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(butlerCli, ["prune-collection", repoName, 

84 runName, 

85 "--purge", 

86 "--unstore"]) 

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

88 self.assertNotIn(runName, result.output) 

89 self.assertNotIn(taggedName, result.output) 

90 

91 

92class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper): 

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

94 verify collections can be pruned. 

95 """ 

96 

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

98 

99 def setUp(self): 

100 self.runner = LogCliRunner() 

101 

102 self.root = makeTestTempDir(TESTDIR) 

103 self.testRepo = MetricTestRepo(self.root, 

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

105 

106 def tearDown(self): 

107 removeTestTempDir(self.root) 

108 

109 def testPruneRun(self): 

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

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

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

113 ("ingest", "TAGGED"))), 

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

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

116 

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

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

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

120 

121 # Try pruning RUN without unstore, should fail. 

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

123 "--purge"]) 

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

125 

126 # Try pruning RUN without purge, should fail. 

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

128 "--unstore"]) 

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

130 

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

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

133 "--purge", "--unstore"]) 

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

135 

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

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

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

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

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

141 

142 def testPruneTagged(self): 

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

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

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

146 ("ingest", "TAGGED"))), 

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

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

149 

150 # Try pruning TAGGED, should succeed. 

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

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

153 

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

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

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

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

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

159 

160 

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

162 unittest.main()