From 3f727d892cf87a96a0bdb460f625be96fc867d48 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Thu, 29 Sep 2016 23:15:39 -0500 Subject: [PATCH] refactor events; add tests --- lbrynet/analytics/events.py | 40 +++++++++++++++-------------- tests/unit/analytics/__init__.py | 0 tests/unit/analytics/test_events.py | 38 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 tests/unit/analytics/__init__.py create mode 100644 tests/unit/analytics/test_events.py diff --git a/lbrynet/analytics/events.py b/lbrynet/analytics/events.py index 8b1c63287..94d6ce679 100644 --- a/lbrynet/analytics/events.py +++ b/lbrynet/analytics/events.py @@ -1,6 +1,6 @@ import logging -from lbrynet.analytics import utils +from lbrynet.core import utils log = logging.getLogger(__name__) @@ -23,31 +23,33 @@ class Events(object): self.session_id = session_id def heartbeat(self): - return { - 'userId': 'lbry', - 'event': 'Heartbeat', - 'properties': { - 'lbry_id': self.lbry_id, - 'session_id': self.session_id - }, - 'context': self.context, - 'timestamp': utils.now() - } + return self._event('Heartbeat') def download_started(self, name, stream_info=None): + properties = { + 'name': name, + 'stream_info': get_sd_hash(stream_info) + } + return self._event('Download Started', properties) + + def _event(self, event, event_properties=None): return { 'userId': 'lbry', - 'event': 'Download Started', - 'properties': { - 'lbry_id': self.lbry_id, - 'session_id': self.session_id, - 'name': name, - 'stream_info': get_sd_hash(stream_info) - }, + 'event': event, + 'properties': self._properties(event_properties), 'context': self.context, - 'timestamp': utils.now() + 'timestamp': utils.isonow() } + def _properties(self, event_properties=None): + event_properties = event_properties or {} + properties = { + 'lbry_id': self.lbry_id, + 'session_id': self.session_id, + } + properties.update(event_properties) + return properties + def make_context(platform, wallet, is_dev=False): # TODO: distinguish between developer and release instances diff --git a/tests/unit/analytics/__init__.py b/tests/unit/analytics/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/analytics/test_events.py b/tests/unit/analytics/test_events.py new file mode 100644 index 000000000..d9baf2e59 --- /dev/null +++ b/tests/unit/analytics/test_events.py @@ -0,0 +1,38 @@ +from lbrynet.analytics import events + +from twisted.trial import unittest + +from tests import util + + +class EventsTest(unittest.TestCase): + def setUp(self): + util.resetTime(self) + self.event_generator = events.Events('any valid json datatype', 'lbry123', 'session456') + + def test_heartbeat(self): + result = self.event_generator.heartbeat() + desired_result = { + 'context': 'any valid json datatype', + 'event': 'Heartbeat', + 'properties': {'lbry_id': 'lbry123', 'session_id': 'session456'}, + 'timestamp': '2016-01-01T00:00:00Z', + 'userId': 'lbry' + } + self.assertEqual(desired_result, result) + + def test_download_started(self): + result = self.event_generator.download_started('great gatsby') + desired_result = { + 'context': 'any valid json datatype', + 'event': 'Download Started', + 'properties': { + 'lbry_id': 'lbry123', + 'session_id': 'session456', + 'name': 'great gatsby', + 'stream_info': None, + }, + 'timestamp': '2016-01-01T00:00:00Z', + 'userId': 'lbry' + } + self.assertEqual(desired_result, result)