<s> from datetime import datetime <EOL> from flask_split . models import Alternative , Experiment <EOL> from flexmock import flexmock <EOL> from . import TestCase <EOL> class TestAlternative ( TestCase ) : <EOL> def test_has_name ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . name == '<STR_LIT>' <EOL> def test_has_default_participation_count_of_0 ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . participant_count == <NUM_LIT:0> <EOL> def test_has_default_completed_count_of_0 ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . completed_count == <NUM_LIT:0> <EOL> def test_belong_to_an_experiment ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . experiment . name == experiment . name <EOL> def test_saves_to_redis ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> alternative . save ( ) <EOL> assert '<STR_LIT>' in self . redis <EOL> def test_increment_participation_count ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> old_participant_count = alternative . participant_count <EOL> alternative . increment_participation ( ) <EOL> assert alternative . participant_count == old_participant_count + <NUM_LIT:1> <EOL> def test_increment_completed_count ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> old_completed_count = alternative . participant_count <EOL> alternative . increment_completion ( ) <EOL> assert alternative . completed_count == old_completed_count + <NUM_LIT:1> <EOL> def test_can_be_reset ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> alternative . participant_count = <NUM_LIT:10> <EOL> alternative . completed_count = <NUM_LIT:4> <EOL> alternative . reset ( ) <EOL> assert alternative . participant_count == <NUM_LIT:0> <EOL> assert alternative . completed_count == <NUM_LIT:0> <EOL> def test_know_if_it_is_the_control_of_an_experiment ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . is_control <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert not alternative . is_control <EOL> def test_conversion_rate_is_0_if_there_are_no_conversions ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . completed_count == <NUM_LIT:0> <EOL> assert alternative . conversion_rate == <NUM_LIT:0> <EOL> def test_conversion_rate_does_something ( self ) : <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> alternative . participant_count = <NUM_LIT:10> <EOL> alternative . completed_count = <NUM_LIT:4> <EOL> assert alternative . conversion_rate == <NUM_LIT> <EOL> def test_z_score_is_none_for_the_control ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . control . z_score is None <EOL> def test_z_score_is_none_when_the_control_has_no_participations ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . z_score is None <EOL> def test_z_score_is_none_when_alternative_has_no_participations ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert alternative . z_score is None <EOL> def test_z_score_when_control_and_alternative_have_perfect_conversion ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> control = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> control . completed_count = <NUM_LIT:10> <EOL> control . participant_count = <NUM_LIT:10> <EOL> alternative = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> alternative . completed_count = <NUM_LIT:8> <EOL> alternative . participant_count = <NUM_LIT:8> <EOL> assert alternative . z_score is None <EOL> def test_z_score ( self ) : <EOL> Experiment . find_or_create ( self . redis , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> control = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> control . participant_count = <NUM_LIT> <EOL> control . completed_count = <NUM_LIT> <EOL> treatment_a = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> treatment_a . participant_count = <NUM_LIT> <EOL> treatment_a . completed_count = <NUM_LIT> <EOL> treatment_b = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> treatment_b . participant_count = <NUM_LIT> <EOL> treatment_b . completed_count = <NUM_LIT> <EOL> treatment_c = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> treatment_c . participant_count = <NUM_LIT> <EOL> treatment_c . completed_count = <NUM_LIT> <EOL> assert control . z_score is None <EOL> assert round ( treatment_a . z_score , <NUM_LIT:2> ) == <NUM_LIT> <EOL> assert round ( treatment_b . z_score , <NUM_LIT:2> ) == - <NUM_LIT> <EOL> assert round ( treatment_c . z_score , <NUM_LIT:2> ) == <NUM_LIT> <EOL> class TestExperiment ( TestCase ) : <EOL> def test_has_name ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . name == '<STR_LIT>' <EOL> def test_has_alternatives ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert len ( experiment . alternatives ) == <NUM_LIT:2> <EOL> def test_saves_to_redis ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> assert '<STR_LIT>' in self . redis <EOL> def test_saves_the_start_time_to_redis ( self ) : <EOL> experiment_start_time = datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:0> <NUM_LIT:1> , <NUM_LIT> ) <EOL> ( flexmock ( Experiment ) <EOL> . should_receive ( '<STR_LIT>' ) <EOL> . and_return ( experiment_start_time ) ) <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> assert experiment . start_time == experiment_start_time <EOL> def test_handles_not_having_a_start_time ( self ) : <EOL> experiment_start_time = datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:0> <NUM_LIT:1> , <NUM_LIT> ) <EOL> ( flexmock ( Experiment ) <EOL> . should_receive ( '<STR_LIT>' ) <EOL> . and_return ( experiment_start_time ) ) <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> self . redis . hdel ( '<STR_LIT>' , experiment . name ) <EOL> assert experiment . start_time is None <EOL> def test_does_not_create_duplicates_when_saving_multiple_times ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> experiment . save ( ) <EOL> assert '<STR_LIT>' in self . redis <EOL> assert self . redis . lrange ( '<STR_LIT>' , <NUM_LIT:0> , - <NUM_LIT:1> ) == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_deleting_should_delete_itself ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> experiment . delete ( ) <EOL> assert '<STR_LIT>' not in self . redis <EOL> def test_deleting_should_increment_the_version ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . version == <NUM_LIT:0> <EOL> experiment . delete ( ) <EOL> assert experiment . version == <NUM_LIT:1> <EOL> def test_is_new_record_knows_if_it_hasnt_been_saved_yet ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . is_new_record <EOL> def test_is_new_record_knows_if_it_has_been_saved_yet ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> assert not experiment . is_new_record <EOL> def test_find_returns_an_existing_experiment ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> assert Experiment . find ( self . redis , '<STR_LIT>' ) . name == '<STR_LIT>' <EOL> def test_handles_non_existing_experiment ( self ) : <EOL> assert Experiment . find ( self . redis , '<STR_LIT>' ) is None <EOL> def test_control_is_the_first_alternative ( self ) : <EOL> experiment = Experiment ( self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> assert experiment . control . name == '<STR_LIT>' <EOL> def test_have_no_winner_initially ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . winner is None <EOL> def test_allow_you_to_specify_a_winner ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . winner = '<STR_LIT>' <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . winner . name == '<STR_LIT>' <EOL> def test_reset_should_reset_all_alternatives ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> green = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . winner = '<STR_LIT>' <EOL> assert experiment . next_alternative ( ) . name == '<STR_LIT>' <EOL> green . increment_participation ( ) <EOL> experiment . reset ( ) <EOL> reset_green = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert reset_green . participant_count == <NUM_LIT:0> <EOL> assert reset_green . completed_count == <NUM_LIT:0> <EOL> def test_reset_should_reset_the_winner ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> green = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . winner = '<STR_LIT>' <EOL> assert experiment . next_alternative ( ) . name == '<STR_LIT>' <EOL> green . increment_participation ( ) <EOL> experiment . reset ( ) <EOL> assert experiment . winner is None <EOL> def test_reset_should_increment_the_version ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . version == <NUM_LIT:0> <EOL> experiment . reset ( ) <EOL> assert experiment . version == <NUM_LIT:1> <EOL> def test_next_alternative_always_returns_the_winner_if_one_exists ( self ) : <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> green = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . winner = '<STR_LIT>' <EOL> assert experiment . next_alternative ( ) . name == '<STR_LIT>' <EOL> green . increment_participation ( ) <EOL> experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert experiment . next_alternative ( ) . name == '<STR_LIT>' <EOL> def test_reset_an_experiment_if_loaded_with_different_alternatives ( self ) : <EOL> experiment = Experiment ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> experiment . save ( ) <EOL> blue = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> blue . participant_count = <NUM_LIT:5> <EOL> blue . save ( ) <EOL> same_experiment = Experiment . find_or_create ( <EOL> self . redis , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> alternative_names = [ a . name for a in same_experiment . alternatives ] <EOL> assert alternative_names == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> new_blue = Alternative ( self . redis , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert new_blue . participant_count == <NUM_LIT:0> </s>
