Coverage for tests/test_dotTools.py: 25%
73 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 02:50 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 02:50 -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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <https://www.gnu.org/licenses/>.
28"""Simple unit test for Pipeline.
29"""
31import io
32import re
33import unittest
35import lsst.pipe.base.connectionTypes as cT
36import lsst.utils.tests
37from lsst.ctrl.mpexec.dotTools import pipeline2dot
38from lsst.pipe.base import Pipeline, PipelineTask, PipelineTaskConfig, PipelineTaskConnections
41class ExamplePipelineTaskConnections(PipelineTaskConnections, dimensions=()):
42 """Connections class used for testing.
44 Parameters
45 ----------
46 config : `PipelineTaskConfig`
47 The config to use for this connections class.
48 """
50 input1 = cT.Input(
51 name="", dimensions=["visit", "detector"], storageClass="example", doc="Input for this task"
52 )
53 input2 = cT.Input(
54 name="", dimensions=["visit", "detector"], storageClass="example", doc="Input for this task"
55 )
56 output1 = cT.Output(
57 name="", dimensions=["visit", "detector"], storageClass="example", doc="Output for this task"
58 )
59 output2 = cT.Output(
60 name="", dimensions=["visit", "detector"], storageClass="example", doc="Output for this task"
61 )
63 def __init__(self, *, config=None):
64 super().__init__(config=config)
65 if not config.connections.input2:
66 self.inputs.remove("input2")
67 if not config.connections.output2:
68 self.outputs.remove("output2")
71class ExamplePipelineTaskConfig(PipelineTaskConfig, pipelineConnections=ExamplePipelineTaskConnections):
72 """Example config used for testing."""
75def _makeConfig(inputName, outputName, pipeline, label):
76 """Add config overrides.
78 Factory method for config instances.
80 inputName and outputName can be either string or tuple of strings
81 with two items max.
82 """
83 if isinstance(inputName, tuple):
84 pipeline.addConfigOverride(label, "connections.input1", inputName[0])
85 pipeline.addConfigOverride(label, "connections.input2", inputName[1] if len(inputName) > 1 else "")
86 else:
87 pipeline.addConfigOverride(label, "connections.input1", inputName)
89 if isinstance(outputName, tuple):
90 pipeline.addConfigOverride(label, "connections.output1", outputName[0])
91 pipeline.addConfigOverride(label, "connections.output2", outputName[1] if len(outputName) > 1 else "")
92 else:
93 pipeline.addConfigOverride(label, "connections.output1", outputName)
96class ExamplePipelineTask(PipelineTask):
97 """Example pipeline task used for testing."""
99 ConfigClass = ExamplePipelineTaskConfig
102def _makePipeline(tasks):
103 """Generate Pipeline instance.
105 Parameters
106 ----------
107 tasks : list of tuples
108 Each tuple in the list has 3 or 4 items:
109 - input DatasetType name(s), string or tuple of strings
110 - output DatasetType name(s), string or tuple of strings
111 - task label, string
112 - optional task class object, can be None
114 Returns
115 -------
116 Pipeline instance
117 """
118 pipe = Pipeline("test pipeline")
119 for task in tasks:
120 inputs = task[0]
121 outputs = task[1]
122 label = task[2]
123 klass = task[3] if len(task) > 3 else ExamplePipelineTask
124 pipe.addTask(klass, label)
125 _makeConfig(inputs, outputs, pipe, label)
126 return list(pipe.toExpandedPipeline())
129class DotToolsTestCase(unittest.TestCase):
130 """A test case for dotTools."""
132 def testPipeline2dot(self):
133 """Tests for dotTools.pipeline2dot method."""
134 pipeline = _makePipeline(
135 [
136 ("A", ("B", "C"), "task0"),
137 ("C", "E", "task1"),
138 ("B", "D", "task2"),
139 (("D", "E"), "F", "task3"),
140 ("D.C", "G", "task4"),
141 ("task3_metadata", "H", "task5"),
142 ]
143 )
144 file = io.StringIO()
145 pipeline2dot(pipeline, file)
147 # It's hard to validate complete output, just checking few basic
148 # things, even that is not terribly stable.
149 lines = file.getvalue().strip().split("\n")
150 nglobals = 3
151 ndatasets = 10
152 ntasks = 6
153 nedges = 16
154 nextra = 2 # graph header and closing
155 self.assertEqual(len(lines), nglobals + ndatasets + ntasks + nedges + nextra)
157 # make sure that all node names are quoted
158 nodeRe = re.compile(r"^([^ ]+) \[.+\];$")
159 edgeRe = re.compile(r"^([^ ]+) *-> *([^ ]+);$")
160 for line in lines:
161 match = nodeRe.match(line)
162 if match:
163 node = match.group(1)
164 if node not in ["graph", "node", "edge"]:
165 self.assertEqual(node[0] + node[-1], '""')
166 continue
167 match = edgeRe.match(line)
168 if match:
169 for group in (1, 2):
170 node = match.group(group)
171 self.assertEqual(node[0] + node[-1], '""')
172 continue
174 # make sure components are connected appropriately
175 self.assertIn('"D" -> "D.C"', file.getvalue())
177 # make sure there is a connection created for metadata if someone
178 # tries to read it in
179 self.assertIn('"task3" -> "task3_metadata"', file.getvalue())
182class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
183 """Generic file handle leak check."""
186def setup_module(module):
187 """Set up the module for pytest.
189 Parameters
190 ----------
191 module : `~types.ModuleType`
192 Module to set up.
193 """
194 lsst.utils.tests.init()
197if __name__ == "__main__":
198 lsst.utils.tests.init()
199 unittest.main()