diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd18cee55ad484fa54aa527b997fd757fbedde0..2587531be083ca7731089866ffd9c9b2b21ad8c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### diff --git a/src/caoscrawler/converters.py b/src/caoscrawler/converters.py index 6f76a1dafc3c41b0cc49bfcf4e3ccc8289115abe..90b894c5d6bd688d83de43f8e7b194a1dbaf7236 100644 --- a/src/caoscrawler/converters.py +++ b/src/caoscrawler/converters.py @@ -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 diff --git a/unittests/test_directories/example_substitutions/ExperimentalData/file.data b/unittests/test_directories/example_substitutions/ExperimentalData/file.data new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/unittests/test_directories/example_substitutions/substitutions_parents.yml b/unittests/test_directories/example_substitutions/substitutions_parents.yml index 107e766ccd833fab618cecfc04f13bc29abc80a6..2e208b522cc79569a7679a9df4ba7782ad1fe1c3 100644 --- a/unittests/test_directories/example_substitutions/substitutions_parents.yml +++ b/unittests/test_directories/example_substitutions/substitutions_parents.yml @@ -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" diff --git a/unittests/test_variable_substitutions.py b/unittests/test_variable_substitutions.py index 203197b7f8af51605a413ac354a0426d61c9c0cb..f6c3b6375a3111faff9d746779805ba16af260b7 100644 --- a/unittests/test_variable_substitutions.py +++ b/unittests/test_variable_substitutions.py @@ -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"