forked from mixpanel/mixpanel-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
executable file
·328 lines (286 loc) · 12.1 KB
/
tests.py
File metadata and controls
executable file
·328 lines (286 loc) · 12.1 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
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
#!/usr/bin/env python
import base64
import contextlib
import json
import time
import unittest
import urlparse
try:
from mock import Mock, patch
except ImportError:
print 'mixpanel-python requires the mock package to run the test suite'
raise
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)))
class MixpanelTestCase(unittest.TestCase):
def setUp(self):
self.TOKEN = '12345'
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'})
self.assertEqual(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):
" Unit test for the `import_data` method. "
timestamp = time.time()
self.mp.import_data('MY_API_KEY', 'ID', 'button press', timestamp, {'size': 'big', 'color': 'blue'})
self.assertEqual(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})
self.assertEqual(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'})
self.assertEqual(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'})
self.assertEqual(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})
self.assertEqual(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'})
self.assertEqual(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'] })
self.assertEqual(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' ])
self.assertEqual(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' })
self.assertEqual(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_clear_charges(self):
self.mp.people_clear_charges('amq')
self.assertEqual(self.consumer.log, [(
'people', {
'$time': int(self.mp._now() * 1000),
'$token': self.TOKEN,
'$distinct_id': 'amq',
'$unset': [ '$transactions' ],
}
)])
def test_alias(self):
mock_response = Mock()
mock_response.read.return_value = '{"status":1, "error": null}'
with patch('urllib2.urlopen', return_value = mock_response) as urlopen:
self.mp.alias('ALIAS','ORIGINAL ID')
self.assertEqual(self.consumer.log, [])
self.assertEqual(urlopen.call_count, 1)
((request,),_) = urlopen.call_args
self.assertEqual(request.get_full_url(), 'https://api.mixpanel.com/track')
self.assertEqual(request.get_data(), '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})
self.assertEqual(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,
}
)])
class ConsumerTestCase(unittest.TestCase):
def setUp(self):
self.consumer = mixpanel.Consumer(request_timeout=30)
@contextlib.contextmanager
def _assertSends(self, expect_url, expect_data):
mock_response = Mock()
mock_response.read.return_value = '{"status":1, "error": null}'
with patch('urllib2.urlopen', return_value = mock_response) as urlopen:
yield
self.assertEqual(urlopen.call_count, 1)
(call_args, kwargs) = urlopen.call_args
(request,) = call_args
timeout = kwargs.get('timeout', None)
self.assertEqual(request.get_full_url(), expect_url)
self.assertEqual(request.get_data(), expect_data)
self.assertEqual(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"')
class BufferedConsumerTestCase(unittest.TestCase):
def setUp(self):
self.MAX_LENGTH = 10
self.consumer = mixpanel.BufferedConsumer(self.MAX_LENGTH)
self.mock = Mock()
self.mock.read.return_value = '{"status":1, "error": null}'
def test_buffer_hold_and_flush(self):
with patch('urllib2.urlopen', return_value = self.mock) as urlopen:
self.consumer.send('events', '"Event"')
self.assertTrue(not self.mock.called)
self.consumer.flush()
self.assertEqual(urlopen.call_count, 1)
(call_args, kwargs) = urlopen.call_args
(request,) = call_args
timeout = kwargs.get('timeout', None)
self.assertEqual(request.get_full_url(), 'https://api.mixpanel.com/track')
self.assertEqual(request.get_data(), 'ip=0&data=WyJFdmVudCJd&verbose=1')
self.assertIsNone(timeout)
def test_buffer_fills_up(self):
with patch('urllib2.urlopen', return_value = self.mock) as urlopen:
for i in xrange(self.MAX_LENGTH - 1):
self.consumer.send('events', '"Event"')
self.assertTrue(not self.mock.called)
self.consumer.send('events', '"Last Event"')
self.assertEqual(urlopen.call_count, 1)
((request,),_) = urlopen.call_args
self.assertEqual(request.get_full_url(), 'https://api.mixpanel.com/track')
self.assertEqual(request.get_data(), 'ip=0&data=WyJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJMYXN0IEV2ZW50Il0%3D&verbose=1')
class FunctionalTestCase(unittest.TestCase):
def setUp(self):
self.TOKEN = '12345'
self.mp = mixpanel.Mixpanel(self.TOKEN)
self.mp._now = lambda : 1000
@contextlib.contextmanager
def _assertRequested(self, expect_url, expect_data):
mock_response = Mock()
mock_response.read.return_value = '{"status":1, "error": null}'
with patch('urllib2.urlopen', return_value = mock_response) as urlopen:
yield
self.assertEqual(urlopen.call_count, 1)
((request,),_) = urlopen.call_args
self.assertEqual(request.get_full_url(), expect_url)
data = urlparse.parse_qs(request.get_data())
self.assertEqual(len(data['data']), 1)
payload_encoded = data['data'][0]
payload_json = base64.b64decode(payload_encoded)
payload = json.loads(payload_json)
self.assertEqual(payload, expect_data)
def test_track_functional(self):
# XXX this includes $lib_version, which means the test breaks
# every time we release.
expect_data = {u'event': {u'color': u'blue', u'size': u'big'}, u'properties': {u'mp_lib': u'python', u'token': u'12345', u'distinct_id': u'button press', u'$lib_version': unicode(mixpanel.VERSION), u'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 = {u'$distinct_id': u'amq', u'$set': {u'birth month': u'october', u'favorite color': u'purple'}, u'$time': 1000000, u'$token': u'12345'}
with self._assertRequested('https://api.mixpanel.com/engage', expect_data):
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'})
if __name__ == "__main__":
unittest.main()