From e6004baa8802584bca62b709f8a7714ae38c102d Mon Sep 17 00:00:00 2001 From: morgan Date: Mon, 19 Sep 2022 09:53:58 +0200 Subject: [PATCH 1/5] add sync_consumer parameter in Mixpanel initializer --- .gitignore | 1 + mixpanel/__init__.py | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 967442f..5fc9c64 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ docs/_build .idea/ .cache/ .DS_Store +.env diff --git a/mixpanel/__init__.py b/mixpanel/__init__.py index d93a1cb..78cf2b3 100644 --- a/mixpanel/__init__.py +++ b/mixpanel/__init__.py @@ -62,9 +62,14 @@ class Mixpanel(object): The *serializer* parameter. """ - def __init__(self, token, consumer=None, serializer=DatetimeSerializer): + def __init__(self, token, consumer=None, serializer=DatetimeSerializer, sync_consumer=None): self._token = token self._consumer = consumer or Consumer() + + self._sync_consumer = sync_consumer or Consumer() + # if getattr(self._sync_consumer, "flush"): + # raise Exception("sync_consumer must be Consumer instance") + self._serializer = serializer def _now(self): @@ -185,8 +190,7 @@ def alias(self, alias_id, original, meta=None): if meta: event.update(meta) - sync_consumer = Consumer() - sync_consumer.send('events', json_dumps(event, cls=self._serializer)) + self._sync_consumer.send('events', json_dumps(event, cls=self._serializer)) def merge(self, api_key, distinct_id1, distinct_id2, meta=None, api_secret=None): """ From 664d992521197bbc32fd1b01f7a7419291f90bbe Mon Sep 17 00:00:00 2001 From: morgan Date: Mon, 19 Sep 2022 10:20:09 +0200 Subject: [PATCH 2/5] add sync_consumer parameter in Mixpanel initializer --- mixpanel/__init__.py | 7 ++++--- test_mixpanel.py | 27 ++++++++++++--------------- 2 files changed, 16 insertions(+), 18 deletions(-) mode change 100644 => 100755 test_mixpanel.py diff --git a/mixpanel/__init__.py b/mixpanel/__init__.py index 78cf2b3..93a74cf 100644 --- a/mixpanel/__init__.py +++ b/mixpanel/__init__.py @@ -66,9 +66,10 @@ def __init__(self, token, consumer=None, serializer=DatetimeSerializer, sync_con self._token = token self._consumer = consumer or Consumer() - self._sync_consumer = sync_consumer or Consumer() - # if getattr(self._sync_consumer, "flush"): - # raise Exception("sync_consumer must be Consumer instance") + if hasattr(self._consumer, "flush"): + self._sync_consumer = sync_consumer or Consumer() + else: + self._sync_consumer = self._consumer self._serializer = serializer diff --git a/test_mixpanel.py b/test_mixpanel.py old mode 100644 new mode 100755 index 0275eba..9ed39f0 --- a/test_mixpanel.py +++ b/test_mixpanel.py @@ -9,7 +9,6 @@ import six from six.moves import range, urllib - import mixpanel @@ -86,8 +85,8 @@ def test_track_empty(self): def test_import_data(self): timestamp = time.time() self.mp.import_data('MY_API_KEY', 'ID', 'button press', timestamp, - {'size': 'big', 'color': 'blue', '$insert_id': 'abc123'}, - api_secret='MY_SECRET') + {'size': 'big', 'color': 'blue', '$insert_id': 'abc123'}, + api_secret='MY_SECRET') assert self.consumer.log == [( 'imports', { 'event': 'button press', @@ -309,7 +308,6 @@ def test_people_meta(self): class TestMixpanelIdentity(TestMixpanelBase): - def test_alias(self): # More complicated since alias() forces a synchronous call. @@ -323,12 +321,8 @@ def test_alias(self): self.mp.alias('ALIAS', 'ORIGINAL ID') - assert self.consumer.log == [] - call = rsps.calls[0] - assert call.request.method == "POST" - assert call.request.url == "https://api.mixpanel.com/track" - posted_data = dict(urllib.parse.parse_qsl(six.ensure_str(call.request.body))) - assert json.loads(posted_data["data"]) == {"event":"$create_alias","properties":{"alias":"ALIAS","token":"12345","distinct_id":"ORIGINAL ID"}} + assert self.mp._consumer == self.mp._sync_consumer + assert self.consumer.log == [('events', {'event': '$create_alias', 'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'}})] def test_merge(self): self.mp.merge('my_good_api_key', 'd1', 'd2') @@ -505,7 +499,8 @@ def test_server_invalid_data(self): 'https://api.mixpanel.com/track', json={"status": 0, "error": error_msg}, status=200, - match=[responses.urlencoded_params_matcher({"ip": "0", "verbose": "1", "data": '{INVALID "foo":"bar"}'})], + match=[ + responses.urlencoded_params_matcher({"ip": "0", "verbose": "1", "data": '{INVALID "foo":"bar"}'})], ) with pytest.raises(mixpanel.MixpanelException) as exc: @@ -643,8 +638,6 @@ def test_send_remembers_api_secret(self): assert self.log == [('imports', ['Event'], (None, 'ZZZZZZ'))] - - class TestFunctional: @classmethod def setup_class(cls): @@ -669,7 +662,10 @@ def test_track_functional(self): del wrapper["data"] assert {"ip": "0", "verbose": "1"} == wrapper - expected_data = {'event': 'button_press', 'properties': {'size': 'big', 'color': 'blue', 'mp_lib': 'python', 'token': '12345', 'distinct_id': 'player1', '$lib_version': mixpanel.__version__, 'time': 1000, '$insert_id': 'xyz1200'}} + expected_data = {'event': 'button_press', + 'properties': {'size': 'big', 'color': 'blue', 'mp_lib': 'python', 'token': '12345', + 'distinct_id': 'player1', '$lib_version': mixpanel.__version__, + 'time': 1000, '$insert_id': 'xyz1200'}} assert expected_data == data def test_people_set_functional(self): @@ -688,5 +684,6 @@ def test_people_set_functional(self): del wrapper["data"] assert {"ip": "0", "verbose": "1"} == wrapper - expected_data = {'$distinct_id': 'amq', '$set': {'birth month': 'october', 'favorite color': 'purple'}, '$time': 1000, '$token': '12345'} + expected_data = {'$distinct_id': 'amq', '$set': {'birth month': 'october', 'favorite color': 'purple'}, + '$time': 1000, '$token': '12345'} assert expected_data == data From f21b489ce20d392401bfa874b2830d86b32607b2 Mon Sep 17 00:00:00 2001 From: morgan Date: Tue, 20 Sep 2022 11:03:15 +0200 Subject: [PATCH 3/5] clean indent --- test_mixpanel.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test_mixpanel.py b/test_mixpanel.py index 9ed39f0..f6082d3 100755 --- a/test_mixpanel.py +++ b/test_mixpanel.py @@ -9,6 +9,7 @@ import six from six.moves import range, urllib + import mixpanel @@ -85,8 +86,8 @@ def test_track_empty(self): def test_import_data(self): timestamp = time.time() self.mp.import_data('MY_API_KEY', 'ID', 'button press', timestamp, - {'size': 'big', 'color': 'blue', '$insert_id': 'abc123'}, - api_secret='MY_SECRET') + {'size': 'big', 'color': 'blue', '$insert_id': 'abc123'}, + api_secret='MY_SECRET') assert self.consumer.log == [( 'imports', { 'event': 'button press', @@ -322,7 +323,12 @@ def test_alias(self): self.mp.alias('ALIAS', 'ORIGINAL ID') assert self.mp._consumer == self.mp._sync_consumer - assert self.consumer.log == [('events', {'event': '$create_alias', 'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'}})] + assert self.consumer.log == [ + ('events', { + 'event': '$create_alias', + 'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'} + }) + ] def test_merge(self): self.mp.merge('my_good_api_key', 'd1', 'd2') @@ -499,8 +505,7 @@ def test_server_invalid_data(self): 'https://api.mixpanel.com/track', json={"status": 0, "error": error_msg}, status=200, - match=[ - responses.urlencoded_params_matcher({"ip": "0", "verbose": "1", "data": '{INVALID "foo":"bar"}'})], + match=[responses.urlencoded_params_matcher({"ip": "0", "verbose": "1", "data": '{INVALID "foo":"bar"}'})], ) with pytest.raises(mixpanel.MixpanelException) as exc: @@ -638,6 +643,8 @@ def test_send_remembers_api_secret(self): assert self.log == [('imports', ['Event'], (None, 'ZZZZZZ'))] + + class TestFunctional: @classmethod def setup_class(cls): @@ -662,10 +669,7 @@ def test_track_functional(self): del wrapper["data"] assert {"ip": "0", "verbose": "1"} == wrapper - expected_data = {'event': 'button_press', - 'properties': {'size': 'big', 'color': 'blue', 'mp_lib': 'python', 'token': '12345', - 'distinct_id': 'player1', '$lib_version': mixpanel.__version__, - 'time': 1000, '$insert_id': 'xyz1200'}} + expected_data = {'event': 'button_press', 'properties': {'size': 'big', 'color': 'blue', 'mp_lib': 'python', 'token': '12345', 'distinct_id': 'player1', '$lib_version': mixpanel.__version__, 'time': 1000, '$insert_id': 'xyz1200'}} assert expected_data == data def test_people_set_functional(self): @@ -684,6 +688,5 @@ def test_people_set_functional(self): del wrapper["data"] assert {"ip": "0", "verbose": "1"} == wrapper - expected_data = {'$distinct_id': 'amq', '$set': {'birth month': 'october', 'favorite color': 'purple'}, - '$time': 1000, '$token': '12345'} + expected_data = {'$distinct_id': 'amq', '$set': {'birth month': 'october', 'favorite color': 'purple'}, '$time': 1000, '$token': '12345'} assert expected_data == data From 4bf02dfe6f381aafee392bf0e2d897da091bfc48 Mon Sep 17 00:00:00 2001 From: morgan Date: Tue, 20 Sep 2022 11:04:23 +0200 Subject: [PATCH 4/5] .gitignores cleaned --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5fc9c64..967442f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ docs/_build .idea/ .cache/ .DS_Store -.env From 07025a8ac6f930bea2513069c5463c40ed1ac926 Mon Sep 17 00:00:00 2001 From: morgan Date: Tue, 20 Sep 2022 11:16:57 +0200 Subject: [PATCH 5/5] extra mock removed --- test_mixpanel.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/test_mixpanel.py b/test_mixpanel.py index f6082d3..7d3ba36 100755 --- a/test_mixpanel.py +++ b/test_mixpanel.py @@ -310,25 +310,15 @@ def test_people_meta(self): class TestMixpanelIdentity(TestMixpanelBase): def test_alias(self): - # More complicated since alias() forces a synchronous call. - - with responses.RequestsMock() as rsps: - rsps.add( - responses.POST, - 'https://api.mixpanel.com/track', - json={"status": 1, "error": None}, - status=200, - ) - - self.mp.alias('ALIAS', 'ORIGINAL ID') - - assert self.mp._consumer == self.mp._sync_consumer - assert self.consumer.log == [ - ('events', { - 'event': '$create_alias', - 'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'} - }) - ] + self.mp.alias('ALIAS', 'ORIGINAL ID') + + assert self.mp._consumer == self.mp._sync_consumer + assert self.consumer.log == [ + ('events', { + 'event': '$create_alias', + 'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'} + }) + ] def test_merge(self): self.mp.merge('my_good_api_key', 'd1', 'd2')