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"
def load_states(self): # Load states logging.info('load_states:') states = self.bootstrap_json["states"] for state in states: state = State(state_abbreviation=state["state_abbreviation"], college_votes=int(state["college_votes"]), party_score_votes=[0, 0], party_score_sway=[0, 0]) state.put()
def load_states(self): # Load states logging.info('load_states:') states = self.bootstrap_json["states"] for state in states: state = State( state_abbreviation = state["state_abbreviation"], college_votes = int(state["college_votes"]), party_score_votes = [0, 0], party_score_sway = [0, 0] ) state.put()
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"])
def __attribute_sway_points_for_user(current_question, user): # At least one vote on current question if len(user.votes) > 0 \ and user.votes[-1].question.id() == current_question.key.id(): user.sway_points += sway_points["submit_answer"] # Voted for winning party state = State.get_state_by_abbreviation(\ user.votes[-1].state_abbreviation) last_winning_party = 1 if current_question.college_score[1] > \ current_question.college_score[0] else 0 # Voted on party that won last question if user.votes[-1].party == last_winning_party: user.votes[-1].winning_vote = True user.sway_points += sway_points["submit_winning_answer"] # Voted for winning party twice in a row if len(user.votes) == 2 and user.votes[-2].winning_vote == True: user.sway_points += sway_points["streak_bonus"] # Return used sway points user.sway_points += int(user.votes[-1].sway_points * \ sway_points["refund"]) else: user.votes[-1].winning_vote = False # Update user in database user.put()
def add_vote_for_question(self, party, sway_points, state_abbreviation, question): # Add vote for question # @TODO: could optimize to O(1) by pre-populating states states = question.state_scores for state in states: if state.state_abbreviation == state_abbreviation: state.party_score_votes[party] += 1 state.party_score_sway[party] += sway_points question.put() return # State had no votes previously states.append( State( state_abbreviation = state_abbreviation, college_votes = self.get_college_votes_for_state_abbreviation( \ state_abbreviation), party_score_votes = [0, 0], party_score_sway = [0, 0] ) ) states[-1].party_score_votes[party] += 1 states[-1].party_score_sway[party] += sway_points # Increment question vote count question.vote_count += 1 question.put()
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
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"
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
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
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"])