Skip to content

Commit e8280c7

Browse files
authored
Merge pull request #97 from networktocode/fix_ref_key
Fix ref key
2 parents d30d359 + b969d0f commit e8280c7

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Let's have a look at a couple of examples:
251251
```
252252
We will define a JMESPath expression for the values we want to test and extract from the reference and comparison objects.
253253
```python
254-
>>> my_jmspath = "global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]"
254+
>>> my_jmspath = "global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]"
255255
>>> reference_value = extract_data_from_json(reference_data, my_jmspath)
256256
>>> reference_value
257257
[{'10.1.0.0': {'accepted_prefixes': 900,

jdiff/utils/jmespath_parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def jmespath_refkey_parser(path: str):
6565
splitted_jmespath[number] = regex_match_anchor.group().replace("$", "")
6666

6767
if regex_match_anchor and not element.startswith("[") and not element.endswith("]"):
68-
splitted_jmespath = splitted_jmespath[: number + 1]
68+
splitted_jmespath = splitted_jmespath[:number]
6969

7070
return ".".join(splitted_jmespath)
7171

tests/test_diff_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Diff test case 1.
99
global_peers = (
1010
"napalm_getter_peer_state_change",
11-
"global.$peers$.*.[is_enabled,is_up]",
11+
"global.peers.$*$.[is_enabled,is_up]",
1212
[],
1313
{
1414
"10.1.0.0": {
@@ -134,7 +134,7 @@
134134
# Diff test case 9. Value in multiple nested lists.
135135
multi_nested_list = (
136136
"exact_match_nested",
137-
"global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes]",
137+
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes]",
138138
[],
139139
{
140140
"10.1.0.0": {"accepted_prefixes": {"new_value": -1, "old_value": -9}},

tests/test_jmespath_parsers.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
"result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]",
2727
)
2828

29+
30+
value_parser_tests = [
31+
value_parser_case_1,
32+
value_parser_case_2,
33+
value_parser_case_3,
34+
value_parser_case_4,
35+
]
36+
37+
38+
@pytest.mark.parametrize("path, expected_output", value_parser_tests)
39+
def test_value_parser(path, expected_output):
40+
output = jmespath_value_parser(path)
41+
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)
42+
43+
2944
keyref_parser_case_1 = (
3045
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]",
3146
"result[0].vrfs.default.peerList[*].peerAddress",
@@ -40,31 +55,23 @@
4055
)
4156
keyref_parser_case_4 = (
4257
"result[0].$vrfs$.default.peerList[*].[peerAddress,prefixesReceived]",
43-
"result[0].vrfs",
58+
"result[0]",
59+
)
60+
keyref_parser_case_5 = (
61+
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
62+
"global.peers",
4463
)
4564

4665

47-
value_parser_tests = [
48-
value_parser_case_1,
49-
value_parser_case_2,
50-
value_parser_case_3,
51-
value_parser_case_4,
52-
]
53-
5466
keyref_parser_tests = [
5567
keyref_parser_case_1,
5668
keyref_parser_case_2,
5769
keyref_parser_case_3,
5870
keyref_parser_case_4,
71+
keyref_parser_case_5,
5972
]
6073

6174

62-
@pytest.mark.parametrize("path, expected_output", value_parser_tests)
63-
def test_value_parser(path, expected_output):
64-
output = jmespath_value_parser(path)
65-
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)
66-
67-
6875
@pytest.mark.parametrize("path, expected_output", keyref_parser_tests)
6976
def test_keyref_parser(path, expected_output):
7077
output = jmespath_refkey_parser(path)
@@ -77,28 +84,30 @@ def test_keyref_parser(path, expected_output):
7784
[{"10.1.0.0": {"is_enabled": False, "is_up": False}}, {"10.2.0.0": {"is_enabled": True, "is_up": True}}],
7885
)
7986

80-
keys_association_case_1 = (
81-
"global.peers.*.[is_enabled,is_up]",
82-
[[True, False], [True, False]],
83-
[{"is_enabled": True, "is_up": False}, {"is_enabled": True, "is_up": False}],
84-
)
85-
8687

8788
keys_zipper_tests = [
8889
keys_zipper_case_1,
8990
]
9091

91-
keys_association_test = [
92-
keys_association_case_1,
93-
]
94-
9592

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

10198

99+
keys_association_case_1 = (
100+
"global.peers.*.[is_enabled,is_up]",
101+
[[True, False], [True, False]],
102+
[{"is_enabled": True, "is_up": False}, {"is_enabled": True, "is_up": False}],
103+
)
104+
105+
106+
keys_association_test = [
107+
keys_association_case_1,
108+
]
109+
110+
102111
@pytest.mark.parametrize("path, wanted_values, expected_output", keys_association_test)
103112
def test_keys_association(path, wanted_values, expected_output):
104113
output = associate_key_of_my_value(path, wanted_values)

tests/test_type_checks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_check_type_results(check_type_str, evaluate_args, folder_name, path, ex
145145
"napalm_get_bgp_neighbors",
146146
"exact_match",
147147
{},
148-
"global.$peers$.*.[is_enabled,is_up]",
148+
"global.peers.$*$.[is_enabled,is_up]",
149149
(
150150
{
151151
"7.7.7.7": {
@@ -160,14 +160,14 @@ def test_check_type_results(check_type_str, evaluate_args, folder_name, path, ex
160160
"napalm_get_bgp_neighbors",
161161
"tolerance",
162162
{"tolerance": 10},
163-
"global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
163+
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
164164
({"10.1.0.0": {"accepted_prefixes": {"new_value": 900, "old_value": 1000}}}, False),
165165
)
166166
napalm_bgp_neighbor_prefixes_ipv6 = (
167167
"napalm_get_bgp_neighbors",
168168
"tolerance",
169169
{"tolerance": 10},
170-
"global.$peers$.*.*.ipv6.[accepted_prefixes,received_prefixes,sent_prefixes]",
170+
"global.peers.$*$.*.ipv6.[accepted_prefixes,received_prefixes,sent_prefixes]",
171171
({"10.64.207.255": {"received_prefixes": {"new_value": 1100, "old_value": 1000}}}, False),
172172
)
173173
napalm_get_lldp_neighbors_exact_raw = (

0 commit comments

Comments
 (0)