Coverage for tests/test_cliScript.py: 29%
71 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 18:04 -0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 18:04 -0800
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/>.
22import click
23import os
24import tempfile
25import unittest
27from lsst.ctrl.mpexec.cli import script, opt
28from lsst.ctrl.mpexec.cli.pipetask import cli as pipetaskCli
29from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
30from lsst.pipe.base import Pipeline
31import lsst.utils.tests
34class BuildTestCase(unittest.TestCase):
35 """Test a few of the inputs to the build script function to test basic
36 funcitonality."""
38 @staticmethod
39 def buildArgs(**kwargs):
40 defaultArgs = dict(log_level=(),
41 order_pipeline=False,
42 pipeline=None,
43 pipeline_actions=(),
44 pipeline_dot=None,
45 save_pipeline=None,
46 show=())
47 defaultArgs.update(kwargs)
48 return defaultArgs
50 def testMakeEmptyPipeline(self):
51 """Test building a pipeline with default arguments.
52 """
53 pipeline = script.build(**self.buildArgs())
54 self.assertIsInstance(pipeline, Pipeline)
55 self.assertEqual(len(pipeline), 0)
57 def testSavePipeline(self):
58 """Test pipeline serialization."""
59 with tempfile.TemporaryDirectory() as tempdir:
60 # make empty pipeline and store it in a file
61 filename = os.path.join(tempdir, "pipeline")
62 pipeline = script.build(**self.buildArgs(filename=filename))
63 self.assertIsInstance(pipeline, Pipeline)
65 # read pipeline from a file
66 pipeline = script.build(**self.buildArgs(filename=filename))
67 self.assertIsInstance(pipeline, Pipeline)
68 self.assertIsInstance(pipeline, Pipeline)
69 self.assertEqual(len(pipeline), 0)
71 def testShowPipeline(self):
72 """Test showing the pipeline."""
73 class ShowInfo:
74 def __init__(self, show, expectedOutput):
75 self.show = show
76 self.expectedOutput = expectedOutput
78 def __repr__(self):
79 return f"ShowInfo({self.show}, {self.expectedOutput}"
81 testdata = [
82 ShowInfo("pipeline", """description: anonymous
83tasks:
84 task:
85 class: lsst.pipe.base.tests.simpleQGraph.AddTask
86 config:
87 - addend: '100'"""),
88 ShowInfo("config", """### Configuration for task `task'
89# Flag to enable/disable metadata saving for a task, enabled by default.
90config.saveMetadata=True
92# Flag to enable/disable saving of log output for a task, enabled by default.
93config.saveLogOutput=True
95# amount to add
96config.addend=100
98# name for connection input
99config.connections.input='add_dataset{in_tmpl}'
101# name for connection output
102config.connections.output='add_dataset{out_tmpl}'
104# name for connection output2
105config.connections.output2='add2_dataset{out_tmpl}'
107# name for connection initout
108config.connections.initout='add_init_output{out_tmpl}'
110# Template parameter used to format corresponding field template parameter
111config.connections.in_tmpl='_in'
113# Template parameter used to format corresponding field template parameter
114config.connections.out_tmpl='_out'"""),
116 # history will contain machine-specific paths, TBD how to verify
117 ShowInfo("history=task::addend", None),
118 ShowInfo("tasks", "### Subtasks for task `AddTask'")
119 ]
121 for showInfo in testdata:
122 runner = LogCliRunner()
123 result = runner.invoke(pipetaskCli, ["build",
124 "--task", "lsst.pipe.base.tests.simpleQGraph.AddTask:task",
125 "--config", "task:addend=100",
126 "--show", showInfo.show])
127 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
128 if showInfo.expectedOutput is not None:
129 self.assertIn(showInfo.expectedOutput, result.output, msg=f"for {showInfo}")
131 def testMissingOption(self):
132 """Test that if options for the build script are missing that it fails.
133 """
135 @click.command()
136 @opt.pipeline_build_options()
137 def cli(**kwargs):
138 script.build(**kwargs)
140 runner = click.testing.CliRunner()
141 result = runner.invoke(cli)
142 # The cli call should fail, because script.build takes more options
143 # than are defined by pipeline_build_options.
144 self.assertNotEqual(result.exit_code, 0)
147class QgraphTestCase(unittest.TestCase):
149 def testMissingOption(self):
150 """Test that if options for the qgraph script are missing that it
151 fails."""
153 @click.command()
154 @opt.pipeline_build_options()
155 def cli(**kwargs):
156 script.qgraph(**kwargs)
158 runner = click.testing.CliRunner()
159 result = runner.invoke(cli)
160 # The cli call should fail, because qgraph.build takes more options
161 # than are defined by pipeline_build_options.
162 self.assertNotEqual(result.exit_code, 0)
165class RunTestCase(unittest.TestCase):
167 def testMissingOption(self):
168 """Test that if options for the run script are missing that it
169 fails."""
171 @click.command()
172 @opt.pipeline_build_options()
173 def cli(**kwargs):
174 script.run(**kwargs)
176 runner = click.testing.CliRunner()
177 result = runner.invoke(cli)
178 # The cli call should fail, because qgraph.run takes more options
179 # than are defined by pipeline_build_options.
180 self.assertNotEqual(result.exit_code, 0)
183if __name__ == "__main__": 183 ↛ 184line 183 didn't jump to line 184, because the condition on line 183 was never true
184 lsst.utils.tests.init()
185 unittest.main()