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

Implement ge and le operator type #89

Merged
merged 5 commits into from
Sep 27, 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
5 changes: 5 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,12 @@ The `operator` check is a collection of more specific checks divided into catego
2. `is-lt`: Check if the value of a specified element is lesser than a given numeric value.
- `is-lt: 55`: checks if value is lower than 55 or not.

3. `is-ge`: Check if the value of a specified element is greater than or equal to a given numeric value.
- `is-ge: 2`: checks if value should be greater or equal than 2.

4. `is-le`: Check if the value of a specified element is lesser than or equal a given numeric value.
- `is-le: 55`: checks if value is lower or equal than 55 or not.

Examples:

```python
Expand Down
2 changes: 1 addition & 1 deletion jdiff/check_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _validate(params) -> None: # type: ignore[override]
"""Validate operator parameters."""
in_operators = ("is-in", "not-in", "in-range", "not-in-range")
bool_operators = ("all-same",)
number_operators = ("is-gt", "is-lt")
number_operators = ("is-gt", "is-lt", "is-ge", "is-le")
string_operators = ("contains", "not-contains")
valid_options = (
in_operators,
Expand Down
10 changes: 10 additions & 0 deletions jdiff/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def call_evaluation_logic():

ops = {
">": operator.gt,
">=": operator.ge,
"<": operator.lt,
"<=": operator.le,
"is_in": operator.contains,
"not_in": operator.contains,
"contains": operator.contains,
Expand Down Expand Up @@ -99,10 +101,18 @@ def is_gt(self) -> Tuple[List, bool]:
"""Is greather than operator caller."""
return self._loop_through_wrapper(">")

def is_ge(self) -> Tuple[List, bool]:
"""Is greather or equal than operator caller."""
return self._loop_through_wrapper(">=")

def is_lt(self) -> Tuple[List, bool]:
"""Is lower than operator caller."""
return self._loop_through_wrapper("<")

def is_le(self) -> Tuple[List, bool]:
"""Is lower or equal than operator caller."""
return self._loop_through_wrapper("<=")

def is_in(self) -> Tuple[List, bool]:
"""Is in operator caller."""
return self._loop_through_wrapper("is_in")
Expand Down
32 changes: 32 additions & 0 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,41 @@
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_ge_equal = (
"pre.json",
"operator",
{"params": {"mode": "is-ge", "operator_data": 50}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_ge_greater = (
"pre.json",
"operator",
{"params": {"mode": "is-ge", "operator_data": 20}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_lt = (
"pre.json",
"operator",
{"params": {"mode": "is-lt", "operator_data": 60}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_le_equal = (
"pre.json",
"operator",
{"params": {"mode": "is-le", "operator_data": 50}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_le_lower = (
"pre.json",
"operator",
{"params": {"mode": "is-le", "operator_data": 60}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
([], True),
)
operator_is_in = (
"pre.json",
"operator",
Expand Down Expand Up @@ -132,7 +160,11 @@
operator_contains,
operator_not_contains,
operator_is_gt,
operator_is_ge_equal,
operator_is_ge_greater,
operator_is_lt,
operator_is_le_equal,
operator_is_le_lower,
operator_is_in,
operator_not_in,
operator_in_range,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_validates.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
operator_params_wrong_operator = (
"operator",
{"params": {"mode": "random", "operator_data": [20, 40, 60]}},
"'params' value must be one of the following: ['is-in', 'not-in', 'in-range', 'not-in-range', 'all-same', 'is-gt', 'is-lt', 'contains', 'not-contains']. You have: random",
"'params' value must be one of the following: ['is-in', 'not-in', 'in-range', 'not-in-range', 'all-same', 'is-gt', 'is-lt', 'is-ge', 'is-le', 'contains', 'not-contains']. You have: random",
)
operator_params_in = (
"operator",
Expand All @@ -85,7 +85,7 @@
operator_params_number = (
"operator",
{"params": {"mode": "is-gt", "operator_data": "1"}},
"check options ('is-gt', 'is-lt') must have value of type float or int. You have: 1 of type <class 'str'>",
"check options ('is-gt', 'is-lt', 'is-ge', 'is-le') must have value of type float or int. You have: 1 of type <class 'str'>",
)
operator_params_contains = (
"operator",
Expand Down