Skip to content

Commit 4e462f8

Browse files
committed
fix pytests
1 parent 3ac1f5f commit 4e462f8

File tree

2 files changed

+6
-98
lines changed

2 files changed

+6
-98
lines changed

netcompare/evaluator.py

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,7 @@
1111

1212

1313
def diff_generator(pre_result: Mapping, post_result: Mapping) -> Dict:
14-
"""
15-
Generates diff between pre and post data based on check definition.
16-
17-
Args:
18-
pre_result: pre data result.
19-
post_result: post data result.
20-
21-
Return:
22-
output: diff between pre and post data.
23-
24-
Example:
25-
>>> from evaluator import diff_generator
26-
>>> pre_data = {"result": [{"bgp_neigh": "10.17.254.2","state": "Idle"}]}
27-
>>> post_data = {"result": [{"bgp_neigh": "10.17.254.2","state": "Up"}]}
28-
>>> check_definition = {"check_type": "exact_match", "value_path": "result[*].[state]", "reference_key_path": "result[*].bgp_neigh"}
29-
>>> print(diff_generator(check_definition, post_data, check_definition))
30-
{'10.17.254.2': {'state': {'new_value': 'Up', 'old_value': 'Idle'}}}
31-
"""
14+
"""Generates diff between pre and post data based on check definition."""
3215
diff_result = DeepDiff(pre_result, post_result)
3316

3417
result = diff_result.get("values_changed", {})
@@ -45,38 +28,7 @@ def diff_generator(pre_result: Mapping, post_result: Mapping) -> Dict:
4528

4629

4730
def get_diff_iterables_items(diff_result: Mapping) -> Mapping:
48-
"""
49-
Return a dict with new and missing values where the values are in a list.
50-
51-
Args:
52-
diff_result: dict retruned from a DeepDiff compare.
53-
54-
Return:
55-
defaultdict
56-
57-
Example:
58-
>>> diff_result = {
59-
"iterable_item_added": {
60-
"root['Ethernet3'][1]": {
61-
"hostname": "ios-xrv-unittest",
62-
"port": "Gi0/0/0/0"
63-
},
64-
"root['Ethernet3'][2]": {
65-
"hostname": "ios-xrv-unittest",
66-
"port": "Gi0/0/0/1"
67-
}
68-
}
69-
}
70-
>>> get_diff_iterables_items(diff_result)
71-
defaultdict(functools.partial(<class 'collections.defaultdict'>, <class 'list'>),
72-
{"['Ethernet3']": defaultdict(list,
73-
{'new': [
74-
{'hostname': 'ios-xrv-unittest', 'port': 'Gi0/0/0/0'},
75-
{'hostname': 'ios-xrv-unittest', 'port': 'Gi0/0/0/1'}
76-
]}
77-
)}
78-
)
79-
"""
31+
"""Return a dict with new and missing values where the values are in a list."""
8032
# DeepDiff iterable_items are returned when the source data is a list
8133
# and provided in the format: "root['Ethernet3'][1]"
8234
# or more generically: root['KEY']['KEY']['KEY']...[numeric_index]
@@ -104,12 +56,10 @@ def get_diff_iterables_items(diff_result: Mapping) -> Mapping:
10456

10557
def fix_deepdiff_key_names(obj: Mapping) -> Mapping:
10658
"""Return a dict based on the provided dict object where the brackets and quotes are removed from the string."""
107-
# sample keys:
108-
# root[2]['10.64.207.255']['accepted_prefixes']
109-
# root['Ethernet1'][0]['port']
11059
pattern = r"'([A-Za-z0-9_\./\\-]*)'"
11160

11261
result = {}
62+
# root[2]['10.64.207.255']['accepted_prefixes']
11363
for key, value in obj.items():
11464
key_parts = re.findall(pattern, key)
11565
partial_res = group_value(key_parts, value)
@@ -118,39 +68,14 @@ def fix_deepdiff_key_names(obj: Mapping) -> Mapping:
11868

11969

12070
def group_value(tree_list: List, value: Mapping) -> Mapping:
121-
"""
122-
Build dictionary based on value's key and reference key.
123-
124-
Args:
125-
tree_list: list of value's key and reference key
126-
value: value results
127-
128-
Return:
129-
value: Mapping of the changed values associated to reference key
130-
131-
Example:
132-
>>> tree_list = ['10.17.254.2', 'state']
133-
>>> value = {'new_value': 'Up', 'old_value': 'Idle'}
134-
{'10.17.254.2': {'state': {'new_value': 'Up', 'old_value': 'Idle'}}}
135-
"""
71+
"""Build dictionary based on value's key and reference key."""
13672
if tree_list:
13773
return {tree_list[0]: group_value(tree_list[1:], value)}
13874
return value
13975

14076

14177
def dict_merger(original_dict: Mapping, merged_dict: Mapping):
142-
"""
143-
Merge dictionaries to build final result.
144-
145-
Args:
146-
original_dict: empty dictionary to be merged
147-
merged_dict: dictionary containing returned key/value and associated reference key
148-
149-
Example:
150-
>>> original_dict = {}
151-
>>> merged_dict = {'10.17.254.2': {'state': {'new_value': 'Up', 'old_value': 'Idle'}}}
152-
{'10.17.254.2': {'state': {'new_value': 'Up', 'old_value': 'Idle'}}}
153-
"""
78+
"""Merge dictionaries to build final result."""
15479
for key in merged_dict.keys():
15580
if key in original_dict and isinstance(original_dict[key], dict) and isinstance(merged_dict[key], dict):
15681
dict_merger(original_dict[key], merged_dict[key])

netcompare/runner.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,7 @@
99

1010

1111
def extract_values_from_output(value: Mapping, path: Mapping, exclude: List) -> Union[Mapping, List, int, str, bool]:
12-
"""Return data from output depending on the check path. See unit text for complete example.
13-
14-
Args:
15-
path: "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]",
16-
value: {
17-
"jsonrpc": "2.0",
18-
"id": "EapiExplorer-1",
19-
"result": [
20-
{
21-
"vrfs": {
22-
"default": {
23-
"peerList": [
24-
{ ...
25-
exclude: ["interfaceStatistics", "interfaceCounters"]
26-
27-
Return:
28-
[{'7.7.7.7': {'prefixesReceived': 101}}, {'10.1.0.0': {'prefixesReceived': 120}}, ...
29-
"""
12+
"""Return data from output depending on the check path. See unit text for complete example."""
3013
# Get the wanted values to be evaluated if jmspath expression is defined, otherwise
3114
# use the entire output if jmespath is not defined in check. This cover the "raw" diff type.
3215
if path and not exclude:

0 commit comments

Comments
 (0)