forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutils.py
More file actions
44 lines (33 loc) · 1.3 KB
/
Copy pathtestutils.py
File metadata and controls
44 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from contextlib import contextmanager
from UnleashClient import UnleashClient
@contextmanager
def mock_unleash_client():
"""
Temporarily replaces UnleashClient's methods with mock implementations
for testing.
This context manager swaps out UnleashClient's __init__ and is_enabled,
methods with mock versions from MockUnleashClient.
Original methods are restored when exiting the context.
After mocking the client class the integration can be initialized.
The methods on the mock client class are overridden by the
integration and flag tracking proceeds as expected.
Example:
with mock_unleash_client():
client = UnleashClient() # Uses mock implementation
sentry_init(integrations=[UnleashIntegration()])
"""
old_init = UnleashClient.__init__
old_is_enabled = UnleashClient.is_enabled
UnleashClient.__init__ = MockUnleashClient.__init__
UnleashClient.is_enabled = MockUnleashClient.is_enabled
yield
UnleashClient.__init__ = old_init
UnleashClient.is_enabled = old_is_enabled
class MockUnleashClient:
def __init__(self, *a, **kw):
self.features = {
"hello": True,
"world": False,
}
def is_enabled(self, feature, *a, **kw):
return self.features.get(feature, False)