Coverage for tests/test_cliCmdConfigDump.py : 30%

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