Coverage for python/lsst/daf/butler/tests/_dummyRegistry.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
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)
37from lsst.daf.butler.registry.bridge.ephemeral import EphemeralDatastoreRegistryBridge
40class DummyOpaqueTableStorage(OpaqueTableStorage):
42 def __init__(self, name: str, spec: ddl.TableSpec):
43 super().__init__(name=name)
44 self._rows = []
45 self._spec = spec
47 def insert(self, *data: dict):
48 # Docstring inherited from OpaqueTableStorage.
49 uniqueConstraints = list(self._spec.unique)
50 uniqueConstraints.append(tuple(field.name for field in self._spec.fields if field.primaryKey))
51 for d in data:
52 for constraint in uniqueConstraints:
53 matching = list(self.fetch(**{k: d[k] for k in constraint}))
54 if len(matching) != 0:
55 raise RuntimeError(f"Unique constraint {constraint} violation "
56 "in external table {self.name}.")
57 self._rows.append(d)
59 def fetch(self, **where: Any) -> Iterator[dict]:
60 # Docstring inherited from OpaqueTableStorage.
61 for d in self._rows:
62 if all(d[k] == v for k, v in where.items()):
63 yield d
65 def delete(self, **where: Any):
66 # Docstring inherited from OpaqueTableStorage.
67 kept = []
68 for d in self._rows:
69 if not all(d[k] == v for k, v in where.items()):
70 kept.append(d)
71 self._rows = kept
74class DummyOpaqueTableStorageManager(OpaqueTableStorageManager):
76 def __init__(self):
77 self._storages = {}
79 @classmethod
80 def initialize(cls, db: Database, context: StaticTablesContext) -> OpaqueTableStorageManager:
81 # Docstring inherited from OpaqueTableStorageManager.
82 # Not used, but needed to satisfy ABC requirement.
83 return cls()
85 def get(self, name: str) -> Optional[OpaqueTableStorage]:
86 # Docstring inherited from OpaqueTableStorageManager.
87 return self._storage.get(name)
89 def register(self, name: str, spec: ddl.TableSpec) -> OpaqueTableStorage:
90 # Docstring inherited from OpaqueTableStorageManager.
91 return self._storages.setdefault(name, DummyOpaqueTableStorage(name, spec))
94class DummyDatastoreRegistryBridgeManager(DatastoreRegistryBridgeManager):
96 def __init__(self, opaque: OpaqueTableStorageManager, universe: DimensionUniverse):
97 super().__init__(opaque=opaque, universe=universe)
98 self._bridges = {}
100 @classmethod
101 def initialize(cls, db: Database, context: StaticTablesContext, *,
102 opaque: OpaqueTableStorageManager,
103 datasets: Type[DatasetRecordStorageManager],
104 universe: DimensionUniverse,
105 ) -> DatastoreRegistryBridgeManager:
106 # Docstring inherited from DatastoreRegistryBridgeManager
107 # Not used, but needed to satisfy ABC requirement.
108 return cls(opaque=opaque, universe=universe)
110 def refresh(self):
111 # Docstring inherited from DatastoreRegistryBridgeManager
112 pass
114 def register(self, name: str, *, ephemeral: bool = False) -> DatastoreRegistryBridge:
115 # Docstring inherited from DatastoreRegistryBridgeManager
116 return self._bridges.setdefault(name, EphemeralDatastoreRegistryBridge(name))
118 def findDatastores(self, ref: DatasetRef) -> Iterable[str]:
119 # Docstring inherited from DatastoreRegistryBridgeManager
120 for name, bridge in self._bridges.items():
121 if ref in bridge:
122 yield name
125class DummyRegistry:
126 """Dummy Registry, for Datastore test purposes.
127 """
128 def __init__(self):
129 self._opaque = DummyOpaqueTableStorageManager()
130 self.dimensions = DimensionUniverse()
131 self._datastoreBridges = DummyDatastoreRegistryBridgeManager(self._opaque, self.dimensions)
133 def getDatastoreBridgeManager(self):
134 return self._datastoreBridges