1+ from typing import Any
12from unittest .mock import Mock
23
34import 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