This repository was archived by the owner on May 5, 2022. It is now read-only.
forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ldclient.py
More file actions
261 lines (184 loc) · 6.92 KB
/
Copy pathtest_ldclient.py
File metadata and controls
261 lines (184 loc) · 6.92 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from builtins import object
from ldclient.client import LDClient, Config
from ldclient.feature_store import InMemoryFeatureStore
from ldclient.interfaces import FeatureRequester, FeatureStore
import pytest
from testing.sync_util import wait_until
try:
import queue
except:
import Queue as queue
class MockFeatureStore(FeatureStore):
def delete(self, key, version):
pass
@property
def initialized(self):
pass
def init(self, features):
pass
def all(self):
pass
def upsert(self, key, feature):
pass
def __init__(self, *_):
pass
def get(self, key):
if key == "feature.key":
return {
u'key': u'feature.key',
u'salt': u'abc',
u'on': True,
u'variations': [
{
u'value': True,
u'weight': 100,
u'targets': []
},
{
u'value': False,
u'weight': 0,
u'targets': []
}
]
}
else:
return None
client = LDClient(config=Config(base_uri="http://localhost:3000", feature_store=MockFeatureStore()))
offline_client = LDClient(config=
Config(sdk_key="secret", base_uri="http://localhost:3000", feature_store=MockFeatureStore(),
offline=True))
user = {
u'key': u'xyz',
u'custom': {
u'bizzle': u'def'
}
}
numeric_key_user = {}
sanitized_numeric_key_user = {
u'key': '33',
u'custom': {
u'bizzle': u'def'
}
}
class MockConsumer(object):
def __init__(self, *_):
self._running = False
def stop(self):
self._running = False
def start(self):
self._running = True
def is_alive(self):
return self._running
def flush(self):
pass
class MockFeatureRequester(FeatureRequester):
def __init__(self, *_):
pass
def get_all(self):
pass
def mock_consumer():
return MockConsumer()
def noop_consumer():
return
def setup_function(function):
global numeric_key_user
numeric_key_user = {
u'key': 33,
u'custom': {
u'bizzle': u'def'
}
}
client._queue = queue.Queue(10)
client._event_consumer = mock_consumer()
def wait_for_event(c, cb):
e = c._queue.get(False)
return cb(e)
def test_ctor_both_sdk_keys_set():
with pytest.raises(Exception):
config = Config(sdk_key="sdk key a", offline=True)
LDClient(sdk_key="sdk key b", config=config)
def test_toggle_offline():
assert offline_client.variation('feature.key', user, default=None) is None
def test_sanitize_user():
client._sanitize_user(numeric_key_user)
assert numeric_key_user == sanitized_numeric_key_user
def test_toggle_event_offline():
offline_client.variation('feature.key', user, default=None)
assert offline_client._queue.empty()
def test_identify():
client.identify(user)
def expected_event(e):
return e['kind'] == 'identify' and e['key'] == u'xyz' and e['user'] == user
assert expected_event(client._queue.get(False))
def test_identify_numeric_key_user():
client.identify(numeric_key_user)
def expected_event(e):
return e['kind'] == 'identify' and e['key'] == '33' and e['user'] == sanitized_numeric_key_user
assert expected_event(client._queue.get(False))
def test_identify_offline():
assert offline_client._queue.empty()
def test_track():
client.track('my_event', user, 42)
def expected_event(e):
return e['kind'] == 'custom' and e['key'] == 'my_event' and e['user'] == user and e['data'] == 42
assert expected_event(client._queue.get(False))
def test_track_numeric_key_user():
client.track('my_event', numeric_key_user, 42)
def expected_event(e):
return e['kind'] == 'custom' and e['key'] == 'my_event' and e['user'] == sanitized_numeric_key_user \
and e['data'] == 42
assert expected_event(client._queue.get(False))
def test_track_offline():
offline_client.track('my_event', user, 42)
assert offline_client._queue.empty()
def test_defaults():
client = LDClient(config=Config(base_uri="http://localhost:3000",
defaults={"foo": "bar"},
offline=True))
assert "bar" == client.variation('foo', user, default=None)
def test_defaults_and_online():
expected = "bar"
my_client = LDClient(config=Config(base_uri="http://localhost:3000",
defaults={"foo": expected},
event_consumer_class=MockConsumer,
feature_requester_class=MockFeatureRequester,
feature_store=InMemoryFeatureStore()))
actual = my_client.variation('foo', user, default="originalDefault")
assert actual == expected
assert wait_for_event(my_client, lambda e: e['kind'] == 'feature' and e['key'] == u'foo' and e['user'] == user)
def test_defaults_and_online_no_default():
client = LDClient(config=Config(base_uri="http://localhost:3000",
defaults={"foo": "bar"},
event_consumer_class=MockConsumer,
feature_requester_class=MockFeatureRequester))
assert "jim" == client.variation('baz', user, default="jim")
assert wait_for_event(client, lambda e: e['kind'] == 'feature' and e['key'] == u'baz' and e['user'] == user)
def test_exception_in_retrieval():
class ExceptionFeatureRequester(FeatureRequester):
def __init__(self, *_):
pass
def get_all(self):
raise Exception("blah")
client = LDClient(config=Config(base_uri="http://localhost:3000",
defaults={"foo": "bar"},
feature_store=InMemoryFeatureStore(),
feature_requester_class=ExceptionFeatureRequester,
event_consumer_class=MockConsumer))
assert "bar" == client.variation('foo', user, default="jim")
assert wait_for_event(client, lambda e: e['kind'] == 'feature' and e['key'] == u'foo' and e['user'] == user)
def test_no_defaults():
assert "bar" == offline_client.variation('foo', user, default="bar")
def test_secure_mode_hash():
user = {'key': 'Message'}
assert offline_client.secure_mode_hash(user) == "aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597"
def drain(queue):
while not queue.empty():
queue.get()
queue.task_done()
return
def test_flush_empties_queue():
client.track('my_event', user, 42)
client.track('my_event', user, 33)
drain(client._queue)
client.flush()
assert client._queue.empty()