Example #1
0
    def test_model_question(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})
        question_entity = Question.get_current_question()

        # Check current question
        assert question_entity.key.id() == "q2"
        State.update_state_scores_for_completed_question(question_entity)
        Question.tally_college_and_vote_scores(question_entity)

        # NY and TX was a draw so random 2 votes will be added
        assert question_entity.vote_score[0] == (505 + 120 + 100 + 150) or \
                                                (505 + 120 + 100 + 150) + 1 or \
                                                (505 + 120 + 100 + 150) + 2
        assert question_entity.vote_score[1] == (510 + 240 + 100 + 150) or \
                                                (510 + 240 + 100 + 150) +1 or \
                                                (510 + 240 + 100 + 150) +2 

        # NY and TX was a draw so could go either way (29, 38)
        assert question_entity.college_score[0] == 0 or 29 or 38 or (29 + 38)
        assert question_entity.college_score[1] == (55 + 12) + 0  or \
                                                   (55 + 12) + 29 or \
                                                   (55 + 12) + 38 or \
                                                   (55 + 12) + (29 + 38)

        # Check get next questions
        question_entity = Question.get_next_question()
        assert question_entity.key.id() == "q3"

        # Check get last question start  time
        current_question_start_time = Question.get_current_question_start_time()
        current_question_start_time_string = str(current_question_start_time)
        assert current_question_start_time_string == "2016-01-23 00:00:00"
Example #2
0
    def test_view_tasks_attribute_sway_points_for_user(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})
        current_question = Question.get_current_question()
        vote_previous = Vote(
            question=current_question.key,
            replyid="123",
            state_abbreviation="CA",
            party=1,
            sway_points=20,
            winning_vote=True,
        )
        vote_current = Vote(
            question=current_question.key,
            replyid="123",
            state_abbreviation="CA",
            party=0,
            sway_points=20,
            winning_vote=None,
        )
        user = User(
            userid="jrgrafton",
            votes=[vote_current],
            # Can use sway points upon creation
            sway_points=30,
        )

        # So that we can determine if user had winning vote
        State.update_state_scores_for_completed_question(current_question)
        Question.tally_college_and_vote_scores(current_question)

        # Vote - loosing (party 0 has least electoral_votes for this question)
        original_sway_points = user.sway_points
        tasks.__dict__["__attribute_sway_points_for_user"](current_question, user)
        assert user.sway_points == original_sway_points + sway_points_tasks["submit_answer"]

        # Vote - winning (party 1 has most electoral_votes for this question)
        original_sway_points = user.sway_points
        vote_current.party = 1
        tasks.__dict__["__attribute_sway_points_for_user"](current_question, user)

        assert user.sway_points == original_sway_points + sway_points_tasks["submit_answer"] + sway_points_tasks[
            "submit_winning_answer"
        ] + int(vote_current.sway_points * sway_points_tasks["refund"])

        # Vote - winning + streak
        original_sway_points = user.sway_points
        user.votes.insert(0, vote_previous)
        tasks.__dict__["__attribute_sway_points_for_user"](current_question, user)
        assert user.sway_points == original_sway_points + sway_points_tasks["submit_answer"] + sway_points_tasks[
            "submit_winning_answer"
        ] + sway_points_tasks["streak_bonus"] + int(vote_current.sway_points * sway_points_tasks["refund"])
Example #3
0
    def test_model_state(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json',
                     kind={
                         'Question': Question,
                         'State': State
                     })

        question_entity = Question.get_current_question()
        State.update_state_scores_for_completed_question(question_entity)

        state_entity = State.get_state_by_abbreviation("CA")
        assert state_entity.party_score_votes[0] == 0
        assert state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 5
        assert state_entity.party_score_sway[1] == 500
        assert state_entity.last_winning_party == 1

        state_entity = State.get_state_by_abbreviation("WA")
        assert state_entity.party_score_votes[0] == 0
        assert state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 50
        assert state_entity.party_score_sway[1] == 100
        assert state_entity.last_winning_party == 1

        # Random winner is allocated for state drawing
        state_entity = State.get_state_by_abbreviation("TX")
        assert state_entity.party_score_votes[0] == 1 or \
               state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 80
        assert state_entity.party_score_sway[1] == 70
        assert state_entity.last_winning_party != None

        # What happens when state hasn't been voted on?
        state_entity = State.get_state_by_abbreviation("NY")
        assert state_entity.party_score_votes[0] == 0 or \
               state_entity.party_score_votes[1] == 0
        assert state_entity.last_winning_party != None

        assert State.is_valid_state("CA") == True
        assert State.is_valid_state("WA") == True
        assert State.is_valid_state("INVALID") == False
