Coverage for tests/test_driver.py : 31%

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
# # This file is part of ap_verify. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. #
"""Shortcut decorator for consistently patching ApPipeTask. """ def wrapper(self, *args, **kwargs): patcher = unittest.mock.patch("lsst.ap.pipe.ApPipeTask", autospec=True, _DefaultName=ApPipeTask._DefaultName, ConfigClass=ApPipeTask.ConfigClass) patchedMethod = patcher(method) return patchedMethod(self, *args, **kwargs)
"""A MagicMock for classes that records requests for objects of that class.
Because ``__init__`` cannot be mocked directly, the calls cannot be identified with the usual ``object.method`` syntax. Instead, filter the object's calls for a ``name`` attribute equal to ``__init__``. """ # super() unsafe because MagicMock does not guarantee support instance = unittest.mock.MagicMock.__call__(self, *args, **kwargs) initCall = unittest.mock.call(*args, **kwargs) initCall.name = "__init__" instance.mock_calls.append(initCall) return instance
self._testDir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self._testDir, ignore_errors=True)
# Fake Butler to avoid Workspace initialization overhead butler = self.setUpMockPatch("lsst.daf.persistence.Butler", autospec=True) butler.getMapperClass.return_value = lsst.obs.test.TestMapper self.setUpMockPatch("lsst.daf.persistence.searchDataRefs", return_value=[{"visit": 42, "ccd": 0}])
self.job = lsst.verify.Job() self.workspace = Workspace(self._testDir) self.apPipeArgs = pipeline_driver.ApPipeParser().parse_args(["--id", "visit=42"])
self.subtaskJob = lsst.verify.Job(measurements=[lsst.verify.Measurement("ip_isr.IsrTime", 2.0 * u.s)])
def dummyMetadata(): result = PropertySet() result.add("lsst.ap.pipe.ccdProcessor.cycleCount", 42) return result
"""Create and register a patcher for a test suite.
The patching process is guaranteed to avoid resource leaks or side effects lasting beyond the test case that calls this method.
Parameters ---------- target : `str` The target to patch. Must obey all restrictions listed for the ``target`` parameter of `unittest.mock.patch`. kwargs : any Any keyword arguments that are allowed for `unittest.mock.patch`, particularly optional attributes for a `unittest.mock.Mock`.
Returns ------- mock : `unittest.mock.MagicMock` Object representing the same type of entity as ``target``. For example, if ``target`` is the name of a class, this method shall return a replacement class (rather than a replacement object of that class). """ patcher = unittest.mock.patch(target, **kwargs) mock = patcher.start() self.addCleanup(patcher.stop) return mock
# Mock up ApPipeTask to avoid doing any processing. def testRunApPipeSteps(self, _mockConfig, mockClass): """Test that runApPipe runs the entire pipeline. """ pipeline_driver.runApPipe(self.job, self.workspace, self.apPipeArgs)
mockClass.return_value.runDataRef.assert_called_once()
"""Test that _updateMetrics does not add metrics if no job files are provided. """ metadata = PipelineDriverTestSuite.dummyMetadata()
pipeline_driver._updateMetrics(metadata, self.job)
self.assertFalse(self.job.measurements)
"""Test that _updateMetrics can load metrics when given temporary Job files. """ subtaskFile = os.path.join(self._testDir, "ccdProcessor.persist") self.subtaskJob.write(subtaskFile) metadata = PipelineDriverTestSuite.dummyMetadata() metadata.add("lsst.ap.pipe.ccdProcessor.verify_json_path", subtaskFile)
self.assertNotEqual(self.job.measurements, self.subtaskJob.measurements)
pipeline_driver._updateMetrics(metadata, self.job)
self.assertEqual(self.job.measurements, self.subtaskJob.measurements)
# Mock up ApPipeTask to avoid doing any processing. def testUpdateMetricsOnError(self, _mockConfig, mockClass): """Test that runApPipe stores metrics in a job even when the pipeline fails. """ subtaskFile = os.path.join(self._testDir, "ccdProcessor.persist") self.subtaskJob.write(subtaskFile) metadata = PipelineDriverTestSuite.dummyMetadata() metadata.add("lsst.ap.pipe.ccdProcessor.verify_json_path", subtaskFile)
mockClass.return_value.getFullMetadata.return_value = metadata mockClass.return_value.runDataRef.side_effect = RuntimeError("DECam is weird!")
self.assertNotEqual(self.job.measurements, self.subtaskJob.measurements)
with self.assertRaises(RuntimeError): pipeline_driver.runApPipe(self.job, self.workspace, self.apPipeArgs)
self.assertEqual(self.job.measurements, self.subtaskJob.measurements)
"""Test that runApPipe can pass custom configs from a workspace to ApPipeTask. """ configFile = os.path.join(self.workspace.configDir, "apPipe.py") with open(configFile, "w") as f: # Illegal value; would never be set by a real config f.write("config.differencer.doWriteSources = False\n") f.write("config.ppdb.db_url = 'sqlite://'\n")
task = self.setUpMockPatch("lsst.ap.pipe.ApPipeTask", spec=True, new_callable=InitRecordingMock, _DefaultName=ApPipeTask._DefaultName, ConfigClass=ApPipeTask.ConfigClass).return_value
pipeline_driver.runApPipe(self.job, self.workspace, self.apPipeArgs) initCalls = (c for c in task.mock_calls if c.name == "__init__") for call in initCalls: kwargs = call[2] self.assertIn("config", kwargs) taskConfig = kwargs["config"] self.assertFalse(taskConfig.differencer.doWriteSources) self.assertNotEqual(taskConfig.ppdb.db_url, "sqlite:///" + self.workspace.dbLocation)
"""Test that runApPipe places a database in the workspace location by default. """ task = self.setUpMockPatch("lsst.ap.pipe.ApPipeTask", spec=True, new_callable=InitRecordingMock, _DefaultName=ApPipeTask._DefaultName, ConfigClass=ApPipeTask.ConfigClass).return_value
pipeline_driver.runApPipe(self.job, self.workspace, self.apPipeArgs) initCalls = (c for c in task.mock_calls if c.name == "__init__") for call in initCalls: kwargs = call[2] self.assertIn("config", kwargs) taskConfig = kwargs["config"] self.assertEqual(taskConfig.ppdb.db_url, "sqlite:///" + self.workspace.dbLocation)
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |