Coverage for tests/test_cliScript.py: 32%

71 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-15 02:54 -0700

1# This file is part of ctrl_mpexec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 <https://www.gnu.org/licenses/>. 

21 

22import os 

23import tempfile 

24import unittest 

25 

26import click 

27import lsst.utils.tests 

28from lsst.ctrl.mpexec.cli import opt, script 

29from lsst.ctrl.mpexec.cli.pipetask import cli as pipetaskCli 

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

31from lsst.pipe.base import Pipeline 

32 

33 

34class BuildTestCase(unittest.TestCase): 

35 """Test a few of the inputs to the build script function to test basic 

36 funcitonality.""" 

37 

38 @staticmethod 

39 def buildArgs(**kwargs): 

40 defaultArgs = dict( 

41 log_level=(), 

42 order_pipeline=False, 

43 pipeline=None, 

44 pipeline_actions=(), 

45 pipeline_dot=None, 

46 save_pipeline=None, 

47 show=(), 

48 ) 

49 defaultArgs.update(kwargs) 

50 return defaultArgs 

51 

52 def testMakeEmptyPipeline(self): 

53 """Test building a pipeline with default arguments.""" 

54 pipeline = script.build(**self.buildArgs()) 

55 self.assertIsInstance(pipeline, Pipeline) 

56 self.assertEqual(len(pipeline), 0) 

57 

58 def testSavePipeline(self): 

59 """Test pipeline serialization.""" 

60 with tempfile.TemporaryDirectory() as tempdir: 

61 # make empty pipeline and store it in a file 

62 filename = os.path.join(tempdir, "pipeline") 

63 pipeline = script.build(**self.buildArgs(filename=filename)) 

64 self.assertIsInstance(pipeline, Pipeline) 

65 

66 # read pipeline from a file 

67 pipeline = script.build(**self.buildArgs(filename=filename)) 

68 self.assertIsInstance(pipeline, Pipeline) 

69 self.assertIsInstance(pipeline, Pipeline) 

70 self.assertEqual(len(pipeline), 0) 

71 

72 def testShowPipeline(self): 

73 """Test showing the pipeline.""" 

74 

75 class ShowInfo: 

76 def __init__(self, show, expectedOutput): 

77 self.show = show 

78 self.expectedOutput = expectedOutput 

79 

80 def __repr__(self): 

81 return f"ShowInfo({self.show}, {self.expectedOutput}" 

82 

83 testdata = [ 

84 ShowInfo( 

85 "pipeline", 

86 """description: anonymous 

87tasks: 

88 task: 

89 class: lsst.pipe.base.tests.simpleQGraph.AddTask 

90 config: 

91 - addend: '100'""", 

92 ), 

93 ShowInfo( 

94 "config", 

95 """### Configuration for task `task' 

96# Flag to enable/disable metadata saving for a task, enabled by default. 

97config.saveMetadata=True 

98 

99# Flag to enable/disable saving of log output for a task, enabled by default. 

100config.saveLogOutput=True 

101 

102# amount to add 

103config.addend=100 

104 

105# name for connection input 

106config.connections.input='add_dataset{in_tmpl}' 

107 

108# name for connection output 

109config.connections.output='add_dataset{out_tmpl}' 

110 

111# name for connection output2 

112config.connections.output2='add2_dataset{out_tmpl}' 

113 

114# name for connection initout 

115config.connections.initout='add_init_output{out_tmpl}' 

116 

117# Template parameter used to format corresponding field template parameter 

118config.connections.in_tmpl='_in' 

119 

120# Template parameter used to format corresponding field template parameter 

121config.connections.out_tmpl='_out'""", 

122 ), 

123 # history will contain machine-specific paths, TBD how to verify 

124 ShowInfo("history=task::addend", None), 

125 ShowInfo("tasks", "### Subtasks for task `lsst.pipe.base.tests.simpleQGraph.AddTask'"), 

126 ] 

127 

128 for showInfo in testdata: 

129 runner = LogCliRunner() 

130 result = runner.invoke( 

131 pipetaskCli, 

132 [ 

133 "build", 

134 "--task", 

135 "lsst.pipe.base.tests.simpleQGraph.AddTask:task", 

136 "--config", 

137 "task:addend=100", 

138 "--show", 

139 showInfo.show, 

140 ], 

141 ) 

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

143 if showInfo.expectedOutput is not None: 

144 self.assertIn(showInfo.expectedOutput, result.output, msg=f"for {showInfo}") 

145 

146 def testMissingOption(self): 

147 """Test that the build script fails if options are missing.""" 

148 

149 @click.command() 

150 @opt.pipeline_build_options() 

151 def cli(**kwargs): 

152 script.build(**kwargs) 

153 

154 runner = click.testing.CliRunner() 

155 result = runner.invoke(cli) 

156 # The cli call should fail, because script.build takes more options 

157 # than are defined by pipeline_build_options. 

158 self.assertNotEqual(result.exit_code, 0) 

159 

160 

161class QgraphTestCase(unittest.TestCase): 

162 def testMissingOption(self): 

163 """Test that if options for the qgraph script are missing that it 

164 fails.""" 

165 

166 @click.command() 

167 @opt.pipeline_build_options() 

168 def cli(**kwargs): 

169 script.qgraph(**kwargs) 

170 

171 runner = click.testing.CliRunner() 

172 result = runner.invoke(cli) 

173 # The cli call should fail, because qgraph.build takes more options 

174 # than are defined by pipeline_build_options. 

175 self.assertNotEqual(result.exit_code, 0) 

176 

177 

178class RunTestCase(unittest.TestCase): 

179 def testMissingOption(self): 

180 """Test that if options for the run script are missing that it 

181 fails.""" 

182 

183 @click.command() 

184 @opt.pipeline_build_options() 

185 def cli(**kwargs): 

186 script.run(**kwargs) 

187 

188 runner = click.testing.CliRunner() 

189 result = runner.invoke(cli) 

190 # The cli call should fail, because qgraph.run takes more options 

191 # than are defined by pipeline_build_options. 

192 self.assertNotEqual(result.exit_code, 0) 

193 

194 

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

196 lsst.utils.tests.init() 

197 unittest.main()