Coverage for python/lsst/daf/butler/tests/_datasetsHelper.py : 26%

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 daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://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 <http://www.gnu.org/licenses/>.
22__all__ = ("DatasetTestHelper", "DatastoreTestHelper",
23 "BadWriteFormatter", "BadNoWriteFormatter", "MultiDetectorFormatter")
25import os
26from lsst.daf.butler import DatasetType, DatasetRef
27from lsst.daf.butler.formatters.yamlFormatter import YamlFormatter
30class DatasetTestHelper:
31 """Helper methods for Datasets"""
33 def makeDatasetRef(self, datasetTypeName, dimensions, storageClass, dataId, *, id=None, run=None,
34 conform=True):
35 """Make a DatasetType and wrap it in a DatasetRef for a test"""
36 return self._makeDatasetRef(datasetTypeName, dimensions, storageClass, dataId, id=id, run=run,
37 conform=conform)
39 def _makeDatasetRef(self, datasetTypeName, dimensions, storageClass, dataId, *, id=None, run=None,
40 conform=True):
41 # helper for makeDatasetRef
42 datasetType = DatasetType(datasetTypeName, dimensions, storageClass)
43 if id is None:
44 self.id += 1
45 id = self.id
46 if run is None:
47 run = "dummy"
48 return DatasetRef(datasetType, dataId, id=id, run=run, conform=conform)
51class DatastoreTestHelper:
52 """Helper methods for Datastore tests"""
54 def setUpDatastoreTests(self, registryClass, configClass):
55 """Shared setUp code for all Datastore tests"""
56 self.registry = registryClass()
58 # Need to keep ID for each datasetRef since we have no butler
59 # for these tests
60 self.id = 1
62 self.config = configClass(self.configFile)
64 # Some subclasses override the working root directory
65 if self.root is not None:
66 self.datastoreType.setConfigRoot(self.root, self.config, self.config.copy())
68 def makeDatastore(self, sub=None):
69 """Make a new Datastore instance of the appropriate type.
71 Parameters
72 ----------
73 sub : str, optional
74 If not None, the returned Datastore will be distinct from any
75 Datastore constructed with a different value of ``sub``. For
76 PosixDatastore, for example, the converse is also true, and ``sub``
77 is used as a subdirectory to form the new root.
79 Returns
80 -------
81 datastore : `Datastore`
82 Datastore constructed by this routine using the supplied
83 optional subdirectory if supported.
84 """
85 config = self.config.copy()
86 if sub is not None and self.root is not None:
87 self.datastoreType.setConfigRoot(os.path.join(self.root, sub), config, self.config)
88 if sub is not None:
89 # Ensure that each datastore gets its own registry
90 registryClass = type(self.registry)
91 registry = registryClass()
92 else:
93 registry = self.registry
94 return self.datastoreType(config=config, bridgeManager=registry.getDatastoreBridgeManager())
97class BadWriteFormatter(YamlFormatter):
98 """A formatter that never works but does leave a file behind."""
100 def _readFile(self, path, pytype=None):
101 raise NotImplementedError("This formatter can not read anything")
103 def _writeFile(self, inMemoryDataset):
104 """Write an empty file and then raise an exception."""
105 with open(self.fileDescriptor.location.path, "wb"):
106 pass
107 raise RuntimeError("Did not succeed in writing file")
110class BadNoWriteFormatter(BadWriteFormatter):
111 """A formatter that always fails without writing anything."""
113 def _writeFile(self, inMemoryDataset):
114 raise RuntimeError("Did not writing anything at all")
117class MultiDetectorFormatter(YamlFormatter):
119 def _writeFile(self, inMemoryDataset):
120 raise NotImplementedError("Can not write")
122 def _fromBytes(self, serializedDataset, pytype=None):
123 data = super()._fromBytes(serializedDataset)
124 if self.dataId is None:
125 raise RuntimeError("This formatter requires a dataId")
126 if "detector" not in self.dataId:
127 raise RuntimeError("This formatter requires detector to be present in dataId")
128 key = f"detector{self.dataId['detector']}"
129 if key in data:
130 return pytype(data[key])
131 raise RuntimeError(f"Could not find '{key}' in data file")