Coverage for tests/test_cliCmdConfigDump.py: 28%
84 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +0000
2# This file is part of daf_butler.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
23"""Unit tests for daf_butler CLI config-dump command.
24"""
26import os.path
27import unittest
28import yaml
30from lsst.daf.butler.cli import butler
31from lsst.daf.butler.cli.cmd import config_dump
32from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
33from lsst.daf.butler.tests import CliCmdTestBase
35TESTDIR = os.path.abspath(os.path.dirname(__file__))
38class ConfigDumpTest(CliCmdTestBase, unittest.TestCase):
40 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.configDump"
42 @staticmethod
43 def defaultExpected():
44 return dict()
46 @staticmethod
47 def command():
48 return config_dump
51class ConfigDumpUseTest(unittest.TestCase):
52 """Test executing the command."""
54 def setUp(self):
55 self.runner = LogCliRunner()
57 def test_stdout(self):
58 """Test dumping the config to stdout."""
59 with self.runner.isolated_filesystem():
60 result = self.runner.invoke(butler.cli, ["create", "here"])
61 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
63 # test dumping to stdout:
64 result = self.runner.invoke(butler.cli, ["config-dump", "here"])
65 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
66 # check for some expected keywords:
67 cfg = yaml.safe_load(result.stdout)
68 self.assertIn("datastore", cfg)
69 self.assertIn("composites", cfg["datastore"])
70 self.assertIn("storageClasses", cfg)
72 def test_file(self):
73 """test dumping the config to a file."""
74 with self.runner.isolated_filesystem():
75 result = self.runner.invoke(butler.cli, ["create", "here"])
76 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
77 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--file=there"])
78 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
79 # check for some expected keywords:
80 with open("there", "r") as f:
81 cfg = yaml.safe_load(f)
82 self.assertIn("datastore", cfg)
83 self.assertIn("composites", cfg["datastore"])
84 self.assertIn("storageClasses", cfg)
86 def test_subset(self):
87 """Test selecting a subset of the config."""
88 with self.runner.isolated_filesystem():
89 result = self.runner.invoke(butler.cli, ["create", "here"])
90 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
91 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "datastore"])
92 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
93 cfg = yaml.safe_load(result.stdout)
94 # count the keys in the datastore config
95 self.assertEqual(len(cfg), 8)
96 self.assertIn("cls", cfg)
97 self.assertIn("create", cfg)
98 self.assertIn("formatters", cfg)
99 self.assertIn("records", cfg)
100 self.assertIn("root", cfg)
101 self.assertIn("templates", cfg)
103 def test_invalidSubset(self):
104 """Test selecting a subset key that does not exist in the config."""
105 with self.runner.isolated_filesystem():
106 result = self.runner.invoke(butler.cli, ["create", "here"])
107 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
108 # test dumping to stdout:
109 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "foo"])
110 self.assertEqual(result.exit_code, 1)
111 # exception type is click.Exit, and its argument is a return code
112 self.assertEqual(result.exception.args, (1,))
114 def test_presets(self):
115 """Test that file overrides can set command line options in bulk.
116 """
117 with self.runner.isolated_filesystem():
118 result = self.runner.invoke(butler.cli, ["create", "here"])
119 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
120 overrides_path = os.path.join(TESTDIR, "data", "config-overrides.yaml")
122 # Run with a presets file
123 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--options-file", overrides_path])
124 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
125 cfg = yaml.safe_load(result.stdout)
126 # Look for datastore information
127 self.assertIn("formatters", cfg)
128 self.assertIn("root", cfg)
130 # Now run with an explicit subset and presets
131 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{overrides_path}",
132 "--subset", ".registry"])
133 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
134 cfg = yaml.safe_load(result.stdout)
135 # Look for datastore information
136 self.assertNotIn("formatters", cfg)
137 self.assertIn("managers", cfg)
139 # Now with subset before presets -- explicit always trumps
140 # presets.
141 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", ".registry",
142 "--options-file", overrides_path])
143 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
144 cfg = yaml.safe_load(result.stdout)
145 # Look for datastore information
146 self.assertNotIn("formatters", cfg)
147 self.assertIn("managers", cfg)
150if __name__ == "__main__": 150 ↛ 151line 150 didn't jump to line 151, because the condition on line 150 was never true
151 unittest.main()