Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Let's have a look at a couple of examples:
```
We will define a JMESPath expression for the values we want to test and extract from the reference and comparison objects.
```python
>>> my_jmspath = "global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]"
>>> my_jmspath = "global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]"
>>> reference_value = extract_data_from_json(reference_data, my_jmspath)
>>> reference_value
[{'10.1.0.0': {'accepted_prefixes': 900,
Expand Down
2 changes: 1 addition & 1 deletion jdiff/utils/jmespath_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def jmespath_refkey_parser(path: str):
splitted_jmespath[number] = regex_match_anchor.group().replace("$", "")

if regex_match_anchor and not element.startswith("[") and not element.endswith("]"):
splitted_jmespath = splitted_jmespath[: number + 1]
splitted_jmespath = splitted_jmespath[:number]
Copy link
Collaborator Author

@pszulczewski pszulczewski Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the change to fix it.
Other changes are just $$ moved right and tests have been reordered to follow the pattern:
-> define test cases params
-> wrap above test cases into a list
-> test function for the wrapped list with test cases.
plus one additional test case keyref_parser_case_5


return ".".join(splitted_jmespath)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_diff_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Diff test case 1.
global_peers = (
"napalm_getter_peer_state_change",
"global.$peers$.*.[is_enabled,is_up]",
"global.peers.$*$.[is_enabled,is_up]",
[],
{
"10.1.0.0": {
Expand Down Expand Up @@ -134,7 +134,7 @@
# Diff test case 9. Value in multiple nested lists.
multi_nested_list = (
"exact_match_nested",
"global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes]",
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes]",
[],
{
"10.1.0.0": {"accepted_prefixes": {"new_value": -1, "old_value": -9}},
Expand Down
57 changes: 33 additions & 24 deletions tests/test_jmespath_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@
"result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]",
)


value_parser_tests = [
value_parser_case_1,
value_parser_case_2,
value_parser_case_3,
value_parser_case_4,
]


@pytest.mark.parametrize("path, expected_output", value_parser_tests)
def test_value_parser(path, expected_output):
output = jmespath_value_parser(path)
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)


keyref_parser_case_1 = (
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]",
"result[0].vrfs.default.peerList[*].peerAddress",
Expand All @@ -40,31 +55,23 @@
)
keyref_parser_case_4 = (
"result[0].$vrfs$.default.peerList[*].[peerAddress,prefixesReceived]",
"result[0].vrfs",
"result[0]",
)
keyref_parser_case_5 = (
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
"global.peers",
)


value_parser_tests = [
value_parser_case_1,
value_parser_case_2,
value_parser_case_3,
value_parser_case_4,
]

keyref_parser_tests = [
keyref_parser_case_1,
keyref_parser_case_2,
keyref_parser_case_3,
keyref_parser_case_4,
keyref_parser_case_5,
]


@pytest.mark.parametrize("path, expected_output", value_parser_tests)
def test_value_parser(path, expected_output):
output = jmespath_value_parser(path)
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)


@pytest.mark.parametrize("path, expected_output", keyref_parser_tests)
def test_keyref_parser(path, expected_output):
output = jmespath_refkey_parser(path)
Expand All @@ -77,28 +84,30 @@ def test_keyref_parser(path, expected_output):
[{"10.1.0.0": {"is_enabled": False, "is_up": False}}, {"10.2.0.0": {"is_enabled": True, "is_up": True}}],
)

keys_association_case_1 = (
"global.peers.*.[is_enabled,is_up]",
[[True, False], [True, False]],
[{"is_enabled": True, "is_up": False}, {"is_enabled": True, "is_up": False}],
)


keys_zipper_tests = [
keys_zipper_case_1,
]

keys_association_test = [
keys_association_case_1,
]


@pytest.mark.parametrize("ref_keys, wanted_values, expected_output", keys_zipper_tests)
def test_keys_zipper(ref_keys, wanted_values, expected_output):
output = keys_values_zipper(ref_keys, wanted_values)
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)


keys_association_case_1 = (
"global.peers.*.[is_enabled,is_up]",
[[True, False], [True, False]],
[{"is_enabled": True, "is_up": False}, {"is_enabled": True, "is_up": False}],
)


keys_association_test = [
keys_association_case_1,
]


@pytest.mark.parametrize("path, wanted_values, expected_output", keys_association_test)
def test_keys_association(path, wanted_values, expected_output):
output = associate_key_of_my_value(path, wanted_values)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_type_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_check_type_results(check_type_str, evaluate_args, folder_name, path, ex
"napalm_get_bgp_neighbors",
"exact_match",
{},
"global.$peers$.*.[is_enabled,is_up]",
"global.peers.$*$.[is_enabled,is_up]",
(
{
"7.7.7.7": {
Expand All @@ -160,14 +160,14 @@ def test_check_type_results(check_type_str, evaluate_args, folder_name, path, ex
"napalm_get_bgp_neighbors",
"tolerance",
{"tolerance": 10},
"global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
({"10.1.0.0": {"accepted_prefixes": {"new_value": 900, "old_value": 1000}}}, False),
)
napalm_bgp_neighbor_prefixes_ipv6 = (
"napalm_get_bgp_neighbors",
"tolerance",
{"tolerance": 10},
"global.$peers$.*.*.ipv6.[accepted_prefixes,received_prefixes,sent_prefixes]",
"global.peers.$*$.*.ipv6.[accepted_prefixes,received_prefixes,sent_prefixes]",
({"10.64.207.255": {"received_prefixes": {"new_value": 1100, "old_value": 1000}}}, False),
)
napalm_get_lldp_neighbors_exact_raw = (
Expand Down