Coverage for tests/test_cliCmdTestIngest.py: 59%
Shortcuts 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
Shortcuts 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 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):
39 mockFuncName = "lsst.obs.base.cli.cmd.commands.script.ingestRaws"
41 @staticmethod
42 def defaultExpected():
43 return dict(
44 config={},
45 config_file=None,
46 ingest_task="lsst.obs.base.RawIngestTask",
47 locations=(),
48 output_run=None,
49 processes=1,
50 regex=fits_re,
51 transfer="auto",
52 )
54 @staticmethod
55 def command():
56 return ingest_raws
58 def test_repoAndOutput(self):
59 """Test the most basic required arguments, repo and output run"""
60 self.run_test(
61 ["ingest-raws", "repo", "resources", "--output-run", "out"],
62 self.makeExpected(repo="repo", locations=("resources",), output_run="out"),
63 )
65 def test_configMulti(self):
66 """Test config overrides"""
67 self.run_test(
68 [
69 "ingest-raws",
70 "repo",
71 "resources",
72 "--output-run",
73 "out",
74 "-c",
75 "foo=1",
76 "--config",
77 "bar=2,baz=3",
78 ],
79 self.makeExpected(
80 repo="repo",
81 locations=("resources",),
82 output_run="out",
83 config=dict(foo="1", bar="2", baz="3"),
84 ),
85 )
87 def test_configFile(self):
88 """Test config file override"""
89 configFile = "path/to/file.txt"
90 self.run_test(
91 ["ingest-raws", "repo", "resources", "--output-run", "out", "--config-file", configFile],
92 self.makeExpected(
93 repo="repo", locations=("resources",), output_run="out", config_file=configFile
94 ),
95 withTempFile=configFile,
96 )
98 def test_transfer(self):
99 """Test the transfer argument"""
100 self.run_test(
101 ["ingest-raws", "repo", "resources", "--output-run", "out", "--transfer", "symlink"],
102 self.makeExpected(repo="repo", locations=("resources",), output_run="out", transfer="symlink"),
103 )
105 def test_ingestTask(self):
106 """Test the ingest task argument"""
107 self.run_test(
108 ["ingest-raws", "repo", "resources", "--output-run", "out", "--ingest-task", "foo.bar.baz"],
109 self.makeExpected(
110 repo="repo", locations=("resources",), output_run="out", ingest_task="foo.bar.baz"
111 ),
112 )
114 def test_locations(self):
115 """Test that the locations argument accepts multiple inputs and splits
116 commas."""
117 self.run_test(
118 ["ingest-raws", "repo", "in/directory/,in/another/dir/", "other/file.fits"],
119 self.makeExpected(repo="repo", locations=("in/directory/", "in/another/dir/", "other/file.fits")),
120 )
123class PatchRawIngestTask(lsst.obs.base.RawIngestTask):
125 init_args = []
127 def __init__(self, *args, **kwargs):
128 self.init_args.append((args, kwargs))
129 super().__init__(*args, **kwargs)
131 def run(self, *args, **kwargs):
132 pass
135class RawIngestMockTest(unittest.TestCase):
136 def setUp(self):
137 self.runner = LogCliRunner()
139 def test(self):
140 """Verify config gets applied properly."""
141 with self.runner.isolated_filesystem():
142 result = self.runner.invoke(butlerCli, ["create", "repo"])
143 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
144 with unittest.mock.patch("lsst.obs.base.RawIngestTask", new=PatchRawIngestTask) as mock:
145 # call and override the name parameter of the config
146 result = self.runner.invoke(
147 butlerCli, ["ingest-raws", "repo", "resources", "--config", "transfer=hardlink"]
148 )
149 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
150 # Verify the mock class was initialized exactly once:
151 self.assertEqual(len(mock.init_args), 1)
152 # Verify that the task was initialized with a 'butler' kwarg
153 # that received a butler instance:
154 self.assertIsInstance(mock.init_args[0][1]["butler"], Butler)
155 # Verify that the task was initialized with a 'config' kwarg
156 # that received an expected config:
157 expectedConfig = RawIngestConfig()
158 expectedConfig.update(transfer="hardlink")
159 self.assertEqual(mock.init_args[0][1]["config"], expectedConfig)
162if __name__ == "__main__": 162 ↛ 163line 162 didn't jump to line 163, because the condition on line 162 was never true
163 unittest.main()