Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit f6924b4

Browse files
authored
Add support for oneOf operator (#9)
1 parent d180ace commit f6924b4

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

eppo_client/rules.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class OperatorType(Enum):
1212
GT = "GT"
1313
LTE = "LTE"
1414
LT = "LT"
15+
ONE_OF = "ONE_OF"
16+
NOT_ONE_OF = "NOT_ONE_OF"
1517

1618

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

4143
def evaluate_condition(subject_attributes: dict, condition: Condition) -> bool:
4244
subject_value = subject_attributes.get(condition.attribute, None)
43-
if subject_value:
45+
if subject_value is not None:
4446
if condition.operator == OperatorType.MATCHES:
4547
return bool(re.match(condition.value, str(subject_value)))
48+
elif condition.operator == OperatorType.ONE_OF:
49+
return str(subject_value) in condition.value
50+
elif condition.operator == OperatorType.NOT_ONE_OF:
51+
return str(subject_value) not in condition.value
4652
else:
4753
return isinstance(
4854
subject_value, numbers.Number

test/rules_test.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,64 @@ def test_matches_rules_true_with_numeric_value_and_regex():
4545
)
4646
rule = Rule(conditions=[condition])
4747
assert matches_any_rule({"age": 99}, [rule]) is True
48+
49+
50+
def test_one_of_operator_with_boolean():
51+
oneOfRule = Rule(
52+
conditions=[
53+
Condition(operator=OperatorType.ONE_OF, value=["True"], attribute="enabled")
54+
]
55+
)
56+
notOneOfRule = Rule(
57+
conditions=[
58+
Condition(
59+
operator=OperatorType.NOT_ONE_OF, value=["True"], attribute="enabled"
60+
)
61+
]
62+
)
63+
assert matches_any_rule({"enabled": True}, [oneOfRule]) is True
64+
assert matches_any_rule({"enabled": False}, [oneOfRule]) is False
65+
assert matches_any_rule({"enabled": True}, [notOneOfRule]) is False
66+
assert matches_any_rule({"enabled": False}, [notOneOfRule]) is True
67+
68+
69+
def test_one_of_operator_with_string():
70+
oneOfRule = Rule(
71+
conditions=[
72+
Condition(
73+
operator=OperatorType.ONE_OF, value=["john", "ron"], attribute="name"
74+
)
75+
]
76+
)
77+
notOneOfRule = Rule(
78+
conditions=[
79+
Condition(operator=OperatorType.NOT_ONE_OF, value=["ron"], attribute="name")
80+
]
81+
)
82+
assert matches_any_rule({"name": "john"}, [oneOfRule]) is True
83+
assert matches_any_rule({"name": "ron"}, [oneOfRule]) is True
84+
assert matches_any_rule({"name": "sam"}, [oneOfRule]) is False
85+
assert matches_any_rule({"name": "ron"}, [notOneOfRule]) is False
86+
assert matches_any_rule({"name": "sam"}, [notOneOfRule]) is True
87+
88+
89+
def test_one_of_operator_with_number():
90+
oneOfRule = Rule(
91+
conditions=[
92+
Condition(
93+
operator=OperatorType.ONE_OF, value=["14", "15.11"], attribute="number"
94+
)
95+
]
96+
)
97+
notOneOfRule = Rule(
98+
conditions=[
99+
Condition(
100+
operator=OperatorType.NOT_ONE_OF, value=["10"], attribute="number"
101+
)
102+
]
103+
)
104+
assert matches_any_rule({"number": "14"}, [oneOfRule]) is True
105+
assert matches_any_rule({"number": 15.11}, [oneOfRule]) is True
106+
assert matches_any_rule({"number": "10"}, [oneOfRule]) is False
107+
assert matches_any_rule({"number": "10"}, [notOneOfRule]) is False
108+
assert matches_any_rule({"number": 11}, [notOneOfRule]) is True

0 commit comments

Comments
 (0)