Skip to content

Commit

Permalink
Fix segment condition serialization when property is None (#42)
Browse files Browse the repository at this point in the history
* Fix segment condition serialization when `property` is None

* Add basic unit test to exercise issue
  • Loading branch information
matthewelwell authored Nov 29, 2021
1 parent b7c1fdf commit fb2396d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion flag_engine/segments/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Meta:
model_class = SegmentConditionModel

def serialize_property(self, obj: typing.Any) -> str:
return getattr(obj, "property", None) or getattr(obj, "property_")
# Note that property can be None for e.g. percentage split operator
return getattr(obj, "property", None) or getattr(obj, "property_", None)

def deserialize_property(self, value: str):
return value
Expand Down
2 changes: 1 addition & 1 deletion tests/mock_django_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@dataclass
class DjangoSegmentCondition:
operator: str
property: str
value: typing.Any
property: str = None # property can be none e.g. for % split operator


@dataclass
Expand Down
20 changes: 18 additions & 2 deletions tests/segments/test_segments_schemas.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from flag_engine.features.constants import STANDARD
from flag_engine.features.models import FeatureModel, FeatureStateModel
from flag_engine.segments.constants import ALL_RULE, EQUAL
from flag_engine.segments.constants import ALL_RULE, EQUAL, PERCENTAGE_SPLIT
from flag_engine.segments.models import (
SegmentConditionModel,
SegmentModel,
SegmentRuleModel,
)
from flag_engine.segments.schemas import SegmentSchema
from flag_engine.segments.schemas import SegmentConditionSchema, SegmentSchema
from tests.mock_django_classes import (
DjangoFeature,
DjangoFeatureSegment,
Expand Down Expand Up @@ -128,3 +128,19 @@ def test_dict_to_segment_model():
assert segment_model.id == segment_dict["id"]
assert len(segment_model.rules) == 1
assert len(segment_model.feature_states) == 1


def test_segment_condition_schema_dump_when_property_is_none():
# Given
schema = SegmentConditionSchema()
mock_django_segment_condition = DjangoSegmentCondition(
operator=PERCENTAGE_SPLIT, value=10
)

# When
data = schema.dump(mock_django_segment_condition)

# Then
assert data["value"] == mock_django_segment_condition.value
assert data["operator"] == mock_django_segment_condition.operator
assert data["property_"] is None

0 comments on commit fb2396d

Please sign in to comment.