<s> import mox <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import Context <EOL> from django . test . client import Client <EOL> from django . contrib . auth . models import User <EOL> from easy_split . experiments . models import Experiment , Participant <EOL> from easy_split . experiments . templatetags . experiments import ( <EOL> experiment , clientsideexperiment <EOL> ) <EOL> from easy_split . experiments . tests . utils import TestCase , TestUser <EOL> class ExperimentTagsTest ( TestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . experiment = Experiment ( name = "<STR_LIT>" ) <EOL> self . experiment . save ( ) <EOL> self . experiment . state = Experiment . ENABLED_STATE <EOL> self . experiment . save ( ) <EOL> self . other_experiment = Experiment ( name = "<STR_LIT>" ) <EOL> self . other_experiment . save ( ) <EOL> self . other_experiment . state = Experiment . ENABLED_STATE <EOL> self . other_experiment . save ( ) <EOL> self . mox = mox . Mox ( ) <EOL> def testIntegrationWithAnonymousVisitor ( self ) : <EOL> self . doTestIntegration ( <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ self . experiment . name ] ) , <EOL> client_factory = lambda i : Client ( ) ) <EOL> self . doTestIntegration ( <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ self . other_experiment . name ] ) , <EOL> client_factory = lambda i : Client ( ) ) <EOL> def testIntegrationWithRegisteredUser ( self ) : <EOL> def create_registered_user_client ( i ) : <EOL> user = User ( username = "<STR_LIT>" % i , email = "<STR_LIT>" % i ) <EOL> user . set_password ( "<STR_LIT:password>" ) <EOL> user . save ( ) <EOL> client = Client ( ) <EOL> if not client . login ( username = user . username , password = "<STR_LIT:password>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return client <EOL> self . doTestIntegration ( <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ self . experiment . name ] ) , <EOL> client_factory = create_registered_user_client ) <EOL> self . doTestIntegration ( <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ self . other_experiment . name ] ) , <EOL> client_factory = lambda i : create_registered_user_client ( <NUM_LIT:1000> + i ) ) <EOL> def doTestIntegration ( self , url , client_factory ) : <EOL> confirm_human_url = reverse ( '<STR_LIT>' ) <EOL> found_control = False <EOL> found_test = False <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> client = client_factory ( i ) <EOL> response = client . get ( confirm_human_url ) <EOL> self . assertEquals ( <NUM_LIT> , response . status_code ) <EOL> response = client . get ( url ) <EOL> self . assertEquals ( <NUM_LIT:200> , response . status_code ) <EOL> in_test = "<STR_LIT:test>" in response . content . lower ( ) <EOL> in_control = "<STR_LIT>" in response . content . lower ( ) <EOL> self . assertTrue ( in_test != in_control ) <EOL> found_control = found_control or in_control <EOL> found_test = found_test or in_test <EOL> self . assertTrue ( found_control ) <EOL> self . assertTrue ( found_test ) <EOL> def testIllegalGroupName ( self ) : <EOL> self . doRenderExperiment ( "<STR_LIT:username>" , "<STR_LIT>" , <EOL> expect_render_exception = True ) <EOL> def testBadSyntax ( self ) : <EOL> self . doRenderExperimentToken ( <EOL> "<STR_LIT:username>" , ( "<STR_LIT>" , self . experiment . name , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> expect_parse_exception = True ) <EOL> def doRenderExperimentToken ( self , username , token_tuple , <EOL> expect_parse_exception = False , <EOL> expect_render_exception = False ) : <EOL> internal_render_result = "<STR_LIT>" <EOL> parser = self . mox . CreateMockAnything ( ) <EOL> child_node_list = self . mox . CreateMockAnything ( ) <EOL> context = { } <EOL> user_factory = self . mox . CreateMockAnything ( ) <EOL> token = self . mox . CreateMockAnything ( ) <EOL> token . split_contents ( ) . AndReturn ( token_tuple ) <EOL> parser . parse ( ( '<STR_LIT>' , ) ) . MultipleTimes ( ) . AndReturn ( <EOL> child_node_list ) <EOL> parser . delete_first_token ( ) . MultipleTimes ( ) <EOL> user_factory . create_user ( context ) . MultipleTimes ( ) . AndReturn ( <EOL> TestUser ( username = username ) ) <EOL> child_node_list . render ( context ) . MultipleTimes ( ) . AndReturn ( <EOL> internal_render_result ) <EOL> self . mox . ReplayAll ( ) <EOL> child_node_list . render ( context ) <EOL> parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> user_factory . create_user ( context ) <EOL> do_parse = lambda : experiment ( parser , token , user_factory = user_factory ) <EOL> if expect_parse_exception : <EOL> self . assertRaises ( Exception , do_parse ) <EOL> else : <EOL> node = do_parse ( ) <EOL> if expect_parse_exception : <EOL> self . mox . VerifyAll ( ) <EOL> return None <EOL> do_render = lambda : node . render ( context ) <EOL> if expect_render_exception : <EOL> self . assertRaises ( Exception , do_render ) <EOL> else : <EOL> actual_render_result = do_render ( ) <EOL> if expect_render_exception : <EOL> self . mox . VerifyAll ( ) <EOL> return None <EOL> self . mox . VerifyAll ( ) <EOL> self . assertTrue ( actual_render_result == "<STR_LIT>" or <EOL> actual_render_result == internal_render_result ) <EOL> in_group = ( actual_render_result == internal_render_result ) <EOL> return in_group <EOL> def doRenderExperiment ( self , username , groupname , ** kwargs ) : <EOL> return self . doRenderExperimentToken ( <EOL> username , ( "<STR_LIT>" , self . experiment . name , groupname ) , <EOL> ** kwargs ) <EOL> def testExperimentTag ( self ) : <EOL> found_test = False <EOL> found_control = False <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> username = "<STR_LIT>" % i <EOL> in_test = self . doRenderExperiment ( username , "<STR_LIT:test>" ) <EOL> found_test = found_test or in_test <EOL> found_control = found_control or ( not in_test ) <EOL> self . assertEquals ( in_test , <EOL> self . doRenderExperiment ( username , "<STR_LIT:test>" ) ) <EOL> in_control = self . doRenderExperiment ( username , "<STR_LIT>" ) <EOL> self . assertEquals ( not in_test , in_control ) <EOL> self . assertTrue ( found_control and found_test ) <EOL> def doRenderClientSideExperiment ( self , context , username , experiment_name ) : <EOL> parser = self . mox . CreateMockAnything ( ) <EOL> user_factory = self . mox . CreateMockAnything ( ) <EOL> token = self . mox . CreateMockAnything ( ) <EOL> token . split_contents ( ) . AndReturn ( ( "<STR_LIT>" , <EOL> experiment_name ) ) <EOL> user_factory . create_user ( context ) . MultipleTimes ( ) . AndReturn ( <EOL> TestUser ( username = username ) ) <EOL> self . mox . ReplayAll ( ) <EOL> user_factory . create_user ( context ) <EOL> node = clientsideexperiment ( parser , token , user_factory = user_factory ) <EOL> node . render ( context ) <EOL> self . mox . VerifyAll ( ) <EOL> def testClientSideExperimentTag ( self ) : <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> user = User ( username = "<STR_LIT>" % i , email = "<STR_LIT>" % i ) <EOL> user . save ( ) <EOL> c = Context ( { "<STR_LIT:user>" : user } ) <EOL> self . doRenderClientSideExperiment ( c , "<STR_LIT>" % i , <EOL> self . experiment . name ) <EOL> self . assertTrue ( "<STR_LIT>" in c ) <EOL> self . doRenderClientSideExperiment ( c , "<STR_LIT>" % i , <EOL> self . experiment . name ) <EOL> self . doRenderClientSideExperiment ( c , "<STR_LIT>" % i , <EOL> self . other_experiment . name ) <EOL> self . doRenderClientSideExperiment ( c , "<STR_LIT>" % i , <EOL> self . other_experiment . name ) <EOL> experiment_partcipant = Participant . objects . get ( <EOL> user = user , <EOL> experiment = self . experiment ) <EOL> other_experiment_partcipant = Participant . objects . get ( <EOL> user = user , <EOL> experiment = self . other_experiment ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( c [ '<STR_LIT>' ] . keys ( ) ) ) <EOL> self . assertTrue ( self . experiment . name in <EOL> c [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . other_experiment . name in <EOL> c [ '<STR_LIT>' ] ) <EOL> group_name = c [ '<STR_LIT>' ] [ self . experiment . name ] <EOL> other_group_name = c [ '<STR_LIT>' ] [ self . other_experiment . name ] <EOL> group_id = experiment_partcipant . group <EOL> other_group_id = other_experiment_partcipant . group <EOL> self . assertEqual ( group_id == Participant . TEST_GROUP , <EOL> group_name == "<STR_LIT:test>" ) <EOL> self . assertEqual ( group_id == Participant . CONTROL_GROUP , <EOL> group_name == "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> other_group_id == Participant . TEST_GROUP , <EOL> other_group_name == "<STR_LIT:test>" ) <EOL> self . assertEqual ( <EOL> other_group_id == Participant . CONTROL_GROUP , <EOL> other_group_name == "<STR_LIT>" ) </s>
