Coverage for python/lsst/daf/butler/tests/_dummyRegistry.py : 32%

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__ = ("DummyRegistry", )
25from typing import Any, Iterable, Iterator, Optional, Type
27from lsst.daf.butler import ddl, DatasetRef, DimensionUniverse
28from lsst.daf.butler.registry.interfaces import (
29 Database,
30 DatasetRecordStorageManager,
31 DatastoreRegistryBridge,
32 DatastoreRegistryBridgeManager,
33 OpaqueTableStorageManager,
34 OpaqueTableStorage,
35 StaticTablesContext,
36 VersionTuple
37)
38from lsst.daf.butler.registry.bridge.ephemeral import EphemeralDatastoreRegistryBridge
41class DummyOpaqueTableStorage(OpaqueTableStorage):
43 def __init__(self, name: str, spec: ddl.TableSpec):
44 super().__init__(name=name)
45 self._rows = []
46 self._spec = spec
48 def insert(self, *data: dict):
49 # Docstring inherited from OpaqueTableStorage.
50 uniqueConstraints = list(self._spec.unique)
51 uniqueConstraints.append(tuple(field.name for field in self._spec.fields if field.primaryKey))
52 for d in data:
53 for constraint in uniqueConstraints:
54 matching = list(self.fetch(**{k: d[k] for k in constraint}))
55 if len(matching) != 0:
56 raise RuntimeError(f"Unique constraint {constraint} violation "
57 "in external table {self.name}.")
58 self._rows.append(d)
60 def fetch(self, **where: Any) -> Iterator[dict]:
61 # Docstring inherited from OpaqueTableStorage.
62 for d in self._rows:
63 if all(d[k] == v for k, v in where.items()):
64 yield d
66 def delete(self, columns: Iterable[str], *rows: dict):
67 # Docstring inherited from OpaqueTableStorage.
68 kept_rows = []
69 for table_row in self._rows:
70 for where_row in rows:
71 if all(table_row[k] == v for k, v in where_row.items()):
72 break
73 else:
74 kept_rows.append(table_row)
75 self._rows = kept_rows
78class DummyOpaqueTableStorageManager(OpaqueTableStorageManager):
80 def __init__(self):
81 self._storages = {}
83 @classmethod
84 def initialize(cls, db: Database, context: StaticTablesContext) -> OpaqueTableStorageManager:
85 # Docstring inherited from OpaqueTableStorageManager.
86 # Not used, but needed to satisfy ABC requirement.
87 return cls()
89 def get(self, name: str) -> Optional[OpaqueTableStorage]:
90 # Docstring inherited from OpaqueTableStorageManager.
91 return self._storage.get(name)
93 def register(self, name: str, spec: ddl.TableSpec) -> OpaqueTableStorage:
94 # Docstring inherited from OpaqueTableStorageManager.
95 return self._storages.setdefault(name, DummyOpaqueTableStorage(name, spec))
97 @classmethod
98 def currentVersion(cls) -> Optional[VersionTuple]:
99 # Docstring inherited from VersionedExtension.
100 return None
102 def schemaDigest(self) -> Optional[str]:
103 # Docstring inherited from VersionedExtension.
104 return None
107class DummyDatastoreRegistryBridgeManager(DatastoreRegistryBridgeManager):
109 def __init__(self, opaque: OpaqueTableStorageManager, universe: DimensionUniverse):
110 super().__init__(opaque=opaque, universe=universe)
111 self._bridges = {}
113 @classmethod
114 def initialize(cls, db: Database, context: StaticTablesContext, *,
115 opaque: OpaqueTableStorageManager,
116 datasets: Type[DatasetRecordStorageManager],
117 universe: DimensionUniverse,
118 ) -> DatastoreRegistryBridgeManager:
119 # Docstring inherited from DatastoreRegistryBridgeManager
120 # Not used, but needed to satisfy ABC requirement.
121 return cls(opaque=opaque, universe=universe)
123 def refresh(self):
124 # Docstring inherited from DatastoreRegistryBridgeManager
125 pass
127 def register(self, name: str, *, ephemeral: bool = False) -> DatastoreRegistryBridge:
128 # Docstring inherited from DatastoreRegistryBridgeManager
129 return self._bridges.setdefault(name, EphemeralDatastoreRegistryBridge(name))
131 def findDatastores(self, ref: DatasetRef) -> Iterable[str]:
132 # Docstring inherited from DatastoreRegistryBridgeManager
133 for name, bridge in self._bridges.items():
134 if ref in bridge:
135 yield name
137 @classmethod
138 def currentVersion(cls) -> Optional[VersionTuple]:
139 # Docstring inherited from VersionedExtension.
140 return None
142 def schemaDigest(self) -> Optional[str]:
143 # Docstring inherited from VersionedExtension.
144 return None
147class DummyRegistry:
148 """Dummy Registry, for Datastore test purposes.
149 """
150 def __init__(self):
151 self._opaque = DummyOpaqueTableStorageManager()
152 self.dimensions = DimensionUniverse()
153 self._datastoreBridges = DummyDatastoreRegistryBridgeManager(self._opaque, self.dimensions)
155 def getDatastoreBridgeManager(self):
156 return self._datastoreBridges