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# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

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

23""" 

24 

25import click 

26import click.testing 

27import unittest 

28 

29from lsst.daf.butler.cli import butler 

30from lsst.daf.butler.cli.utils import clickResultMsg, Mocker, mockEnvVar 

31 

32 

33def makeExpectedKwargs(**kwargs): 

34 expected = dict(repo=None, 

35 transfer="auto", 

36 output_run=None, 

37 directory=None, 

38 export_file=None) 

39 expected.update(kwargs) 

40 return expected 

41 

42 

43class Case(unittest.TestCase): 

44 

45 def setUp(self): 

46 self.runner = click.testing.CliRunner(env=mockEnvVar) 

47 

48 def run_test(self, inputs, expectedKwargs): 

49 """Test command line interaction with import command function. 

50 

51 Parameters 

52 ---------- 

53 inputs : [`str`] 

54 A list of the arguments to the butler command, starting with 

55 `import` 

56 expectedKwargs : `dict` [`str`, `str`] 

57 The expected arguments to the import command function, keys are 

58 the argument name and values are the argument value. 

59 """ 

60 result = self.runner.invoke(butler.cli, inputs) 

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

62 Mocker.mock.assert_called_with(**expectedKwargs) 

63 

64 def test_minimal(self): 

65 """Test only the required parameters, and omit the optional parameters. 

66 """ 

67 expected = makeExpectedKwargs(repo="here", directory="foo", output_run="out") 

68 self.run_test(["import", "here", 

69 "foo", 

70 "--output-run", "out"], expected) 

71 

72 def test_almostAll(self): 

73 """Test all the parameters, except export_file which gets its own test 

74 case below. 

75 """ 

76 with self.runner.isolated_filesystem(): 

77 expected = makeExpectedKwargs(repo="here", directory="foo", output_run="out", transfer="symlink") 

78 self.run_test(["import", "here", 

79 "foo", 

80 "--output-run", "out", 

81 "--transfer", "symlink"], expected) 

82 

83 def test_missingArgument(self): 

84 """Verify the command fails if a positional argument is missing""" 

85 runner = click.testing.CliRunner(env=mockEnvVar) 

86 result = runner.invoke(butler.cli, ["import", "foo", "--output-run", "out"]) 

87 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

88 

89 

90class ExportFileCase(unittest.TestCase): 

91 

92 didRead = None 

93 

94 def setUp(self): 

95 # add a side effect to Mocker so that it will call our method when it 

96 # is called. 

97 Mocker.mock.side_effect = self.read_test 

98 

99 def tearDown(self): 

100 # reset the Mocker's side effect on our way out! 

101 Mocker.mock.side_effect = None 

102 

103 @staticmethod 

104 def read_test(*args, **kwargs): 

105 """This gets called by the Mocker's side effect when the Mocker is 

106 called. Our export_file argument is a File so Click will open it before 

107 calling the Mocker, and thus before it gets here. A little bit is 

108 written into the file here and that is verified later. 

109 """ 

110 print("in read_test") 

111 ExportFileCase.didRead = kwargs["export_file"].read() 

112 

113 def test_exportFile(self): 

114 """Test all the parameters, except export_file. 

115 """ 

116 runner = click.testing.CliRunner(env=mockEnvVar) 

117 with runner.isolated_filesystem(): 

118 f = open("output.yaml", "w") 

119 f.write("foobarbaz") 

120 f.close() 

121 result = runner.invoke(butler.cli, ["import", "here", "foo", "--output-run", "out", 

122 "--export-file", "output.yaml"]) 

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

124 self.assertEqual("foobarbaz", ExportFileCase.didRead) 

125 

126 

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

128 unittest.main()