Coverage for tests/test_cliCmdConfigDump.py : 27%

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 os
29import unittest
30import yaml
32from lsst.daf.butler.cli import butler
35TESTDIR = os.path.abspath(os.path.dirname(__file__))
37os.environ["DAF_BUTLER_PLUGINS"] = "lsst.daf.butler.cli.cmd"
40class Suite(unittest.TestCase):
42 def test_stdout(self):
43 """Test dumping the config to stdout."""
44 runner = click.testing.CliRunner()
45 with runner.isolated_filesystem():
46 result = runner.invoke(butler.cli, ["create", "--repo", "here"])
47 self.assertEqual(result.exit_code, 0)
49 # test dumping to stdout:
50 result = runner.invoke(butler.cli, ["config-dump", "--repo", "here"])
51 self.assertEqual(result.exit_code, 0)
52 # check for some expected keywords:
53 cfg = yaml.safe_load(result.stdout)
54 self.assertIn("composites", cfg)
55 self.assertIn("datastore", cfg)
56 self.assertIn("storageClasses", cfg)
58 def test_file(self):
59 """test dumping the config to a file."""
60 runner = click.testing.CliRunner()
61 with runner.isolated_filesystem():
62 result = runner.invoke(butler.cli, ["create", "--repo", "here"])
63 self.assertEqual(result.exit_code, 0)
64 result = runner.invoke(butler.cli, ["config-dump", "--repo", "here", "--file=there"])
65 self.assertEqual(result.exit_code, 0)
66 # check for some expected keywords:
67 with open("there", "r") as f:
68 cfg = yaml.safe_load(f)
69 self.assertIn("composites", cfg)
70 self.assertIn("datastore", cfg)
71 self.assertIn("storageClasses", cfg)
73 def test_subset(self):
74 """Test selecting a subset of the config."""
75 runner = click.testing.CliRunner()
76 with runner.isolated_filesystem():
77 result = runner.invoke(butler.cli, ["create", "--repo", "here"])
78 self.assertEqual(result.exit_code, 0)
79 result = runner.invoke(butler.cli, ["config-dump", "--repo", "here", "--subset", "datastore"])
80 self.assertEqual(result.exit_code, 0)
81 cfg = yaml.safe_load(result.stdout)
82 # the datastore cfg is expected to have exactly six keys:
83 self.assertIs(len(cfg.keys()), 6)
84 self.assertIn("cls", cfg)
85 self.assertIn("create", cfg)
86 self.assertIn("formatters", cfg)
87 self.assertIn("records", cfg)
88 self.assertIn("root", cfg)
89 self.assertIn("templates", cfg)
91 def test_invalidSubset(self):
92 """Test selecting a subset key that does not exist in the config."""
93 runner = click.testing.CliRunner()
94 with runner.isolated_filesystem():
95 result = runner.invoke(butler.cli, ["create", "--repo", "here"])
96 self.assertEqual(result.exit_code, 0)
97 # test dumping to stdout:
98 result = runner.invoke(butler.cli, ["config-dump", "--repo", "here", "--subset", "foo"])
99 self.assertEqual(result.exit_code, 1)
100 self.assertEqual(result.output, "Error: foo not found in config at here\n")
103if __name__ == "__main__": 103 ↛ 104line 103 didn't jump to line 104, because the condition on line 103 was never true
104 unittest.main()