Coverage for tests/test_cliCmdTestIngest.py: 59%

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

55 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 ) 

54 

55 @staticmethod 

56 def command(): 

57 return ingest_raws 

58 

59 def test_repoAndOutput(self): 

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

61 self.run_test( 

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

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

64 ) 

65 

66 def test_configMulti(self): 

67 """Test config overrides""" 

68 self.run_test( 

69 [ 

70 "ingest-raws", 

71 "repo", 

72 "resources", 

73 "--output-run", 

74 "out", 

75 "-c", 

76 "foo=1", 

77 "--config", 

78 "bar=2,baz=3", 

79 ], 

80 self.makeExpected( 

81 repo="repo", 

82 locations=("resources",), 

83 output_run="out", 

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

85 ), 

86 ) 

87 

88 def test_configFile(self): 

89 """Test config file override""" 

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

91 self.run_test( 

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

93 self.makeExpected( 

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

95 ), 

96 withTempFile=configFile, 

97 ) 

98 

99 def test_transfer(self): 

100 """Test the transfer argument""" 

101 self.run_test( 

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

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

104 ) 

105 

106 def test_ingestTask(self): 

107 """Test the ingest task argument""" 

108 self.run_test( 

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

110 self.makeExpected( 

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

112 ), 

113 ) 

114 

115 def test_locations(self): 

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

117 commas.""" 

118 self.run_test( 

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

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

121 ) 

122 

123 

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

125 

126 init_args = [] 

127 

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

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

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

131 

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

133 pass 

134 

135 

136class RawIngestMockTest(unittest.TestCase): 

137 def setUp(self): 

138 self.runner = LogCliRunner() 

139 

140 def test(self): 

141 """Verify config gets applied properly.""" 

142 with self.runner.isolated_filesystem(): 

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

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

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

146 # call and override the name parameter of the config 

147 result = self.runner.invoke( 

148 butlerCli, ["ingest-raws", "repo", "resources", "--config", "transfer=hardlink"] 

149 ) 

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

151 # Verify the mock class was initialized exactly once: 

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

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

154 # that received a butler instance: 

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

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

157 # that received an expected config: 

158 expectedConfig = RawIngestConfig() 

159 expectedConfig.update(transfer="hardlink") 

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

161 

162 

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

164 unittest.main()