Hide keyboard shortcuts

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

1 

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/>. 

22 

23"""Unit tests for daf_butler CLI config-dump command. 

24""" 

25 

26import os.path 

27import unittest 

28import yaml 

29 

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 

34 

35TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

36 

37 

38class ConfigDumpTest(CliCmdTestBase, unittest.TestCase): 

39 

40 @staticmethod 

41 def defaultExpected(): 

42 return dict() 

43 

44 @staticmethod 

45 def command(): 

46 return config_dump 

47 

48 

49class ConfigDumpUseTest(unittest.TestCase): 

50 """Test executing the command.""" 

51 

52 def setUp(self): 

53 self.runner = LogCliRunner() 

54 

55 def test_stdout(self): 

56 """Test dumping the config to stdout.""" 

57 with self.runner.isolated_filesystem(): 

58 result = self.runner.invoke(butler.cli, ["create", "here"]) 

59 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

60 

61 # test dumping to stdout: 

62 result = self.runner.invoke(butler.cli, ["config-dump", "here"]) 

63 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

64 # check for some expected keywords: 

65 cfg = yaml.safe_load(result.stdout) 

66 self.assertIn("datastore", cfg) 

67 self.assertIn("composites", cfg["datastore"]) 

68 self.assertIn("storageClasses", cfg) 

69 

70 def test_file(self): 

71 """test dumping the config to a file.""" 

72 with self.runner.isolated_filesystem(): 

73 result = self.runner.invoke(butler.cli, ["create", "here"]) 

74 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

75 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--file=there"]) 

76 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

77 # check for some expected keywords: 

78 with open("there", "r") as f: 

79 cfg = yaml.safe_load(f) 

80 self.assertIn("datastore", cfg) 

81 self.assertIn("composites", cfg["datastore"]) 

82 self.assertIn("storageClasses", cfg) 

83 

84 def test_subset(self): 

85 """Test selecting a subset of the config.""" 

86 with self.runner.isolated_filesystem(): 

87 result = self.runner.invoke(butler.cli, ["create", "here"]) 

88 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

89 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "datastore"]) 

90 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

91 cfg = yaml.safe_load(result.stdout) 

92 # count the keys in the datastore config 

93 self.assertIs(len(cfg), 7) 

94 self.assertIn("cls", cfg) 

95 self.assertIn("create", cfg) 

96 self.assertIn("formatters", cfg) 

97 self.assertIn("records", cfg) 

98 self.assertIn("root", cfg) 

99 self.assertIn("templates", cfg) 

100 

101 def test_invalidSubset(self): 

102 """Test selecting a subset key that does not exist in the config.""" 

103 with self.runner.isolated_filesystem(): 

104 result = self.runner.invoke(butler.cli, ["create", "here"]) 

105 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

106 # test dumping to stdout: 

107 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "foo"]) 

108 self.assertEqual(result.exit_code, 1) 

109 self.assertIn("Error: 'foo not found in config at here'", result.output) 

110 

111 def test_presets(self): 

112 """Test that file overrides can set command line options in bulk. 

113 """ 

114 with self.runner.isolated_filesystem(): 

115 result = self.runner.invoke(butler.cli, ["create", "here"]) 

116 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

117 overrides_path = os.path.join(TESTDIR, "data", "config-overrides.yaml") 

118 

119 # Run with a presets file 

120 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--options-file", overrides_path]) 

121 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

122 cfg = yaml.safe_load(result.stdout) 

123 # Look for datastore information 

124 self.assertIn("formatters", cfg) 

125 self.assertIn("root", cfg) 

126 

127 # Now run with an explicit subset and presets 

128 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{overrides_path}", 

129 "--subset", ".registry"]) 

130 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

131 cfg = yaml.safe_load(result.stdout) 

132 # Look for datastore information 

133 self.assertNotIn("formatters", cfg) 

134 self.assertIn("managers", cfg) 

135 

136 # Now with subset before presets -- explicit always trumps 

137 # presets. 

138 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", ".registry", 

139 "--options-file", overrides_path]) 

140 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

141 cfg = yaml.safe_load(result.stdout) 

142 # Look for datastore information 

143 self.assertNotIn("formatters", cfg) 

144 self.assertIn("managers", cfg) 

145 

146 

147if __name__ == "__main__": 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true

148 unittest.main()