Coverage for tests/test_cliCmdTestIngest.py: 54%
55 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 01:53 -0800
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 01:53 -0800
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
27from lsst.daf.butler import Butler
28from lsst.daf.butler.cli.butler import cli as butlerCli
29from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
30from lsst.daf.butler.tests import CliCmdTestBase
31from lsst.obs.base.cli.cmd import ingest_raws
32from lsst.obs.base.cli.cmd.commands import fits_re
33from lsst.obs.base.ingest import RawIngestConfig
34import lsst.obs.base
37class IngestRawsTestCase(CliCmdTestBase, unittest.TestCase):
39 mockFuncName = "lsst.obs.base.cli.cmd.commands.script.ingestRaws"
41 @staticmethod
42 def defaultExpected():
43 return dict(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")
52 @staticmethod
53 def command():
54 return ingest_raws
56 def test_repoAndOutput(self):
57 """Test the most basic required arguments, repo and output run"""
58 self.run_test(["ingest-raws", "repo", "resources",
59 "--output-run", "out"],
60 self.makeExpected(repo="repo",
61 locations=("resources",),
62 output_run="out"))
64 def test_configMulti(self):
65 """Test config overrides"""
66 self.run_test(["ingest-raws", "repo", "resources",
67 "--output-run", "out",
68 "-c", "foo=1",
69 "--config", "bar=2,baz=3"],
70 self.makeExpected(repo="repo",
71 locations=("resources",),
72 output_run="out",
73 config=dict(foo="1", bar="2", baz="3")))
75 def test_configFile(self):
76 """Test config file override"""
77 configFile = "path/to/file.txt"
78 self.run_test(["ingest-raws", "repo", "resources",
79 "--output-run", "out",
80 "--config-file", configFile],
81 self.makeExpected(repo="repo",
82 locations=("resources",),
83 output_run="out",
84 config_file=configFile),
85 withTempFile=configFile)
87 def test_transfer(self):
88 """Test the transfer argument"""
89 self.run_test(["ingest-raws", "repo", "resources",
90 "--output-run", "out",
91 "--transfer", "symlink"],
92 self.makeExpected(repo="repo",
93 locations=("resources",),
94 output_run="out",
95 transfer="symlink"))
97 def test_ingestTask(self):
98 """Test the ingest task argument"""
99 self.run_test(["ingest-raws", "repo", "resources",
100 "--output-run", "out",
101 "--ingest-task", "foo.bar.baz"],
102 self.makeExpected(repo="repo",
103 locations=("resources",),
104 output_run="out",
105 ingest_task="foo.bar.baz"))
107 def test_locations(self):
108 """Test that the locations argument accepts multiple inputs and splits
109 commas."""
110 self.run_test(["ingest-raws", "repo", "in/directory/,in/another/dir/", "other/file.fits"],
111 self.makeExpected(repo="repo",
112 locations=("in/directory/", "in/another/dir/", "other/file.fits")))
115class PatchRawIngestTask(lsst.obs.base.RawIngestTask):
117 init_args = []
119 def __init__(self, *args, **kwargs):
120 self.init_args.append((args, kwargs))
121 super().__init__(*args, **kwargs)
123 def run(self, *args, **kwargs):
124 pass
127class RawIngestMockTest(unittest.TestCase):
129 def setUp(self):
130 self.runner = LogCliRunner()
132 def test(self):
133 """Verify config gets applied properly."""
134 with self.runner.isolated_filesystem():
135 result = self.runner.invoke(butlerCli, ["create", "repo"])
136 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
137 with unittest.mock.patch("lsst.obs.base.RawIngestTask", new=PatchRawIngestTask) as mock:
138 # call and override the name parameter of the config
139 result = self.runner.invoke(butlerCli, ["ingest-raws", "repo", "resources",
140 "--config", "transfer=hardlink"])
141 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
142 # Verify the mock class was initialized exactly once:
143 self.assertEqual(len(mock.init_args), 1)
144 # Verify that the task was initialized with a 'butler' kwarg
145 # that received a butler instance:
146 self.assertIsInstance(mock.init_args[0][1]["butler"], Butler)
147 # Verify that the task was initialized with a 'config' kwarg
148 # that received an expected config:
149 expectedConfig = RawIngestConfig()
150 expectedConfig.update(transfer="hardlink")
151 self.assertEqual(mock.init_args[0][1]["config"], expectedConfig)
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 unittest.main()