Skip to content

Commit

Permalink
(HP-1827) use regex for checking array indexing in prop names
Browse files Browse the repository at this point in the history
  • Loading branch information
george42-ctds committed Jan 8, 2025
1 parent cc771ed commit 330463a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
11 changes: 8 additions & 3 deletions heal/vlmd/extract/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ def unflatten_from_jsonpath(field):
nested_names_length = len(nested_names)
for i, prop_name in enumerate(nested_names):
if "[" in prop_name:
array_name, array_index = prop_name.split("[")
g = re.match(r"(.+)\[(\d+)\]$", prop_name)
if not g:
raise jsonschema.ValidationError(
f"Incorrect array indexing in name {prop_name}"
)
try:
array_index = int(array_index[:-1])
array_name = g[1]
array_index = int(g[2])
except Exception as e:
raise jsonschema.ValidationError(str(e))
if array_name not in prop_json:
Expand Down Expand Up @@ -227,7 +232,7 @@ def join_dict_items(dictionary: dict, sep_key_val="=", sep_items="|"):


# Working with schemas
def flatten_properties(properties, parent_key="", sep=".", item_sep="\[\d+\]"):
def flatten_properties(properties, parent_key="", sep=".", item_sep=r"\[\d+\]"):
"""
flatten schema properties
"""
Expand Down
51 changes: 39 additions & 12 deletions tests/test_vlmd_extract_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,46 @@ def test_unflatten_from_jsonpath():
), "Problem with converting input dictionary to output dictionary"


def test_unflatten_from_jsonpath_invalid_input():
input = {
"module": "Testing",
"constraints.enum": "1|2|3|4",
"standardsMappings[1].item.url": "http//:helloitem1",
"standardsMappings['some_string'].item.url": "http//:helloitem0",
"standardsMappings[2].item.url": "http//:helloitem2",
}

@pytest.mark.parametrize(
"invalid_input, invalid_name",
[
(
{
"module": "Testing",
"constraints.enum": "1|2|3|4",
"standardsMappings[1].item.url": "http//:helloitem1",
"standardsMappings['some_string'].item.url": "http//:helloitem0",
"standardsMappings[2].item.url": "http//:helloitem2",
},
"standardsMappings['some_string']",
),
(
{
"module": "Testing",
"constraints.enum": "1|2|3|4",
"[1].item.url": "http//:helloitem1",
"standardsMappings[0].item.url": "http//:helloitem0",
"standardsMappings[2].item.url": "http//:helloitem2",
},
"[1]",
),
(
{
"module": "Testing",
"constraints.enum": "1|2|3|4",
"standardsMappings[1).item.url": "http//:helloitem1",
"standardsMappings[0].item.url": "http//:helloitem0",
"standardsMappings[2].item.url": "http//:helloitem2",
},
"standardsMappings[1)",
),
],
)
def test_unflatten_from_jsonpath_invalid_input(invalid_input, invalid_name):
with pytest.raises(ValidationError) as e:
unflatten_from_jsonpath(input)
expected_message = "invalid literal for int() with base 10:"
assert expected_message in str(e.value)
unflatten_from_jsonpath(invalid_input)
expected_error_message = f"Incorrect array indexing in name {invalid_name}"
assert expected_error_message in str(e.value)


def test_embed_data_dictionary_props():
Expand Down

0 comments on commit 330463a

Please sign in to comment.