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 os 

26import unittest 

27import unittest.mock 

28 

29from lsst.daf.butler.tests import CliCmdTestBase 

30from lsst.daf.butler.cli.cmd import butler_import 

31from lsst.daf.butler.cli.utils import Mocker 

32 

33 

34class ImportTestCase(CliCmdTestBase, unittest.TestCase): 

35 

36 @staticmethod 

37 def defaultExpected(): 

38 return dict(repo=None, 

39 transfer="auto", 

40 output_run=None, 

41 directory=None, 

42 skip_dimensions=(), 

43 export_file=None) 

44 

45 @staticmethod 

46 def command(): 

47 return butler_import 

48 

49 def test_minimal(self): 

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

51 """ 

52 self.run_test(["import", "here", "foo", 

53 "--output-run", "out"], 

54 self.makeExpected(repo="here", directory="foo", 

55 output_run="out")) 

56 

57 def test_almostAll(self): 

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

59 case below. 

60 """ 

61 self.run_test(["import", "here", "foo", 

62 "--output-run", "out", 

63 "--transfer", "symlink"], 

64 self.makeExpected(repo="here", directory="foo", 

65 output_run="out", 

66 transfer="symlink")) 

67 

68 def test_missingArgument(self): 

69 """Verify the command fails if either of the positional arguments, 

70 REPO or DIRECTORY, is missing.""" 

71 self.run_missing(["import", "foo", "--output-run", "out"], 

72 r"Error: Missing argument ['\"]DIRECTORY['\"].") 

73 

74 

75class ExportFileCase(CliCmdTestBase, unittest.TestCase): 

76 

77 didRead = None 

78 

79 @staticmethod 

80 def defaultExpected(): 

81 return dict(repo=None, 

82 transfer="auto", 

83 output_run=None, 

84 directory=None, 

85 export_file=None) 

86 

87 @staticmethod 

88 def command(): 

89 return butler_import 

90 

91 def setUp(self): 

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

93 # is called. 

94 Mocker.mock.side_effect = self.read_test 

95 super().setUp() 

96 

97 def tearDown(self): 

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

99 Mocker.mock.side_effect = None 

100 super().tearDown() 

101 

102 @staticmethod 

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

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

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

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

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

108 """ 

109 print("in read_test") 

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

111 

112 def test_exportFile(self): 

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

114 """ 

115 # export_file is ANY in makeExpected because that variable is opened by 

116 # click and the open handle is passed to the command function as a 

117 # TestIOWrapper. It doesn't work to test it with 

118 # MagicMock.assert_called_with because if a TextIOWrapper is created 

119 # here it will be a different instance and not compare equal. We test 

120 # that variable via the mocker.side_effect used in self.read_test. 

121 with self.runner.isolated_filesystem(): 

122 with open("output.yaml", "w") as f: 

123 f.write("foobarbaz") 

124 self.run_test(["import", "here", "foo", 

125 "--output-run", "out", 

126 "--skip-dimensions", "instrument", "-s", "detector", 

127 "--export-file", os.path.abspath("output.yaml")], 

128 self.makeExpected(repo="here", directory="foo", 

129 output_run="out", skip_dimensions=("instrument", "detector"), 

130 export_file=unittest.mock.ANY)) 

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

132 

133 

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

135 unittest.main()