From 6e1c58503c374c9be58526488fc4cbc84af4d767 Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Fri, 15 Sep 2023 10:53:36 +0200 Subject: [PATCH] Adds test for completely removed optional group and fixes test with req-group-in-opt-parent --- pynxtools/dataconverter/helpers.py | 9 +++++---- tests/dataconverter/test_helpers.py | 25 +++++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/pynxtools/dataconverter/helpers.py b/pynxtools/dataconverter/helpers.py index 2b0e1192f..5fa18073c 100644 --- a/pynxtools/dataconverter/helpers.py +++ b/pynxtools/dataconverter/helpers.py @@ -438,12 +438,13 @@ def ensure_all_required_fields_exist(template, data, nxdl_root): renamed_path = path if renamed_path is None else renamed_path if path in template["lone_groups"]: opt_parent = check_for_optional_parent(path, nxdl_root) - if not does_group_exist(renamed_path, data): - raise ValueError(f"The required group, {path}, hasn't been supplied.") - if opt_parent != "<>" and does_group_exist(opt_parent, data): - if not does_group_exist(renamed_path, data): + if opt_parent != "<>": + if does_group_exist(opt_parent, data) and not does_group_exist(renamed_path, data): raise ValueError(f"The required group, {path}, hasn't been supplied" f" while its optional parent, {path}, is supplied.") + continue + if not does_group_exist(renamed_path, data): + raise ValueError(f"The required group, {path}, hasn't been supplied.") continue if not is_path_in_data_dict or data[renamed_path] is None: raise ValueError(f"The data entry corresponding to {path} is required " diff --git a/tests/dataconverter/test_helpers.py b/tests/dataconverter/test_helpers.py index f0fe29af8..47663b69a 100644 --- a/tests/dataconverter/test_helpers.py +++ b/tests/dataconverter/test_helpers.py @@ -27,6 +27,16 @@ from pynxtools.dataconverter.template import Template +def remove_optional_parent(data_dict: Template): + """Completely removes the optional group from the test Template.""" + internal_dict = Template(data_dict) + del internal_dict["/ENTRY[my_entry]/optional_parent/required_child"] + del internal_dict["/ENTRY[my_entry]/optional_parent/optional_child"] + del internal_dict["/ENTRY[my_entry]/optional_parent/req_group_in_opt_group/DATA[data]"] + + return internal_dict + + def alter_dict(data_dict: Template, key: str, value: object): """Helper function to alter a single entry in dict for parametrize.""" if data_dict is not None: @@ -264,10 +274,16 @@ def fixture_filled_test_data(template, tmp_path): "/ENTRY[my_entry]/optional_parent/req_group_in_opt_group/DATA[data]", "required" ), - ("The required group, /ENTRY[entry]/optional_parent/req_group_in_opt_group, hasn't" - " been supplied."), + ("The required group, /ENTRY[entry]/optional_parent/req_group_in_opt_group, hasn't been " + "supplied while its optional parent, /ENTRY[entry]/optional_parent/" + "req_group_in_opt_group, is supplied."), id="req-group-in-opt-parent-removed" ), + pytest.param( + remove_optional_parent(TEMPLATE), + (""), + id="opt-group-completely-removed" + ), ]) def test_validate_data_dict(data_dict, error_message, template, nxdl_root, request): """Unit test for the data validation routine""" @@ -279,12 +295,13 @@ def test_validate_data_dict(data_dict, error_message, template, nxdl_root, reque "no-child-provided-optional-parent", "int-instead-of-chars", "link-dict-instead-of-bool", - "allow-required-and-empty-group"): + "allow-required-and-empty-group", + "opt-group-completely-removed"): helpers.validate_data_dict(template, data_dict, nxdl_root) else: with pytest.raises(Exception) as execinfo: helpers.validate_data_dict(template, data_dict, nxdl_root) - + print(execinfo.value) assert (error_message) == str(execinfo.value)