11import concurrent .futures as cf
22import sys
33from random import random
4- from unittest . mock import patch
4+ from unittest import mock
55
66import pytest
77
88import sentry_sdk
99from sentry_sdk .integrations .unleash import UnleashIntegration
1010from tests .integrations .unleash .testutils import MockUnleashClient
1111
12- original_is_enabled = MockUnleashClient .is_enabled
13- original_get_variant = MockUnleashClient .get_variant
1412
15-
16- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
1713def test_is_enabled (sentry_init , capture_events , uninstall_integration ):
1814 client = MockUnleashClient ()
1915 uninstall_integration (UnleashIntegration )
20- sentry_init (integrations = [UnleashIntegration ()])
16+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
2117
2218 client .is_enabled ("hello" )
2319 client .is_enabled ("world" )
@@ -36,11 +32,10 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
3632 }
3733
3834
39- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
4035def test_get_variant (sentry_init , capture_events , uninstall_integration ):
4136 client = MockUnleashClient ()
4237 uninstall_integration (UnleashIntegration )
43- sentry_init (integrations = [UnleashIntegration ()])
38+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
4439
4540 client .get_variant ("no_payload_feature" )
4641 client .get_variant ("string_feature" )
@@ -65,11 +60,10 @@ def test_get_variant(sentry_init, capture_events, uninstall_integration):
6560 }
6661
6762
68- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
6963def test_is_enabled_threaded (sentry_init , capture_events , uninstall_integration ):
7064 client = MockUnleashClient ()
7165 uninstall_integration (UnleashIntegration )
72- sentry_init (integrations = [UnleashIntegration ()])
66+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
7367 events = capture_events ()
7468
7569 def task (flag_key ):
@@ -113,11 +107,10 @@ def task(flag_key):
113107 }
114108
115109
116- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
117110def test_get_variant_threaded (sentry_init , capture_events , uninstall_integration ):
118111 client = MockUnleashClient ()
119112 uninstall_integration (UnleashIntegration )
120- sentry_init (integrations = [UnleashIntegration ()])
113+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
121114 events = capture_events ()
122115
123116 def task (flag_key ):
@@ -162,13 +155,12 @@ def task(flag_key):
162155
163156
164157@pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher" )
165- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
166158def test_is_enabled_asyncio (sentry_init , capture_events , uninstall_integration ):
167159 asyncio = pytest .importorskip ("asyncio" )
168160
169161 client = MockUnleashClient ()
170162 uninstall_integration (UnleashIntegration )
171- sentry_init (integrations = [UnleashIntegration ()])
163+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
172164 events = capture_events ()
173165
174166 async def task (flag_key ):
@@ -213,13 +205,12 @@ async def runner():
213205
214206
215207@pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher" )
216- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
217208def test_get_variant_asyncio (sentry_init , capture_events , uninstall_integration ):
218209 asyncio = pytest .importorskip ("asyncio" )
219210
220211 client = MockUnleashClient ()
221212 uninstall_integration (UnleashIntegration )
222- sentry_init (integrations = [UnleashIntegration ()])
213+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
223214 events = capture_events ()
224215
225216 async def task (flag_key ):
@@ -263,50 +254,41 @@ async def runner():
263254 }
264255
265256
266- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
267257def test_wraps_original (sentry_init , uninstall_integration ):
258+ client = MockUnleashClient ()
259+ mock_is_enabled = mock .Mock (return_value = random () < 0.5 )
260+ client .is_enabled = mock_is_enabled
261+ mock_get_variant = mock .Mock (return_value = {"enabled" : random () < 0.5 })
262+ client .get_variant = mock_get_variant
263+
268264 uninstall_integration (UnleashIntegration )
265+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
266+
267+ res = client .is_enabled ("test-flag" , "arg" , kwarg = 1 )
268+ assert res == mock_is_enabled .return_value
269+ assert mock_is_enabled .call_args == (
270+ ("test-flag" , "arg" ),
271+ {"kwarg" : 1 },
272+ )
273+
274+ res = client .get_variant ("test-flag" , "arg" , kwarg = 1 )
275+ assert res == mock_get_variant .return_value
276+ assert mock_get_variant .call_args == (
277+ ("test-flag" , "arg" ),
278+ {"kwarg" : 1 },
279+ )
280+
269281
270- with patch (
271- "sentry_sdk.integrations.unleash.UnleashClient.is_enabled"
272- ) as mock_is_enabled :
273- with patch (
274- "sentry_sdk.integrations.unleash.UnleashClient.get_variant"
275- ) as mock_get_variant :
276- mock_is_enabled .return_value = random () < 0.5
277- mock_get_variant .return_value = {"enabled" : random () < 0.5 }
278- sentry_init (integrations = [UnleashIntegration ()])
279- client = MockUnleashClient ()
280-
281- res = client .is_enabled ("test-flag" , "arg" , kwarg = 1 )
282- assert res == mock_is_enabled .return_value
283- assert mock_is_enabled .call_args == (
284- (client , "test-flag" , "arg" ),
285- {"kwarg" : 1 },
286- )
287-
288- res = client .get_variant ("test-flag" , "arg" , kwarg = 1 )
289- assert res == mock_get_variant .return_value
290- assert mock_get_variant .call_args == (
291- (client , "test-flag" , "arg" ),
292- {"kwarg" : 1 },
293- )
294-
295-
296- @patch ("sentry_sdk.integrations.unleash.UnleashClient" , MockUnleashClient )
297282def test_wrapper_attributes (sentry_init , uninstall_integration ):
283+ client = MockUnleashClient ()
284+ original_is_enabled = client .is_enabled
285+ original_get_variant = client .get_variant
286+
298287 uninstall_integration (UnleashIntegration )
299- sentry_init (integrations = [UnleashIntegration ()])
288+ sentry_init (integrations = [UnleashIntegration (client )]) # type: ignore
300289
301- client = MockUnleashClient ()
302290 assert client .is_enabled .__name__ == "is_enabled"
303291 assert client .is_enabled .__qualname__ == original_is_enabled .__qualname__
304- assert MockUnleashClient .is_enabled .__name__ == "is_enabled"
305- assert MockUnleashClient .is_enabled .__qualname__ == original_is_enabled .__qualname__
306292
307293 assert client .get_variant .__name__ == "get_variant"
308294 assert client .get_variant .__qualname__ == original_get_variant .__qualname__
309- assert MockUnleashClient .get_variant .__name__ == "get_variant"
310- assert (
311- MockUnleashClient .get_variant .__qualname__ == original_get_variant .__qualname__
312- )
0 commit comments