Coverage for tests/test_cliCmdTestIngest.py: 53%

56 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-02 10:32 +0000

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 """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 fail_fast=False, 

55 ) 

56 

57 @staticmethod 

58 def command(): 

59 return ingest_raws 

60 

61 def test_repoAndOutput(self): 

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

63 self.run_test( 

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

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

66 ) 

67 

68 def test_configMulti(self): 

69 """Test config overrides.""" 

70 self.run_test( 

71 [ 

72 "ingest-raws", 

73 "repo", 

74 "resources", 

75 "--output-run", 

76 "out", 

77 "-c", 

78 "foo=1", 

79 "--config", 

80 "bar=2", 

81 "--config", 

82 "baz=3", 

83 ], 

84 self.makeExpected( 

85 repo="repo", 

86 locations=("resources",), 

87 output_run="out", 

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

89 ), 

90 ) 

91 

92 def test_configFile(self): 

93 """Test config file override.""" 

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

95 self.run_test( 

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

97 self.makeExpected( 

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

99 ), 

100 withTempFile=configFile, 

101 ) 

102 

103 def test_transfer(self): 

104 """Test the transfer argument.""" 

105 self.run_test( 

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

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

108 ) 

109 

110 def test_ingestTask(self): 

111 """Test the ingest task argument.""" 

112 self.run_test( 

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

114 self.makeExpected( 

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

116 ), 

117 ) 

118 

119 def test_locations(self): 

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

121 commas. 

122 """ 

123 self.run_test( 

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

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

126 ) 

127 

128 

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

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

131 

132 init_args = [] 

133 

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

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

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

137 

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

139 pass 

140 

141 

142class RawIngestMockTest(unittest.TestCase): 

143 """Run ingest tests with mock.""" 

144 

145 def setUp(self): 

146 self.runner = LogCliRunner() 

147 

148 def test(self): 

149 """Verify config gets applied properly.""" 

150 with self.runner.isolated_filesystem(): 

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

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

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

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

155 # fail-fast. 

156 result = self.runner.invoke( 

157 butlerCli, 

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

159 ) 

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

161 # Verify the mock class was initialized exactly once: 

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

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

164 # that received a butler instance: 

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

166 expectedConfig = RawIngestConfig() 

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

168 # that received an expected config: 

169 expectedConfig.update(transfer="hardlink") 

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

171 # parameter to be set to True. 

172 expectedConfig.update(failFast=True) 

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

174 

175 

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

177 unittest.main()