Coverage for tests/test_cliCmdQueryCollections.py : 35%

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 query-collections command.
23"""
25from astropy.table import Table
26from numpy import array
27import os
28import unittest
30from lsst.daf.butler import (
31 Butler,
32 CollectionType,
33)
34from lsst.daf.butler.cli.butler import cli
35from lsst.daf.butler.cli.cmd import query_collections
36from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
37from lsst.daf.butler.script import queryCollections
38from lsst.daf.butler.tests import CliCmdTestBase, DatastoreMock
39from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable
42TESTDIR = os.path.abspath(os.path.dirname(__file__))
45class QueryCollectionsCmdTest(CliCmdTestBase, unittest.TestCase):
47 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryCollections"
49 @staticmethod
50 def defaultExpected():
51 return dict(repo=None,
52 collection_type=tuple(CollectionType.__members__.values()),
53 chains="TABLE",
54 glob=())
56 @staticmethod
57 def command():
58 return query_collections
60 def test_minimal(self):
61 """Test only the required parameters, and omit the optional parameters.
62 """
63 self.run_test(["query-collections", "here"],
64 self.makeExpected(repo="here"))
66 def test_all(self):
67 """Test all parameters"""
68 self.run_test(["query-collections", "here", "foo*",
69 "--collection-type", "TAGGED",
70 "--collection-type", "RUN"],
71 self.makeExpected(repo="here",
72 glob=("foo*",),
73 collection_type=(CollectionType.TAGGED, CollectionType.RUN),
74 chains="TABLE"))
77class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase):
79 def setUp(self):
80 self.runner = LogCliRunner()
82 def testGetCollections(self):
83 run = "ingest/run"
84 tag = "tag"
85 with self.runner.isolated_filesystem():
86 butlerCfg = Butler.makeRepo("here")
87 # the purpose of this call is to create some collections
88 Butler(butlerCfg, run=run, tags=[tag], collections=[tag], writeable=True)
90 # Verify collections that were created are found by
91 # query-collections.
92 result = self.runner.invoke(cli, ["query-collections", "here"])
93 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
94 expected = Table((("ingest/run", "tag"), ("RUN", "TAGGED")),
95 names=("Name", "Type"))
96 self.assertAstropyTablesEqual(readTable(result.output), expected)
98 # Verify that with a glob argument, that only collections whose
99 # name matches with the specified pattern are returned.
100 result = self.runner.invoke(cli, ["query-collections", "here", "t*"])
101 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
102 expected = Table((("tag",), ("TAGGED",)),
103 names=("Name", "Type"))
104 self.assertAstropyTablesEqual(readTable(result.output), expected)
106 # Verify that with a collection type argument, only collections of
107 # that type are returned.
108 result = self.runner.invoke(cli, ["query-collections", "here", "--collection-type", "RUN"])
109 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
110 expected = Table((("ingest/run",), ("RUN",)),
111 names=("Name", "Type"))
112 self.assertAstropyTablesEqual(readTable(result.output), expected)
115class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase):
117 def setUp(self):
118 self.runner = LogCliRunner()
120 def testChained(self):
121 with self.runner.isolated_filesystem():
123 # Create a butler and add some chained collections:
124 butlerCfg = Butler.makeRepo("here")
126 butler1 = Butler(butlerCfg, writeable=True)
128 # Replace datastore functions with mocks:
129 DatastoreMock.apply(butler1)
131 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "base.yaml"))
132 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "datasets.yaml"))
133 registry1 = butler1.registry
134 registry1.registerRun("run1")
135 registry1.registerCollection("tag1", CollectionType.TAGGED)
136 registry1.registerCollection("calibration1", CollectionType.CALIBRATION)
137 registry1.registerCollection("chain1", CollectionType.CHAINED)
138 registry1.registerCollection("chain2", CollectionType.CHAINED)
139 registry1.setCollectionChain("chain1", ["tag1", "run1", "chain2"])
140 registry1.setCollectionChain("chain2", ["calibration1", "run1"])
142 # Use the script function to test the query-collections TREE
143 # option, because the astropy.table.Table.read method, which we are
144 # using for verification elsewhere in this file, seems to strip
145 # leading whitespace from columns. This makes it impossible to test
146 # the nested TREE output of the query-collections subcommand from
147 # the command line interface.
148 table = queryCollections("here", glob=(), collection_type=CollectionType.all(), chains="TREE")
150 # self.assertEqual(result.exit_code, 0, clickResultMsg(result))
151 expected = Table(array((("imported_g", "RUN"),
152 ("imported_r", "RUN"),
153 ("run1", "RUN"),
154 ("tag1", "TAGGED"),
155 ("calibration1", "CALIBRATION"),
156 ("chain1", "CHAINED"),
157 (" tag1", "TAGGED"),
158 (" run1", "RUN"),
159 (" chain2", "CHAINED"),
160 (" calibration1", "CALIBRATION"),
161 (" run1", "RUN"),
162 ("chain2", "CHAINED"),
163 (" calibration1", "CALIBRATION"),
164 (" run1", "RUN"))),
165 names=("Name", "Type"))
166 self.assertAstropyTablesEqual(table, expected)
168 result = self.runner.invoke(cli, ["query-collections", "here"])
169 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
170 expected = Table(array((
171 ("imported_g", "RUN", ""),
172 ("imported_r", "RUN", ""),
173 ("run1", "RUN", ""),
174 ("tag1", "TAGGED", ""),
175 ("calibration1", "CALIBRATION", ""),
176 ("chain1", "CHAINED", "[tag1, run1, chain2]"),
177 ("chain2", "CHAINED", "[calibration1, run1]"))),
178 names=("Name", "Type", "Definition"))
179 table = readTable(result.output)
180 self.assertAstropyTablesEqual(readTable(result.output), expected)
182 result = self.runner.invoke(cli, ["query-collections", "here", "--chains", "FLATTEN"])
183 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
184 expected = Table(array((
185 ("imported_g", "RUN"),
186 ("imported_r", "RUN"),
187 ("run1", "RUN"),
188 ("tag1", "TAGGED"),
189 ("calibration1", "CALIBRATION"),
190 ("tag1", "TAGGED"),
191 ("run1", "RUN"),
192 ("calibration1", "CALIBRATION"),
193 ("calibration1", "CALIBRATION"),
194 ("run1", "RUN"))),
195 names=("Name", "Type"))
196 self.assertAstropyTablesEqual(readTable(result.output), expected)
199if __name__ == "__main__": 199 ↛ 200line 199 didn't jump to line 200, because the condition on line 199 was never true
200 unittest.main()