|
24 | 24 | } |
25 | 25 | } |
26 | 26 |
|
| 27 | +anonymous_user = { |
| 28 | + u'key': u'abc', |
| 29 | + u'anonymous': True |
| 30 | +} |
27 | 31 |
|
28 | 32 | def make_client(store = InMemoryFeatureStore()): |
29 | 33 | return LDClient(config=Config(sdk_key = 'SDK_KEY', |
@@ -172,6 +176,26 @@ def test_track_no_user_key(): |
172 | 176 | assert count_events(client) == 0 |
173 | 177 |
|
174 | 178 |
|
| 179 | +def test_track_anonymous_user(): |
| 180 | + with make_client() as client: |
| 181 | + client.track('my_event', anonymous_user) |
| 182 | + e = get_first_event(client) |
| 183 | + assert e['kind'] == 'custom' and e['key'] == 'my_event' and e['user'] == anonymous_user and e.get('data') is None and e.get('metricValue') is None and e.get('contextKind') == 'anonymousUser' |
| 184 | + |
| 185 | + |
| 186 | +def test_alias(): |
| 187 | + with make_client() as client: |
| 188 | + client.alias(user, anonymous_user) |
| 189 | + e = get_first_event(client) |
| 190 | + assert e['kind'] == 'alias' and e['key'] == 'xyz' and e['contextKind'] == 'user' and e['previousKey'] == 'abc' and e['previousContextKind'] == 'anonymousUser' |
| 191 | + |
| 192 | + |
| 193 | +def test_alias_no_user(): |
| 194 | + with make_client() as client: |
| 195 | + client.alias(None, None) |
| 196 | + assert count_events(client) == 0 |
| 197 | + |
| 198 | + |
175 | 199 | def test_defaults(): |
176 | 200 | config=Config("SDK_KEY", base_uri="http://localhost:3000", defaults={"foo": "bar"}, offline=True) |
177 | 201 | with LDClient(config=config) as client: |
@@ -226,7 +250,30 @@ def test_event_for_existing_feature(): |
226 | 250 | e.get('reason') is None and |
227 | 251 | e['default'] == 'default' and |
228 | 252 | e['trackEvents'] == True and |
229 | | - e['debugEventsUntilDate'] == 1000) |
| 253 | + e['debugEventsUntilDate'] == 1000 and |
| 254 | + e.get('contextKind') is None) |
| 255 | + |
| 256 | + |
| 257 | +def test_event_for_existing_feature_anonymous_user(): |
| 258 | + feature = make_off_flag_with_value('feature.key', 'value') |
| 259 | + feature['trackEvents'] = True |
| 260 | + feature['debugEventsUntilDate'] = 1000 |
| 261 | + store = InMemoryFeatureStore() |
| 262 | + store.init({FEATURES: {'feature.key': feature}}) |
| 263 | + with make_client(store) as client: |
| 264 | + assert 'value' == client.variation('feature.key', anonymous_user, default='default') |
| 265 | + e = get_first_event(client) |
| 266 | + assert (e['kind'] == 'feature' and |
| 267 | + e['key'] == 'feature.key' and |
| 268 | + e['user'] == anonymous_user and |
| 269 | + e['version'] == feature['version'] and |
| 270 | + e['value'] == 'value' and |
| 271 | + e['variation'] == 0 and |
| 272 | + e.get('reason') is None and |
| 273 | + e['default'] == 'default' and |
| 274 | + e['trackEvents'] == True and |
| 275 | + e['debugEventsUntilDate'] == 1000 and |
| 276 | + e['contextKind'] == 'anonymousUser') |
230 | 277 |
|
231 | 278 |
|
232 | 279 | def test_event_for_existing_feature_with_reason(): |
|
0 commit comments