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 unittest 

26import unittest.mock 

27 

28from lsst.daf.butler.tests.mockeredTest import MockeredTestBase 

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

30 

31 

32class ImportTestCase(MockeredTestBase): 

33 

34 defaultExpected = dict(repo=None, 

35 transfer="auto", 

36 output_run=None, 

37 directory=None, 

38 export_file=None) 

39 

40 def test_minimal(self): 

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

42 """ 

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

44 "--output-run", "out"], 

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

46 output_run="out")) 

47 

48 def test_almostAll(self): 

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

50 case below. 

51 """ 

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

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

54 "--transfer", "symlink"], 

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

56 output_run="out", 

57 transfer="symlink")) 

58 

59 def test_missingArgument(self): 

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

61 REPO or DIRECTORY, is missing.""" 

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

63 'Error: Missing argument "DIRECTORY".') 

64 

65 

66class ExportFileCase(MockeredTestBase): 

67 

68 didRead = None 

69 

70 defaultExpected = dict(repo=None, 

71 transfer="auto", 

72 output_run=None, 

73 directory=None, 

74 export_file=None) 

75 

76 def setUp(self): 

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

78 # is called. 

79 Mocker.mock.side_effect = self.read_test 

80 super().setUp() 

81 

82 def tearDown(self): 

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

84 Mocker.mock.side_effect = None 

85 super().tearDown() 

86 

87 @staticmethod 

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

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

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

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

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

93 """ 

94 print("in read_test") 

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

96 

97 def test_exportFile(self): 

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

99 """ 

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

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

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

103 # MagicMock.assert_called_with because if a TextIOWrapper is created 

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

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

106 with self.runner.isolated_filesystem(): 

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

108 f.write("foobarbaz") 

109 f.close() 

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

111 "--output-run", "out", 

112 "--export-file", "output.yaml"], 

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

114 output_run="out", 

115 export_file=unittest.mock.ANY)) 

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

117 

118 

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

120 unittest.main()