Coverage for tests/test_apdbMetricTask.py : 39%

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 verify.
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 shutil
23import tempfile
24import unittest.mock
26import astropy.units as u
28import lsst.utils.tests
29from lsst.pex.config import Config
30from lsst.daf.butler import Quantum
31from lsst.pipe.base import Task, Struct
33from lsst.verify import Measurement
34from lsst.verify.tasks import ApdbMetricTask
35from lsst.verify.tasks.testUtils import ApdbMetricTestCase
36from butler_utils import make_test_butler, make_dataset_type, \
37 ref_from_connection, run_quantum
40class DummyTask(ApdbMetricTask):
41 _DefaultName = "NotARealTask"
43 def makeMeasurement(self, _dbHandle, outputDataId):
44 if outputDataId:
45 nChars = len(outputDataId["instrument"])
46 return Measurement(self.config.metricName,
47 nChars * u.dimensionless_unscaled)
48 else:
49 return Measurement(self.config.metricName,
50 0 * u.dimensionless_unscaled)
53class Gen3ApdbTestSuite(ApdbMetricTestCase):
54 @classmethod
55 def makeTask(cls):
56 class MockDbLoader(Task):
57 ConfigClass = Config
59 def run(self, _):
60 return Struct(apdb=unittest.mock.Mock())
62 config = DummyTask.ConfigClass()
63 config.dbLoader.retarget(MockDbLoader)
64 config.connections.package = "verify"
65 config.connections.metric = "DummyApdb"
66 config.validate()
67 return DummyTask(config=config)
69 def setUp(self):
70 super().setUp()
72 self.connections = self.task.config.ConnectionsClass(
73 config=self.task.config)
74 self.CAMERA_ID = "NotACam"
75 self.VISIT_ID = 42
76 self.CHIP_ID = 5
78 def _makeButler(self):
79 """Construct a repository that supports the inputs and outputs of a
80 generic `ApdbMetricTask`.
82 This method is *very* slow; call it only from tests that need it.
83 """
85 root = tempfile.mkdtemp()
86 self.addCleanup(shutil.rmtree, root, ignore_errors=True)
87 butler = make_test_butler(
88 root,
89 {
90 "instrument": [{"name": self.CAMERA_ID}],
91 "visit": [{"id": self.VISIT_ID,
92 "name": "only_visit",
93 "instrument": self.CAMERA_ID}],
94 "detector": [{"id": self.CHIP_ID,
95 "full_name": "only_ccd",
96 "instrument": self.CAMERA_ID}],
97 })
98 make_dataset_type(
99 butler,
100 self.connections.measurement.name,
101 self.connections.measurement.dimensions,
102 self.connections.measurement.storageClass)
103 make_dataset_type(
104 butler,
105 self.connections.dbInfo.name,
106 self.connections.dbInfo.dimensions,
107 self.connections.dbInfo.storageClass)
108 return butler
110 def testRunQuantum(self):
111 inputId = {
112 "instrument": self.CAMERA_ID,
113 "visit": self.VISIT_ID,
114 "detector": self.CHIP_ID,
115 }
117 butler = self._makeButler()
118 # self.task.config not persistable because it refers to a local class
119 # We don't actually use the persisted config, so just make a new one
120 butler.put(self.task.ConfigClass(), "apdb_marker", inputId)
122 quantum = Quantum(taskClass=self.taskClass)
123 quantum.addPredictedInput(ref_from_connection(
124 butler,
125 self.connections.dbInfo,
126 inputId))
127 quantum.addOutput(ref_from_connection(
128 butler,
129 self.connections.measurement,
130 {"instrument": self.CAMERA_ID, }))
132 run_quantum(self.task, butler, quantum)
134 # Did output data ID get passed to DummyTask.run?
135 measurement = butler.get(self.connections.measurement.name,
136 instrument=self.CAMERA_ID)
137 self.assertEqual(measurement.quantity,
138 len(self.CAMERA_ID) * u.dimensionless_unscaled)
141# Hack around unittest's hacky test setup system
142del ApdbMetricTestCase
145class MemoryTester(lsst.utils.tests.MemoryTestCase):
146 pass
149def setup_module(module):
150 lsst.utils.tests.init()
153if __name__ == "__main__": 153 ↛ 154line 153 didn't jump to line 154, because the condition on line 153 was never true
154 lsst.utils.tests.init()
155 unittest.main()