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 operator checks to follow other check_type logic. #85

Merged
merged 2 commits into from
Sep 23, 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
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v0.0.2

- Update operator logic for returned result
- Update docs

## v0.0.1

- Initial release

## v0.0.1-beta.1

Initial release
- First beta release
12 changes: 5 additions & 7 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ Let's run an example where we want to check the `burnedInAddress` key has a stri
```
We define the regex for matching a MAC address string. Then we define the path query to extract the data and create the check.
```python
>>> mac_regex = "(?:[0-9a-fA-F]:?){12}"
>>> mac_regex = "^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$"
>>> path = "result[*].interfaces.*.[$name$,burnedInAddress]"
>>> check = CheckType.create(check_type="regex")
>>> actual_value = extract_data_from_json(actual_data, path)
Expand Down Expand Up @@ -556,7 +556,7 @@ We are looking for peers that have the same peerGroup, vrf, and state. Return pe
{'10.1.0.0': {'peerGroup': 'IPv4-UNDERLAY-SPINE',
'vrf': 'default',
'state': 'Idle'}}],
True)
False)
```

Let's now look to an example for the `in` operator. Keeping the same `data` and class object as above:
Expand All @@ -573,7 +573,7 @@ We are looking for "prefixesReceived" value in the operator_data list.
```python
>>> result = check.evaluate(check_args, value)
>>> result
([{'10.1.0.0': {'prefixesReceived': 50}}], False)
([{'7.7.7.7': {'prefixesReceived': 101}}], False)
```

What about `str` operator?
Expand All @@ -587,7 +587,7 @@ What about `str` operator?
{'10.1.0.0': {'peerGroup': 'IPv4-UNDERLAY-SPINE'}}]
>>> result = check.evaluate(check_args, value)
>>> result
([{'7.7.7.7': {'peerGroup': 'EVPN-OVERLAY-SPINE'}}], False)
([{'10.1.0.0': {'peerGroup': 'IPv4-UNDERLAY-SPINE'}}], False)
```

Can you guess what would be the outcome for an `int`, `float` operator?
Expand All @@ -601,9 +601,7 @@ Can you guess what would be the outcome for an `int`, `float` operator?
{'10.1.0.0': {'prefixesReceived': 50}}]
>>> result = check.evaluate(check_args, value)
>>> result
([{'7.7.7.7': {'prefixesReceived': 101}},
{'10.1.0.0': {'prefixesReceived': 50}}],
False)
([], True)
```

See `tests` folder in the repo for more examples.
4 changes: 2 additions & 2 deletions jdiff/check_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _validate(params) -> None: # type: ignore[override]
params_key = params.get("params", {}).get("mode")
params_value = params.get("params", {}).get("operator_data")

if not params_key or not params_value:
if not params_key or params_value is None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was making all-same check to fail here when False was passed as value.

raise ValueError(
f"'mode' and 'operator_data' arguments must be provided. You have: {list(params['params'].keys())}."
)
Expand Down Expand Up @@ -258,4 +258,4 @@ def result(self, evaluation_result):

This is required as Opertor return its own boolean within result.
"""
return evaluation_result[0], not evaluation_result[1]
return evaluation_result[0], evaluation_result[1]
24 changes: 11 additions & 13 deletions jdiff/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ def call_evaluation_logic():
"""Operator valuation logic wrapper."""
# reverse operands: https://docs.python.org/3.8/library/operator.html#operator.contains
if call_ops == "is_in":
if ops[call_ops](self.reference_data, evaluated_value):
if not ops[call_ops](self.reference_data, evaluated_value):
result.append(item)
elif call_ops == "not_contains":
if not ops[call_ops](evaluated_value, self.reference_data):
if ops[call_ops](evaluated_value, self.reference_data):
result.append(item)
elif call_ops == "not_in":
if not ops[call_ops](self.reference_data, evaluated_value):
if ops[call_ops](self.reference_data, evaluated_value):
result.append(item)
elif call_ops == "in_range":
if self.reference_data[0] < evaluated_value < self.reference_data[1]:
if not self.reference_data[0] < evaluated_value < self.reference_data[1]:
result.append(item)
elif call_ops == "not_in_range":
if not self.reference_data[0] < evaluated_value < self.reference_data[1]:
if self.reference_data[0] < evaluated_value < self.reference_data[1]:
result.append(item)
# "<", ">", "contains"
elif ops[call_ops](evaluated_value, self.reference_data):
elif not ops[call_ops](evaluated_value, self.reference_data):
result.append(item)

ops = {
Expand All @@ -64,14 +64,13 @@ def call_evaluation_logic():
for evaluated_value in value.values():
call_evaluation_logic()
if result:
return (result, True)
return (result, False)
return (result, False)
return (result, True)

def all_same(self) -> Tuple[bool, Any]:
def all_same(self) -> Tuple[Any, bool]:
"""All same operator type implementation."""
list_of_values = []
result = []

for item in self.value_to_compare:
# Create a list for compare values.
list_of_values.extend(iter(item.values()))
Expand All @@ -80,13 +79,12 @@ def all_same(self) -> Tuple[bool, Any]:
result.append(False)
else:
result.append(True)

if self.reference_data and not all(result):
return (self.value_to_compare, False)
if self.reference_data:
return (self.value_to_compare, True)
return ([], True)
if not all(result):
return (self.value_to_compare, True)
return ([], True)
return (self.value_to_compare, False)

def contains(self) -> Tuple[List, bool]:
Expand Down
Loading