Coverage for tests/test_cliCmdTestIngest.py: 53%

56 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 04:19 -0700

1# This file is part of obs_base. 

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 """Test the ingest-raws command-line tool.""" 

39 

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

41 

42 @staticmethod 

43 def defaultExpected(): 

44 return dict( 

45 config={}, 

46 config_file=None, 

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

48 locations=(), 

49 output_run=None, 

50 processes=1, 

51 regex=fits_re, 

52 transfer="auto", 

53 track_file_attrs=True, 

54 update_records=False, 

55 fail_fast=False, 

56 ) 

57 

58 @staticmethod 

59 def command(): 

60 return ingest_raws 

61 

62 def test_repoAndOutput(self): 

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

64 self.run_test( 

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

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

67 ) 

68 

69 def test_configMulti(self): 

70 """Test config overrides.""" 

71 self.run_test( 

72 [ 

73 "ingest-raws", 

74 "repo", 

75 "resources", 

76 "--output-run", 

77 "out", 

78 "-c", 

79 "foo=1", 

80 "--config", 

81 "bar=2", 

82 "--config", 

83 "baz=3", 

84 ], 

85 self.makeExpected( 

86 repo="repo", 

87 locations=("resources",), 

88 output_run="out", 

89 config={"foo": "1", "bar": "2", "baz": "3"}, 

90 ), 

91 ) 

92 

93 def test_configFile(self): 

94 """Test config file override.""" 

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

96 self.run_test( 

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

98 self.makeExpected( 

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

100 ), 

101 withTempFile=configFile, 

102 ) 

103 

104 def test_transfer(self): 

105 """Test the transfer argument.""" 

106 self.run_test( 

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

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

109 ) 

110 

111 def test_ingestTask(self): 

112 """Test the ingest task argument.""" 

113 self.run_test( 

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

115 self.makeExpected( 

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

117 ), 

118 ) 

119 

120 def test_locations(self): 

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

122 commas. 

123 """ 

124 self.run_test( 

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

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

127 ) 

128 

129 

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

131 """Ingest task with run() method disabled.""" 

132 

133 init_args = [] 

134 

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

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

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

138 

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

140 pass 

141 

142 

143class RawIngestMockTest(unittest.TestCase): 

144 """Run ingest tests with mock.""" 

145 

146 def setUp(self): 

147 self.runner = LogCliRunner() 

148 

149 def test(self): 

150 """Verify config gets applied properly.""" 

151 with self.runner.isolated_filesystem(): 

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

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

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

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

156 # fail-fast. 

157 result = self.runner.invoke( 

158 butlerCli, 

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

160 ) 

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

162 # Verify the mock class was initialized exactly once: 

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

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

165 # that received a butler instance: 

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

167 expectedConfig = RawIngestConfig() 

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

169 # that received an expected config: 

170 expectedConfig.update(transfer="hardlink") 

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

172 # parameter to be set to True. 

173 expectedConfig.update(failFast=True) 

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

175 

176 

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

178 unittest.main()