Coverage for tests/test_cliCmd.py: 33%
100 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-22 03:00 -0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-22 03:00 -0700
1# This file is part of pipe_tasks.
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 make-discrete-skymap command.
23"""
25from astropy.table import Table as AstropyTable
26import unittest
28from lsst.daf.butler.cli.butler import cli as butlerCli
29from lsst.daf.butler import Butler
30from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
31from lsst.daf.butler.tests import CliCmdTestBase
32from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable
33from lsst.pipe.tasks.script import registerSkymap, registerDcrSubfilters
34from lsst.pipe.tasks.cli.cmd import make_discrete_skymap, register_skymap
37class RegisterSkymapTest(CliCmdTestBase, unittest.TestCase):
39 mockFuncName = "lsst.pipe.tasks.cli.cmd.commands.script.registerSkymap.registerSkymap"
41 @staticmethod
42 def defaultExpected():
43 return dict(config={}, config_file=None)
45 @staticmethod
46 def command():
47 return register_skymap
49 def test_minimal(self):
50 self.run_test(["register-skymap", "repo"],
51 self.makeExpected(repo="repo"))
53 def test_all(self):
54 self.run_test(["register-skymap", "repo",
55 "--config-file", "path/to/file",
56 "--config", "foo=bar"],
57 self.makeExpected(repo="repo",
58 config_file="path/to/file",
59 config=dict(foo="bar")))
61 def test_missing(self):
62 self.run_missing(["register-skymap"],
63 "Missing argument ['\"]REPO['\"]")
66class RegisterSkymapConfigTest(unittest.TestCase):
68 def setUp(self):
69 self.runner = LogCliRunner()
71 def testNoConfigOverride(self):
72 """Verify expected arguments are passed to makeSkyMap with no config
73 overrides."""
74 with self.runner.isolated_filesystem():
75 result = self.runner.invoke(butlerCli, ["create", "repo"])
76 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
77 with unittest.mock.patch("lsst.pipe.tasks.script.registerSkymap.makeSkyMap") as mock:
78 # call without any config overrides
79 result = self.runner.invoke(butlerCli, ["register-skymap", "repo"])
80 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
81 expectedConfig = registerSkymap.MakeSkyMapConfig()
82 mock.assert_called_once()
83 # assert that the first argument to the call to makeSkyMap was a butler
84 self.assertIsInstance(mock.call_args[0][0], Butler)
85 # assert that the second argument matches the expected config
86 self.assertEqual(mock.call_args[0][1], expectedConfig)
88 def testConfigOverride(self):
89 """Verify expected arguments are passed to makeSkyMap with config
90 overrides."""
91 with self.runner.isolated_filesystem():
92 result = self.runner.invoke(butlerCli, ["create", "repo"])
93 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
94 with unittest.mock.patch("lsst.pipe.tasks.script.registerSkymap.makeSkyMap") as mock:
95 # call and override the name parameter of the config
96 result = self.runner.invoke(butlerCli, ["register-skymap", "repo",
97 "--config", "name=bar"])
98 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
99 expectedConfig = registerSkymap.MakeSkyMapConfig()
100 expectedConfig.update(name="bar")
101 mock.assert_called_once()
102 # assert that the first argument to the makeSkyMap call was a butler
103 self.assertIsInstance(mock.call_args[0][0], Butler)
104 # assert that the second argument matches the expected config
105 self.assertEqual(mock.call_args[0][1], expectedConfig)
107 def testNonExistantConfigFile(self):
108 """Verify an expected error when a given config override file does not
109 exist. """
110 with self.runner.isolated_filesystem():
111 result = self.runner.invoke(butlerCli, ["create", "repo"])
112 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
113 result = self.runner.invoke(butlerCli, ["register-skymap", "repo",
114 "--config-file", "foo.py"])
115 # foo.py does not exist; exit could should be non-zero.
116 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
119class DefineMakeDiscreteSkymap(CliCmdTestBase, unittest.TestCase):
121 mockFuncName = "lsst.pipe.tasks.cli.cmd.commands.script.makeDiscreteSkyMap"
123 @staticmethod
124 def defaultExpected():
125 return dict(config_file=None,
126 collections=())
128 @staticmethod
129 def command():
130 return make_discrete_skymap
132 def test_repoBasic(self):
133 """Test the most basic required arguments."""
134 self.run_test(["make-discrete-skymap",
135 "--collections", "foo/bar,baz",
136 "here", "a.b.c"],
137 self.makeExpected(repo="here",
138 collections=("foo/bar", "baz"),
139 skymap_id="discrete",
140 instrument="a.b.c",
141 old_skymap_id=None))
143 def test_all(self):
144 """Test all the arguments."""
145 self.run_test(["make-discrete-skymap",
146 "--collections", "foo/bar,baz",
147 "--config-file", "/path/to/config",
148 "--collections", "boz",
149 "--skymap-id", "wiz",
150 "--old-skymap-id", "nuz",
151 "here", "a.b.c"],
152 self.makeExpected(repo="here",
153 instrument="a.b.c",
154 config_file="/path/to/config",
155 skymap_id="wiz",
156 old_skymap_id="nuz",
157 # The list of collections must be in
158 # exactly the same order as it is
159 # passed in the list of arguments to
160 # run_test.
161 collections=("foo/bar", "baz", "boz")))
163 def test_missing(self):
164 """test a missing argument"""
165 self.run_missing(["make-discrete-skymap", "--collections", "foo/bar,baz"],
166 "Missing argument ['\"]REPO['\"]")
167 self.run_missing(["make-discrete-skymap", "--collections", "foo/bar,baz", "here"],
168 "Missing argument ['\"]INSTRUMENT['\"]")
169 self.run_missing(["make-discrete-skymap", "here", "a.b.c"],
170 "Error: Missing option ['\"]--collections['\"].")
173class RegisterDcrSubfiltersTest(unittest.TestCase, ButlerTestHelper):
175 def setUp(self):
176 self.runner = LogCliRunner()
177 self.repo = "here"
179 def testRegisterFilters(self):
180 """Register a few filters and verify they are added to the repo."""
182 with self.runner.isolated_filesystem():
183 result = self.runner.invoke(butlerCli, ["create", self.repo])
184 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
186 result = self.runner.invoke(butlerCli, ["register-dcr-subfilters", self.repo, "3", "foo"])
187 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
188 self.assertIn(registerDcrSubfilters.registeredMsg.format(band="foo", subfilters="[0, 1, 2]"),
189 result.output)
191 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.repo, "subfilter"])
192 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
193 self.assertAstropyTablesEqual(
194 AstropyTable((("foo", "foo", "foo"), (0, 1, 2)), names=("band", "id")),
195 readTable(result.output))
197 # Verify expected output message for registering subfilters in a
198 # band that already has subfilters
199 result = self.runner.invoke(butlerCli, ["register-dcr-subfilters", self.repo, "5", "foo"])
200 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
201 self.assertIn(registerDcrSubfilters.notRegisteredMsg.format(band="foo", subfilters="[0, 1, 2]"),
202 result.output)
204 # Add subfilters for two filters, one new filter and one existing.
205 # Verify expected result messages and registry values.
206 result = self.runner.invoke(butlerCli, ["register-dcr-subfilters", self.repo, "3", "foo", "bar"])
207 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
208 self.assertIn(registerDcrSubfilters.notRegisteredMsg.format(band="foo", subfilters="[0, 1, 2]"),
209 result.output)
210 self.assertIn(registerDcrSubfilters.registeredMsg.format(band="bar", subfilters="[0, 1, 2]"),
211 result.output)
212 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.repo, "subfilter"])
213 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
214 resultTable = readTable(result.output)
215 resultTable.sort(["band", "id"])
216 self.assertAstropyTablesEqual(
217 AstropyTable((("bar", "bar", "bar", "foo", "foo", "foo"),
218 (0, 1, 2, 0, 1, 2)), names=("band", "id")),
219 resultTable)
222if __name__ == "__main__": 222 ↛ 223line 222 didn't jump to line 223, because the condition on line 222 was never true
223 unittest.main()