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
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
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/>.
22"""Unit tests for daf_butler CLI prune-collections subcommand.
23"""
25from astropy.table import Table
26from numpy import array
27import os
28import unittest
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)
42TESTDIR = os.path.abspath(os.path.dirname(__file__))
45class PruneCollectionsTest(unittest.TestCase):
47 def setUp(self):
48 self.runner = LogCliRunner()
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"
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)
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)
72 # Verify the tagged collection can be removed:
73 result = self.runner.invoke(
74 butlerCli,
75 ["prune-collection", repoName, taggedName, "--unstore"],
76 input="yes",
77 )
78 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
79 result = self.runner.invoke(butlerCli, ["query-collections", repoName])
80 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
81 self.assertIn(runName, result.output)
82 self.assertNotIn(taggedName, result.output)
84 # Verify the run can be removed:
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)
97class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper):
98 """Test executing a small number of basic prune-collections commands to
99 verify collections can be pruned.
100 """
102 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.pruneCollection"
104 def setUp(self):
105 self.runner = LogCliRunner()
107 self.root = makeTestTempDir(TESTDIR)
108 self.testRepo = MetricTestRepo(self.root,
109 configFile=os.path.join(TESTDIR, "config/basic/butler.yaml"))
111 def tearDown(self):
112 removeTestTempDir(self.root)
114 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/run", "RUN"),
120 ("ingest", "TAGGED"))),
121 names=("Name", "Type"))
122 self.assertAstropyTablesEqual(readTable(result.output), expected)
124 confirm_initial_tables()
126 # Try pruning RUN without purge or unstore, should fail.
127 result = self.runner.invoke(
128 butlerCli,
129 ["prune-collection", self.root, "ingest/run"],
130 input="yes",
131 )
132 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
134 # Try pruning RUN without unstore, should fail.
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))
142 # Try pruning RUN without purge, should fail.
143 result = self.runner.invoke(
144 butlerCli,
145 ["prune-collection", self.root, "ingest/run", "--unstore"],
146 input="yes",
147 )
148 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
150 # Try pruning RUN with purge and unstore but say "no" for confirmation,
151 # should succeed but not change datasets.
152 result = self.runner.invoke(
153 butlerCli,
154 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore"],
155 input="no",
156 )
157 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
159 confirm_initial_tables()
161 # Try pruning RUN with purge and unstore, should succeed.
162 result = self.runner.invoke(
163 butlerCli,
164 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore"],
165 input="no",
166 )
167 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
169 # Try pruning RUN with purge and unstore, and use --no-confirm instead
170 # of confirm dialog, should succeed.
171 result = self.runner.invoke(
172 butlerCli,
173 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore", "--no-confirm"],
174 )
175 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
177 result = self.runner.invoke(
178 butlerCli,
179 ["query-collections", self.root],
180 )
181 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
182 expected = Table((["ingest"], ["TAGGED"]),
183 names=("Name", "Type"))
184 self.assertAstropyTablesEqual(readTable(result.output), expected)
186 def testPruneTagged(self):
187 result = self.runner.invoke(
188 butlerCli,
189 ["query-collections", self.root]
190 )
191 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
192 expected = Table(array((("ingest/run", "RUN"),
193 ("ingest", "TAGGED"))),
194 names=("Name", "Type"))
195 self.assertAstropyTablesEqual(readTable(result.output), expected)
197 # Try pruning TAGGED, should succeed.
198 result = self.runner.invoke(
199 butlerCli,
200 ["prune-collection", self.root, "ingest", "--unstore"],
201 input="yes",
202 )
203 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
205 result = self.runner.invoke(
206 butlerCli,
207 ["query-collections", self.root]
208 )
209 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
210 expected = Table((["ingest/run"], ["RUN"]),
211 names=("Name", "Type"))
212 self.assertAstropyTablesEqual(readTable(result.output), expected)
215if __name__ == "__main__": 215 ↛ 216line 215 didn't jump to line 216, because the condition on line 215 was never true
216 unittest.main()