Coverage for tests/test_cliCmd.py : 41%

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 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"""
25import unittest
27from lsst.daf.butler.cli.butler import cli as butlerCli
28from lsst.daf.butler import Butler
29from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
30from lsst.daf.butler.tests import CliCmdTestBase
31from lsst.pipe.tasks.script import registerSkymap
32from lsst.pipe.tasks.cli.cmd import make_discrete_skymap, register_skymap
35class RegisterSkymapTest(CliCmdTestBase, unittest.TestCase):
37 mockFuncName = "lsst.pipe.tasks.cli.cmd.commands.script.registerSkymap.registerSkymap"
39 @staticmethod
40 def defaultExpected():
41 return dict(config={}, config_file=None)
43 @staticmethod
44 def command():
45 return register_skymap
47 def test_minimal(self):
48 self.run_test(["register-skymap", "repo"],
49 self.makeExpected(repo="repo"))
51 def test_all(self):
52 self.run_test(["register-skymap", "repo",
53 "--config-file", "path/to/file",
54 "--config", "foo=bar"],
55 self.makeExpected(repo="repo",
56 config_file="path/to/file",
57 config=dict(foo="bar")))
59 def test_missing(self):
60 self.run_missing(["register-skymap"],
61 "Missing argument ['\"]REPO['\"]")
64class RegisterSkymapConfigTest(unittest.TestCase):
66 def setUp(self):
67 self.runner = LogCliRunner()
69 def testNoConfigOverride(self):
70 """Verify expected arguments are passed to makeSkyMap with no config
71 overrides."""
72 with self.runner.isolated_filesystem():
73 result = self.runner.invoke(butlerCli, ["create", "repo"])
74 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
75 with unittest.mock.patch("lsst.pipe.tasks.script.registerSkymap.makeSkyMap") as mock:
76 # call without any config overrides
77 result = self.runner.invoke(butlerCli, ["register-skymap", "repo"])
78 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
79 expectedConfig = registerSkymap.MakeSkyMapConfig()
80 mock.assert_called_once()
81 # assert that the first argument to the call to makeSkyMap was a butler
82 self.assertIsInstance(mock.call_args[0][0], Butler)
83 # assert that the second argument matches the expected config
84 self.assertEqual(mock.call_args[0][1], expectedConfig)
86 def testConfigOverride(self):
87 """Verify expected arguments are passed to makeSkyMap with config
88 overrides."""
89 with self.runner.isolated_filesystem():
90 result = self.runner.invoke(butlerCli, ["create", "repo"])
91 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
92 with unittest.mock.patch("lsst.pipe.tasks.script.registerSkymap.makeSkyMap") as mock:
93 # call and override the name parameter of the config
94 result = self.runner.invoke(butlerCli, ["register-skymap", "repo",
95 "--config", "name=bar"])
96 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
97 expectedConfig = registerSkymap.MakeSkyMapConfig()
98 expectedConfig.update(name="bar")
99 mock.assert_called_once()
100 # assert that the first argument to the makeSkyMap call was a butler
101 self.assertIsInstance(mock.call_args[0][0], Butler)
102 # assert that the second argument matches the expected config
103 self.assertEqual(mock.call_args[0][1], expectedConfig)
105 def testNonExistantConfigFile(self):
106 """Verify an expected error when a given config override file does not
107 exist. """
108 with self.runner.isolated_filesystem():
109 result = self.runner.invoke(butlerCli, ["create", "repo"])
110 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
111 result = self.runner.invoke(butlerCli, ["register-skymap", "repo",
112 "--config-file", "foo.py"])
113 # foo.py does not exist; exit could should be non-zero.
114 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
117class DefineMakeDiscreteSkymap(CliCmdTestBase, unittest.TestCase):
119 mockFuncName = "lsst.pipe.tasks.cli.cmd.commands.script.makeDiscreteSkyMap"
121 @staticmethod
122 def defaultExpected():
123 return dict(config_file=None,
124 collections=())
126 @staticmethod
127 def command():
128 return make_discrete_skymap
130 def test_repoBasic(self):
131 """Test the most basic required arguments."""
132 self.run_test(["make-discrete-skymap",
133 "--collections", "foo/bar,baz",
134 "here", "a.b.c"],
135 self.makeExpected(repo="here",
136 collections=("foo/bar", "baz"),
137 skymap_id="discrete",
138 instrument="a.b.c",
139 old_skymap_id=None))
141 def test_all(self):
142 """Test all the arguments."""
143 self.run_test(["make-discrete-skymap",
144 "--collections", "foo/bar,baz",
145 "--config-file", "/path/to/config",
146 "--collections", "boz",
147 "--skymap-id", "wiz",
148 "--old-skymap-id", "nuz",
149 "here", "a.b.c"],
150 self.makeExpected(repo="here",
151 instrument="a.b.c",
152 config_file="/path/to/config",
153 skymap_id="wiz",
154 old_skymap_id="nuz",
155 # The list of collections must be in
156 # exactly the same order as it is
157 # passed in the list of arguments to
158 # run_test.
159 collections=("foo/bar", "baz", "boz")))
161 def test_missing(self):
162 """test a missing argument"""
163 self.run_missing(["make-discrete-skymap", "--collections", "foo/bar,baz"],
164 "Missing argument ['\"]REPO['\"]")
165 self.run_missing(["make-discrete-skymap", "--collections", "foo/bar,baz", "here"],
166 "Missing argument ['\"]INSTRUMENT['\"]")
167 self.run_missing(["make-discrete-skymap", "here", "a.b.c"],
168 "Error: Missing option ['\"]--collections['\"].")
171if __name__ == "__main__": 171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true
172 unittest.main()