From f77415b5a5f7a21b43ef1a067a30ad4a387788ed Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Sun, 25 May 2025 18:03:20 +0200 Subject: [PATCH] handle broken links, add test --- src/pynxtools/dataconverter/validation.py | 5 ++++ tests/dataconverter/test_validation.py | 36 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/pynxtools/dataconverter/validation.py b/src/pynxtools/dataconverter/validation.py index bfaf37e1e..b5457dea7 100644 --- a/src/pynxtools/dataconverter/validation.py +++ b/src/pynxtools/dataconverter/validation.py @@ -545,6 +545,7 @@ def _follow_link( ValidationProblem.KeyToBeRemoved, "key", ) + keys_to_remove.append(key_path) del resolved_keys[key] else: resolved_keys[key] = current_keys @@ -763,6 +764,10 @@ def is_documented(key: str, tree: NexusNode) -> bool: if isinstance(mapping[key], dict) and "link" in mapping[key]: resolved_link = _follow_link({key: mapping[key]}, "") + if key not in resolved_link: + # Link is broken and key will be removed; no need to check further + return False + is_mapping = isinstance(resolved_link[key], Mapping) if node.type == "group" and not is_mapping: diff --git a/tests/dataconverter/test_validation.py b/tests/dataconverter/test_validation.py index 4769e817b..3a40c39e1 100644 --- a/tests/dataconverter/test_validation.py +++ b/tests/dataconverter/test_validation.py @@ -816,6 +816,24 @@ def listify_template(data_dict: Template): ], id="field-instead-of-named-group", ), + pytest.param( + alter_dict( + alter_dict( + TEMPLATE, + "/ENTRY[my_entry]/identified_calibration", + {"link": "/my_entry/some_group"}, + ), + "/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value", + {"link": "/my_entry/specified_group/some_field"}, + ), + [ + "Broken link at /ENTRY[my_entry]/identified_calibration to /my_entry/some_group.", + "The key /ENTRY[my_entry]/identified_calibration will not be written.", + "Broken link at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value to /my_entry/specified_group/some_field.", + "The key /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value will not be written.", + ], + id="appdef-broken-links", + ), pytest.param( alter_dict( alter_dict( @@ -850,6 +868,24 @@ def listify_template(data_dict: Template): ], id="appdef-links-with-wrong-nexus-types", ), + pytest.param( + alter_dict( + alter_dict( + TEMPLATE, + "/ENTRY[my_entry]/SAMPLE[my_sample]", + {"link": "/my_entry/some_group"}, + ), + "/ENTRY[my_entry]/SAMPLE[my_sample2]/name", + {"link": "/my_entry/specified_group/some_field223"}, + ), + [ + "Broken link at /ENTRY[my_entry]/SAMPLE[my_sample] to /my_entry/some_group.", + "The key /ENTRY[my_entry]/SAMPLE[my_sample] will not be written.", + "Broken link at /ENTRY[my_entry]/SAMPLE[my_sample2]/name to /my_entry/specified_group/some_field223.", + "The key /ENTRY[my_entry]/SAMPLE[my_sample2]/name will not be written.", + ], + id="baseclass-broken-links", + ), pytest.param( alter_dict( alter_dict(