Skip to content

Commit 4ed9751

Browse files
committed
improve logic, update tests
1 parent 05bc0b9 commit 4ed9751

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

flag_engine/segments/evaluator.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,15 @@ def context_matches_condition(
216216
if condition["operator"] == constants.PERCENTAGE_SPLIT:
217217
if context_value is not None:
218218
object_ids = [segment_key, context_value]
219-
elif (
220-
identity_key := (
221-
(identity_context := context.get("identity"))
222-
and identity_context["key"]
223-
)
224-
is not None
225-
):
226-
object_ids = [segment_key, identity_key]
219+
elif identity_context := context.get("identity"):
220+
object_ids = [segment_key, identity_context["key"]]
227221
else:
228222
return False
229223

230-
float_value = float(condition["value"])
224+
try:
225+
float_value = float(condition["value"])
226+
except ValueError:
227+
return False
231228
return get_hashed_percentage_for_object_ids(object_ids) <= float_value
232229

233230
if condition["operator"] == constants.IS_NOT_SET:

tests/unit/segments/test_segments_evaluator.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ def test_context_in_segment(
224224

225225
@pytest.mark.parametrize(
226226
"segment_split_value, identity_hashed_percentage, expected_result",
227-
((10, 1, True), (100, 50, True), (0, 1, False), (10, 20, False)),
227+
(
228+
(10, 1, True),
229+
(100, 50, True),
230+
(0, 1, False),
231+
(10, 20, False),
232+
("invalid", 100, False),
233+
),
228234
)
229235
def test_context_in_segment_percentage_split(
230236
mocker: MockerFixture,
@@ -270,6 +276,49 @@ def test_context_in_segment_percentage_split(
270276
assert result == expected_result
271277

272278

279+
def test_context_in_segment_percentage_split__no_identity__returns_expected(
280+
mocker: MockerFixture,
281+
context: EvaluationContext,
282+
) -> None:
283+
# Given
284+
del context["identity"]
285+
286+
segment_context: SegmentContext = {
287+
"key": "1",
288+
"name": "% split",
289+
"rules": [
290+
{
291+
"type": constants.ALL_RULE,
292+
"conditions": [],
293+
"rules": [
294+
{
295+
"type": constants.ALL_RULE,
296+
"conditions": [
297+
{
298+
"operator": constants.PERCENTAGE_SPLIT,
299+
"property": "",
300+
"value": "10",
301+
}
302+
],
303+
"rules": [],
304+
}
305+
],
306+
}
307+
],
308+
}
309+
310+
mock_get_hashed_percentage = mocker.patch(
311+
"flag_engine.segments.evaluator.get_hashed_percentage_for_object_ids"
312+
)
313+
314+
# When
315+
result = is_context_in_segment(context=context, segment_context=segment_context)
316+
317+
# Then
318+
mock_get_hashed_percentage.assert_not_called()
319+
assert result is False
320+
321+
273322
def test_context_in_segment_percentage_split__trait_value__calls_expected(
274323
mocker: MockerFixture,
275324
context: EvaluationContext,

0 commit comments

Comments
 (0)