Example #4
0
def twitter_post_status():
    """ Potentially post a new status to Twitter
    """
    # Can be overriden in GET request
    question_cadence_minutes = \
        int(request.args.get('question_cadence_minutes', 60 * 6))
    post_to_twitter = request.args.get('post_to_twitter', False)

    next_question = Question.get_next_question()
    if next_question is not None \
            and __is_time_for_new_question(question_cadence_minutes):
        current_question = Question.get_current_question()

        if post_to_twitter != False:
            try:
                twitter_api = TwitterAPI()
                status = twitter_api.update_status(next_question.question_text)
                next_question.twitterid = str(status.id)
                next_question.put()
            except TweepError as e:
                pass  #TODO: do something - message to monitoring?

        if current_question is not None:
            # Update overall state scores
            State.update_state_scores_for_completed_question(current_question)

            # Update overall question scores
            Question.tally_college_and_vote_scores(current_question)

            # Update end of question user sway scores
            users = User.get_all()
            for user in users:
                __attribute_sway_points_for_user(current_question, user)
            current_question.end_time = datetime.datetime.now()
            current_question.put()

        next_question.start_time = datetime.datetime.now()
        next_question.put()

        return 'Posted new status [%s]' % next_question.question_text, 200

    return 'New question not ready', 200
Example #5
0
def twitter_post_status():
    """ Potentially post a new status to Twitter
    """
    # Can be overriden in GET request
    question_cadence_minutes = \
        int(request.args.get('question_cadence_minutes', 60 * 6))
    post_to_twitter = request.args.get('post_to_twitter', False)
    
    next_question = Question.get_next_question()
    if next_question is not None \
            and __is_time_for_new_question(question_cadence_minutes):
        current_question = Question.get_current_question()
        
        if post_to_twitter != False:
            try:
                twitter_api = TwitterAPI()
                status = twitter_api.update_status(next_question.question_text)
                next_question.twitterid = str(status.id)
                next_question.put()
            except TweepError as e:
                pass  #TODO: do something - message to monitoring?

        if current_question is not None:
            # Update overall state scores
            State.update_state_scores_for_completed_question(current_question)
            
            # Update overall question scores
            Question.tally_college_and_vote_scores(current_question)

            # Update end of question user sway scores
            users = User.get_all()
            for user in users:
                __attribute_sway_points_for_user(current_question, user)
            current_question.end_time = datetime.datetime.now()
            current_question.put()
        
        next_question.start_time = datetime.datetime.now()
        next_question.put()

        return 'Posted new status [%s]' % next_question.question_text, 200

    return 'New question not ready', 200
Example #6
0
    def test_model_state(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})

        question_entity = Question.get_current_question()
        State.update_state_scores_for_completed_question(question_entity)

        state_entity = State.get_state_by_abbreviation("CA")
        assert state_entity.party_score_votes[0] == 0
        assert state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 5
        assert state_entity.party_score_sway[1] == 500
        assert state_entity.last_winning_party == 1

        state_entity = State.get_state_by_abbreviation("WA")
        assert state_entity.party_score_votes[0] == 0
        assert state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 50
        assert state_entity.party_score_sway[1] == 100
        assert state_entity.last_winning_party == 1

        # Random winner is allocated for state drawing
        state_entity = State.get_state_by_abbreviation("TX")
        assert state_entity.party_score_votes[0] == 1 or \
               state_entity.party_score_votes[1] == 1
        assert state_entity.party_score_sway[0] == 80
        assert state_entity.party_score_sway[1] == 70
        assert state_entity.last_winning_party != None

        # What happens when state hasn't been voted on?
        state_entity = State.get_state_by_abbreviation("NY")
        assert state_entity.party_score_votes[0] == 0 or \
               state_entity.party_score_votes[1] == 0
        assert state_entity.last_winning_party != None

        assert State.is_valid_state("CA") == True
        assert State.is_valid_state("WA") == True
        assert State.is_valid_state("INVALID") == False
