Coverage for tests/test_cliCmdConfigDump.py : 28%

Hot-keys 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
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 self.assertEqual(result.exception.args, KeyError('foo not found in config at here').args)
113 def test_presets(self):
114 """Test that file overrides can set command line options in bulk.
115 """
116 with self.runner.isolated_filesystem():
117 result = self.runner.invoke(butler.cli, ["create", "here"])
118 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
119 overrides_path = os.path.join(TESTDIR, "data", "config-overrides.yaml")
121 # Run with a presets file
122 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--options-file", overrides_path])
123 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
124 cfg = yaml.safe_load(result.stdout)
125 # Look for datastore information
126 self.assertIn("formatters", cfg)
127 self.assertIn("root", cfg)
129 # Now run with an explicit subset and presets
130 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{overrides_path}",
131 "--subset", ".registry"])
132 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
133 cfg = yaml.safe_load(result.stdout)
134 # Look for datastore information
135 self.assertNotIn("formatters", cfg)
136 self.assertIn("managers", cfg)
138 # Now with subset before presets -- explicit always trumps
139 # presets.
140 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", ".registry",
141 "--options-file", overrides_path])
142 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
143 cfg = yaml.safe_load(result.stdout)
144 # Look for datastore information
145 self.assertNotIn("formatters", cfg)
146 self.assertIn("managers", cfg)
149if __name__ == "__main__": 149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true
150 unittest.main()