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 unittest 

27import yaml 

28 

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 

33 

34 

35class ConfigDumpTest(CliCmdTestBase, unittest.TestCase): 

36 defaultExpected = dict() 

37 command = config_dump 

38 

39 

40class ConfigDumpUseTest(unittest.TestCase): 

41 """Test executing the command.""" 

42 

43 def setUp(self): 

44 self.runner = LogCliRunner() 

45 

46 def test_stdout(self): 

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

48 with self.runner.isolated_filesystem(): 

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

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

51 

52 # test dumping to stdout: 

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

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

55 # check for some expected keywords: 

56 cfg = yaml.safe_load(result.stdout) 

57 self.assertIn("datastore", cfg) 

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

59 self.assertIn("storageClasses", cfg) 

60 

61 def test_file(self): 

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

63 with self.runner.isolated_filesystem(): 

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

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

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

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

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) 

74 

75 def test_subset(self): 

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

77 with self.runner.isolated_filesystem(): 

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

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

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

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

82 cfg = yaml.safe_load(result.stdout) 

83 # count the keys in the datastore config 

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

85 self.assertIn("cls", cfg) 

86 self.assertIn("create", cfg) 

87 self.assertIn("formatters", cfg) 

88 self.assertIn("records", cfg) 

89 self.assertIn("root", cfg) 

90 self.assertIn("templates", cfg) 

91 

92 def test_invalidSubset(self): 

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

94 with self.runner.isolated_filesystem(): 

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

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

97 # test dumping to stdout: 

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

99 self.assertEqual(result.exit_code, 1) 

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

101 

102 

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

104 unittest.main()