Skip to content

Commit

Permalink
Adds test for completely removed optional group and fixes test with r…
Browse files Browse the repository at this point in the history
…eq-group-in-opt-parent
  • Loading branch information
sherjeelshabih committed Sep 15, 2023
1 parent e3a55d8 commit 6e1c585
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
9 changes: 5 additions & 4 deletions pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "<<NOT_FOUND>>" and does_group_exist(opt_parent, data):
if not does_group_exist(renamed_path, data):
if opt_parent != "<<NOT_FOUND>>":
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 "
Expand Down
25 changes: 21 additions & 4 deletions tests/dataconverter/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"""
Expand All @@ -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)


Expand Down

0 comments on commit 6e1c585

Please sign in to comment.