Skip to content

Commit fab147d

Browse files
authored
Merge pull request #645 from FAIRmat-NFDI/644-bug-validation-fails-with-key-error-if-link-cannot-be-resolved
handle broken links, add test
2 parents 8bf3aab + f77415b commit fab147d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/pynxtools/dataconverter/validation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ def _follow_link(
545545
ValidationProblem.KeyToBeRemoved,
546546
"key",
547547
)
548+
keys_to_remove.append(key_path)
548549
del resolved_keys[key]
549550
else:
550551
resolved_keys[key] = current_keys
@@ -763,6 +764,10 @@ def is_documented(key: str, tree: NexusNode) -> bool:
763764
if isinstance(mapping[key], dict) and "link" in mapping[key]:
764765
resolved_link = _follow_link({key: mapping[key]}, "")
765766

767+
if key not in resolved_link:
768+
# Link is broken and key will be removed; no need to check further
769+
return False
770+
766771
is_mapping = isinstance(resolved_link[key], Mapping)
767772

768773
if node.type == "group" and not is_mapping:

tests/dataconverter/test_validation.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,24 @@ def listify_template(data_dict: Template):
816816
],
817817
id="field-instead-of-named-group",
818818
),
819+
pytest.param(
820+
alter_dict(
821+
alter_dict(
822+
TEMPLATE,
823+
"/ENTRY[my_entry]/identified_calibration",
824+
{"link": "/my_entry/some_group"},
825+
),
826+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
827+
{"link": "/my_entry/specified_group/some_field"},
828+
),
829+
[
830+
"Broken link at /ENTRY[my_entry]/identified_calibration to /my_entry/some_group.",
831+
"The key /ENTRY[my_entry]/identified_calibration will not be written.",
832+
"Broken link at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value to /my_entry/specified_group/some_field.",
833+
"The key /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value will not be written.",
834+
],
835+
id="appdef-broken-links",
836+
),
819837
pytest.param(
820838
alter_dict(
821839
alter_dict(
@@ -850,6 +868,24 @@ def listify_template(data_dict: Template):
850868
],
851869
id="appdef-links-with-wrong-nexus-types",
852870
),
871+
pytest.param(
872+
alter_dict(
873+
alter_dict(
874+
TEMPLATE,
875+
"/ENTRY[my_entry]/SAMPLE[my_sample]",
876+
{"link": "/my_entry/some_group"},
877+
),
878+
"/ENTRY[my_entry]/SAMPLE[my_sample2]/name",
879+
{"link": "/my_entry/specified_group/some_field223"},
880+
),
881+
[
882+
"Broken link at /ENTRY[my_entry]/SAMPLE[my_sample] to /my_entry/some_group.",
883+
"The key /ENTRY[my_entry]/SAMPLE[my_sample] will not be written.",
884+
"Broken link at /ENTRY[my_entry]/SAMPLE[my_sample2]/name to /my_entry/specified_group/some_field223.",
885+
"The key /ENTRY[my_entry]/SAMPLE[my_sample2]/name will not be written.",
886+
],
887+
id="baseclass-broken-links",
888+
),
853889
pytest.param(
854890
alter_dict(
855891
alter_dict(

0 commit comments

Comments
 (0)