Skip to content
Snippets Groups Projects
Commit c7885044 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-wildcard' into dev

parents e293f579 d4d6b99e
No related branches found
No related tags found
2 merge requests!160STY: styling,!146ENH: allow wildcard is_referenced_by
Pipeline #47142 passed
...@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### ### Added ###
* 'transform' sections can be added to a CFood to apply functions to values stored in variables. * 'transform' sections can be added to a CFood to apply functions to values stored in variables.
* default transform functions: submatch, split and replace. * default transform functions: submatch, split and replace.
* `*` can now be used as a wildcard in the identifiables parameter file to denote
that any Record may reference the identified one.
### Changed ### ### Changed ###
- If the `parents` key is used in a cfood at a lower level for a Record that - If the `parents` key is used in a cfood at a lower level for a Record that
......
...@@ -219,18 +219,25 @@ identifiabel, identifiable and identified record) for a Record. ...@@ -219,18 +219,25 @@ identifiabel, identifiable and identified record) for a Record.
# separate class too # separate class too
if prop.name.lower() == "is_referenced_by": if prop.name.lower() == "is_referenced_by":
for givenrt in prop.value: for givenrt in prop.value:
rt_and_children = get_children_of_rt(givenrt)
found = False found = False
for rtname in rt_and_children: if givenrt == "*":
if (id(record) in referencing_entities if id(record) not in referencing_entities:
and rtname in referencing_entities[id(record)]): continue
identifiable_backrefs.extend( for rt, rec in referencing_entities[id(record)].items():
referencing_entities[id(record)][rtname]) identifiable_backrefs.extend(rec)
found = True found = True
else:
rt_and_children = get_children_of_rt(givenrt)
for rtname in rt_and_children:
if (id(record) in referencing_entities
and (rtname in referencing_entities[id(record)])):
identifiable_backrefs.extend(
referencing_entities[id(record)][rtname])
found = True
if not found: if not found:
# TODO: is this the appropriate error? # TODO: is this the appropriate error?
raise NotImplementedError( raise NotImplementedError(
f"The following record is missing an identifying property:" f"The following record is missing an identifying property:\n"
f"RECORD\n{record}\nIdentifying PROPERTY\n{prop.name}" f"RECORD\n{record}\nIdentifying PROPERTY\n{prop.name}"
) )
continue continue
......
...@@ -85,7 +85,9 @@ we can check whether a Record with the parent "Project" is referencing the "Expe ...@@ -85,7 +85,9 @@ we can check whether a Record with the parent "Project" is referencing the "Expe
Record. If that is the case, this reference is part of the identifiable for the "Experiment" Record. If that is the case, this reference is part of the identifiable for the "Experiment"
Record. Note, that if there are multiple Records with the appropriate parent (e.g. Record. Note, that if there are multiple Records with the appropriate parent (e.g.
multiple "Project" Records in the above example) it will be required that all of them multiple "Project" Records in the above example) it will be required that all of them
reference the object to be identified. 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
references the Record at hand.
Identified Records Identified Records
......
...@@ -133,6 +133,22 @@ def test_non_default_name(): ...@@ -133,6 +133,22 @@ def test_non_default_name():
assert identifiable.name is None assert identifiable.name is None
def test_wildcard_ref():
ident = CaosDBIdentifiableAdapter()
ident.register_identifiable(
"Person", db.RecordType()
.add_parent(name="Person")
.add_property(name="is_referenced_by", value=["*"]))
rec = (db.Record(name="don't touch it").add_parent("Person")
.add_property(name="last_name", value='Tom'))
identifiable = ident.get_identifiable(rec,
referencing_entities={
id(rec):
{'A': [db.Record(id=1).add_parent("A")]}}
)
assert identifiable.backrefs[0].id == 1
def test_convert_value(): def test_convert_value():
# test that string representation of objects stay unchanged. No stripping or so. # test that string representation of objects stay unchanged. No stripping or so.
class A(): class A():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment