Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parameter_match check, to support non-normalized data #90

Merged
merged 4 commits into from
Nov 15, 2022
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
6 changes: 3 additions & 3 deletions jdiff/check_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""CheckType Implementation."""
from typing import Mapping, Tuple, Dict, Any, Union
from typing import List, Tuple, Dict, Any, Union
from abc import ABC, abstractmethod
from .evaluators import diff_generator, parameter_evaluator, regex_evaluator, operator_evaluator

Expand Down Expand Up @@ -136,7 +136,7 @@ def _validate(params, mode) -> None: # type: ignore[override]
f"'mode' argument should be one of the following: {', '.join(mode_options)}. You have: {mode}"
)

def evaluate(self, params: Dict, value_to_compare: Mapping, mode: str) -> Tuple[Dict, bool]: # type: ignore[override]
def evaluate(self, params: Dict, value_to_compare: List[Dict], mode: str) -> Tuple[Dict, bool]: # type: ignore[override]
"""Parameter Match evaluator implementation."""
self._validate(params=params, mode=mode)
# TODO: we don't use the mode?
Expand All @@ -161,7 +161,7 @@ def _validate(regex, mode) -> None: # type: ignore[override]
if mode not in mode_options:
raise ValueError(f"'mode' argument should be {mode_options}. You have: {mode}")

def evaluate(self, regex: str, value_to_compare: Mapping, mode: str) -> Tuple[Dict, bool]: # type: ignore[override]
def evaluate(self, regex: str, value_to_compare: List[Dict[Any, Dict]], mode: str) -> Tuple[Dict, bool]: # type: ignore[override]
"""Regex Match evaluator implementation."""
self._validate(regex=regex, mode=mode)
evaluation_result = regex_evaluator(value_to_compare, regex, mode)
Expand Down
42 changes: 21 additions & 21 deletions jdiff/evaluators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Evaluators."""
import re
from typing import Any, Mapping, Dict, Tuple
from typing import Any, Mapping, Dict, Tuple, List
from deepdiff import DeepDiff
from .utils.diff_helpers import get_diff_iterables_items, fix_deepdiff_key_names
from .operator import Operator
Expand Down Expand Up @@ -36,7 +36,7 @@ def diff_generator(pre_result: Any, post_result: Any) -> Dict:
return fix_deepdiff_key_names(result)


def parameter_evaluator(values: Mapping, parameters: Mapping, mode: str) -> Dict:
def parameter_evaluator(values: List[Dict], parameters: Mapping, mode: str) -> Dict:
"""Parameter Match evaluator engine.

Args:
Expand All @@ -51,41 +51,41 @@ def parameter_evaluator(values: Mapping, parameters: Mapping, mode: str) -> Dict
Dictionary with all the items that have some value not matching the expectations from parameters
"""
if not isinstance(values, list):
raise TypeError("Something went wrong during jmespath parsing. 'values' must be of type List.")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Jmespath parsing is now optional.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Question around this. If JMSPATH is now optional, how the library will understand when is required and when is not?

raise TypeError("'values' must be of type List.")

result = {}
for value in values:
for index, value in enumerate(values):
# value: {'7.7.7.7': {'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals
if not isinstance(value, dict):
raise TypeError(
"Something went wrong during jmespath parsing. ",
f"'value' ({value}) must be of type Dict, and it's {type(value)}",
)
raise TypeError(f"'value' ({value}) must be of type Dict, and it's {type(value)}")

result_item = {}

# TODO: Why the 'value' dict has always ONE single element? we have to explain
# inner_key: '7.7.7.7'
inner_key = list(value.keys())[0]
# inner_value: [{'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals'}]
inner_value = list(value.values())[0]
# When data has been normalized with $key$, get inner key and value
if len(value) == 1:
# inner_key: '7.7.7.7'
inner_key = list(value.keys())[0]
# inner_value: [{'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals'}]
value = list(value.values())[0]
else:
inner_key = index
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the value in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

An element from the top for loop, where values is a function argument.

Copy link
Collaborator Author

@pszulczewski pszulczewski Oct 27, 2022

Choose a reason for hiding this comment

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

If data normalisation is detected, then reassign value from inner element.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Jmespath parsing is now optional.

I believe this answering to my question above (?)


for parameter_key, parameter_value in parameters.items():
if mode == "match" and inner_value[parameter_key] != parameter_value:
result_item[parameter_key] = inner_value[parameter_key]
elif mode == "no-match" and inner_value[parameter_key] == parameter_value:
result_item[parameter_key] = inner_value[parameter_key]
if mode == "match" and value[parameter_key] != parameter_value:
result_item[parameter_key] = value[parameter_key]
elif mode == "no-match" and value[parameter_key] == parameter_value:
result_item[parameter_key] = value[parameter_key]

if result_item:
result[inner_key] = result_item

return result


def regex_evaluator(values: Mapping, regex_expression: str, mode: str) -> Dict:
def regex_evaluator(values: List[Dict[Any, Dict]], regex_expression: str, mode: str) -> Dict:
"""Regex Match evaluator engine."""
# values: [{'7.7.7.7': {'peerGroup': 'EVPN-OVERLAY-SPINE'}}]
# parameter: {'regex': '.*UNDERLAY.*', 'mode': 'include'}
# parameter: {'regex': '.*UNDERLAY.*', 'mode': 'match'}
result = {}
if not isinstance(values, list):
raise TypeError("Something went wrong during JMSPath parsing. 'values' must be of type List.")
Expand All @@ -94,10 +94,10 @@ def regex_evaluator(values: Mapping, regex_expression: str, mode: str) -> Dict:
for founded_value in item.values():
for value in founded_value.values():
match_result = re.search(regex_expression, value)
# Fail if there is not regex match
# Fail if there is no regex match for "match" mode
if mode == "match" and not match_result:
result.update(item)
# Fail if there is regex match
# Fail if there is regex match for "no-match" mode.
elif mode == "no-match" and match_result:
result.update(item)

Expand Down
Loading