Coverage for tests/test_cliCmdTestIngest.py: 50%

56 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-10 11:03 +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 

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", 

80 "--config", 

81 "baz=3", 

82 ], 

83 self.makeExpected( 

84 repo="repo", 

85 locations=("resources",), 

86 output_run="out", 

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

88 ), 

89 ) 

90 

91 def test_configFile(self): 

92 """Test config file override""" 

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

94 self.run_test( 

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

96 self.makeExpected( 

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

98 ), 

99 withTempFile=configFile, 

100 ) 

101 

102 def test_transfer(self): 

103 """Test the transfer argument""" 

104 self.run_test( 

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

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

107 ) 

108 

109 def test_ingestTask(self): 

110 """Test the ingest task argument""" 

111 self.run_test( 

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

113 self.makeExpected( 

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

115 ), 

116 ) 

117 

118 def test_locations(self): 

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

120 commas.""" 

121 self.run_test( 

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

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

124 ) 

125 

126 

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

128 

129 init_args = [] 

130 

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

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

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

134 

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

136 pass 

137 

138 

139class RawIngestMockTest(unittest.TestCase): 

140 def setUp(self): 

141 self.runner = LogCliRunner() 

142 

143 def test(self): 

144 """Verify config gets applied properly.""" 

145 with self.runner.isolated_filesystem(): 

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

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

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

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

150 # fail-fast. 

151 result = self.runner.invoke( 

152 butlerCli, 

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

154 ) 

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

156 # Verify the mock class was initialized exactly once: 

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

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

159 # that received a butler instance: 

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

161 expectedConfig = RawIngestConfig() 

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

163 # that received an expected config: 

164 expectedConfig.update(transfer="hardlink") 

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

166 # parameter to be set to True. 

167 expectedConfig.update(failFast=True) 

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

169 

170 

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

172 unittest.main()