Coverage for tests/test_cliCmdPruneCollection.py : 28%

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/>.
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
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 and tagged
62 # collection.
63 Butler(repoName, run=runName, tags=[taggedName])
65 # Verify the run and tag show up in query-collections:
66 result = self.runner.invoke(butlerCli, ["query-collections", repoName])
67 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
68 self.assertIn(runName, result.output)
69 self.assertIn(taggedName, result.output)
71 # Verify the tagged collection can be removed:
72 result = self.runner.invoke(butlerCli, ["prune-collection", repoName,
73 taggedName,
74 "--unstore"])
75 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
76 result = self.runner.invoke(butlerCli, ["query-collections", repoName])
77 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
78 self.assertIn(runName, result.output)
79 self.assertNotIn(taggedName, result.output)
81 # Verify the run can be removed:
82 result = self.runner.invoke(butlerCli, ["prune-collection", repoName,
83 runName,
84 "--purge",
85 "--unstore"])
86 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
87 self.assertNotIn(runName, result.output)
88 self.assertNotIn(taggedName, result.output)
91class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper):
92 """Test executing a small number of basic prune-collections commands to
93 verify collections can be pruned.
94 """
96 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.pruneCollection"
98 def setUp(self):
99 self.runner = LogCliRunner()
101 self.root = makeTestTempDir(TESTDIR)
102 self.testRepo = MetricTestRepo(self.root,
103 configFile=os.path.join(TESTDIR, "config/basic/butler.yaml"))
105 def tearDown(self):
106 removeTestTempDir(self.root)
108 def testPruneRun(self):
109 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
110 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
111 expected = Table(array((("ingest/run", "RUN"),
112 ("ingest", "TAGGED"))),
113 names=("Name", "Type"))
114 self.assertAstropyTablesEqual(readTable(result.output), expected)
116 # Try pruning RUN without purge or unstore, should fail.
117 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run"])
118 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
120 # Try pruning RUN without unstore, should fail.
121 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run",
122 "--purge"])
123 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
125 # Try pruning RUN without purge, should fail.
126 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run",
127 "--unstore"])
128 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
130 # Try pruning RUN with purge and unstore, should succeed.
131 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run",
132 "--purge", "--unstore"])
133 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
135 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
136 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
137 expected = Table((["ingest"], ["TAGGED"]),
138 names=("Name", "Type"))
139 self.assertAstropyTablesEqual(readTable(result.output), expected)
141 def testPruneTagged(self):
142 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
143 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
144 expected = Table(array((("ingest/run", "RUN"),
145 ("ingest", "TAGGED"))),
146 names=("Name", "Type"))
147 self.assertAstropyTablesEqual(readTable(result.output), expected)
149 # Try pruning TAGGED, should succeed.
150 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest", "--unstore"])
151 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
153 result = self.runner.invoke(butlerCli, ["query-collections", self.root])
154 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
155 expected = Table((["ingest/run"], ["RUN"]),
156 names=("Name", "Type"))
157 self.assertAstropyTablesEqual(readTable(result.output), expected)
160if __name__ == "__main__": 160 ↛ 161line 160 didn't jump to line 161, because the condition on line 160 was never true
161 unittest.main()