Coverage for tests/test_cliCmdImport.py: 58%
44 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-04 02:04 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-04 02:04 -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 config-dump command.
23"""
25import os
26import unittest
27import unittest.mock
29from lsst.daf.butler.cli.cmd import butler_import
30from lsst.daf.butler.tests import CliCmdTestBase
33class ImportTestCase(CliCmdTestBase, unittest.TestCase):
34 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.butlerImport"
36 @staticmethod
37 def defaultExpected():
38 return dict(
39 repo=None, transfer="auto", directory=None, skip_dimensions=(), export_file=None, reuse_ids=False
40 )
42 @staticmethod
43 def command():
44 return butler_import
46 def test_minimal(self):
47 """Test only required parameters, and omit optional parameters."""
48 self.run_test(["import", "here", "foo"], self.makeExpected(repo="here", directory="foo"))
50 def test_almostAll(self):
51 """Test all the parameters, except export_file which gets its own test
52 case below.
53 """
54 self.run_test(
55 ["import", "here", "foo", "--transfer", "symlink"],
56 self.makeExpected(repo="here", directory="foo", transfer="symlink"),
57 )
59 def test_missingArgument(self):
60 """Verify the command fails if either of the positional arguments,
61 REPO or DIRECTORY, is missing."""
62 self.run_missing(["import", "foo"], r"Error: Missing argument ['\"]DIRECTORY['\"].")
65class ExportFileCase(CliCmdTestBase, unittest.TestCase):
66 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.butlerImport"
68 @property
69 def mock(self):
70 return unittest.mock.MagicMock(side_effect=self.read_test)
72 didRead = None
74 @staticmethod
75 def defaultExpected():
76 return dict(repo=None, transfer="auto", directory=None, export_file=None, reuse_ids=False)
78 @staticmethod
79 def command():
80 return butler_import
82 @staticmethod
83 def read_test(*args, **kwargs):
84 """This gets called by the MagicMock's side effect when the MagicMock
85 is called. Our export_file argument is a File so Click will open it
86 before calling the MagicMock, and thus before it gets here. A little
87 bit is written into the file here and that is verified later.
88 """
89 print("in read_test")
90 ExportFileCase.didRead = kwargs["export_file"].read()
92 def test_exportFile(self):
93 """Test all the parameters, except export_file."""
94 # export_file is ANY in makeExpected because that variable is opened by
95 # click and the open handle is passed to the command function as a
96 # TestIOWrapper. It doesn't work to test it with
97 # MagicMock.assert_called_with because if a TextIOWrapper is created
98 # here it will be a different instance and not compare equal. We test
99 # that variable via the MagicMock.side_effect used in self.read_test.
100 with self.runner.isolated_filesystem():
101 with open("output.yaml", "w") as f:
102 f.write("foobarbaz")
103 self.run_test(
104 [
105 "import",
106 "here",
107 "foo",
108 "--skip-dimensions",
109 "instrument",
110 "-s",
111 "detector",
112 "--export-file",
113 os.path.abspath("output.yaml"),
114 ],
115 self.makeExpected(
116 repo="here",
117 directory="foo",
118 skip_dimensions=("instrument", "detector"),
119 export_file=unittest.mock.ANY,
120 ),
121 )
122 self.assertEqual("foobarbaz", ExportFileCase.didRead)
125if __name__ == "__main__": 125 ↛ 126line 125 didn't jump to line 126, because the condition on line 125 was never true
126 unittest.main()