Skip to content

Commit 7034c99

Browse files
Proposed fix for issue 44 (#45)
* propose fix ofr issue 44 * using flatten list utils * add test with index in result * improve reference key normal logic * Update netcompare/check_types.py Co-authored-by: Patryk Szulczewski <[email protected]> * Update netcompare/check_types.py Co-authored-by: Patryk Szulczewski <[email protected]> * Update netcompare/check_types.py Co-authored-by: Patryk Szulczewski <[email protected]> * Update netcompare/check_types.py Co-authored-by: Patryk Szulczewski <[email protected]> * Update netcompare/check_types.py Co-authored-by: Patryk Szulczewski <[email protected]> Co-authored-by: Patryk Szulczewski <[email protected]>
1 parent da7675a commit 7034c99

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

netcompare/check_types.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def get_value(output: Union[Mapping, List], path: str, exclude: List = None) ->
5757
Evaluated data, may be anything depending on JMESPath used.
5858
"""
5959
if exclude and isinstance(output, Dict):
60+
if not isinstance(exclude, list):
61+
raise ValueError(f"Exclude list must be defined as a list. You have {type(exclude)}")
62+
6063
exclude_filter(output, exclude) # exclude unwanted elements
6164

6265
if not path:
@@ -81,9 +84,21 @@ def get_value(output: Union[Mapping, List], path: str, exclude: List = None) ->
8184

8285
paired_key_value = associate_key_of_my_value(jmespath_value_parser(path), values)
8386

84-
if re.search(r"\$.*\$", path): # normalize
87+
# We need to get a list of reference keys - list of strings.
88+
# Based on the expression or output type we might have different data types
89+
# therefore we need to normalize.
90+
if re.search(r"\$.*\$", path):
8591
wanted_reference_keys = jmespath.search(jmespath_refkey_parser(path), output)
86-
list_of_reference_keys = keys_cleaner(wanted_reference_keys)
92+
93+
if isinstance(wanted_reference_keys, dict): # when wanted_reference_keys is dict() type
94+
list_of_reference_keys = keys_cleaner(wanted_reference_keys)
95+
elif any(isinstance(element, list) for element in wanted_reference_keys): # when wanted_reference_keys is a nested list
96+
list_of_reference_keys = flatten_list(wanted_reference_keys)[0]
97+
elif isinstance(wanted_reference_keys, list): # when wanted_reference_keys is a list
98+
list_of_reference_keys = wanted_reference_keys
99+
else:
100+
raise ValueError("Reference Key normalization failure. Please verify data type returned.")
101+
87102
return keys_values_zipper(list_of_reference_keys, paired_key_value)
88103

89104
return values

tests/mock/raw_novalue_exclude/pre.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"lastStatusChangeTimestamp": 1626247820.0720868,
99
"lanes": 0,
1010
"name": "Management1",
11-
"interfaceStatus": "connected",
11+
"interfaceStatus": "down",
1212
"autoNegotiate": "success",
1313
"burnedInAddress": "08:00:27:e6:b2:f8",
1414
"loopbackMode": "loopbackNone",

tests/test_diff_generator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"result": {
5858
"interfaces": {
5959
"Management1": {
60+
"interfaceStatus": {"new_value": "connected", "old_value": "down"},
6061
"lastStatusChangeTimestamp": {"new_value": 1626247821.123456, "old_value": 1626247820.0720868},
6162
"interfaceAddress": {
6263
"primaryIp": {"address": {"new_value": "10.2.2.15", "old_value": "10.0.2.15"}}
@@ -148,6 +149,21 @@
148149
},
149150
)
150151

152+
exact_match_test_issue_44_case_1 = (
153+
"raw_novalue_exclude",
154+
"result[*].interfaces.*.[$name$,interfaceStatus]",
155+
[],
156+
{"Management1": {"interfaceStatus": {"new_value": "connected", "old_value": "down"}}},
157+
)
158+
159+
exact_match_test_issue_44_case_2 = (
160+
"raw_novalue_exclude",
161+
"result[0].interfaces.*.[$name$,interfaceStatus]",
162+
[],
163+
{"Management1": {"interfaceStatus": {"new_value": "connected", "old_value": "down"}}},
164+
)
165+
166+
151167
eval_tests = [
152168
exact_match_of_global_peers_via_napalm_getter,
153169
exact_match_of_bgp_peer_caps_via_api,
@@ -161,6 +177,8 @@
161177
exact_match_multi_nested_list,
162178
exact_match_textfsm_ospf_int_br_raw,
163179
exact_match_textfsm_ospf_int_br_normalized,
180+
exact_match_test_issue_44_case_1,
181+
exact_match_test_issue_44_case_2,
164182
]
165183

166184

0 commit comments

Comments
 (0)