forked from mixpanel/mixpanel-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mixpanel.py
437 lines (379 loc) · 15.3 KB
/
test_mixpanel.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from __future__ import absolute_import, unicode_literals
import base64
import cgi
import contextlib
import datetime
import decimal
import json
import time
from mock import Mock, patch
import pytest
import six
from six.moves import range, urllib
import mixpanel
class LogConsumer(object):
def __init__(self):
self.log = []
def send(self, endpoint, event, api_key=None):
if api_key:
self.log.append((endpoint, json.loads(event), api_key))
else:
self.log.append((endpoint, json.loads(event)))
# Convert a query string with base64 data into a dict for safe comparison.
def qs(s):
if isinstance(s, six.binary_type):
s = s.decode('utf8')
blob = cgi.parse_qs(s)
if len(blob['data']) != 1:
pytest.fail('found multi-item data: %s' % blob['data'])
json_bytes = base64.b64decode(blob['data'][0])
blob['data'] = json.loads(json_bytes.decode('utf8'))
return blob
class TestMixpanel:
TOKEN = '12345'
def setup_method(self, method):
self.consumer = LogConsumer()
self.mp = mixpanel.Mixpanel('12345', consumer=self.consumer)
self.mp._now = lambda: 1000.1
def test_track(self):
self.mp.track('ID', 'button press', {'size': 'big', 'color': 'blue'})
assert self.consumer.log == [(
'events', {
'event': 'button press',
'properties': {
'token': self.TOKEN,
'size': 'big',
'color': 'blue',
'distinct_id': 'ID',
'time': int(self.mp._now()),
'mp_lib': 'python',
'$lib_version': mixpanel.__version__,
}
}
)]
def test_import_data(self):
timestamp = time.time()
self.mp.import_data('MY_API_KEY', 'ID', 'button press', timestamp, {'size': 'big', 'color': 'blue'})
assert self.consumer.log == [(
'imports', {
'event': 'button press',
'properties': {
'token': self.TOKEN,
'size': 'big',
'color': 'blue',
'distinct_id': 'ID',
'time': int(timestamp),
'mp_lib': 'python',
'$lib_version': mixpanel.__version__,
},
},
'MY_API_KEY'
)]
def test_track_meta(self):
self.mp.track('ID', 'button press', {'size': 'big', 'color': 'blue'},
meta={'ip': 0})
assert self.consumer.log == [(
'events', {
'event': 'button press',
'properties': {
'token': self.TOKEN,
'size': 'big',
'color': 'blue',
'distinct_id': 'ID',
'time': int(self.mp._now()),
'mp_lib': 'python',
'$lib_version': mixpanel.__version__,
},
'ip': 0,
}
)]
def test_people_set(self):
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$set': {
'birth month': 'october',
'favorite color': 'purple',
},
}
)]
def test_people_set_once(self):
self.mp.people_set_once('amq', {'birth month': 'october', 'favorite color': 'purple'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$set_once': {
'birth month': 'october',
'favorite color': 'purple',
},
}
)]
def test_people_increment(self):
self.mp.people_increment('amq', {'Albums Released': 1})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$add': {
'Albums Released': 1,
},
}
)]
def test_people_append(self):
self.mp.people_append('amq', {'birth month': 'october', 'favorite color': 'purple'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$append': {
'birth month': 'october',
'favorite color': 'purple',
},
}
)]
def test_people_union(self):
self.mp.people_union('amq', {'Albums': ['Diamond Dogs']})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$union': {
'Albums': ['Diamond Dogs'],
},
}
)]
def test_people_unset(self):
self.mp.people_unset('amq', ['Albums', 'Singles'])
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$unset': ['Albums', 'Singles'],
}
)]
def test_people_track_charge(self):
self.mp.people_track_charge('amq', 12.65, {'$time': '2013-04-01T09:02:00'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$append': {
'$transactions': {
'$time': '2013-04-01T09:02:00',
'$amount': 12.65,
},
},
}
)]
def test_people_track_charge_without_properties(self):
self.mp.people_track_charge('amq', 12.65)
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$append': {
'$transactions': {
'$amount': 12.65,
},
},
}
)]
def test_people_clear_charges(self):
self.mp.people_clear_charges('amq')
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$unset': ['$transactions'],
}
)]
def test_people_set_created_date_string(self):
created = '2014-02-14T01:02:03'
self.mp.people_set('amq', {'$created': created, 'favorite color': 'purple'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$set': {
'$created': created,
'favorite color': 'purple',
},
}
)]
def test_people_set_created_date_datetime(self):
created = datetime.datetime(2014, 2, 14, 1, 2, 3)
self.mp.people_set('amq', {'$created': created, 'favorite color': 'purple'})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$set': {
'$created': '2014-02-14T01:02:03',
'favorite color': 'purple',
},
}
)]
def test_alias(self):
mock_response = Mock()
mock_response.read.return_value = six.b('{"status":1, "error": null}')
with patch('six.moves.urllib.request.urlopen', return_value=mock_response) as urlopen:
self.mp.alias('ALIAS', 'ORIGINAL ID')
assert self.consumer.log == []
assert urlopen.call_count == 1
((request,), _) = urlopen.call_args
assert request.get_full_url() == 'https://api.mixpanel.com/track'
assert qs(request.data) == \
qs('ip=0&data=eyJldmVudCI6IiRjcmVhdGVfYWxpYXMiLCJwcm9wZXJ0aWVzIjp7ImFsaWFzIjoiQUxJQVMiLCJ0b2tlbiI6IjEyMzQ1IiwiZGlzdGluY3RfaWQiOiJPUklHSU5BTCBJRCJ9fQ%3D%3D&verbose=1')
def test_people_meta(self):
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'},
meta={'$ip': 0, '$ignore_time': True})
assert self.consumer.log == [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$set': {
'birth month': 'october',
'favorite color': 'purple',
},
'$ip': 0,
'$ignore_time': True,
}
)]
def test_custom_json_serializer(self):
decimal_string = '12.05'
with pytest.raises(TypeError) as excinfo:
self.mp.track('ID', 'button press', {'size': decimal.Decimal(decimal_string)})
assert "Decimal('%s') is not JSON serializable" % decimal_string in str(excinfo.value)
class CustomSerializer(mixpanel.DatetimeSerializer):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return obj.to_eng_string()
self.mp._serializer = CustomSerializer
self.mp.track('ID', 'button press', {'size': decimal.Decimal(decimal_string)})
assert self.consumer.log == [(
'events', {
'event': 'button press',
'properties': {
'token': self.TOKEN,
'size': decimal_string,
'distinct_id': 'ID',
'time': int(self.mp._now()),
'mp_lib': 'python',
'$lib_version': mixpanel.__version__,
}
}
)]
class TestConsumer:
@classmethod
def setup_class(cls):
cls.consumer = mixpanel.Consumer(request_timeout=30)
@contextlib.contextmanager
def _assertSends(self, expect_url, expect_data):
mock_response = Mock()
mock_response.read.return_value = six.b('{"status":1, "error": null}')
with patch('six.moves.urllib.request.urlopen', return_value=mock_response) as urlopen:
yield
assert urlopen.call_count == 1
(call_args, kwargs) = urlopen.call_args
(request,) = call_args
timeout = kwargs.get('timeout', None)
assert request.get_full_url() == expect_url
assert qs(request.data) == qs(expect_data)
assert timeout == self.consumer._request_timeout
def test_send_events(self):
with self._assertSends('https://api.mixpanel.com/track', 'ip=0&data=IkV2ZW50Ig%3D%3D&verbose=1'):
self.consumer.send('events', '"Event"')
def test_send_people(self):
with self._assertSends('https://api.mixpanel.com/engage', 'ip=0&data=IlBlb3BsZSI%3D&verbose=1'):
self.consumer.send('people', '"People"')
def test_unknown_endpoint(self):
with pytest.raises(mixpanel.MixpanelException):
self.consumer.send('unknown', '1')
class TestBufferedConsumer:
@classmethod
def setup_class(cls):
cls.MAX_LENGTH = 10
cls.consumer = mixpanel.BufferedConsumer(cls.MAX_LENGTH)
cls.consumer._consumer = LogConsumer()
cls.log = cls.consumer._consumer.log
def setup_method(self):
del self.log[:]
def test_buffer_hold_and_flush(self):
self.consumer.send('events', '"Event"')
assert len(self.log) == 0
self.consumer.flush()
assert self.log == [('events', ['Event'])]
def test_buffer_fills_up(self):
for i in range(self.MAX_LENGTH - 1):
self.consumer.send('events', '"Event"')
assert len(self.log) == 0
self.consumer.send('events', '"Last Event"')
assert len(self.log) == 1
assert self.log == [('events', [
'Event', 'Event', 'Event', 'Event', 'Event',
'Event', 'Event', 'Event', 'Event', 'Last Event',
])]
def test_unknown_endpoint_raises_on_send(self):
# Ensure the exception isn't hidden until a flush.
with pytest.raises(mixpanel.MixpanelException):
self.consumer.send('unknown', '1')
def test_useful_reraise_in_flush_endpoint(self):
error_mock = Mock()
error_mock.read.return_value = six.b('{"status": 0, "error": "arbitrary error"}')
broken_json = '{broken JSON'
consumer = mixpanel.BufferedConsumer(2)
with patch('six.moves.urllib.request.urlopen', return_value=error_mock):
consumer.send('events', broken_json)
with pytest.raises(mixpanel.MixpanelException) as excinfo:
consumer.flush()
assert excinfo.value.message == '[%s]' % broken_json
assert excinfo.value.endpoint == 'events'
def test_send_remembers_api_key(self):
self.consumer.send('imports', '"Event"', api_key='MY_API_KEY')
assert len(self.log) == 0
self.consumer.flush()
assert self.log == [('imports', ['Event'], 'MY_API_KEY')]
class TestFunctional:
@classmethod
def setup_class(cls):
cls.TOKEN = '12345'
cls.mp = mixpanel.Mixpanel(cls.TOKEN)
cls.mp._now = lambda: 1000
@contextlib.contextmanager
def _assertRequested(self, expect_url, expect_data):
mock_response = Mock()
mock_response.read.return_value = six.b('{"status":1, "error": null}')
with patch('six.moves.urllib.request.urlopen', return_value=mock_response) as urlopen:
yield
assert urlopen.call_count == 1
((request,), _) = urlopen.call_args
assert request.get_full_url() == expect_url
data = urllib.parse.parse_qs(request.data.decode('utf8'))
assert len(data['data']) == 1
payload_encoded = data['data'][0]
payload_json = base64.b64decode(payload_encoded).decode('utf8')
payload = json.loads(payload_json)
assert payload == expect_data
def test_track_functional(self):
expect_data = {'event': {'color': 'blue', 'size': 'big'}, 'properties': {'mp_lib': 'python', 'token': '12345', 'distinct_id': 'button press', '$lib_version': mixpanel.__version__, 'time': 1000}}
with self._assertRequested('https://api.mixpanel.com/track', expect_data):
self.mp.track('button press', {'size': 'big', 'color': 'blue'})
def test_people_set_functional(self):
expect_data = {'$distinct_id': 'amq', '$set': {'birth month': 'october', 'favorite color': 'purple'}, '$time': 1000000, '$token': '12345'}
with self._assertRequested('https://api.mixpanel.com/engage', expect_data):
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'})