From 3c23f0159f717f952c11ef9b4e50ea5a817b2ab0 Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Wed, 9 Sep 2020 16:15:46 +0200 Subject: [PATCH 1/8] Fix tests --- .github/tests.yml | 45 ++++++++++++ example.py | 10 +-- posthog/client.py | 68 ++--------------- posthog/test/__init__.py | 2 +- posthog/test/client.py | 154 ++++++++------------------------------- posthog/test/consumer.py | 8 +- posthog/test/module.py | 40 ++++------ posthog/test/request.py | 6 +- simulator.py | 22 +++--- 9 files changed, 123 insertions(+), 232 deletions(-) create mode 100644 .github/tests.yml diff --git a/.github/tests.yml b/.github/tests.yml new file mode 100644 index 00000000..9659ea9a --- /dev/null +++ b/.github/tests.yml @@ -0,0 +1,45 @@ +name: Backend CI + +on: + - pull_request + +jobs: + tests: + name: Python tests + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Set up Python 3.7 + uses: actions/setup-python@v1 + with: + python-version: 3.7 + + - name: Install requirements.txt dependencies with pip + run: | + python -m pip install -e . + + - name: Run posthog tests + env: + SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only + DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres' + run: | + python setup.py test + - name: Run EE tests + env: + SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only + DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres' + PRIMARY_DB: 'clickhouse' + CLICKHOUSE_HOST: 'localhost' + CLICKHOUSE_DATABASE: 'posthog_test' + CLICKHOUSE_SECURE: 'False' + CLICKHOUSE_VERIFY: 'False' + run: | + mkdir -p frontend/dist + touch frontend/dist/index.html + touch frontend/dist/layout.html + touch frontend/dist/shared_dashboard.html + python manage.py test ee --testrunner="ee.clickhouse.clickhouse_test_runner.ClickhouseTestRunner" diff --git a/example.py b/example.py index ef303ced..1c0da624 100644 --- a/example.py +++ b/example.py @@ -8,13 +8,13 @@ # Where you host PostHog, with no trailing /. # You can remove this line if you're using posthog.com -posthog.host = 'http://127.0.0.1:8000' +# posthog.host = 'http://127.0.0.1:8000' # Capture an event posthog.capture('distinct_id', 'event', {'property1': 'value', 'property2': 'value'}) -# Alias a previous distinct id with a new one -posthog.alias('distinct_id', 'new_distinct_id') +# # Alias a previous distinct id with a new one +# posthog.alias('distinct_id', 'new_distinct_id') -# Add properties to the person -posthog.identify('distinct_id', {'email': 'something@something.com'}) \ No newline at end of file +# # Add properties to the person +# posthog.identify('distinct_id', {'email': 'something@something.com'}) \ No newline at end of file diff --git a/posthog/client.py b/posthog/client.py index b94d58da..b98eacba 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -78,7 +78,6 @@ def identify(self, distinct_id=None, properties=None, context=None, timestamp=No msg = { 'timestamp': timestamp, 'context': context, - 'type': 'identify', 'distinct_id': distinct_id, '$set': properties, 'event': '$identify', @@ -100,7 +99,6 @@ def capture(self, distinct_id=None, event=None, properties=None, context=None, 'timestamp': timestamp, 'context': context, 'distinct_id': distinct_id, - 'type': 'capture', 'event': event, 'messageId': message_id, } @@ -121,34 +119,12 @@ def alias(self, previous_id=None, distinct_id=None, context=None, }, 'timestamp': timestamp, 'context': context, - 'type': 'alias', 'event': '$create_alias' } return self._enqueue(msg) - def group(self, distinct_id=None, group_id=None, traits=None, context=None, - timestamp=None, message_id=None): - traits = traits or {} - context = context or {} - - require('distinct_id', distinct_id, ID_TYPES) - require('group_id', group_id, ID_TYPES) - require('traits', traits, dict) - - msg = { - 'timestamp': timestamp, - 'groupId': group_id, - 'context': context, - 'distinct_id': distinct_id, - 'traits': traits, - 'type': 'group', - 'messageId': message_id, - } - - return self._enqueue(msg) - - def page(self, distinct_id=None, category=None, name=None, properties=None, + def page(self, distinct_id=None, url=None, properties=None, context=None, timestamp=None, message_id=None): properties = properties or {} context = context or {} @@ -156,46 +132,15 @@ def page(self, distinct_id=None, category=None, name=None, properties=None, require('distinct_id', distinct_id, ID_TYPES) require('properties', properties, dict) - if name: - require('name', name, string_types) - if category: - require('category', category, string_types) - - msg = { - 'properties': properties, - 'timestamp': timestamp, - 'category': category, - 'context': context, - 'distinct_id': distinct_id, - 'type': 'page', - 'name': name, - 'messageId': message_id, - } - - return self._enqueue(msg) - - def screen(self, distinct_id=None, category=None, name=None, properties=None, - context=None, timestamp=None, message_id=None): - properties = properties or {} - context = context or {} - - require('distinct_id', distinct_id, ID_TYPES) - require('properties', properties, dict) - - if name: - require('name', name, string_types) - if category: - require('category', category, string_types) + require('url', url, string_types) + properties['$current_url'] = url msg = { - + 'event': '$pageview', 'properties': properties, 'timestamp': timestamp, - 'category': category, 'context': context, 'distinct_id': distinct_id, - 'type': 'screen', - 'name': name, 'messageId': message_id, } @@ -210,7 +155,6 @@ def _enqueue(self, msg): if message_id is None: message_id = uuid4() - require('type', msg['type'], string_types) require('timestamp', timestamp, datetime) require('context', msg['context'], dict) @@ -233,7 +177,7 @@ def _enqueue(self, msg): return True, msg if self.sync_mode: - self.log.debug('enqueued with blocking %s.', msg['type']) + self.log.debug('enqueued with blocking %s.', msg['event']) post(self.api_key, self.host, gzip=self.gzip, timeout=self.timeout, batch=[msg]) @@ -241,7 +185,7 @@ def _enqueue(self, msg): try: self.queue.put(msg, block=False) - self.log.debug('enqueued %s.', msg['type']) + self.log.debug('enqueued %s.', msg['event']) return True, msg except queue.Full: self.log.warning('analytics-python queue is full') diff --git a/posthog/test/__init__.py b/posthog/test/__init__.py index 4a920f8e..ab03604e 100644 --- a/posthog/test/__init__.py +++ b/posthog/test/__init__.py @@ -6,7 +6,7 @@ def all_names(): for _, modname, _ in pkgutil.iter_modules(__path__): - yield 'analytics.test.' + modname + yield 'posthog.test.' + modname def all(): diff --git a/posthog/test/client.py b/posthog/test/client.py index 934c024f..31b02580 100644 --- a/posthog/test/client.py +++ b/posthog/test/client.py @@ -7,16 +7,18 @@ from posthog.version import VERSION from posthog.client import Client +TEST_API_KEY = 'kOOlRy2QlMY9jHZQv0bKz0FZyazBUoY8Arj0lFVNjs4' class TestClient(unittest.TestCase): def fail(self, e, batch): """Mark the failure handler""" + print('FAILL', e, batch) self.failed = True def setUp(self): self.failed = False - self.client = Client('testsecret', on_error=self.fail) + self.client = Client(TEST_API_KEY, on_error=self.fail) def test_requires_api_key(self): self.assertRaises(AssertionError, Client) @@ -24,9 +26,9 @@ def test_requires_api_key(self): def test_empty_flush(self): self.client.flush() - def test_basic_track(self): + def test_basic_capture(self): client = self.client - success, msg = client.track('distinct_id', 'python test event') + success, msg = client.capture('distinct_id', 'python test event') client.flush() self.assertTrue(success) self.assertFalse(self.failed) @@ -35,14 +37,14 @@ def test_basic_track(self): self.assertTrue(isinstance(msg['timestamp'], str)) self.assertTrue(isinstance(msg['messageId'], str)) self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['properties'], {}) - self.assertEqual(msg['type'], 'track') + self.assertEqual(msg['properties']['$lib'], 'posthog-python') + self.assertEqual(msg['properties']['$lib_version'], VERSION) def test_stringifies_distinct_id(self): # A large number that loses precision in node: # node -e "console.log(157963456373623802 + 1)" > 157963456373623800 client = self.client - success, msg = client.track( + success, msg = client.capture( distinct_id=157963456373623802, event='python test event') client.flush() self.assertTrue(success) @@ -50,9 +52,9 @@ def test_stringifies_distinct_id(self): self.assertEqual(msg['distinct_id'], '157963456373623802') - def test_advanced_track(self): + def test_advanced_capture(self): client = self.client - success, msg = client.track( + success, msg = client.capture( 'distinct_id', 'python test event', {'property': 'value'}, {'ip': '192.168.0.1'}, datetime(2014, 9, 3), 'messageId') @@ -60,14 +62,13 @@ def test_advanced_track(self): self.assertTrue(success) self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') - self.assertEqual(msg['properties'], {'property': 'value'}) + self.assertEqual(msg['properties']['property'], 'value') self.assertEqual(msg['context']['ip'], '192.168.0.1') self.assertEqual(msg['event'], 'python test event') self.assertEqual(msg['properties']['$lib'], 'posthog-python') self.assertEqual(msg['properties']['$lib_version'], VERSION) self.assertEqual(msg['messageId'], 'messageId') self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'track') def test_basic_identify(self): client = self.client @@ -76,11 +77,10 @@ def test_basic_identify(self): self.assertTrue(success) self.assertFalse(self.failed) - self.assertEqual(msg['traits'], {'trait': 'value'}) + self.assertEqual(msg['$set'], {'trait': 'value'}) self.assertTrue(isinstance(msg['timestamp'], str)) self.assertTrue(isinstance(msg['messageId'], str)) self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'identify') def test_advanced_identify(self): client = self.client @@ -92,46 +92,12 @@ def test_advanced_identify(self): self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') self.assertEqual(msg['context']['ip'], '192.168.0.1') - self.assertEqual(msg['traits'], {'trait': 'value'}) - self.assertEqual(msg['context']['library'], { - 'name': 'analytics-python', - 'version': VERSION - }) - self.assertTrue(isinstance(msg['timestamp'], str)) - self.assertEqual(msg['messageId'], 'messageId') - self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'identify') - - def test_basic_group(self): - client = self.client - success, msg = client.group('distinct_id', 'groupId') - client.flush() - self.assertTrue(success) - self.assertFalse(self.failed) - - self.assertEqual(msg['groupId'], 'groupId') - self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'group') - - def test_advanced_group(self): - client = self.client - success, msg = client.group( - 'distinct_id', 'groupId', {'trait': 'value'}, {'ip': '192.168.0.1'}, - datetime(2014, 9, 3), 'messageId') - - self.assertTrue(success) - - self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') - self.assertEqual(msg['context']['ip'], '192.168.0.1') - self.assertEqual(msg['traits'], {'trait': 'value'}) - self.assertEqual(msg['context']['library'], { - 'name': 'analytics-python', - 'version': VERSION - }) + self.assertEqual(msg['$set'], {'trait': 'value'}) + self.assertEqual(msg['properties']['$lib'], 'posthog-python') + self.assertEqual(msg['properties']['$lib_version'], VERSION) self.assertTrue(isinstance(msg['timestamp'], str)) self.assertEqual(msg['messageId'], 'messageId') self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'group') def test_basic_alias(self): client = self.client @@ -139,71 +105,35 @@ def test_basic_alias(self): client.flush() self.assertTrue(success) self.assertFalse(self.failed) - self.assertEqual(msg['previousId'], 'previousId') - self.assertEqual(msg['distinct_id'], 'distinct_id') + self.assertEqual(msg['properties']['distinct_id'], 'previousId') + self.assertEqual(msg['properties']['alias'], 'distinct_id') def test_basic_page(self): client = self.client - success, msg = client.page('distinct_id', name='name') + success, msg = client.page('distinct_id', url='https://posthog.com/contact') self.assertFalse(self.failed) client.flush() self.assertTrue(success) self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'page') - self.assertEqual(msg['name'], 'name') + self.assertEqual(msg['properties']['$current_url'], 'https://posthog.com/contact') def test_advanced_page(self): client = self.client success, msg = client.page( - 'distinct_id', 'category', 'name', {'property': 'value'}, + 'distinct_id', 'https://posthog.com/contact', {'property': 'value'}, {'ip': '192.168.0.1'}, datetime(2014, 9, 3), 'messageId') self.assertTrue(success) self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') self.assertEqual(msg['context']['ip'], '192.168.0.1') - self.assertEqual(msg['properties'], {'property': 'value'}) - self.assertEqual(msg['context']['library'], { - 'name': 'analytics-python', - 'version': VERSION - }) - self.assertEqual(msg['category'], 'category') - self.assertTrue(isinstance(msg['timestamp'], str)) - self.assertEqual(msg['messageId'], 'messageId') - self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'page') - self.assertEqual(msg['name'], 'name') - - def test_basic_screen(self): - client = self.client - success, msg = client.screen('distinct_id', name='name') - client.flush() - self.assertTrue(success) - self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'screen') - self.assertEqual(msg['name'], 'name') - - def test_advanced_screen(self): - client = self.client - success, msg = client.screen( - 'distinct_id', 'category', 'name', {'property': 'value'}, - {'ip': '192.168.0.1'}, datetime(2014, 9, 3), 'messageId') - - self.assertTrue(success) - - self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') - self.assertEqual(msg['context']['ip'], '192.168.0.1') - self.assertEqual(msg['properties'], {'property': 'value'}) - self.assertEqual(msg['context']['library'], { - 'name': 'analytics-python', - 'version': VERSION - }) + self.assertEqual(msg['properties']['$current_url'], 'https://posthog.com/contact') + self.assertEqual(msg['properties']['property'], 'value') + self.assertEqual(msg['properties']['$lib'], 'posthog-python') + self.assertEqual(msg['properties']['$lib_version'], VERSION) self.assertTrue(isinstance(msg['timestamp'], str)) self.assertEqual(msg['messageId'], 'messageId') - self.assertEqual(msg['category'], 'category') self.assertEqual(msg['distinct_id'], 'distinct_id') - self.assertEqual(msg['type'], 'screen') - self.assertEqual(msg['name'], 'name') def test_flush(self): client = self.client @@ -230,7 +160,7 @@ def test_shutdown(self): self.assertFalse(consumer.is_alive()) def test_synchronous(self): - client = Client('testsecret', sync_mode=True) + client = Client(TEST_API_KEY, sync_mode=True) success, message = client.identify('distinct_id') self.assertFalse(client.consumers) @@ -238,7 +168,7 @@ def test_synchronous(self): self.assertTrue(success) def test_overflow(self): - client = Client('testsecret', max_queue_size=1) + client = Client(TEST_API_KEY, max_queue_size=1) # Ensure consumer thread is no longer uploading client.join() @@ -249,46 +179,26 @@ def test_overflow(self): # Make sure we are informed that the queue is at capacity self.assertFalse(success) - def test_success_on_invalid_api_key(self): - client = Client('bad_key', on_error=self.fail) - client.track('distinct_id', 'event') - client.flush() - self.assertFalse(self.failed) - def test_unicode(self): Client(six.u('unicode_key')) def test_numeric_distinct_id(self): - self.client.track(1234, 'python event') + self.client.capture(1234, 'python event') self.client.flush() self.assertFalse(self.failed) def test_debug(self): Client('bad_key', debug=True) - def test_identify_with_date_object(self): - client = self.client - success, msg = client.identify( - 'distinct_id', - { - 'birthdate': date(1981, 2, 2), - }, - ) - client.flush() - self.assertTrue(success) - self.assertFalse(self.failed) - - self.assertEqual(msg['traits'], {'birthdate': date(1981, 2, 2)}) - def test_gzip(self): - client = Client('testsecret', on_error=self.fail, gzip=True) + client = Client(TEST_API_KEY, on_error=self.fail, gzip=True) for _ in range(10): client.identify('distinct_id', {'trait': 'value'}) client.flush() self.assertFalse(self.failed) def test_user_defined_flush_at(self): - client = Client('testsecret', on_error=self.fail, + client = Client(TEST_API_KEY, on_error=self.fail, flush_at=10, flush_interval=3) def mock_post_fn(*args, **kwargs): @@ -296,7 +206,7 @@ def mock_post_fn(*args, **kwargs): # the post function should be called 2 times, with a batch size of 10 # each time. - with mock.patch('analytics.consumer.post', side_effect=mock_post_fn) \ + with mock.patch('posthog.consumer.post', side_effect=mock_post_fn) \ as mock_post: for _ in range(20): client.identify('distinct_id', {'trait': 'value'}) @@ -304,11 +214,11 @@ def mock_post_fn(*args, **kwargs): self.assertEquals(mock_post.call_count, 2) def test_user_defined_timeout(self): - client = Client('testsecret', timeout=10) + client = Client(TEST_API_KEY, timeout=10) for consumer in client.consumers: self.assertEquals(consumer.timeout, 10) def test_default_timeout_15(self): - client = Client('testsecret') + client = Client(TEST_API_KEY) for consumer in client.consumers: self.assertEquals(consumer.timeout, 15) diff --git a/posthog/test/consumer.py b/posthog/test/consumer.py index 7e6f312b..2835b3b3 100644 --- a/posthog/test/consumer.py +++ b/posthog/test/consumer.py @@ -59,7 +59,7 @@ def test_flush_interval(self): flush_interval = 0.3 consumer = Consumer(q, 'testsecret', flush_at=10, flush_interval=flush_interval) - with mock.patch('analytics.consumer.post') as mock_post: + with mock.patch('posthog.consumer.post') as mock_post: consumer.start() for i in range(0, 3): track = { @@ -79,7 +79,7 @@ def test_multiple_uploads_per_interval(self): flush_at = 10 consumer = Consumer(q, 'testsecret', flush_at=flush_at, flush_interval=flush_interval) - with mock.patch('analytics.consumer.post') as mock_post: + with mock.patch('posthog.consumer.post') as mock_post: consumer.start() for i in range(0, flush_at * 2): track = { @@ -109,7 +109,7 @@ def mock_post(*args, **kwargs): raise expected_exception mock_post.call_count = 0 - with mock.patch('analytics.consumer.post', + with mock.patch('posthog.consumer.post', mock.Mock(side_effect=mock_post)): track = { 'type': 'track', @@ -189,7 +189,7 @@ def mock_post_fn(_, data, **kwargs): % len(data.encode())) return res - with mock.patch('analytics.request._session.post', + with mock.patch('posthog.request._session.post', side_effect=mock_post_fn) as mock_post: consumer.start() for _ in range(0, n_msgs + 2): diff --git a/posthog/test/module.py b/posthog/test/module.py index f02d0976..7aba2810 100644 --- a/posthog/test/module.py +++ b/posthog/test/module.py @@ -1,6 +1,6 @@ import unittest -import analytics +import posthog class TestModule(unittest.TestCase): @@ -10,40 +10,32 @@ def failed(self): def setUp(self): self.failed = False - analytics.api_key = 'testsecret' - analytics.on_error = self.failed + posthog.api_key = 'testsecret' + posthog.on_error = self.failed def test_no_api_key(self): - analytics.api_key = None - self.assertRaises(Exception, analytics.track) + posthog.api_key = None + self.assertRaises(Exception, posthog.capture) def test_no_host(self): - analytics.host = None - self.assertRaises(Exception, analytics.track) + posthog.host = None + self.assertRaises(Exception, posthog.capture) def test_track(self): - analytics.track('distinct_id', 'python module event') - analytics.flush() + posthog.capture('distinct_id', 'python module event') + posthog.flush() def test_identify(self): - analytics.identify('distinct_id', {'email': 'user@email.com'}) - analytics.flush() - - def test_group(self): - analytics.group('distinct_id', 'groupId') - analytics.flush() + posthog.identify('distinct_id', {'email': 'user@email.com'}) + posthog.flush() def test_alias(self): - analytics.alias('previousId', 'distinct_id') - analytics.flush() - - def test_page(self): - analytics.page('distinct_id') - analytics.flush() + posthog.alias('previousId', 'distinct_id') + posthog.flush() def test_screen(self): - analytics.screen('distinct_id') - analytics.flush() + posthog.screen('distinct_id') + posthog.flush() def test_flush(self): - analytics.flush() + posthog.flush() diff --git a/posthog/test/request.py b/posthog/test/request.py index 4bcc7b93..5602986d 100644 --- a/posthog/test/request.py +++ b/posthog/test/request.py @@ -9,7 +9,7 @@ class TestRequests(unittest.TestCase): def test_valid_request(self): - res = post(batch=[{ + res = post('key', batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' @@ -37,7 +37,7 @@ def test_date_serialization(self): self.assertEqual(result, expected) def test_should_not_timeout(self): - res = post(batch=[{ + res = post('key', batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' @@ -46,7 +46,7 @@ def test_should_not_timeout(self): def test_should_timeout(self): with self.assertRaises(requests.ReadTimeout): - post(batch=[{ + post('key', batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' diff --git a/simulator.py b/simulator.py index d352aa08..533c2c8b 100644 --- a/simulator.py +++ b/simulator.py @@ -1,4 +1,4 @@ -import analytics +import posthog import argparse import json import logging @@ -12,7 +12,7 @@ def json_hash(str): if str: return json.loads(str) -# analytics -method= -posthog-write-key= [options] +# posthog -method= -posthog-write-key= [options] parser = argparse.ArgumentParser(description='send a posthog message') @@ -46,27 +46,27 @@ def failed(status, msg): def track(): - analytics.track(options.distinct_id, options.event, anonymous_id=options.anonymousId, + posthog.track(options.distinct_id, options.event, anonymous_id=options.anonymousId, properties=json_hash(options.properties), context=json_hash(options.context)) def page(): - analytics.page(options.distinct_id, name=options.name, anonymous_id=options.anonymousId, + posthog.page(options.distinct_id, name=options.name, anonymous_id=options.anonymousId, properties=json_hash(options.properties), context=json_hash(options.context)) def screen(): - analytics.screen(options.distinct_id, name=options.name, anonymous_id=options.anonymousId, + posthog.screen(options.distinct_id, name=options.name, anonymous_id=options.anonymousId, properties=json_hash(options.properties), context=json_hash(options.context)) def identify(): - analytics.identify(options.distinct_id, anonymous_id=options.anonymousId, + posthog.identify(options.distinct_id, anonymous_id=options.anonymousId, traits=json_hash(options.traits), context=json_hash(options.context)) def group(): - analytics.group(options.distinct_id, options.groupId, json_hash(options.traits), + posthog.group(options.distinct_id, options.groupId, json_hash(options.traits), json_hash(options.context), anonymous_id=options.anonymousId) @@ -74,9 +74,9 @@ def unknown(): print() -analytics.api_key = options.writeKey -analytics.on_error = failed -analytics.debug = True +posthog.api_key = options.writeKey +posthog.on_error = failed +posthog.debug = True log = logging.getLogger('posthog') ch = logging.StreamHandler() @@ -94,6 +94,6 @@ def unknown(): func = switcher.get(options.type) if func: func() - analytics.shutdown() + posthog.shutdown() else: print("Invalid Message Type " + options.type) From 8fee12f004926b3375ba86b6edaee76c4925c6d5 Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Wed, 9 Sep 2020 16:16:54 +0200 Subject: [PATCH 2/8] Move tests into correct folder --- .github/{ => workflows}/tests.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{ => workflows}/tests.yml (100%) diff --git a/.github/tests.yml b/.github/workflows/tests.yml similarity index 100% rename from .github/tests.yml rename to .github/workflows/tests.yml From b9e323bf478ed14984b12192ac51e65484fe353c Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Wed, 9 Sep 2020 16:22:40 +0200 Subject: [PATCH 3/8] Fix tests --- posthog/test/client.py | 3 +-- posthog/test/consumer.py | 23 ++++++++++++----------- posthog/test/module.py | 4 ++-- posthog/test/request.py | 5 +++-- posthog/test/utils.py | 1 + 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/posthog/test/client.py b/posthog/test/client.py index 31b02580..6a35ab19 100644 --- a/posthog/test/client.py +++ b/posthog/test/client.py @@ -6,8 +6,7 @@ from posthog.version import VERSION from posthog.client import Client - -TEST_API_KEY = 'kOOlRy2QlMY9jHZQv0bKz0FZyazBUoY8Arj0lFVNjs4' +from posthog.test.utils import TEST_API_KEY class TestClient(unittest.TestCase): diff --git a/posthog/test/consumer.py b/posthog/test/consumer.py index 2835b3b3..85615195 100644 --- a/posthog/test/consumer.py +++ b/posthog/test/consumer.py @@ -10,6 +10,7 @@ from posthog.consumer import Consumer, MAX_MSG_SIZE from posthog.request import APIError +from posthog.test.utils import TEST_API_KEY class TestConsumer(unittest.TestCase): @@ -41,7 +42,7 @@ def test_dropping_oversize_msg(self): def test_upload(self): q = Queue() - consumer = Consumer(q, 'testsecret') + consumer = Consumer(q, TEST_API_KEY) track = { 'type': 'track', 'event': 'python event', @@ -57,7 +58,7 @@ def test_flush_interval(self): # The consumer should upload _n_ times. q = Queue() flush_interval = 0.3 - consumer = Consumer(q, 'testsecret', flush_at=10, + consumer = Consumer(q, TEST_API_KEY, flush_at=10, flush_interval=flush_interval) with mock.patch('posthog.consumer.post') as mock_post: consumer.start() @@ -77,7 +78,7 @@ def test_multiple_uploads_per_interval(self): q = Queue() flush_interval = 0.5 flush_at = 10 - consumer = Consumer(q, 'testsecret', flush_at=flush_at, + consumer = Consumer(q, TEST_API_KEY, flush_at=flush_at, flush_interval=flush_interval) with mock.patch('posthog.consumer.post') as mock_post: consumer.start() @@ -92,7 +93,7 @@ def test_multiple_uploads_per_interval(self): self.assertEqual(mock_post.call_count, 2) def test_request(self): - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) track = { 'type': 'track', 'event': 'python event', @@ -135,21 +136,21 @@ def mock_post(*args, **kwargs): def test_request_retry(self): # we should retry on general errors - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) self._test_request_retry(consumer, Exception('generic exception'), 2) # we should retry on server errors - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) self._test_request_retry(consumer, APIError( 500, 'code', 'Internal Server Error'), 2) # we should retry on HTTP 429 errors - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) self._test_request_retry(consumer, APIError( 429, 'code', 'Too Many Requests'), 2) # we should NOT retry on other client errors - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) api_error = APIError(400, 'code', 'Client Errors') try: self._test_request_retry(consumer, api_error, 1) @@ -159,19 +160,19 @@ def test_request_retry(self): self.fail('request() should not retry on client errors') # test for number of exceptions raise > retries value - consumer = Consumer(None, 'testsecret', retries=3) + consumer = Consumer(None, TEST_API_KEY, retries=3) self._test_request_retry(consumer, APIError( 500, 'code', 'Internal Server Error'), 3) def test_pause(self): - consumer = Consumer(None, 'testsecret') + consumer = Consumer(None, TEST_API_KEY) consumer.pause() self.assertFalse(consumer.running) def test_max_batch_size(self): q = Queue() consumer = Consumer( - q, 'testsecret', flush_at=100000, flush_interval=3) + q, TEST_API_KEY, flush_at=100000, flush_interval=3) track = { 'type': 'track', 'event': 'python event', diff --git a/posthog/test/module.py b/posthog/test/module.py index 7aba2810..4f092cac 100644 --- a/posthog/test/module.py +++ b/posthog/test/module.py @@ -33,8 +33,8 @@ def test_alias(self): posthog.alias('previousId', 'distinct_id') posthog.flush() - def test_screen(self): - posthog.screen('distinct_id') + def test_page(self): + posthog.page('distinct_id') posthog.flush() def test_flush(self): diff --git a/posthog/test/request.py b/posthog/test/request.py index 5602986d..dfb6a35d 100644 --- a/posthog/test/request.py +++ b/posthog/test/request.py @@ -4,12 +4,13 @@ import requests from posthog.request import post, DatetimeSerializer +from posthog.test.utils import TEST_API_KEY class TestRequests(unittest.TestCase): def test_valid_request(self): - res = post('key', batch=[{ + res = post(TEST_API_KEY, batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' @@ -37,7 +38,7 @@ def test_date_serialization(self): self.assertEqual(result, expected) def test_should_not_timeout(self): - res = post('key', batch=[{ + res = post(TEST_API_KEY, batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' diff --git a/posthog/test/utils.py b/posthog/test/utils.py index 988bb385..749e51f1 100644 --- a/posthog/test/utils.py +++ b/posthog/test/utils.py @@ -7,6 +7,7 @@ from posthog import utils +TEST_API_KEY = 'kOOlRy2QlMY9jHZQv0bKz0FZyazBUoY8Arj0lFVNjs4' class TestUtils(unittest.TestCase): From ac74ee9a5c9fd8c799f5b5d18d0ca000df1ee3df Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Wed, 9 Sep 2020 16:26:59 +0200 Subject: [PATCH 4/8] fix test --- posthog/test/module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/test/module.py b/posthog/test/module.py index 4f092cac..a0f53e23 100644 --- a/posthog/test/module.py +++ b/posthog/test/module.py @@ -34,7 +34,7 @@ def test_alias(self): posthog.flush() def test_page(self): - posthog.page('distinct_id') + posthog.page('distinct_id', 'https://posthog.com/contact') posthog.flush() def test_flush(self): From 3b7f37aa394c53f1ce8b2bc5c678f28ffa0f9868 Mon Sep 17 00:00:00 2001 From: Yakko Majuri Date: Wed, 9 Sep 2020 16:44:41 +0000 Subject: [PATCH 5/8] updated tests action --- .github/workflows/tests.yml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9659ea9a..148ce004 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,18 +28,3 @@ jobs: DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres' run: | python setup.py test - - name: Run EE tests - env: - SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only - DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres' - PRIMARY_DB: 'clickhouse' - CLICKHOUSE_HOST: 'localhost' - CLICKHOUSE_DATABASE: 'posthog_test' - CLICKHOUSE_SECURE: 'False' - CLICKHOUSE_VERIFY: 'False' - run: | - mkdir -p frontend/dist - touch frontend/dist/index.html - touch frontend/dist/layout.html - touch frontend/dist/shared_dashboard.html - python manage.py test ee --testrunner="ee.clickhouse.clickhouse_test_runner.ClickhouseTestRunner" From 6508aa699494a2804a67467286e88a64c150b534 Mon Sep 17 00:00:00 2001 From: Yakko Majuri Date: Wed, 9 Sep 2020 16:48:07 +0000 Subject: [PATCH 6/8] further cleanup of action --- .github/workflows/tests.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 148ce004..fd8f8dd9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,8 +23,5 @@ jobs: python -m pip install -e . - name: Run posthog tests - env: - SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only - DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres' run: | python setup.py test From 678e4ac97b4404ce0c8a8b166ee9678930113eda Mon Sep 17 00:00:00 2001 From: Yakko Majuri Date: Fri, 11 Sep 2020 08:50:50 +0000 Subject: [PATCH 7/8] minor changes --- posthog/test/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/posthog/test/client.py b/posthog/test/client.py index 6a35ab19..94b54d0c 100644 --- a/posthog/test/client.py +++ b/posthog/test/client.py @@ -12,7 +12,7 @@ class TestClient(unittest.TestCase): def fail(self, e, batch): """Mark the failure handler""" - print('FAILL', e, batch) + print('FAIL', e, batch) self.failed = True def setUp(self): @@ -76,7 +76,7 @@ def test_basic_identify(self): self.assertTrue(success) self.assertFalse(self.failed) - self.assertEqual(msg['$set'], {'trait': 'value'}) + self.assertEqual(msg['$set']['trait'], 'value') self.assertTrue(isinstance(msg['timestamp'], str)) self.assertTrue(isinstance(msg['messageId'], str)) self.assertEqual(msg['distinct_id'], 'distinct_id') @@ -91,7 +91,7 @@ def test_advanced_identify(self): self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00') self.assertEqual(msg['context']['ip'], '192.168.0.1') - self.assertEqual(msg['$set'], {'trait': 'value'}) + self.assertEqual(msg['$set']['trait'], 'value') self.assertEqual(msg['properties']['$lib'], 'posthog-python') self.assertEqual(msg['properties']['$lib_version'], VERSION) self.assertTrue(isinstance(msg['timestamp'], str)) From 710ac05862d571ab67f1a79b86ef6048f2ef9757 Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Fri, 11 Sep 2020 15:37:06 +0200 Subject: [PATCH 8/8] Cleaned up simulator --- simulator.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/simulator.py b/simulator.py index 533c2c8b..74a8e62c 100644 --- a/simulator.py +++ b/simulator.py @@ -45,8 +45,8 @@ def failed(status, msg): raise Exception(msg) -def track(): - posthog.track(options.distinct_id, options.event, anonymous_id=options.anonymousId, +def capture(): + posthog.capture(options.distinct_id, options.event, anonymous_id=options.anonymousId, properties=json_hash(options.properties), context=json_hash(options.context)) @@ -55,21 +55,11 @@ def page(): properties=json_hash(options.properties), context=json_hash(options.context)) -def screen(): - posthog.screen(options.distinct_id, name=options.name, anonymous_id=options.anonymousId, - properties=json_hash(options.properties), context=json_hash(options.context)) - - def identify(): posthog.identify(options.distinct_id, anonymous_id=options.anonymousId, traits=json_hash(options.traits), context=json_hash(options.context)) -def group(): - posthog.group(options.distinct_id, options.groupId, json_hash(options.traits), - json_hash(options.context), anonymous_id=options.anonymousId) - - def unknown(): print() @@ -84,11 +74,9 @@ def unknown(): log.addHandler(ch) switcher = { - "track": track, + "capture": capture, "page": page, - "screen": screen, - "identify": identify, - "group": group + "identify": identify } func = switcher.get(options.type)