Coverage for tests/test_cliCmdPruneCollection.py: 26%
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"""
25import os
26import unittest
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
41TESTDIR = os.path.abspath(os.path.dirname(__file__))
44class PruneCollectionsTest(unittest.TestCase):
45 def setUp(self):
46 self.runner = LogCliRunner()
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"
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)
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)
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)
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)
95class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper):
96 """Test executing a small number of basic prune-collections commands to
97 verify collections can be pruned.
98 """
100 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.pruneCollection"
102 def setUp(self):
103 self.runner = LogCliRunner()
105 self.root = makeTestTempDir(TESTDIR)
106 self.testRepo = MetricTestRepo(
107 self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")
108 )
110 def tearDown(self):
111 removeTestTempDir(self.root)
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)
120 confirm_initial_tables()
122 # Try pruning RUN without purge or unstore, should fail.
123 with self.assertWarns(FutureWarning): # Capture the deprecation warning
124 result = self.runner.invoke(
125 butlerCli,
126 ["prune-collection", self.root, "ingest/run"],
127 input="yes",
128 )
129 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
131 # Try pruning RUN without unstore, should fail.
132 with self.assertWarns(FutureWarning): # Capture the deprecation warning
133 result = self.runner.invoke(
134 butlerCli,
135 ["prune-collection", self.root, "ingest/run", "--purge"],
136 input="yes",
137 )
138 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
140 # Try pruning RUN without purge, should fail.
141 with self.assertWarns(FutureWarning): # Capture the deprecation warning
142 result = self.runner.invoke(
143 butlerCli,
144 ["prune-collection", self.root, "ingest/run", "--unstore"],
145 input="yes",
146 )
147 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
149 # Try pruning RUN with purge and unstore but say "no" for confirmation,
150 # should succeed but not change datasets.
151 with self.assertWarns(FutureWarning): # Capture the deprecation warning
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 with self.assertWarns(FutureWarning): # Capture the deprecation warning
163 result = self.runner.invoke(
164 butlerCli,
165 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore"],
166 input="no",
167 )
168 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
170 # Try pruning RUN with purge and unstore, and use --no-confirm instead
171 # of confirm dialog, should succeed.
172 with self.assertWarns(FutureWarning): # Capture the deprecation warning
173 result = self.runner.invoke(
174 butlerCli,
175 ["prune-collection", self.root, "ingest/run", "--purge", "--unstore", "--no-confirm"],
176 )
177 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
179 result = self.runner.invoke(
180 butlerCli,
181 ["query-collections", self.root],
182 )
183 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
184 expected = Table((["ingest"], ["TAGGED"]), names=("Name", "Type"))
185 self.assertAstropyTablesEqual(readTable(result.output), expected)
187 def testPruneTagged(self):
188 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
189 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
190 expected = Table(array((("ingest/run", "RUN"), ("ingest", "TAGGED"))), names=("Name", "Type"))
191 self.assertAstropyTablesEqual(readTable(result.output), expected)
193 # Try pruning TAGGED, should succeed.
194 with self.assertWarns(FutureWarning): # Capture the deprecation warning
195 result = self.runner.invoke(
196 butlerCli,
197 ["prune-collection", self.root, "ingest", "--unstore"],
198 input="yes",
199 )
200 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
202 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
203 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
204 expected = Table((["ingest/run"], ["RUN"]), names=("Name", "Type"))
205 self.assertAstropyTablesEqual(readTable(result.output), expected)
208if __name__ == "__main__": 208 ↛ 209line 208 didn't jump to line 209, because the condition on line 208 was never true
209 unittest.main()