Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
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
8 changes: 7 additions & 1 deletion eppo_client/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class OperatorType(Enum):
GT = "GT"
LTE = "LTE"
LT = "LT"
ONE_OF = "ONE_OF"
NOT_ONE_OF = "NOT_ONE_OF"


class Condition(SdkBaseModel):
Expand Down Expand Up @@ -40,9 +42,13 @@ def matches_rule(subject_attributes: dict, rule: Rule):

def evaluate_condition(subject_attributes: dict, condition: Condition) -> bool:
subject_value = subject_attributes.get(condition.attribute, None)
if subject_value:
if subject_value is not None:
if condition.operator == OperatorType.MATCHES:
return bool(re.match(condition.value, str(subject_value)))
elif condition.operator == OperatorType.ONE_OF:
return str(subject_value) in condition.value
elif condition.operator == OperatorType.NOT_ONE_OF:
return str(subject_value) not in condition.value
else:
return isinstance(
subject_value, numbers.Number
Expand Down
61 changes: 61 additions & 0 deletions test/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,64 @@ def test_matches_rules_true_with_numeric_value_and_regex():
)
rule = Rule(conditions=[condition])
assert matches_any_rule({"age": 99}, [rule]) is True


def test_one_of_operator_with_boolean():
oneOfRule = Rule(
conditions=[
Condition(operator=OperatorType.ONE_OF, value=["True"], attribute="enabled")
]
)
notOneOfRule = Rule(
conditions=[
Condition(
operator=OperatorType.NOT_ONE_OF, value=["True"], attribute="enabled"
)
]
)
assert matches_any_rule({"enabled": True}, [oneOfRule]) is True
assert matches_any_rule({"enabled": False}, [oneOfRule]) is False
assert matches_any_rule({"enabled": True}, [notOneOfRule]) is False
assert matches_any_rule({"enabled": False}, [notOneOfRule]) is True


def test_one_of_operator_with_string():
oneOfRule = Rule(
conditions=[
Condition(
operator=OperatorType.ONE_OF, value=["john", "ron"], attribute="name"
)
]
)
notOneOfRule = Rule(
conditions=[
Condition(operator=OperatorType.NOT_ONE_OF, value=["ron"], attribute="name")
]
)
assert matches_any_rule({"name": "john"}, [oneOfRule]) is True
assert matches_any_rule({"name": "ron"}, [oneOfRule]) is True
assert matches_any_rule({"name": "sam"}, [oneOfRule]) is False
assert matches_any_rule({"name": "ron"}, [notOneOfRule]) is False
assert matches_any_rule({"name": "sam"}, [notOneOfRule]) is True


def test_one_of_operator_with_number():
oneOfRule = Rule(
conditions=[
Condition(
operator=OperatorType.ONE_OF, value=["14", "15.11"], attribute="number"
)
]
)
notOneOfRule = Rule(
conditions=[
Condition(
operator=OperatorType.NOT_ONE_OF, value=["10"], attribute="number"
)
]
)
assert matches_any_rule({"number": "14"}, [oneOfRule]) is True
assert matches_any_rule({"number": 15.11}, [oneOfRule]) is True
assert matches_any_rule({"number": "10"}, [oneOfRule]) is False
assert matches_any_rule({"number": "10"}, [notOneOfRule]) is False
assert matches_any_rule({"number": 11}, [notOneOfRule]) is True