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