Skip to content

Commit 3b78bd6

Browse files
committed
Test additional variant payload types and split up to 1 file per method
1 parent e974188 commit 3b78bd6

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

tests/integrations/unleash/test_unleash.py renamed to tests/integrations/unleash/test_unleash_get_variant_method.py

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
import pytest
12
from typing import Any
23
from unittest.mock import Mock
34

45
import sentry_sdk
56
from sentry_sdk.integrations.unleash import UnleashIntegration
67

7-
import pytest
8-
98

109
@pytest.fixture
1110
def mock_unleash_client():
12-
features = {
13-
"hello": True,
14-
"world": False,
15-
}
16-
17-
def is_enabled(feature: str, *a, **kw) -> bool:
18-
return features.get(feature, False)
19-
2011
feature_to_variant = {
21-
"str_feature": {
12+
"string_feature": {
2213
"name": "variant1",
2314
"enabled": True,
2415
"payload": {"type": "string", "value": "val1"},
2516
},
2617
"json_feature": {
2718
"name": "variant1",
2819
"enabled": True,
29-
"payload": {"type": "json", "value": {"key1": 0.53}},
20+
"payload": {"type": "json", "value": '{"key1": 0.53}'},
21+
},
22+
"number_feature": {
23+
"name": "variant1",
24+
"enabled": True,
25+
"payload": {"type": "number", "value": "134.5"},
26+
},
27+
"csv_feature": {
28+
"name": "variant1",
29+
"enabled": True,
30+
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
3031
},
3132
"toggle_feature": {"name": "variant1", "enabled": True},
3233
}
@@ -37,43 +38,21 @@ def get_variant(feature: str, *a, **kw) -> dict[str, Any]:
3738
return feature_to_variant.get(feature, disabled_variant)
3839

3940
client = Mock()
40-
client.is_enabled = is_enabled
4141
client.get_variant = get_variant
4242
return client
4343

4444

45-
def test_is_enabled(
46-
sentry_init, capture_events, uninstall_integration, mock_unleash_client
47-
):
48-
uninstall_integration(UnleashIntegration.identifier)
49-
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])
50-
51-
mock_unleash_client.is_enabled("hello")
52-
mock_unleash_client.is_enabled("world")
53-
mock_unleash_client.is_enabled("other")
54-
55-
events = capture_events()
56-
sentry_sdk.capture_exception(Exception("something wrong!"))
57-
58-
assert len(events) == 1
59-
assert events[0]["contexts"]["flags"] == {
60-
"values": [
61-
{"flag": "hello", "result": True},
62-
{"flag": "world", "result": False},
63-
{"flag": "other", "result": False},
64-
]
65-
}
66-
67-
6845
def test_get_variant(
6946
sentry_init, capture_events, uninstall_integration, mock_unleash_client
7047
):
7148
uninstall_integration(UnleashIntegration.identifier)
7249
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])
7350

7451
mock_unleash_client.get_variant("toggle_feature")
75-
mock_unleash_client.get_variant("str_feature")
52+
mock_unleash_client.get_variant("string_feature")
7653
mock_unleash_client.get_variant("json_feature")
54+
mock_unleash_client.get_variant("csv_feature")
55+
mock_unleash_client.get_variant("number_feature")
7756
mock_unleash_client.get_variant("unknown_feature")
7857

7958
events = capture_events()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
from unittest.mock import Mock
3+
4+
import sentry_sdk
5+
from sentry_sdk.integrations.unleash import UnleashIntegration
6+
7+
8+
@pytest.fixture
9+
def mock_unleash_client():
10+
features = {
11+
"hello": True,
12+
"world": False,
13+
}
14+
15+
def is_enabled(feature: str, *a, **kw) -> bool:
16+
return features.get(feature, False)
17+
18+
client = Mock()
19+
client.is_enabled = is_enabled
20+
return client
21+
22+
23+
def test_is_enabled(
24+
sentry_init, capture_events, uninstall_integration, mock_unleash_client
25+
):
26+
uninstall_integration(UnleashIntegration.identifier)
27+
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])
28+
29+
mock_unleash_client.is_enabled("hello")
30+
mock_unleash_client.is_enabled("world")
31+
mock_unleash_client.is_enabled("other")
32+
33+
events = capture_events()
34+
sentry_sdk.capture_exception(Exception("something wrong!"))
35+
36+
assert len(events) == 1
37+
assert events[0]["contexts"]["flags"] == {
38+
"values": [
39+
{"flag": "hello", "result": True},
40+
{"flag": "world", "result": False},
41+
{"flag": "other", "result": False},
42+
]
43+
}

0 commit comments

Comments
 (0)