Skip to content

Commit

Permalink
fix test_validation.py issues and extra matching case in validate_dic…
Browse files Browse the repository at this point in the history
…t_against()
  • Loading branch information
GinzburgLev committed Jan 29, 2025
1 parent ec74a7f commit 0d1cbd4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
39 changes: 28 additions & 11 deletions src/pynxtools/dataconverter/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,18 +586,28 @@ def check_attributes_of_nonexisting_field(
# the parent is a field
attribute_parent_checked = True
break
elif key_iterating[last_index + 1] != "@":
elif (key_iterating[last_index] == "/") and (
key_iterating[last_index + 1] != "@"
):
# the parent is a group
attribute_parent_checked = True
break
if not attribute_parent_checked:
type_of_parent_from_tree = check_type_with_tree(
node, key[0:last_index]
)
# last check: we can potentially have a parent which is a group which
# has only attributes as children. In this case we should still write
# the attributes as usual
if type_of_parent_from_tree != "group":
# last two checks: 1. The parent is a group which has only attributes
# as children. We check for that in the tree built from
# application definition. In this case we should still write
# the attributes as usual. 2. The parent can not be found in the tree
# and therefore is not a part of the schema. The parent might
# have been intended to be a group as in (1) or a field that is
# missing by a mistake. As we have no way to distinguish,
# we have to assume the former.
if not (
type_of_parent_from_tree == "group"
or type_of_parent_from_tree is None
):
keys_to_remove.append(key)
collector.collect_and_log(
key[0:last_index],
Expand Down Expand Up @@ -636,19 +646,26 @@ def check_type_with_tree(
next_child_name_index = (
len(path) - 1
) # -1 because we count from element #1, not #0
next_child_class = split_class_and_name_of(path[1 : next_child_name_index + 1])[
0
]
if next_child_class is not None:
next_child_class, next_child_name = split_class_and_name_of(
path[1 : next_child_name_index + 1]
)
if (next_child_class is not None) or (next_child_name is not None):
output = None
for child in node.children:
# regex removes everything which is not the class of the node
# regexs to separarte the class and the name from full name of the child
child_class_from_node = re.sub(
r"(\@.*)*(\[.*?\])*(\(.*?\))*([a-z]\_)*(\_[a-z])*[a-z]*\s*",
"",
child.__str__(),
)
if child_class_from_node == next_child_class:
child_name_from_node = re.sub(
r"(\@.*)*(\(.*?\))*(.*\[)*(\].*)*\s*",
"",
child.__str__(),
)
if (child_class_from_node == next_child_class) or (
child_name_from_node == next_child_name
):
output_new = check_type_with_tree(
child, path[next_child_name_index + 1 :]
)
Expand Down
36 changes: 19 additions & 17 deletions tests/dataconverter/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,28 @@ def alter_dict(new_values: Dict[str, Any], data_dict: Dict[str, Any]) -> Dict[st
)
def test_valid_data_dict(caplog, data_dict):
with caplog.at_level(logging.WARNING):
validate_dict_against("NXtest", data_dict)[0]
assert validate_dict_against("NXtest", data_dict)[0]
assert caplog.text == ""


# @pytest.mark.parametrize(
# "data_dict, error_message",
# [
# pytest.param(
# remove_from_dict(
# "/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value", get_data_dict()
# ),
# "There were attributes set for the field /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value, but the field does not exist.",
# id="removed-optional-value-with-attribute-remaining",
# ),
# ],
# )
# def test_data_dict_attr_with_no_field(caplog, data_dict, error_message):
# with caplog.at_level(logging.WARNING):
# assert not validate_dict_against("NXtest", data_dict)[0]
# assert error_message in caplog.text
@pytest.mark.parametrize(
"data_dict, error_message",
[
pytest.param(
remove_from_dict(
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value", get_data_dict()
),
"There were attributes set for the field /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value, but the field does not exist.",
id="removed-optional-value-with-attribute-remaining",
),
],
)
def test_data_dict_attr_with_no_field(caplog, data_dict, error_message):
with caplog.at_level(logging.WARNING):
result = validate_dict_against("NXtest", data_dict)
print(result)
assert not result[0]
assert error_message in caplog.text


@pytest.mark.parametrize(
Expand Down

0 comments on commit 0d1cbd4

Please sign in to comment.