Coverage for tests/test_cliCmd.py : 40%

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