Coverage for tests/test_cliScript.py : 29%

Hot-keys 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
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# amount to add
93config.addend=100
95# name for connection input
96config.connections.input='add_dataset{in_tmpl}'
98# name for connection output
99config.connections.output='add_dataset{out_tmpl}'
101# name for connection output2
102config.connections.output2='add2_dataset{out_tmpl}'
104# name for connection initout
105config.connections.initout='add_init_output{out_tmpl}'
107# Template parameter used to format corresponding field template parameter
108config.connections.in_tmpl='_in'
110# Template parameter used to format corresponding field template parameter
111config.connections.out_tmpl='_out'"""),
113 # history will contain machine-specific paths, TBD how to verify
114 ShowInfo("history=task::addend", None),
115 ShowInfo("tasks", "### Subtasks for task `AddTask'")
116 ]
118 for showInfo in testdata:
119 runner = LogCliRunner()
120 result = runner.invoke(pipetaskCli, ["build",
121 "--task", "lsst.pipe.base.tests.simpleQGraph.AddTask:task",
122 "--config", "task:addend=100",
123 "--show", showInfo.show])
124 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
125 if showInfo.expectedOutput is not None:
126 self.assertIn(showInfo.expectedOutput, result.output, msg=f"for {showInfo}")
128 def testMissingOption(self):
129 """Test that if options for the build script are missing that it fails.
130 """
132 @click.command()
133 @opt.pipeline_build_options()
134 def cli(**kwargs):
135 script.build(**kwargs)
137 runner = click.testing.CliRunner()
138 result = runner.invoke(cli)
139 # The cli call should fail, because script.build takes more options
140 # than are defined by pipeline_build_options.
141 self.assertNotEqual(result.exit_code, 0)
144class QgraphTestCase(unittest.TestCase):
146 def testMissingOption(self):
147 """Test that if options for the qgraph script are missing that it
148 fails."""
150 @click.command()
151 @opt.pipeline_build_options()
152 def cli(**kwargs):
153 script.qgraph(**kwargs)
155 runner = click.testing.CliRunner()
156 result = runner.invoke(cli)
157 # The cli call should fail, because qgraph.build takes more options
158 # than are defined by pipeline_build_options.
159 self.assertNotEqual(result.exit_code, 0)
162class RunTestCase(unittest.TestCase):
164 def testMissingOption(self):
165 """Test that if options for the run script are missing that it
166 fails."""
168 @click.command()
169 @opt.pipeline_build_options()
170 def cli(**kwargs):
171 script.run(**kwargs)
173 runner = click.testing.CliRunner()
174 result = runner.invoke(cli)
175 # The cli call should fail, because qgraph.run takes more options
176 # than are defined by pipeline_build_options.
177 self.assertNotEqual(result.exit_code, 0)
180if __name__ == "__main__": 180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never true
181 lsst.utils.tests.init()
182 unittest.main()