Coverage for tests/test_cliCmdTestIngest.py: 50%
56 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-19 09:07 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-19 09:07 +0000
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 ingest-raws command.
23"""
25import unittest
27import lsst.obs.base
28from lsst.daf.butler import Butler
29from lsst.daf.butler.cli.butler import cli as butlerCli
30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
31from lsst.daf.butler.tests import CliCmdTestBase
32from lsst.obs.base.cli.cmd import ingest_raws
33from lsst.obs.base.cli.cmd.commands import fits_re
34from lsst.obs.base.ingest import RawIngestConfig
37class IngestRawsTestCase(CliCmdTestBase, unittest.TestCase):
38 mockFuncName = "lsst.obs.base.cli.cmd.commands.script.ingestRaws"
40 @staticmethod
41 def defaultExpected():
42 return dict(
43 config={},
44 config_file=None,
45 ingest_task="lsst.obs.base.RawIngestTask",
46 locations=(),
47 output_run=None,
48 processes=1,
49 regex=fits_re,
50 transfer="auto",
51 track_file_attrs=True,
52 fail_fast=False,
53 )
55 @staticmethod
56 def command():
57 return ingest_raws
59 def test_repoAndOutput(self):
60 """Test the most basic required arguments, repo and output run"""
61 self.run_test(
62 ["ingest-raws", "repo", "resources", "--output-run", "out"],
63 self.makeExpected(repo="repo", locations=("resources",), output_run="out"),
64 )
66 def test_configMulti(self):
67 """Test config overrides"""
68 self.run_test(
69 [
70 "ingest-raws",
71 "repo",
72 "resources",
73 "--output-run",
74 "out",
75 "-c",
76 "foo=1",
77 "--config",
78 "bar=2",
79 "--config",
80 "baz=3",
81 ],
82 self.makeExpected(
83 repo="repo",
84 locations=("resources",),
85 output_run="out",
86 config=dict(foo="1", bar="2", baz="3"),
87 ),
88 )
90 def test_configFile(self):
91 """Test config file override"""
92 configFile = "path/to/file.txt"
93 self.run_test(
94 ["ingest-raws", "repo", "resources", "--output-run", "out", "--config-file", configFile],
95 self.makeExpected(
96 repo="repo", locations=("resources",), output_run="out", config_file=configFile
97 ),
98 withTempFile=configFile,
99 )
101 def test_transfer(self):
102 """Test the transfer argument"""
103 self.run_test(
104 ["ingest-raws", "repo", "resources", "--output-run", "out", "--transfer", "symlink"],
105 self.makeExpected(repo="repo", locations=("resources",), output_run="out", transfer="symlink"),
106 )
108 def test_ingestTask(self):
109 """Test the ingest task argument"""
110 self.run_test(
111 ["ingest-raws", "repo", "resources", "--output-run", "out", "--ingest-task", "foo.bar.baz"],
112 self.makeExpected(
113 repo="repo", locations=("resources",), output_run="out", ingest_task="foo.bar.baz"
114 ),
115 )
117 def test_locations(self):
118 """Test that the locations argument accepts multiple inputs and splits
119 commas."""
120 self.run_test(
121 ["ingest-raws", "repo", "in/directory/,in/another/dir/", "other/file.fits"],
122 self.makeExpected(repo="repo", locations=("in/directory/", "in/another/dir/", "other/file.fits")),
123 )
126class PatchRawIngestTask(lsst.obs.base.RawIngestTask):
127 init_args = []
129 def __init__(self, *args, **kwargs):
130 self.init_args.append((args, kwargs))
131 super().__init__(*args, **kwargs)
133 def run(self, *args, **kwargs):
134 pass
137class RawIngestMockTest(unittest.TestCase):
138 def setUp(self):
139 self.runner = LogCliRunner()
141 def test(self):
142 """Verify config gets applied properly."""
143 with self.runner.isolated_filesystem():
144 result = self.runner.invoke(butlerCli, ["create", "repo"])
145 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
146 with unittest.mock.patch("lsst.obs.base.RawIngestTask", new=PatchRawIngestTask) as mock:
147 # Call, override the name parameter of the config and set
148 # fail-fast.
149 result = self.runner.invoke(
150 butlerCli,
151 ["ingest-raws", "repo", "resources", "--config", "transfer=hardlink", "--fail-fast"],
152 )
153 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
154 # Verify the mock class was initialized exactly once:
155 self.assertEqual(len(mock.init_args), 1)
156 # Verify that the task was initialized with a 'butler' kwarg
157 # that received a butler instance:
158 self.assertIsInstance(mock.init_args[0][1]["butler"], Butler)
159 expectedConfig = RawIngestConfig()
160 # Verify that the task was initialized with a 'config' kwarg
161 # that received an expected config:
162 expectedConfig.update(transfer="hardlink")
163 # Verfiy that --failfast caused the config's failFast
164 # parameter to be set to True.
165 expectedConfig.update(failFast=True)
166 self.assertEqual(mock.init_args[0][1]["config"], expectedConfig)
169if __name__ == "__main__": 169 ↛ 170line 169 didn't jump to line 170, because the condition on line 169 was never true
170 unittest.main()