Skip to content
Snippets Groups Projects
Commit 42838e73 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

TST: refine test

parent cf6fdcd4
No related branches found
No related tags found
2 merge requests!217TST: Make NamedTemporaryFiles Windows-compatible,!212ENH: introduce inheritance of identifiables
Pipeline #59881 failed
...@@ -650,15 +650,17 @@ class CaosDBIdentifiableAdapter(IdentifiableAdapter): ...@@ -650,15 +650,17 @@ class CaosDBIdentifiableAdapter(IdentifiableAdapter):
reg = self._get_registered_for_rt(prt) reg = self._get_registered_for_rt(prt)
if reg is not None: if reg is not None:
registered.append(reg) registered.append(reg)
# TODO we might in future want to check whether the registered identifiables are the same
if len(registered) > 1: if len(registered) > 1:
raise RuntimeError("Multiple registered identifiables found.") raise RuntimeError("Multiple registered identifiables found for a Record "
f"with the following parents: {record.parents}")
elif len(registered) == 1: elif len(registered) == 1:
return registered[0] return registered[0]
else: else:
return None return None
def _get_registered_for_rt(self, rt): def _get_registered_for_rt(self, rt: db.RecordType):
""" """
returns the registered identifiable for the given RecordType or the returns the registered identifiable for the given RecordType or the
registered identifiable of the first parent registered identifiable of the first parent
...@@ -671,8 +673,10 @@ class CaosDBIdentifiableAdapter(IdentifiableAdapter): ...@@ -671,8 +673,10 @@ class CaosDBIdentifiableAdapter(IdentifiableAdapter):
for parent in rt.parents: for parent in rt.parents:
prt = _retrieve_RecordType(id=parent.id, name=parent.name) prt = _retrieve_RecordType(id=parent.id, name=parent.name)
registered.append(self._get_registered_for_rt(prt)) registered.append(self._get_registered_for_rt(prt))
# TODO we might in future want to check whether the registered identifiables are the same
if len(registered) > 1: if len(registered) > 1:
raise RuntimeError("Multiple registered identifiables found.") raise RuntimeError("Multiple registered identifiables found for the RecordType "
f" {rt.name} with the following parents: {rt.parents}")
elif len(registered) == 1: elif len(registered) == 1:
return registered[0] return registered[0]
else: else:
......
...@@ -94,20 +94,26 @@ reference the object to be identified. You can also use the wildcard "*" as ...@@ -94,20 +94,26 @@ reference the object to be identified. You can also use the wildcard "*" as
RecordType name in the configuration which will only require, that ANY Record RecordType name in the configuration which will only require, that ANY Record
references the Record at hand. references the Record at hand.
If a Record has multiple parents, only one of them must have an registered identifiable.
Instead of defining registered identifiables for a RecordType directly, they can be
defined for their parents. I.e. if there is no registered identifiable for a RecordType,
then it will be checked whether there is a parent that has one.
If multiple recordtypes exist in the inheritance chain with a registered identifiable, then
the one that is closest to the direct parent is used. In case of multiple inheritance, only one branch must have registered identifiables.
Reasoning: Reasoning:
If there are mutliple registered identifiables that could be used to identify a given record, then only a single If there would be mutliple registered identifiables that could be used to identify a given record and only a single
one of them is used, it might be that the existence check returns a different result than if another one would one of them would used, it might be that the existence check returns a different result than if the other one would
be used. This would allow for unpredictable and inconsistent behavior (Example: one registered identifiable be used. This would allow for unpredictable and inconsistent behavior (Example: one registered identifiable
contains the name another one property date. Using the name might imply that the record does not exist and using contains the name another one property date. Using the name might imply that the record does not exist and using
the date might imply that it does. Thus, for any Record the registered identifiable must be unique). the date might imply that it does. Thus, for any Record the registered identifiable must be unique).
Anlogous Example: If you tinnk in the context, of relational databases, there can always only be a foreign key Anlogous Example: If you think in the context, of relational databases, there can always only be a foreign key
associated with one table. associated with one table.
When no registered identifiable exist for the direct parents, registered identifiables may be used Note:
from their parents. If multiple recordtypes exist in the inheritance chain with a registered identifiable, then In case of using the registered identifiable of a parent, the identifiable will be created by using the parent RecordType. Example: The
the one that is closest to the direct parent is used. In case of multiple inheritance, only one branch must have registered identifiables. registered identifiable is defined for the parent "Experiment" and the RecordType at hand "LaseExperiment" is a child of "Experiment".
Then the identifiable will construct a query that searches for "Experiment" Records (and not "LaseExperiment" Records).
Identified Records Identified Records
......
...@@ -54,6 +54,7 @@ def mock_retrieve_RecordType(id, name): ...@@ -54,6 +54,7 @@ def mock_retrieve_RecordType(id, name):
"Experiment": db.RecordType(name="Experiment"), "Experiment": db.RecordType(name="Experiment"),
"Lab": db.RecordType(name="Lab"), "Lab": db.RecordType(name="Lab"),
"Analysis": db.RecordType(name="Analysis"), "Analysis": db.RecordType(name="Analysis"),
"MetaAnalysis": db.RecordType(name="MetaAnalysis").add_parent("Analysis"),
"Measurement": db.RecordType(name="Measurement").add_parent("Experiment") "Measurement": db.RecordType(name="Measurement").add_parent("Experiment")
}[name] }[name]
...@@ -322,8 +323,8 @@ def test_get_registered_identifiable(): ...@@ -322,8 +323,8 @@ def test_get_registered_identifiable():
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
registered = ident.get_registered_identifiable(rec) registered = ident.get_registered_identifiable(rec)
# Test the same but with an additional parent that also has a registered identifiable # Test the same but with an additional parent that has a parent with a registered identifiable
rec = db.Record().add_parent(name="Measurement").add_parent(name="Experiment") rec = db.Record().add_parent(name="MetaAnalysis").add_parent(name="Experiment")
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
registered = ident.get_registered_identifiable(rec) registered = ident.get_registered_identifiable(rec)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment