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

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 contextlib import contextmanager
26from typing import Any, Iterator
28from lsst.daf.butler import DimensionUniverse, ddl, FakeDatasetRef
31class DummyRegistry:
32 """Dummy Registry, for Datastore test purposes.
33 """
34 def __init__(self):
35 self._counter = 0
36 self._entries = {}
37 self._trashedEntries = {}
38 self._externalTableRows = {}
39 self._externalTableSpecs = {}
40 self.dimensions = DimensionUniverse()
42 def registerOpaqueTable(self, name: str, spec: ddl.TableSpec):
43 self._externalTableSpecs[name] = spec
44 self._externalTableRows[name] = []
46 def insertOpaqueData(self, name: str, *data: dict):
47 spec = self._externalTableSpecs[name]
48 uniqueConstraints = list(spec.unique)
49 uniqueConstraints.append(tuple(field.name for field in spec.fields if field.primaryKey))
50 for d in data:
51 for constraint in uniqueConstraints:
52 matching = list(self.fetchOpaqueData(name, **{k: d[k] for k in constraint}))
53 if len(matching) != 0:
54 raise RuntimeError(f"Unique constraint {constraint} violation in external table {name}.")
55 self._externalTableRows[name].append(d)
57 def fetchOpaqueData(self, name: str, **where: Any) -> Iterator[dict]:
58 for d in self._externalTableRows[name]:
59 if all(d[k] == v for k, v in where.items()):
60 yield d
62 def deleteOpaqueData(self, name: str, **where: Any):
63 kept = []
64 for d in self._externalTableRows[name]:
65 if not all(d[k] == v for k, v in where.items()):
66 kept.append(d)
67 self._externalTableRows[name] = kept
69 def insertDatasetLocations(self, datastoreName, refs):
70 # Only set ID if ID is 0 or None
71 for ref in refs:
72 incrementCounter = True
73 if ref.id is None or ref.id == 0:
74 ref._id = self._counter
75 incrementCounter = False
76 if ref.id not in self._entries:
77 self._entries[ref.id] = set()
78 self._entries[ref.id].add(datastoreName)
79 if incrementCounter:
80 self._counter += 1
82 def moveDatasetLocationToTrash(self, datastoreName, refs):
83 for ref in refs:
84 if ref.id not in self._trashedEntries:
85 self._trashedEntries[ref.id] = set()
86 self._trashedEntries[ref.id].add(datastoreName)
87 self._entries[ref.id].remove(datastoreName)
89 def getTrashedDatasets(self, datastoreName):
90 refs = set()
91 for ref, stores in self._trashedEntries.items():
92 if datastoreName in stores:
93 refs.add(FakeDatasetRef(ref))
94 return refs
96 def emptyDatasetLocationsTrash(self, datastoreName, refs):
97 for ref in refs:
98 self._trashedEntries[ref.id].remove(datastoreName)
100 def getDatasetLocations(self, ref):
101 return self._entries[ref.id].copy()
103 def removeDatasetLocation(self, datastoreName, refs):
104 for ref in refs:
105 self._entries[ref.id].remove(datastoreName)
107 def makeDatabaseDict(self, table, key, value):
108 return dict()
110 @contextmanager
111 def transaction(self):
112 yield