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 

31 

32 

33class ImportTestCase(CliCmdTestBase, unittest.TestCase): 

34 

35 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.butlerImport" 

36 

37 @staticmethod 

38 def defaultExpected(): 

39 return dict(repo=None, 

40 transfer="auto", 

41 directory=None, 

42 skip_dimensions=(), 

43 export_file=None, 

44 reuse_ids=False) 

45 

46 @staticmethod 

47 def command(): 

48 return butler_import 

49 

50 def test_minimal(self): 

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

52 """ 

53 self.run_test(["import", "here", "foo"], 

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

55 

56 def test_almostAll(self): 

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

58 case below. 

59 """ 

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

61 "--transfer", "symlink"], 

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

63 transfer="symlink")) 

64 

65 def test_missingArgument(self): 

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

67 REPO or DIRECTORY, is missing.""" 

68 self.run_missing(["import", "foo"], 

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

70 

71 

72class ExportFileCase(CliCmdTestBase, unittest.TestCase): 

73 

74 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.butlerImport" 

75 

76 @property 

77 def mock(self): 

78 return unittest.mock.MagicMock(side_effect=self.read_test) 

79 

80 didRead = None 

81 

82 @staticmethod 

83 def defaultExpected(): 

84 return dict(repo=None, 

85 transfer="auto", 

86 directory=None, 

87 export_file=None, 

88 reuse_ids=False) 

89 

90 @staticmethod 

91 def command(): 

92 return butler_import 

93 

94 @staticmethod 

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

96 """This gets called by the MagicMock's side effect when the MagicMock 

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

98 before calling the MagicMock, and thus before it gets here. A little 

99 bit is written into the file here and that is verified later. 

100 """ 

101 print("in read_test") 

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

103 

104 def test_exportFile(self): 

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

106 """ 

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

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

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

110 # MagicMock.assert_called_with because if a TextIOWrapper is created 

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

112 # that variable via the MagicMock.side_effect used in self.read_test. 

113 with self.runner.isolated_filesystem(): 

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

115 f.write("foobarbaz") 

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

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

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

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

120 skip_dimensions=("instrument", "detector"), 

121 export_file=unittest.mock.ANY)) 

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

123 

124 

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

126 unittest.main()