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: testUtil.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_input'
98# name for connection output
99config.connections.output='add_output'
101# name for connection initout
102config.connections.initout='add_init_output'"""),
104 # history will contain machine-specific paths, TBD how to verify
105 ShowInfo("history=task::addend", None),
106 ShowInfo("tasks", "### Subtasks for task `AddTask'")
107 ]
109 for showInfo in testdata:
110 runner = LogCliRunner()
111 result = runner.invoke(pipetaskCli, ["build",
112 "--task", "testUtil.AddTask:task",
113 "--config", "task:addend=100",
114 "--show", showInfo.show])
115 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
116 if showInfo.expectedOutput is not None:
117 self.assertIn(showInfo.expectedOutput, result.output, msg=f"for {showInfo}")
119 def testMissingOption(self):
120 """Test that if options for the build script are missing that it fails.
121 """
123 @click.command()
124 @opt.pipeline_build_options()
125 def cli(**kwargs):
126 script.build(**kwargs)
128 runner = click.testing.CliRunner()
129 result = runner.invoke(cli)
130 # The cli call should fail, because script.build takes more options
131 # than are defined by pipeline_build_options.
132 self.assertNotEqual(result.exit_code, 0)
135class QgraphTestCase(unittest.TestCase):
137 def testMissingOption(self):
138 """Test that if options for the qgraph script are missing that it
139 fails."""
141 @click.command()
142 @opt.pipeline_build_options()
143 def cli(**kwargs):
144 script.qgraph(**kwargs)
146 runner = click.testing.CliRunner()
147 result = runner.invoke(cli)
148 # The cli call should fail, because qgraph.build takes more options
149 # than are defined by pipeline_build_options.
150 self.assertNotEqual(result.exit_code, 0)
153class RunTestCase(unittest.TestCase):
155 def testMissingOption(self):
156 """Test that if options for the run script are missing that it
157 fails."""
159 @click.command()
160 @opt.pipeline_build_options()
161 def cli(**kwargs):
162 script.run(**kwargs)
164 runner = click.testing.CliRunner()
165 result = runner.invoke(cli)
166 # The cli call should fail, because qgraph.run takes more options
167 # than are defined by pipeline_build_options.
168 self.assertNotEqual(result.exit_code, 0)
171if __name__ == "__main__": 171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true
172 lsst.utils.tests.init()
173 unittest.main()