def test_event(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action="Event", props={"Foo": "Bar"}) self.mox.ReplayAll() analytics.event(name="Event", properties={"Foo": "Bar"}, request=experiment_user.request) self.mox.VerifyAll()
def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action="Goal Recorded", props={"Goal Type": "Goal Type"}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name="Goal Type") goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
def test_event(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action='Event', props={'Foo': 'Bar'}) self.mox.ReplayAll() analytics.event(name='Event', properties={'Foo': 'Bar'}, request=experiment_user.request) self.mox.VerifyAll()
def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action='Goal Recorded', props={'Goal Type': 'Goal Type'}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name='Goal Type') goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
class TestKissMetrics(TestCase): def setUp(self): self.mox = mox.Mox() self.analytics = KissMetrics() def test_id_from_user(self): user = User.objects.create_user("user", "*****@*****.**", "user") self.assertEqual(self.analytics._id_from_user(user), "User %d" % user.pk) self.assertRaises(IdentificationError, self.analytics._id_from_user, None) def test_id_from_session(self): # With real session with self.web_user(AnonymousUser()) as experiment_user: self.mox.ReplayAll() session = experiment_user.session self.assertEqual( self.analytics._id_from_session(experiment_user.session), "Session %s" % session.session_key ) self.mox.VerifyAll() # With dict as session experiment_user = StaticUser() self.assertRaises(IdentificationError, self.analytics._id_from_session, experiment_user.session) def test_compute_id(self): # With anonymous WebUser with self.web_user(AnonymousUser()) as experiment_user: session = experiment_user.session self.mox.ReplayAll() self.assertEqual(self.analytics._compute_id(experiment_user), "Session %s" % session.session_key) self.mox.VerifyAll() # With authenticated WebUser user = User.objects.create_user("user", "*****@*****.**", "user") with self.web_user(user) as experiment_user: self.mox.ReplayAll() self.assertEqual(self.analytics._compute_id(experiment_user), "User %d" % user.id) self.mox.VerifyAll() # With StaticUser experiment_user = StaticUser() self.assertRaises(IdentificationError, self.analytics._compute_id, experiment_user) def test_identify(self): # With anonymous WebUser with self.web_user(AnonymousUser()) as experiment_user: self.mox.ReplayAll() self.assertTrue(self.analytics._identify(experiment_user)) self.mox.VerifyAll() # With authenticated WebUser user = User.objects.create_user("user", "*****@*****.**", "user") with self.web_user(user) as experiment_user: self.mox.ReplayAll() self.assertTrue(self.analytics._identify(experiment_user)) self.mox.VerifyAll() # With StaticUser experiment_user = StaticUser() self.assertFalse(self.analytics._identify(experiment_user)) def test_enroll(self): experiment = Experiment.objects.create(name="Experiment") user = User.objects.create_user("user", "*****@*****.**", "user") KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(user) as experiment_user: KM.identify(analytics._compute_id(experiment_user)) KM.record(action="Enrolled In Experiment", props={"Experiment": experiment.name, "Group": "Test"}) self.mox.ReplayAll() analytics.enroll( experiment=experiment, experiment_user=experiment_user, group_id=Participant.TEST_GROUP ) self.mox.VerifyAll() def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action="Goal Recorded", props={"Goal Type": "Goal Type"}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name="Goal Type") goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll() def test_event(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action="Event", props={"Foo": "Bar"}) self.mox.ReplayAll() analytics.event(name="Event", properties={"Foo": "Bar"}, request=experiment_user.request) self.mox.VerifyAll() @contextmanager def web_user(self, user): session = get_session(None) request = self.mox.CreateMock(HttpRequest) request.user = user request.session = session experiment_user = WebUser(request) experiment_user.get_or_create_anonymous_visitor() yield experiment_user
class TestKissMetrics(TestCase): def setUp(self): self.mox = mox.Mox() self.analytics = KissMetrics() def test_id_from_user(self): user = User.objects.create_user('user', '*****@*****.**', 'user') self.assertEqual(self.analytics._id_from_user(user), 'User %d' % user.pk) self.assertRaises(IdentificationError, self.analytics._id_from_user, None) def test_id_from_session(self): # With real session with self.web_user(AnonymousUser()) as experiment_user: self.mox.ReplayAll() session = experiment_user.session self.assertEqual( self.analytics._id_from_session(experiment_user.session), 'Session %s' % session.session_key ) self.mox.VerifyAll() # With dict as session experiment_user = StaticUser() self.assertRaises(IdentificationError, self.analytics._id_from_session, experiment_user.session) def test_compute_id(self): # With anonymous WebUser with self.web_user(AnonymousUser()) as experiment_user: session = experiment_user.session self.mox.ReplayAll() self.assertEqual(self.analytics._compute_id(experiment_user), 'Session %s' % session.session_key) self.mox.VerifyAll() # With authenticated WebUser user = User.objects.create_user('user', '*****@*****.**', 'user') with self.web_user(user) as experiment_user: self.mox.ReplayAll() self.assertEqual(self.analytics._compute_id(experiment_user), 'User %d' % user.id) self.mox.VerifyAll() # With StaticUser experiment_user = StaticUser() self.assertRaises(IdentificationError, self.analytics._compute_id, experiment_user) def test_identify(self): # With anonymous WebUser with self.web_user(AnonymousUser()) as experiment_user: self.mox.ReplayAll() self.assertTrue(self.analytics._identify(experiment_user)) self.mox.VerifyAll() # With authenticated WebUser user = User.objects.create_user('user', '*****@*****.**', 'user') with self.web_user(user) as experiment_user: self.mox.ReplayAll() self.assertTrue(self.analytics._identify(experiment_user)) self.mox.VerifyAll() # With StaticUser experiment_user = StaticUser() self.assertFalse(self.analytics._identify(experiment_user)) def test_enroll(self): experiment = Experiment.objects.create(name='Experiment') user = User.objects.create_user('user', '*****@*****.**', 'user') KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(user) as experiment_user: KM.identify(analytics._compute_id(experiment_user)) KM.record(action='Enrolled In Experiment', props={'Experiment': experiment.name, 'Group': 'Test'}) self.mox.ReplayAll() analytics.enroll(experiment=experiment, experiment_user=experiment_user, group_id=Participant.TEST_GROUP) self.mox.VerifyAll() def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action='Goal Recorded', props={'Goal Type': 'Goal Type'}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name='Goal Type') goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll() def test_event(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action='Event', props={'Foo': 'Bar'}) self.mox.ReplayAll() analytics.event(name='Event', properties={'Foo': 'Bar'}, request=experiment_user.request) self.mox.VerifyAll() @contextmanager def web_user(self, user): session = get_session(None) request = self.mox.CreateMock(HttpRequest) request.user = user request.session = session experiment_user = WebUser(request) experiment_user.get_or_create_anonymous_visitor() yield experiment_user