Skip to content

Commit 400a3e8

Browse files
Merge pull request #111 from mixpanel/jared-ms-time
send millisecond precision time with events and people updates
2 parents 241b838 + 9a0767c commit 400a3e8

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

mixpanel/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def track(self, distinct_id, event_name, properties=None, meta=None):
8989
all_properties = {
9090
'token': self._token,
9191
'distinct_id': distinct_id,
92-
'time': int(self._now()),
92+
'time': self._now(),
9393
'$insert_id': self._make_insert_id(),
9494
'mp_lib': 'python',
9595
'$lib_version': __version__,
@@ -140,7 +140,7 @@ def import_data(self, api_key, distinct_id, event_name, timestamp,
140140
all_properties = {
141141
'token': self._token,
142142
'distinct_id': distinct_id,
143-
'time': int(timestamp),
143+
'time': timestamp,
144144
'$insert_id': self._make_insert_id(),
145145
'mp_lib': 'python',
146146
'$lib_version': __version__,
@@ -384,7 +384,7 @@ def people_update(self, message, meta=None):
384384
"""
385385
record = {
386386
'$token': self._token,
387-
'$time': int(self._now()),
387+
'$time': self._now(),
388388
}
389389
record.update(message)
390390
if meta:
@@ -500,7 +500,7 @@ def group_update(self, message, meta=None):
500500
"""
501501
record = {
502502
'$token': self._token,
503-
'$time': int(self._now()),
503+
'$time': self._now(),
504504
}
505505
record.update(message)
506506
if meta:

requirements-testing.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
pytest~=4.6
1+
pytest~=4.6.11 ; python_version<='3.4'
2+
pytest~=5.4.3 ; python_version>='3.5' and python_version<'3.7'
3+
pytest~=7.1.2 ; python_version>='3.7'
24
responses~=0.13.3
35
more-itertools==8.10.0 ; python_version=='3.5' # more-itertools added some f-strings after this.
46
typing; python_version>='3.4' and python_version<'3.5' # To work around CI fail.

test_mixpanel.py

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def clear(self):
3030
self.log = []
3131

3232

33-
class TestMixpanel:
33+
class TestMixpanelBase:
3434
TOKEN = '12345'
3535

3636
def setup_method(self, method):
@@ -39,6 +39,9 @@ def setup_method(self, method):
3939
self.mp._now = lambda: 1000.1
4040
self.mp._make_insert_id = lambda: "abcdefg"
4141

42+
43+
class TestMixpanelTracking(TestMixpanelBase):
44+
4245
def test_track(self):
4346
self.mp.track('ID', 'button press', {'size': 'big', 'color': 'blue', '$insert_id': 'abc123'})
4447
assert self.consumer.log == [(
@@ -49,7 +52,7 @@ def test_track(self):
4952
'size': 'big',
5053
'color': 'blue',
5154
'distinct_id': 'ID',
52-
'time': int(self.mp._now()),
55+
'time': self.mp._now(),
5356
'$insert_id': 'abc123',
5457
'mp_lib': 'python',
5558
'$lib_version': mixpanel.__version__,
@@ -72,7 +75,7 @@ def test_track_empty(self):
7275
'properties': {
7376
'token': self.TOKEN,
7477
'distinct_id': 'person_xyz',
75-
'time': int(self.mp._now()),
78+
'time': self.mp._now(),
7679
'$insert_id': self.mp._make_insert_id(),
7780
'mp_lib': 'python',
7881
'$lib_version': mixpanel.__version__,
@@ -93,7 +96,7 @@ def test_import_data(self):
9396
'size': 'big',
9497
'color': 'blue',
9598
'distinct_id': 'ID',
96-
'time': int(timestamp),
99+
'time': timestamp,
97100
'$insert_id': 'abc123',
98101
'mp_lib': 'python',
99102
'$lib_version': mixpanel.__version__,
@@ -113,7 +116,7 @@ def test_track_meta(self):
113116
'size': 'big',
114117
'color': 'blue',
115118
'distinct_id': 'ID',
116-
'time': int(self.mp._now()),
119+
'time': self.mp._now(),
117120
'$insert_id': 'abc123',
118121
'mp_lib': 'python',
119122
'$lib_version': mixpanel.__version__,
@@ -122,11 +125,14 @@ def test_track_meta(self):
122125
}
123126
)]
124127

128+
129+
class TestMixpanelPeople(TestMixpanelBase):
130+
125131
def test_people_set(self):
126132
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'})
127133
assert self.consumer.log == [(
128134
'people', {
129-
'$time': int(self.mp._now()),
135+
'$time': self.mp._now(),
130136
'$token': self.TOKEN,
131137
'$distinct_id': 'amq',
132138
'$set': {
@@ -140,7 +146,7 @@ def test_people_set_once(self):
140146
self.mp.people_set_once('amq', {'birth month': 'october', 'favorite color': 'purple'})
141147
assert self.consumer.log == [(
142148
'people', {
143-
'$time': int(self.mp._now()),
149+
'$time': self.mp._now(),
144150
'$token': self.TOKEN,
145151
'$distinct_id': 'amq',
146152
'$set_once': {
@@ -154,7 +160,7 @@ def test_people_increment(self):
154160
self.mp.people_increment('amq', {'Albums Released': 1})
155161
assert self.consumer.log == [(
156162
'people', {
157-
'$time': int(self.mp._now()),
163+
'$time': self.mp._now(),
158164
'$token': self.TOKEN,
159165
'$distinct_id': 'amq',
160166
'$add': {
@@ -167,7 +173,7 @@ def test_people_append(self):
167173
self.mp.people_append('amq', {'birth month': 'october', 'favorite color': 'purple'})
168174
assert self.consumer.log == [(
169175
'people', {
170-
'$time': int(self.mp._now()),
176+
'$time': self.mp._now(),
171177
'$token': self.TOKEN,
172178
'$distinct_id': 'amq',
173179
'$append': {
@@ -181,7 +187,7 @@ def test_people_union(self):
181187
self.mp.people_union('amq', {'Albums': ['Diamond Dogs']})
182188
assert self.consumer.log == [(
183189
'people', {
184-
'$time': int(self.mp._now()),
190+
'$time': self.mp._now(),
185191
'$token': self.TOKEN,
186192
'$distinct_id': 'amq',
187193
'$union': {
@@ -194,7 +200,7 @@ def test_people_unset(self):
194200
self.mp.people_unset('amq', ['Albums', 'Singles'])
195201
assert self.consumer.log == [(
196202
'people', {
197-
'$time': int(self.mp._now()),
203+
'$time': self.mp._now(),
198204
'$token': self.TOKEN,
199205
'$distinct_id': 'amq',
200206
'$unset': ['Albums', 'Singles'],
@@ -205,7 +211,7 @@ def test_people_remove(self):
205211
self.mp.people_remove('amq', {'Albums': 'Diamond Dogs'})
206212
assert self.consumer.log == [(
207213
'people', {
208-
'$time': int(self.mp._now()),
214+
'$time': self.mp._now(),
209215
'$token': self.TOKEN,
210216
'$distinct_id': 'amq',
211217
'$remove': {'Albums': 'Diamond Dogs'},
@@ -216,7 +222,7 @@ def test_people_track_charge(self):
216222
self.mp.people_track_charge('amq', 12.65, {'$time': '2013-04-01T09:02:00'})
217223
assert self.consumer.log == [(
218224
'people', {
219-
'$time': int(self.mp._now()),
225+
'$time': self.mp._now(),
220226
'$token': self.TOKEN,
221227
'$distinct_id': 'amq',
222228
'$append': {
@@ -232,7 +238,7 @@ def test_people_track_charge_without_properties(self):
232238
self.mp.people_track_charge('amq', 12.65)
233239
assert self.consumer.log == [(
234240
'people', {
235-
'$time': int(self.mp._now()),
241+
'$time': self.mp._now(),
236242
'$token': self.TOKEN,
237243
'$distinct_id': 'amq',
238244
'$append': {
@@ -247,7 +253,7 @@ def test_people_clear_charges(self):
247253
self.mp.people_clear_charges('amq')
248254
assert self.consumer.log == [(
249255
'people', {
250-
'$time': int(self.mp._now()),
256+
'$time': self.mp._now(),
251257
'$token': self.TOKEN,
252258
'$distinct_id': 'amq',
253259
'$unset': ['$transactions'],
@@ -259,7 +265,7 @@ def test_people_set_created_date_string(self):
259265
self.mp.people_set('amq', {'$created': created, 'favorite color': 'purple'})
260266
assert self.consumer.log == [(
261267
'people', {
262-
'$time': int(self.mp._now()),
268+
'$time': self.mp._now(),
263269
'$token': self.TOKEN,
264270
'$distinct_id': 'amq',
265271
'$set': {
@@ -274,7 +280,7 @@ def test_people_set_created_date_datetime(self):
274280
self.mp.people_set('amq', {'$created': created, 'favorite color': 'purple'})
275281
assert self.consumer.log == [(
276282
'people', {
277-
'$time': int(self.mp._now()),
283+
'$time': self.mp._now(),
278284
'$token': self.TOKEN,
279285
'$distinct_id': 'amq',
280286
'$set': {
@@ -284,6 +290,26 @@ def test_people_set_created_date_datetime(self):
284290
}
285291
)]
286292

293+
def test_people_meta(self):
294+
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'},
295+
meta={'$ip': 0, '$ignore_time': True})
296+
assert self.consumer.log == [(
297+
'people', {
298+
'$time': self.mp._now(),
299+
'$token': self.TOKEN,
300+
'$distinct_id': 'amq',
301+
'$set': {
302+
'birth month': 'october',
303+
'favorite color': 'purple',
304+
},
305+
'$ip': 0,
306+
'$ignore_time': True,
307+
}
308+
)]
309+
310+
311+
class TestMixpanelIdentity(TestMixpanelBase):
312+
287313
def test_alias(self):
288314
# More complicated since alias() forces a synchronous call.
289315

@@ -333,28 +359,14 @@ def test_merge(self):
333359
('my_good_api_key', 'my_secret'),
334360
)]
335361

336-
def test_people_meta(self):
337-
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'},
338-
meta={'$ip': 0, '$ignore_time': True})
339-
assert self.consumer.log == [(
340-
'people', {
341-
'$time': int(self.mp._now()),
342-
'$token': self.TOKEN,
343-
'$distinct_id': 'amq',
344-
'$set': {
345-
'birth month': 'october',
346-
'favorite color': 'purple',
347-
},
348-
'$ip': 0,
349-
'$ignore_time': True,
350-
}
351-
)]
362+
363+
class TestMixpanelGroups(TestMixpanelBase):
352364

353365
def test_group_set(self):
354366
self.mp.group_set('company', 'amq', {'birth month': 'october', 'favorite color': 'purple'})
355367
assert self.consumer.log == [(
356368
'groups', {
357-
'$time': int(self.mp._now()),
369+
'$time': self.mp._now(),
358370
'$token': self.TOKEN,
359371
'$group_key': 'company',
360372
'$group_id': 'amq',
@@ -369,7 +381,7 @@ def test_group_set_once(self):
369381
self.mp.group_set_once('company', 'amq', {'birth month': 'october', 'favorite color': 'purple'})
370382
assert self.consumer.log == [(
371383
'groups', {
372-
'$time': int(self.mp._now()),
384+
'$time': self.mp._now(),
373385
'$token': self.TOKEN,
374386
'$group_key': 'company',
375387
'$group_id': 'amq',
@@ -384,7 +396,7 @@ def test_group_union(self):
384396
self.mp.group_union('company', 'amq', {'Albums': ['Diamond Dogs']})
385397
assert self.consumer.log == [(
386398
'groups', {
387-
'$time': int(self.mp._now()),
399+
'$time': self.mp._now(),
388400
'$token': self.TOKEN,
389401
'$group_key': 'company',
390402
'$group_id': 'amq',
@@ -398,7 +410,7 @@ def test_group_unset(self):
398410
self.mp.group_unset('company', 'amq', ['Albums', 'Singles'])
399411
assert self.consumer.log == [(
400412
'groups', {
401-
'$time': int(self.mp._now()),
413+
'$time': self.mp._now(),
402414
'$token': self.TOKEN,
403415
'$group_key': 'company',
404416
'$group_id': 'amq',
@@ -410,7 +422,7 @@ def test_group_remove(self):
410422
self.mp.group_remove('company', 'amq', {'Albums': 'Diamond Dogs'})
411423
assert self.consumer.log == [(
412424
'groups', {
413-
'$time': int(self.mp._now()),
425+
'$time': self.mp._now(),
414426
'$token': self.TOKEN,
415427
'$group_key': 'company',
416428
'$group_id': 'amq',
@@ -438,7 +450,7 @@ def default(self, obj):
438450
'token': self.TOKEN,
439451
'size': decimal_string,
440452
'distinct_id': 'ID',
441-
'time': int(self.mp._now()),
453+
'time': self.mp._now(),
442454
'$insert_id': 'abc123',
443455
'mp_lib': 'python',
444456
'$lib_version': mixpanel.__version__,

0 commit comments

Comments
 (0)