Skip to content

Commit 60c6172

Browse files
Merge pull request #109 from harness/FFM-12349
fix: [FFM-12349]: fix bool equals clause
2 parents d4810e0 + ed6e18e commit 60c6172

File tree

6 files changed

+47
-26
lines changed

6 files changed

+47
-26
lines changed

featureflags/evaluations/evaluator.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,37 @@ def _evaluate_clause(self, clause: Clause, target: Target) -> bool:
214214
operator = clause.op.lower()
215215
type = target.get_type(clause.attribute)
216216

217-
if type is None:
218-
if operator == SEGMENT_MATCH_OPERATOR.lower():
219-
log.debug("Clause operator is %s, evaluate on segment",
220-
operator)
221-
return self._check_target_in_segment(clause.values, target)
222-
log.debug("Attribute type %s is none return false", type)
223-
return False
224-
log.debug("evaluate clause with object %s operator %s and value %s",
225-
type, operator.upper(), clause.values)
226-
if operator == IN_OPERATOR.lower():
227-
return type.in_list(clause.values)
228-
if operator == EQUAL_OPERATOR.lower():
229-
return type.equal(clause.values)
230-
if operator == GT_OPERATOR.lower():
231-
return type.greater_than(clause.values)
232-
if operator == STARTS_WITH_OPERATOR.lower():
233-
return type.starts_with(clause.values)
234-
if operator == ENDS_WITH_OPERATOR.lower():
235-
return type.ends_with(clause.values)
236-
if operator == CONTAINS_OPERATOR.lower():
237-
return type.contains(clause.values)
238-
if operator == EQUAL_SENSITIVE_OPERATOR.lower():
239-
return type.equal_sensitive(clause.values)
217+
try:
218+
if type is None:
219+
if operator == SEGMENT_MATCH_OPERATOR.lower():
220+
log.debug("Clause operator is %s, evaluate on segment",
221+
operator)
222+
return self._check_target_in_segment(clause.values, target)
223+
log.debug("Attribute type %s is none return false", type)
224+
return False
225+
log.debug("evaluate clause with object %s "
226+
"operator %s and value %s",
227+
type, operator.upper(), clause.values)
228+
if operator == IN_OPERATOR.lower():
229+
return type.in_list(clause.values)
230+
if operator == EQUAL_OPERATOR.lower():
231+
return type.equal(clause.values)
232+
if operator == GT_OPERATOR.lower():
233+
return type.greater_than(clause.values)
234+
if operator == STARTS_WITH_OPERATOR.lower():
235+
return type.starts_with(clause.values)
236+
if operator == ENDS_WITH_OPERATOR.lower():
237+
return type.ends_with(clause.values)
238+
if operator == CONTAINS_OPERATOR.lower():
239+
return type.contains(clause.values)
240+
if operator == EQUAL_SENSITIVE_OPERATOR.lower():
241+
return type.equal_sensitive(clause.values)
242+
except ValueError:
243+
log.debug("couldn't convert %s to type %s", clause.values, type)
244+
except Exception as e:
245+
log.warning("exception processing clause: "
246+
"values: %s, type: %s, "
247+
"error: %s", clause.values, type, e)
240248
# unknown operation
241249
return False
242250

featureflags/ftypes/boolean.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import attr
44

55
from .interface import Interface
6-
from .utils import get_value
6+
from .utils import get_bool_value
77

88

99
@attr.s(auto_attribs=True)
@@ -27,7 +27,7 @@ def equal_sensitive(self, value: typing.Any) -> bool:
2727
return False
2828

2929
def equal(self, value: typing.Any) -> bool:
30-
_value = get_value(value)
30+
_value = get_bool_value(value)
3131
return self.value is _value
3232

3333
def greater_than(self, value: typing.Any) -> bool:

featureflags/ftypes/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ def get_int_value(value: typing.List[typing.Any]) -> int:
2020

2121
def get_float_value(value: typing.List[typing.Any]) -> float:
2222
return float(get_value(value))
23+
24+
def get_bool_value(value: typing.List[typing.Any]) -> int:
25+
return bool(get_value(value))

tests/unit/test_boolean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def test_equal(mocker):
55
input = [True, False]
66

7-
m = mocker.patch("featureflags.ftypes.boolean.get_value",
7+
m = mocker.patch("featureflags.ftypes.boolean.get_bool_value",
88
return_value=True)
99
boolean = Boolean(value=True)
1010

tests/unit/test_integer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
("", [], None, "contains", False),
1313
("", [], None, "equal_sensitive", False),
1414
(1, 1, 1, "equal", True),
15+
(2, 2, 1, "equal", False),
1516
(2, 1, 1, "greater_than", True),
17+
(1, 1, 2, "greater_than", False),
1618
(2, 1, 2, "greater_than_equal", True),
19+
(1, 1, 2, "greater_than_equal", False),
1720
(1, 1, 2, "less_than", True),
21+
(2, 1, 2, "less_than", False),
1822
(1, 1, 2, "less_than_equal", True),
23+
(2, 2, 1, "less_than_equal", False),
1924
],
2025
)
2126
def test_type_methods(mocker, arg, input, mocked, method, expected):

tests/unit/test_number.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
("", [], None, "match", False),
1212
("", [], None, "contains", False),
1313
("", [], None, "equal_sensitive", False),
14+
(1.5, 1.5, 1.1, "equal", False),
1415
(1.1, 1.1, 1.1, "equal", True),
1516
(2.0, 1.0, 1.0, "greater_than", True),
17+
(1.0, 1.0, 2.0, "greater_than", False),
1618
(2.0, 1.0, 2.0, "greater_than_equal", True),
19+
(1.0, 1.0, 2.0, "greater_than_equal", False),
1720
(1.0, 1.0, 2.0, "less_than", True),
21+
(2.0, 2.0, 1.0, "less_than", False),
1822
(1.1, 1.1, 2.0, "less_than_equal", True),
23+
(2.0, 2.0, 1.0, "less_than_equal", False),
1924
],
2025
)
2126
def test_type_methods(mocker, arg, input, mocked, method, expected):

0 commit comments

Comments
 (0)