Coverage for tests / test_cliCmdTestIngest.py: 53%

57 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-19 01:15 -0800

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 

24import unittest 

25 

26import lsst.obs.base 

27from lsst.daf.butler import Butler 

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

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

30from lsst.daf.butler.tests import CliCmdTestBase 

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

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

33from lsst.obs.base.ingest import RawIngestConfig 

34 

35 

36class IngestRawsTestCase(CliCmdTestBase, unittest.TestCase): 

37 """Test the ingest-raws command-line tool.""" 

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 update_records=False, 

54 fail_fast=False, 

55 skip_existing=True, 

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 def test_no_skip(self): 

130 """Test that no skip does appear.""" 

131 self.run_test( 

132 ["ingest-raws", "repo", "in/directory/", "--no-skip-existing"], 

133 self.makeExpected(repo="repo", locations=("in/directory/",), skip_existing=False), 

134 ) 

135 

136 

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

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

139 

140 init_args = [] 

141 

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

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

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

145 

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

147 pass 

148 

149 

150class RawIngestMockTest(unittest.TestCase): 

151 """Run ingest tests with mock.""" 

152 

153 def setUp(self): 

154 self.runner = LogCliRunner() 

155 

156 def test(self): 

157 """Verify config gets applied properly.""" 

158 with self.runner.isolated_filesystem(): 

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

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

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

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

163 # fail-fast. 

164 result = self.runner.invoke( 

165 butlerCli, 

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

167 ) 

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

169 # Verify the mock class was initialized exactly once: 

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

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

172 # that received a butler instance: 

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

174 expectedConfig = RawIngestConfig() 

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

176 # that received an expected config: 

177 expectedConfig.update(transfer="hardlink") 

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

179 # parameter to be set to True. 

180 expectedConfig.update(failFast=True) 

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

182 

183 

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

185 unittest.main()