<s> from __future__ import with_statement <EOL> from contextlib import contextmanager <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import AnonymousUser , User <EOL> from django . http import HttpRequest <EOL> from django_lean . experiments . models import ( AnonymousVisitor , Experiment , <EOL> GoalRecord , GoalType , Participant ) <EOL> from django_lean . experiments . tests . utils import get_session , patch , TestCase <EOL> from django_lean . experiments . utils import StaticUser , WebUser <EOL> from django_lean . lean_analytics import ( get_all_analytics , <EOL> get_all_analytics_names , <EOL> reset_caches , <EOL> IdentificationError ) <EOL> from django_lean . lean_analytics . base import BaseAnalytics <EOL> import mox <EOL> class TestAnalytics ( TestCase ) : <EOL> def test_get_all_analytics_names ( self ) : <EOL> with patch ( settings , '<STR_LIT>' , NotImplemented ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( get_all_analytics_names ( ) , ( ) ) <EOL> with patch ( settings , '<STR_LIT>' , [ ] ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( get_all_analytics_names ( ) , [ ] ) <EOL> base_name = '<STR_LIT>' % ( BaseAnalytics . __module__ , BaseAnalytics . __name__ ) <EOL> with patch ( settings , '<STR_LIT>' , [ base_name ] ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( get_all_analytics_names ( ) , [ base_name ] ) <EOL> def test_get_all_analytics ( self ) : <EOL> with patch ( settings , '<STR_LIT>' , NotImplemented ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( get_all_analytics ( ) , [ ] ) <EOL> with patch ( settings , '<STR_LIT>' , [ ] ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( get_all_analytics ( ) , [ ] ) <EOL> base_name = '<STR_LIT>' % ( BaseAnalytics . __module__ , BaseAnalytics . __name__ ) <EOL> with patch ( settings , '<STR_LIT>' , [ base_name ] ) : <EOL> reset_caches ( ) <EOL> self . assertEqual ( [ a . __class__ . __name__ for a in get_all_analytics ( ) ] , <EOL> [ BaseAnalytics . __name__ ] ) <EOL> try : <EOL> import django_kissmetrics <EOL> except ImportError : <EOL> if '<STR_LIT>' in get_all_analytics_names ( ) : <EOL> traceback . print_exc ( ) <EOL> else : <EOL> from django_lean . lean_analytics . kissmetrics import KissMetrics <EOL> class TestKissMetrics ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mox = mox . Mox ( ) <EOL> self . analytics = KissMetrics ( ) <EOL> def test_id_from_user ( self ) : <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> self . assertEqual ( self . analytics . _id_from_user ( user ) , <EOL> '<STR_LIT>' % user . pk ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _id_from_user , None ) <EOL> def test_id_from_session ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> session = experiment_user . session <EOL> self . assertEqual ( <EOL> self . analytics . _id_from_session ( experiment_user . session ) , <EOL> '<STR_LIT>' % session . session_key <EOL> ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _id_from_session , <EOL> experiment_user . session ) <EOL> def test_compute_id ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> session = experiment_user . session <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( self . analytics . _compute_id ( experiment_user ) , <EOL> '<STR_LIT>' % session . session_key ) <EOL> self . mox . VerifyAll ( ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( self . analytics . _compute_id ( experiment_user ) , <EOL> '<STR_LIT>' % user . id ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _compute_id , experiment_user ) <EOL> def test_identify ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertTrue ( self . analytics . _identify ( experiment_user ) ) <EOL> self . mox . VerifyAll ( ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertTrue ( self . analytics . _identify ( experiment_user ) ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertFalse ( self . analytics . _identify ( experiment_user ) ) <EOL> def test_enroll ( self ) : <EOL> experiment = Experiment . objects . create ( name = '<STR_LIT>' ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> KM = self . mox . CreateMockAnything ( ) <EOL> analytics = KissMetrics ( KM = KM ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> KM . identify ( analytics . _compute_id ( experiment_user ) ) <EOL> KM . record ( action = '<STR_LIT>' , <EOL> props = { '<STR_LIT>' : experiment . name , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . mox . ReplayAll ( ) <EOL> analytics . enroll ( experiment = experiment , <EOL> experiment_user = experiment_user , <EOL> group_id = Participant . TEST_GROUP ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_record ( self ) : <EOL> KM = self . mox . CreateMockAnything ( ) <EOL> analytics = KissMetrics ( KM = KM ) <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> KM . identify ( analytics . _id_from_session ( experiment_user . session ) ) <EOL> KM . record ( action = '<STR_LIT>' , <EOL> props = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . mox . ReplayAll ( ) <EOL> goal_type = GoalType . objects . create ( name = '<STR_LIT>' ) <EOL> goal_record = GoalRecord . record ( goal_name = goal_type . name , <EOL> experiment_user = experiment_user ) <EOL> analytics . record ( goal_record = goal_record , <EOL> experiment_user = experiment_user ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_event ( self ) : <EOL> KM = self . mox . CreateMockAnything ( ) <EOL> analytics = KissMetrics ( KM = KM ) <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> KM . identify ( analytics . _id_from_session ( experiment_user . session ) ) <EOL> KM . record ( action = '<STR_LIT>' , props = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . mox . ReplayAll ( ) <EOL> analytics . event ( name = '<STR_LIT>' , <EOL> properties = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> request = experiment_user . request ) <EOL> self . mox . VerifyAll ( ) <EOL> @ contextmanager <EOL> def web_user ( self , user ) : <EOL> session = get_session ( None ) <EOL> request = self . mox . CreateMock ( HttpRequest ) <EOL> request . user = user <EOL> request . session = session <EOL> experiment_user = WebUser ( request ) <EOL> experiment_user . get_or_create_anonymous_visitor ( ) <EOL> yield experiment_user <EOL> try : <EOL> import mixpanel <EOL> except ImportError : <EOL> if '<STR_LIT>' in get_all_analytics_names ( ) : <EOL> traceback . print_exc ( ) <EOL> else : <EOL> from django_lean . lean_analytics . mixpanel import Mixpanel <EOL> class TestMixpanel ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mox = mox . Mox ( ) <EOL> self . analytics = Mixpanel ( ) <EOL> def tearDown ( self ) : <EOL> self . mox . UnsetStubs ( ) <EOL> def test_id_from_user ( self ) : <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> self . assertEqual ( self . analytics . _id_from_user ( user ) , <EOL> '<STR_LIT>' % user . pk ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _id_from_user , None ) <EOL> def test_id_from_session ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> session = experiment_user . session <EOL> self . assertEqual ( <EOL> self . analytics . _id_from_session ( experiment_user . session ) , <EOL> '<STR_LIT>' % session . session_key <EOL> ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _id_from_session , <EOL> experiment_user . session ) <EOL> def test_compute_id ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> session = experiment_user . session <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( self . analytics . _compute_id ( experiment_user ) , <EOL> '<STR_LIT>' % session . session_key ) <EOL> self . mox . VerifyAll ( ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( self . analytics . _compute_id ( experiment_user ) , <EOL> '<STR_LIT>' % user . id ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertRaises ( IdentificationError , <EOL> self . analytics . _compute_id , experiment_user ) <EOL> def test_identify ( self ) : <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertTrue ( self . analytics . _identify ( experiment_user ) ) <EOL> self . assertEqual ( <EOL> self . analytics . identity , <EOL> '<STR_LIT>' % experiment_user . session . session_key <EOL> ) <EOL> self . mox . VerifyAll ( ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> self . mox . ReplayAll ( ) <EOL> self . assertTrue ( self . analytics . _identify ( experiment_user ) ) <EOL> self . assertEqual ( self . analytics . identity , <EOL> '<STR_LIT>' % experiment_user . user . pk ) <EOL> self . mox . VerifyAll ( ) <EOL> experiment_user = StaticUser ( ) <EOL> self . assertFalse ( self . analytics . _identify ( experiment_user ) ) <EOL> self . assertEqual ( self . analytics . identity , None ) <EOL> def test_enroll ( self ) : <EOL> import time <EOL> experiment = Experiment . objects . create ( name = '<STR_LIT>' ) <EOL> user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> tracker = self . mox . CreateMockAnything ( ) <EOL> analytics = Mixpanel ( tracker = tracker ) <EOL> now = time . gmtime ( ) <EOL> self . mox . StubOutWithMock ( time , '<STR_LIT>' ) <EOL> time . gmtime ( ) . AndReturn ( now ) <EOL> with self . web_user ( user ) as experiment_user : <EOL> properties = { '<STR_LIT:time>' : '<STR_LIT>' % time . mktime ( now ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % user . pk , <EOL> '<STR_LIT>' : experiment . name , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> tracker . run ( event_name = '<STR_LIT>' , <EOL> properties = properties ) <EOL> self . mox . ReplayAll ( ) <EOL> analytics . enroll ( experiment = experiment , <EOL> experiment_user = experiment_user , <EOL> group_id = Participant . TEST_GROUP ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_record ( self ) : <EOL> import time <EOL> tracker = self . mox . CreateMockAnything ( ) <EOL> analytics = Mixpanel ( tracker = tracker ) <EOL> now = time . gmtime ( ) <EOL> self . mox . StubOutWithMock ( time , '<STR_LIT>' ) <EOL> time . gmtime ( ) . AndReturn ( now ) <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> properties = { <EOL> '<STR_LIT:time>' : '<STR_LIT>' % time . mktime ( now ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' % <EOL> experiment_user . session . session_key ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> tracker . run ( event_name = '<STR_LIT>' , <EOL> properties = properties ) <EOL> self . mox . ReplayAll ( ) <EOL> goal_type = GoalType . objects . create ( name = '<STR_LIT>' ) <EOL> goal_record = GoalRecord . record ( goal_name = goal_type . name , <EOL> experiment_user = experiment_user ) <EOL> analytics . record ( goal_record = goal_record , <EOL> experiment_user = experiment_user ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_event ( self ) : <EOL> import time <EOL> tracker = self . mox . CreateMockAnything ( ) <EOL> analytics = Mixpanel ( tracker = tracker ) <EOL> now = time . gmtime ( ) <EOL> self . mox . StubOutWithMock ( time , '<STR_LIT>' ) <EOL> time . gmtime ( ) . AndReturn ( now ) <EOL> with self . web_user ( AnonymousUser ( ) ) as experiment_user : <EOL> properties = { <EOL> '<STR_LIT:time>' : '<STR_LIT>' % time . mktime ( now ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' % <EOL> experiment_user . session . session_key ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> tracker . run ( event_name = '<STR_LIT>' , <EOL> properties = properties ) <EOL> self . mox . ReplayAll ( ) <EOL> analytics . event ( name = '<STR_LIT>' , <EOL> properties = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> request = experiment_user . request ) <EOL> self . mox . VerifyAll ( ) <EOL> @ contextmanager <EOL> def web_user ( self , user ) : <EOL> session = get_session ( None ) <EOL> request = self . mox . CreateMock ( HttpRequest ) <EOL> request . user = user <EOL> request . session = session <EOL> experiment_user = WebUser ( request ) <EOL> experiment_user . get_or_create_anonymous_visitor ( ) <EOL> yield experiment_user </s>
