From 05faf70afcf555607346de46393d6bf5f73f5139 Mon Sep 17 00:00:00 2001 From: pszulczewski Date: Tue, 18 Apr 2023 13:34:47 +0200 Subject: [PATCH 1/2] Fix ref_key parsing --- jdiff/utils/jmespath_parsers.py | 2 +- tests/test_diff_generator.py | 4 +-- tests/test_jmespath_parsers.py | 57 +++++++++++++++++++-------------- tests/test_type_checks.py | 6 ++-- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/jdiff/utils/jmespath_parsers.py b/jdiff/utils/jmespath_parsers.py index 5503eb7..34f7777 100644 --- a/jdiff/utils/jmespath_parsers.py +++ b/jdiff/utils/jmespath_parsers.py @@ -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] return ".".join(splitted_jmespath) diff --git a/tests/test_diff_generator.py b/tests/test_diff_generator.py index fcf5ee8..9c0892d 100644 --- a/tests/test_diff_generator.py +++ b/tests/test_diff_generator.py @@ -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": { @@ -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}}, diff --git a/tests/test_jmespath_parsers.py b/tests/test_jmespath_parsers.py index 3e37728..a3c64de 100644 --- a/tests/test_jmespath_parsers.py +++ b/tests/test_jmespath_parsers.py @@ -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", @@ -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) @@ -77,21 +84,11 @@ 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): @@ -99,6 +96,18 @@ def test_keys_zipper(ref_keys, wanted_values, expected_output): 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) diff --git a/tests/test_type_checks.py b/tests/test_type_checks.py index 2500f59..e141a52 100644 --- a/tests/test_type_checks.py +++ b/tests/test_type_checks.py @@ -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": { @@ -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 = ( From b969d0f10abfe9a06c4561047d6c6b71f2079fbc Mon Sep 17 00:00:00 2001 From: pszulczewski Date: Tue, 18 Apr 2023 14:02:48 +0200 Subject: [PATCH 2/2] Update docs --- docs/usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage.md b/docs/usage.md index eb58a4a..ce7692a 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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,