Skip to content

Commit fc5bf44

Browse files
authored
fix: added-none-check-in-context-matches-condition (#236)
* fix: added-none-check-in-context-matches-condition * fix: unused-import * fix: renamed-test-function * fix: lint
1 parent f43dcc8 commit fc5bf44

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

flag_engine/segments/evaluator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ def context_matches_condition(
100100
if condition.operator == constants.IS_SET:
101101
return context_value is not None
102102

103-
return _matches_context_value(condition, context_value) if context_value else False
103+
return (
104+
_matches_context_value(condition, context_value)
105+
if context_value is not None
106+
else False
107+
)
104108

105109

106110
def _get_trait(context: EvaluationContext, trait_key: str) -> ContextValue:

tests/unit/segments/test_segments_evaluator.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
from flag_engine.context.types import EvaluationContext
88
from flag_engine.segments import constants
9-
from flag_engine.segments.evaluator import _matches_context_value, is_context_in_segment
9+
from flag_engine.segments.evaluator import (
10+
_matches_context_value,
11+
context_matches_condition,
12+
is_context_in_segment,
13+
)
1014
from flag_engine.segments.models import (
1115
SegmentConditionModel,
1216
SegmentModel,
@@ -479,6 +483,63 @@ def test_segment_condition_matches_context_value_for_semver(
479483
assert result == expected_result
480484

481485

486+
@pytest.mark.parametrize(
487+
"context,condition,segment_key,expected_result",
488+
(
489+
(
490+
{"identity": {"traits": {trait_key_1: False}}},
491+
SegmentConditionModel(
492+
operator=constants.EQUAL,
493+
property_=trait_key_1,
494+
value="false",
495+
),
496+
"segment_key",
497+
True,
498+
),
499+
(
500+
{"identity": {"traits": {trait_key_1: True}}},
501+
SegmentConditionModel(
502+
operator=constants.EQUAL,
503+
property_=trait_key_1,
504+
value="true",
505+
),
506+
"segment_key",
507+
True,
508+
),
509+
(
510+
{"identity": {"traits": {trait_key_1: 12}}},
511+
SegmentConditionModel(
512+
operator=constants.EQUAL,
513+
property_=trait_key_1,
514+
value="12",
515+
),
516+
"segment_key",
517+
True,
518+
),
519+
(
520+
{"identity": {"traits": {trait_key_1: None}}},
521+
SegmentConditionModel(
522+
operator=constants.IS_SET,
523+
property_=trait_key_1,
524+
value="false",
525+
),
526+
"segment_key",
527+
False,
528+
),
529+
),
530+
)
531+
def test_context_matches_condition(
532+
context: EvaluationContext,
533+
condition: SegmentConditionModel,
534+
segment_key: str,
535+
expected_result: bool,
536+
) -> None:
537+
# Given / When
538+
result = context_matches_condition(context, condition, segment_key)
539+
# Then
540+
assert result == expected_result
541+
542+
482543
@pytest.mark.parametrize(
483544
"trait_value, condition_value, expected_result",
484545
[

0 commit comments

Comments
 (0)