1+ import pytest
12from typing import Any
23from unittest .mock import Mock
34
45import sentry_sdk
56from sentry_sdk .integrations .unleash import UnleashIntegration
67
7- import pytest
8-
98
109@pytest .fixture
1110def 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\n csbq 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-
6845def 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 ()
0 commit comments