Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segment condition serialization when property is None #42

Merged
merged 2 commits into from
Nov 29, 2021
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
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