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 click 

27import click.testing 

28import os 

29import unittest 

30import yaml 

31 

32from lsst.daf.butler.cli import butler 

33 

34 

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

36 

37 

38class Suite(unittest.TestCase): 

39 

40 def test_stdout(self): 

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

42 runner = click.testing.CliRunner() 

43 with runner.isolated_filesystem(): 

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

45 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

46 

47 # test dumping to stdout: 

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

49 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

50 # check for some expected keywords: 

51 cfg = yaml.safe_load(result.stdout) 

52 self.assertIn("datastore", cfg) 

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

54 self.assertIn("storageClasses", cfg) 

55 

56 def test_file(self): 

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

58 runner = click.testing.CliRunner() 

59 with runner.isolated_filesystem(): 

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

61 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

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

63 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

64 # check for some expected keywords: 

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

66 cfg = yaml.safe_load(f) 

67 self.assertIn("datastore", cfg) 

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

69 self.assertIn("storageClasses", cfg) 

70 

71 def test_subset(self): 

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

73 runner = click.testing.CliRunner() 

74 with runner.isolated_filesystem(): 

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

76 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

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

78 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

79 cfg = yaml.safe_load(result.stdout) 

80 # count the keys in the datastore config 

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

82 self.assertIn("cls", cfg) 

83 self.assertIn("create", cfg) 

84 self.assertIn("formatters", cfg) 

85 self.assertIn("records", cfg) 

86 self.assertIn("root", cfg) 

87 self.assertIn("templates", cfg) 

88 

89 def test_invalidSubset(self): 

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

91 runner = click.testing.CliRunner() 

92 with runner.isolated_filesystem(): 

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

94 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

95 # test dumping to stdout: 

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

97 self.assertEqual(result.exit_code, 1) 

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

99 

100 

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

102 unittest.main()