Coverage for tests/test_cliCmdTestIngest.py: 58%

Shortcuts 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

56 statements  

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 ingest-raws command. 

23""" 

24 

25import unittest 

26 

27import lsst.obs.base 

28from lsst.daf.butler import Butler 

29from lsst.daf.butler.cli.butler import cli as butlerCli 

30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg 

31from lsst.daf.butler.tests import CliCmdTestBase 

32from lsst.obs.base.cli.cmd import ingest_raws 

33from lsst.obs.base.cli.cmd.commands import fits_re 

34from lsst.obs.base.ingest import RawIngestConfig 

35 

36 

37class IngestRawsTestCase(CliCmdTestBase, unittest.TestCase): 

38 

39 mockFuncName = "lsst.obs.base.cli.cmd.commands.script.ingestRaws" 

40 

41 @staticmethod 

42 def defaultExpected(): 

43 return dict( 

44 config={}, 

45 config_file=None, 

46 ingest_task="lsst.obs.base.RawIngestTask", 

47 locations=(), 

48 output_run=None, 

49 processes=1, 

50 regex=fits_re, 

51 transfer="auto", 

52 track_file_attrs=True, 

53 fail_fast=False, 

54 ) 

55 

56 @staticmethod 

57 def command(): 

58 return ingest_raws 

59 

60 def test_repoAndOutput(self): 

61 """Test the most basic required arguments, repo and output run""" 

62 self.run_test( 

63 ["ingest-raws", "repo", "resources", "--output-run", "out"], 

64 self.makeExpected(repo="repo", locations=("resources",), output_run="out"), 

65 ) 

66 

67 def test_configMulti(self): 

68 """Test config overrides""" 

69 self.run_test( 

70 [ 

71 "ingest-raws", 

72 "repo", 

73 "resources", 

74 "--output-run", 

75 "out", 

76 "-c", 

77 "foo=1", 

78 "--config", 

79 "bar=2,baz=3", 

80 ], 

81 self.makeExpected( 

82 repo="repo", 

83 locations=("resources",), 

84 output_run="out", 

85 config=dict(foo="1", bar="2", baz="3"), 

86 ), 

87 ) 

88 

89 def test_configFile(self): 

90 """Test config file override""" 

91 configFile = "path/to/file.txt" 

92 self.run_test( 

93 ["ingest-raws", "repo", "resources", "--output-run", "out", "--config-file", configFile], 

94 self.makeExpected( 

95 repo="repo", locations=("resources",), output_run="out", config_file=configFile 

96 ), 

97 withTempFile=configFile, 

98 ) 

99 

100 def test_transfer(self): 

101 """Test the transfer argument""" 

102 self.run_test( 

103 ["ingest-raws", "repo", "resources", "--output-run", "out", "--transfer", "symlink"], 

104 self.makeExpected(repo="repo", locations=("resources",), output_run="out", transfer="symlink"), 

105 ) 

106 

107 def test_ingestTask(self): 

108 """Test the ingest task argument""" 

109 self.run_test( 

110 ["ingest-raws", "repo", "resources", "--output-run", "out", "--ingest-task", "foo.bar.baz"], 

111 self.makeExpected( 

112 repo="repo", locations=("resources",), output_run="out", ingest_task="foo.bar.baz" 

113 ), 

114 ) 

115 

116 def test_locations(self): 

117 """Test that the locations argument accepts multiple inputs and splits 

118 commas.""" 

119 self.run_test( 

120 ["ingest-raws", "repo", "in/directory/,in/another/dir/", "other/file.fits"], 

121 self.makeExpected(repo="repo", locations=("in/directory/", "in/another/dir/", "other/file.fits")), 

122 ) 

123 

124 

125class PatchRawIngestTask(lsst.obs.base.RawIngestTask): 

126 

127 init_args = [] 

128 

129 def __init__(self, *args, **kwargs): 

130 self.init_args.append((args, kwargs)) 

131 super().__init__(*args, **kwargs) 

132 

133 def run(self, *args, **kwargs): 

134 pass 

135 

136 

137class RawIngestMockTest(unittest.TestCase): 

138 def setUp(self): 

139 self.runner = LogCliRunner() 

140 

141 def test(self): 

142 """Verify config gets applied properly.""" 

143 with self.runner.isolated_filesystem(): 

144 result = self.runner.invoke(butlerCli, ["create", "repo"]) 

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

146 with unittest.mock.patch("lsst.obs.base.RawIngestTask", new=PatchRawIngestTask) as mock: 

147 # Call, override the name parameter of the config and set 

148 # fail-fast. 

149 result = self.runner.invoke( 

150 butlerCli, 

151 ["ingest-raws", "repo", "resources", "--config", "transfer=hardlink", "--fail-fast"], 

152 ) 

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

154 # Verify the mock class was initialized exactly once: 

155 self.assertEqual(len(mock.init_args), 1) 

156 # Verify that the task was initialized with a 'butler' kwarg 

157 # that received a butler instance: 

158 self.assertIsInstance(mock.init_args[0][1]["butler"], Butler) 

159 expectedConfig = RawIngestConfig() 

160 # Verify that the task was initialized with a 'config' kwarg 

161 # that received an expected config: 

162 expectedConfig.update(transfer="hardlink") 

163 # Verfiy that --failfast caused the config's failFast 

164 # parameter to be set to True. 

165 expectedConfig.update(failFast=True) 

166 self.assertEqual(mock.init_args[0][1]["config"], expectedConfig) 

167 

168 

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

170 unittest.main()