22 """Base class for writing Gen3 raw data ingest tests.
25 __all__ = (
"IngestTestBase",)
33 from lsst.daf.butler
import Butler
34 from lsst.daf.butler.cli.butler
import cli
as butlerCli
35 from lsst.daf.butler.cli.utils
import LogCliRunner
38 from .utils
import getInstrument
43 """Base class for tests of gen3 ingest. Subclass from this, then
44 `unittest.TestCase` to get a working test suite.
48 """Root path to ingest files into. Typically `obs_package/tests/`; the
49 actual directory will be a tempdir under this one.
53 """list of butler data IDs of files that should have been ingested."""
56 """Full path to a file to ingest in tests."""
58 rawIngestTask =
"lsst.obs.base.RawIngestTask"
59 """The task to use in the Ingest test."""
61 curatedCalibrationDatasetTypes =
None
62 """List or tuple of Datasets types that should be present after calling
63 writeCuratedCalibrations. If `None` writeCuratedCalibrations will
64 not be called and the test will be skipped."""
67 """The task to use to define visits from groups of exposures.
68 This is ignored if ``visits`` is `None`.
72 """A dictionary mapping visit data IDs the lists of exposure data IDs that
73 are associated with them.
74 If this is empty (but not `None`), visit definition will be run but no
75 visits will be expected (e.g. because no exposures are on-sky
80 """The name of the output run to use in tests.
86 """The fully qualified instrument class name.
91 The fully qualified instrument class name.
97 """The instrument class."""
102 """The name of the instrument.
107 The name of the instrument.
120 if os.path.exists(self.
root):
121 shutil.rmtree(self.
root, ignore_errors=
True)
125 Test that RawIngestTask ingested the expected files.
129 files : `list` [`str`], or None
130 List of files to be ingested, or None to use ``self.file``
133 datasets = butler.registry.queryDatasets(self.
outputRun, collections=...)
134 self.assertEqual(len(list(datasets)), len(self.
dataIds))
136 exposure = butler.get(self.
outputRun, dataId)
137 metadata = butler.get(
"raw.metadata", dataId)
138 self.assertEqual(metadata.toDict(), exposure.getMetadata().toDict())
143 wcs = butler.get(
"raw.wcs", dataId)
144 self.assertEqual(wcs, exposure.getWcs())
146 rawImage = butler.get(
"raw.image", dataId)
147 self.assertEqual(rawImage.getBBox(), exposure.getBBox())
152 """Check the state of the repository after ingest.
154 This is an optional hook provided for subclasses; by default it does
159 files : `list` [`str`], or None
160 List of files to be ingested, or None to use ``self.file``
164 def _createRepo(self):
165 """Use the Click `testing` module to call the butler command line api
166 to create a repository."""
167 runner = LogCliRunner()
168 result = runner.invoke(butlerCli, [
"create", self.root])
169 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
171 def _ingestRaws(self, transfer):
172 """Use the Click `testing` module to call the butler command line api
178 The external data transfer type.
180 runner = LogCliRunner()
181 result = runner.invoke(butlerCli, [
"ingest-raws", self.root, self.file,
182 "--output-run", self.outputRun,
183 "--transfer", transfer,
184 "--ingest-task", self.rawIngestTask])
185 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
187 def _registerInstrument(self):
188 """Use the Click `testing` module to call the butler command line api
189 to register the instrument."""
190 runner = LogCliRunner()
191 result = runner.invoke(butlerCli, [
"register-instrument", self.root, self.instrumentClassName])
192 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
194 def _writeCuratedCalibrations(self):
195 """Use the Click `testing` module to call the butler command line api
196 to write curated calibrations."""
197 runner = LogCliRunner()
198 result = runner.invoke(butlerCli, [
"write-curated-calibrations", self.root,
199 "--instrument", self.instrumentName,
200 "--output-run", self.outputRun])
201 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
219 except PermissionError
as err:
220 raise unittest.SkipTest(
"Skipping hard-link test because input data"
221 " is on a different filesystem.")
from err
224 """Test that files already in the directory can be added to the
229 newPath = butler.datastore.root.join(os.path.basename(self.
file))
230 os.symlink(os.path.abspath(self.
file), newPath.ospath)
235 """Re-ingesting the same data into the repository should fail.
238 with self.assertRaises(Exception):
242 """Test that we can ingest the curated calibrations"""
244 raise unittest.SkipTest(
"Class requests disabling of writeCuratedCalibrations test")
251 with self.subTest(dtype=datasetTypeName, dataId=dataId):
252 datasets = list(butler.registry.queryDatasets(datasetTypeName, collections=...,
254 self.assertGreater(len(datasets), 0, f
"Checking {datasetTypeName}")
258 self.skipTest(
"Expected visits were not defined.")
264 script.defineVisits(self.
root, config_file=
None, collections=self.
outputRun,
269 visits = butler.registry.queryDataIds([
"visit"]).expanded().toSet()
270 self.assertCountEqual(visits, self.
visits.keys())
272 camera = instr.getCamera()
273 for foundVisit, (expectedVisit, expectedExposures)
in zip(visits, self.
visits.items()):
275 foundExposures = butler.registry.queryDataIds([
"exposure"], dataId=expectedVisit
277 self.assertCountEqual(foundExposures, expectedExposures)
280 self.assertIsNotNone(foundVisit.region)
281 detectorVisitDataIds = butler.registry.queryDataIds([
"visit",
"detector"], dataId=expectedVisit
283 self.assertEqual(len(detectorVisitDataIds), len(camera))
284 for dataId
in detectorVisitDataIds:
285 self.assertTrue(foundVisit.region.contains(dataId.region))