Coverage for tests/test_cliCmdConfigDump.py : 29%

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 click
27import click.testing
28import unittest
29import yaml
31from lsst.daf.butler.cli import butler
32from lsst.daf.butler.cli.cmd import config_dump
33from lsst.daf.butler.tests import CliCmdTestBase
36class ConfigDumpTest(CliCmdTestBase, unittest.TestCase):
37 defaultExpected = dict()
38 command = config_dump
41class ConfigDumpUseTest(unittest.TestCase):
42 """Test executing the command."""
44 def test_stdout(self):
45 """Test dumping the config to stdout."""
46 runner = click.testing.CliRunner()
47 with runner.isolated_filesystem():
48 result = runner.invoke(butler.cli, ["create", "here"])
49 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
51 # test dumping to stdout:
52 result = runner.invoke(butler.cli, ["config-dump", "here"])
53 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
54 # check for some expected keywords:
55 cfg = yaml.safe_load(result.stdout)
56 self.assertIn("datastore", cfg)
57 self.assertIn("composites", cfg["datastore"])
58 self.assertIn("storageClasses", cfg)
60 def test_file(self):
61 """test dumping the config to a file."""
62 runner = click.testing.CliRunner()
63 with runner.isolated_filesystem():
64 result = runner.invoke(butler.cli, ["create", "here"])
65 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
66 result = runner.invoke(butler.cli, ["config-dump", "here", "--file=there"])
67 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
68 # check for some expected keywords:
69 with open("there", "r") as f:
70 cfg = yaml.safe_load(f)
71 self.assertIn("datastore", cfg)
72 self.assertIn("composites", cfg["datastore"])
73 self.assertIn("storageClasses", cfg)
75 def test_subset(self):
76 """Test selecting a subset of the config."""
77 runner = click.testing.CliRunner()
78 with runner.isolated_filesystem():
79 result = runner.invoke(butler.cli, ["create", "here"])
80 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
81 result = runner.invoke(butler.cli, ["config-dump", "here", "--subset", "datastore"])
82 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
83 cfg = yaml.safe_load(result.stdout)
84 # count the keys in the datastore config
85 self.assertIs(len(cfg), 7)
86 self.assertIn("cls", cfg)
87 self.assertIn("create", cfg)
88 self.assertIn("formatters", cfg)
89 self.assertIn("records", cfg)
90 self.assertIn("root", cfg)
91 self.assertIn("templates", cfg)
93 def test_invalidSubset(self):
94 """Test selecting a subset key that does not exist in the config."""
95 runner = click.testing.CliRunner()
96 with runner.isolated_filesystem():
97 result = runner.invoke(butler.cli, ["create", "here"])
98 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
99 # test dumping to stdout:
100 result = runner.invoke(butler.cli, ["config-dump", "here", "--subset", "foo"])
101 self.assertEqual(result.exit_code, 1)
102 self.assertIn("Error: 'foo not found in config at here'", result.output)
105if __name__ == "__main__": 105 ↛ 106line 105 didn't jump to line 106, because the condition on line 105 was never true
106 unittest.main()