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

Merge branch 'f-empty-parents' into 'dev'

Create fallback parent only when Record object is created

See merge request !38
parents b6407785 3c815a85
No related branches found
No related tags found
2 merge requests!71REL: RElease v0.2.0,!38Create fallback parent only when Record object is created
Pipeline #30289 passed
......@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ###
- `_AbstractDictElementConverter` uses `re.DOTALL` for `match_value`
- The "fallback" parent, the name of the element in the cfood, is only used
when the object is created and only if there are no parents given.
### Deprecated ###
......
......@@ -205,6 +205,10 @@ def create_records(values: GeneralStore,
# additionally add the new record to the general store:
values[name] = c_record
# add the "fallback" parent only for Records, not for Files:
if (role == "Record" and "parents" not in record):
c_record.add_parent(name)
c_record = records[name]
for key, value in record.items():
......@@ -252,12 +256,6 @@ def create_records(values: GeneralStore,
var_replaced_parent = replace_variables(parent, values)
if not has_parent(c_record, var_replaced_parent):
c_record.add_parent(var_replaced_parent)
else:
# add the "fallback" parent only for Records, not for Files:
if role == "Record":
# if not has_parent(c_record, name):
if len(c_record.parents) == 0:
c_record.add_parent(name)
return keys_modified
......
......@@ -5,6 +5,10 @@ ExperimentalData: # name of the converter
records:
Project:
name: project
RecordWithoutParents:
parents: []
RecordThatGetsParentsLater:
parents: []
subtree:
File: # name of the converter
type: SimpleFile
......@@ -22,4 +26,13 @@ ExperimentalData: # name of the converter
Project:
Experiments: +$Experiment
dates: +20$year-$month-$day
RecordThatGetsParentsLater:
parents:
- Month_$month # This adds a special parent as record type
OtherFile:
type: SimpleFile
match: ".*\\.data"
records:
RecordThatGetsParentsLater:
someId: "23"
......@@ -83,3 +83,18 @@ def test_substitutions_parents(crawler_2):
assert len(parents) == 2
assert parents[0].name == "Experiment"
assert parents[1].name == "Month_05"
def test_empty_parents(crawler_2):
# This is a test for:
# https://gitlab.com/caosdb/caosdb-crawler/-/issues/8
subd = crawler_2.debug_tree[dircheckstr(
"File", "ExperimentalData", "220512_data.dat")]
parents = subd[1]["RecordWithoutParents"].get_parents()
assert len(parents) == 0
parents = subd[1]["RecordThatGetsParentsLater"].get_parents()
assert len(parents) == 1
assert parents[0].name == "Month_05"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment