Skip to content

Commit da54e49

Browse files
committed
wraps_original test
1 parent 38f279b commit da54e49

File tree

3 files changed

+83
-53
lines changed

3 files changed

+83
-53
lines changed
Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,3 @@
11
import pytest
22

33
pytest.importorskip("UnleashClient")
4-
5-
6-
class MockUnleashClient:
7-
8-
def __init__(self, *a, **kw):
9-
self.features = {
10-
"hello": True,
11-
"world": False,
12-
}
13-
14-
self.feature_to_variant = {
15-
"string_feature": {
16-
"name": "variant1",
17-
"enabled": True,
18-
"payload": {"type": "string", "value": "val1"},
19-
},
20-
"json_feature": {
21-
"name": "variant1",
22-
"enabled": True,
23-
"payload": {"type": "json", "value": '{"key1": 0.53}'},
24-
},
25-
"number_feature": {
26-
"name": "variant1",
27-
"enabled": True,
28-
"payload": {"type": "number", "value": "134.5"},
29-
},
30-
"csv_feature": {
31-
"name": "variant1",
32-
"enabled": True,
33-
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
34-
},
35-
"toggle_feature": {"name": "variant1", "enabled": True},
36-
}
37-
38-
self.disabled_variant = {"name": "disabled", "enabled": False}
39-
40-
def is_enabled(self, feature, *a, **kw):
41-
return self.features.get(feature, False)
42-
43-
def get_variant(self, feature, *a, **kw):
44-
return self.feature_to_variant.get(feature, self.disabled_variant)

tests/integrations/unleash/test_unleash.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
from random import random
12
from unittest.mock import patch
23

34
import sentry_sdk
45
from sentry_sdk.integrations.unleash import UnleashIntegration
5-
from tests.integrations.unleash import MockUnleashClient
6+
from tests.integrations.unleash.testutils import MockUnleashClient
67

78
original_is_enabled = MockUnleashClient.is_enabled
89
original_get_variant = MockUnleashClient.get_variant
910

1011

1112
@patch("sentry_sdk.integrations.unleash.UnleashClient", MockUnleashClient)
1213
def test_is_enabled(sentry_init, capture_events, reset_integration):
13-
mock_unleash_client = MockUnleashClient()
14+
client = MockUnleashClient()
1415
reset_integration(UnleashIntegration.identifier)
1516
sentry_init(integrations=[UnleashIntegration()])
1617

17-
mock_unleash_client.is_enabled("hello")
18-
mock_unleash_client.is_enabled("world")
19-
mock_unleash_client.is_enabled("other")
18+
client.is_enabled("hello")
19+
client.is_enabled("world")
20+
client.is_enabled("other")
2021

2122
events = capture_events()
2223
sentry_sdk.capture_exception(Exception("something wrong!"))
@@ -33,16 +34,16 @@ def test_is_enabled(sentry_init, capture_events, reset_integration):
3334

3435
@patch("sentry_sdk.integrations.unleash.UnleashClient", MockUnleashClient)
3536
def test_get_variant(sentry_init, capture_events, reset_integration):
36-
mock_unleash_client = MockUnleashClient()
37+
client = MockUnleashClient()
3738
reset_integration(UnleashIntegration.identifier)
3839
sentry_init(integrations=[UnleashIntegration()])
3940

40-
mock_unleash_client.get_variant("toggle_feature")
41-
mock_unleash_client.get_variant("string_feature")
42-
mock_unleash_client.get_variant("json_feature")
43-
mock_unleash_client.get_variant("csv_feature")
44-
mock_unleash_client.get_variant("number_feature")
45-
mock_unleash_client.get_variant("unknown_feature")
41+
client.get_variant("toggle_feature")
42+
client.get_variant("string_feature")
43+
client.get_variant("json_feature")
44+
client.get_variant("csv_feature")
45+
client.get_variant("number_feature")
46+
client.get_variant("unknown_feature")
4647

4748
events = capture_events()
4849
sentry_sdk.capture_exception(Exception("something wrong!"))
@@ -56,6 +57,37 @@ def test_get_variant(sentry_init, capture_events, reset_integration):
5657
}
5758

5859

60+
@patch("sentry_sdk.integrations.unleash.UnleashClient", MockUnleashClient)
61+
def test_wraps_original(sentry_init, reset_integration):
62+
reset_integration(UnleashIntegration.identifier)
63+
64+
with patch(
65+
"sentry_sdk.integrations.unleash.UnleashClient.is_enabled"
66+
) as mock_is_enabled:
67+
with patch(
68+
"sentry_sdk.integrations.unleash.UnleashClient.get_variant"
69+
) as mock_get_variant:
70+
mock_is_enabled.return_value = random() < 0.5
71+
mock_get_variant.return_value = {"enabled": random() < 0.5}
72+
sentry_init(integrations=[UnleashIntegration()])
73+
client = MockUnleashClient()
74+
75+
res = client.is_enabled("test-flag", "arg", kwarg=1)
76+
assert res == mock_is_enabled.return_value
77+
assert mock_is_enabled.call_args == (
78+
(client, "test-flag", "arg"),
79+
{"kwarg": 1},
80+
)
81+
82+
res = client.get_variant("test-flag", "arg", kwarg=1)
83+
assert res == mock_get_variant.return_value
84+
assert mock_get_variant.call_args == (
85+
(client, "test-flag", "arg"),
86+
{"kwarg": 1},
87+
)
88+
89+
90+
@patch("sentry_sdk.integrations.unleash.UnleashClient", MockUnleashClient)
5991
def test_wrapper_attributes(sentry_init, reset_integration):
6092
reset_integration(UnleashIntegration.identifier)
6193
sentry_init(integrations=[UnleashIntegration()])
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class MockUnleashClient:
2+
3+
def __init__(self, *a, **kw):
4+
self.features = {
5+
"hello": True,
6+
"world": False,
7+
}
8+
9+
self.feature_to_variant = {
10+
"string_feature": {
11+
"name": "variant1",
12+
"enabled": True,
13+
"payload": {"type": "string", "value": "val1"},
14+
},
15+
"json_feature": {
16+
"name": "variant1",
17+
"enabled": True,
18+
"payload": {"type": "json", "value": '{"key1": 0.53}'},
19+
},
20+
"number_feature": {
21+
"name": "variant1",
22+
"enabled": True,
23+
"payload": {"type": "number", "value": "134.5"},
24+
},
25+
"csv_feature": {
26+
"name": "variant1",
27+
"enabled": True,
28+
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
29+
},
30+
"toggle_feature": {"name": "variant1", "enabled": True},
31+
}
32+
33+
self.disabled_variant = {"name": "disabled", "enabled": False}
34+
35+
def is_enabled(self, feature, *a, **kw):
36+
return self.features.get(feature, False)
37+
38+
def get_variant(self, feature, *a, **kw):
39+
return self.feature_to_variant.get(feature, self.disabled_variant)

0 commit comments

Comments
 (0)