Example #7
0
    def test_model_question(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json',
                     kind={
                         'Question': Question,
                         'State': State
                     })
        question_entity = Question.get_current_question()

        # Check current question
        assert question_entity.key.id() == "q2"
        State.update_state_scores_for_completed_question(question_entity)
        Question.tally_college_and_vote_scores(question_entity)

        # NY and TX was a draw so random 2 votes will be added
        assert question_entity.vote_score[0] == (505 + 120 + 100 + 150) or \
                                                (505 + 120 + 100 + 150) + 1 or \
                                                (505 + 120 + 100 + 150) + 2
        assert question_entity.vote_score[1] == (510 + 240 + 100 + 150) or \
                                                (510 + 240 + 100 + 150) +1 or \
                                                (510 + 240 + 100 + 150) +2

        # NY and TX was a draw so could go either way (29, 38)
        assert question_entity.college_score[0] == 0 or 29 or 38 or (29 + 38)
        assert question_entity.college_score[1] == (55 + 12) + 0  or \
                                                   (55 + 12) + 29 or \
                                                   (55 + 12) + 38 or \
                                                   (55 + 12) + (29 + 38)

        # Check get next questions
        question_entity = Question.get_next_question()
        assert question_entity.key.id() == "q3"

        # Check get last question start  time
        current_question_start_time = Question.get_current_question_start_time(
        )
        current_question_start_time_string = str(current_question_start_time)
        assert current_question_start_time_string == "2016-01-23 00:00:00"
Example #8
0
    def test_view_tasks_attribute_sway_points_for_user(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})
        current_question = Question.get_current_question()
        vote_previous = Vote(
            question = current_question.key,
            replyid = "123",
            state_abbreviation = "CA",
            party = 1,
            sway_points = 20,
            winning_vote = True
        )
        vote_current = Vote(
            question = current_question.key,
            replyid = "123",
            state_abbreviation = "CA",
            party = 0,
            sway_points = 20,
            winning_vote = None
        )
        user = User(
            userid = "jrgrafton",
            votes = [vote_current],
            # Can use sway points upon creation
            sway_points = 30
        )

        # So that we can determine if user had winning vote
        State.update_state_scores_for_completed_question(current_question)
        Question.tally_college_and_vote_scores(current_question)

        # Vote - loosing (party 0 has least electoral_votes for this question)
        original_sway_points = user.sway_points
        tasks.__dict__\
            ["__attribute_sway_points_for_user"](current_question, user)
        assert user.sway_points == original_sway_points + \
                                   sway_points_tasks["submit_answer"]

        # Vote - winning (party 1 has most electoral_votes for this question)
        original_sway_points = user.sway_points
        vote_current.party = 1
        tasks.__dict__\
            ["__attribute_sway_points_for_user"](current_question, user)

        assert user.sway_points == original_sway_points + \
                                  sway_points_tasks["submit_answer"] + \
                                  sway_points_tasks["submit_winning_answer"] + \
                                  int(vote_current.sway_points * \
                                      sway_points_tasks["refund"])

        # Vote - winning + streak
        original_sway_points = user.sway_points
        user.votes.insert(0, vote_previous)
        tasks.__dict__\
            ["__attribute_sway_points_for_user"](current_question, user)
        assert user.sway_points == original_sway_points + \
                                  sway_points_tasks["submit_answer"] + \
                                  sway_points_tasks["submit_winning_answer"] + \
                                  sway_points_tasks["streak_bonus"] + \
                                  int(vote_current.sway_points * \
                                      sway_points_tasks["refund"])