Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions flagsmith/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ class Flag(BaseFlag):
def from_evaluation_result(
cls,
flag_result: SDKFlagResult,
) -> typing.Optional[Flag]:
) -> Flag:
if metadata := flag_result.get("metadata"):
return Flag(
enabled=flag_result["enabled"],
value=flag_result["value"],
feature_name=flag_result["name"],
feature_id=metadata["flagsmith_id"],
)
return None
raise ValueError(
"FlagResult metadata is missing. Cannot create Flag instance. "
"This means a bug in the SDK, please report it."
)

@classmethod
def from_api_flag(cls, flag_data: typing.Mapping[str, typing.Any]) -> Flag:
Expand Down
22 changes: 15 additions & 7 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ def test_flag_from_evaluation_result() -> None:
"value": 42,
"metadata": {"flagsmith_id": 3},
},
"feature4": {
"enabled": True,
"feature_key": "4",
"name": "feature4",
"reason": "DEFAULT",
"value": 42,
},
},
["feature1", "feature2", "feature3"],
),
Expand Down Expand Up @@ -151,3 +144,18 @@ def test_flag_from_evaluation_result_value_types(
# Then
assert flag
assert flag.value == expected


def test_flag_from_evaluation_result_missing_metadata__raises_expected() -> None:
# Given
flag_result: SDKFlagResult = {
"enabled": True,
"feature_key": "123",
"name": "test_feature",
"reason": "DEFAULT",
"value": "test-value",
}

# When & Then
with pytest.raises(ValueError):
Flag.from_evaluation_result(flag_result)