Skip to content

Commit e974188

Browse files
committed
Test get_variant
1 parent d4c9b81 commit e974188

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

sentry_sdk/integrations/unleash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def sentry_get_variant(feature, *a, **kw):
5252
enabled = variant.get("enabled", False)
5353
payload_type = variant.get("payload", {}).get("type")
5454

55-
if payload_type is None or payload_type == "boolean":
55+
if payload_type is None:
5656
flags = sentry_sdk.get_current_scope().flags
5757
flags.set(feature, enabled)
5858

tests/integrations/unleash/test_unleash.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Any
12
from unittest.mock import Mock
23

34
import sentry_sdk
@@ -16,18 +17,37 @@ def mock_unleash_client():
1617
def is_enabled(feature: str, *a, **kw) -> bool:
1718
return features.get(feature, False)
1819

20+
feature_to_variant = {
21+
"str_feature": {
22+
"name": "variant1",
23+
"enabled": True,
24+
"payload": {"type": "string", "value": "val1"},
25+
},
26+
"json_feature": {
27+
"name": "variant1",
28+
"enabled": True,
29+
"payload": {"type": "json", "value": {"key1": 0.53}},
30+
},
31+
"toggle_feature": {"name": "variant1", "enabled": True},
32+
}
33+
34+
disabled_variant = {"name": "disabled", "enabled": False}
35+
36+
def get_variant(feature: str, *a, **kw) -> dict[str, Any]:
37+
return feature_to_variant.get(feature, disabled_variant)
38+
1939
client = Mock()
2040
client.is_enabled = is_enabled
41+
client.get_variant = get_variant
2142
return client
2243

2344

24-
def test_unleash_integration(
45+
def test_is_enabled(
2546
sentry_init, capture_events, uninstall_integration, mock_unleash_client
2647
):
2748
uninstall_integration(UnleashIntegration.identifier)
2849
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])
2950

30-
# Evaluate
3151
mock_unleash_client.is_enabled("hello")
3252
mock_unleash_client.is_enabled("world")
3353
mock_unleash_client.is_enabled("other")
@@ -43,3 +63,26 @@ def test_unleash_integration(
4363
{"flag": "other", "result": False},
4464
]
4565
}
66+
67+
68+
def test_get_variant(
69+
sentry_init, capture_events, uninstall_integration, mock_unleash_client
70+
):
71+
uninstall_integration(UnleashIntegration.identifier)
72+
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])
73+
74+
mock_unleash_client.get_variant("toggle_feature")
75+
mock_unleash_client.get_variant("str_feature")
76+
mock_unleash_client.get_variant("json_feature")
77+
mock_unleash_client.get_variant("unknown_feature")
78+
79+
events = capture_events()
80+
sentry_sdk.capture_exception(Exception("something wrong!"))
81+
82+
assert len(events) == 1
83+
assert events[0]["contexts"]["flags"] == {
84+
"values": [
85+
{"flag": "toggle_feature", "result": True},
86+
{"flag": "unknown_feature", "result": False},
87+
]
88+
}

0 commit comments

Comments
 (0)