示例#1
0
    def test_view_tasks_twitter_stream_listener_get_sway(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_no_votes.json', 
                        kind={'Question': Question,'State': State})

        twitter_stream_listener = TwitterStreamListener()

        test_text = "#SWAY20 #SWAY10"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO #SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO #SWAY20 #SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "#SWAY2 Testing"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 2

        test_text = "Testing #SWAY200"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 200
示例#2
0
    def test_view_tasks_twitter_stream_listener_get_state(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State})

        twitter_stream_listener = TwitterStreamListener()

        test_text = "#AL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == "AL"

        test_text = "#no I agree #AL"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == "AL"

        test_text = "#ALAL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "AL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "#AL #AK #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "#no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None
示例#3
0
    def test_view_tasks_twitter_stream_listener_add_vote_for_question(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State})

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 0

        twitter_stream_listener = TwitterStreamListener()
        twitter_stream_listener.add_vote_for_question(0, 30, "CA", current_question)

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 30
        assert current_question.state_scores[0].party_score_sway[1] == 0

        twitter_stream_listener.add_vote_for_question(0, 50, "CA", current_question)
        twitter_stream_listener.add_vote_for_question(1, 20, "CA", current_question)
        assert current_question.state_scores[0].party_score_votes[0] == 2
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 80
        assert current_question.state_scores[0].party_score_sway[1] == 20

        twitter_stream_listener.add_vote_for_question(1, 30, "WA", current_question)
        assert len(current_question.state_scores) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 0
        assert current_question.state_scores[1].party_score_votes[1] == 1
        assert current_question.state_scores[1].party_score_sway[0] == 0
        assert current_question.state_scores[1].party_score_sway[1] == 30
示例#4
0
    def test_view_tasks_twitter_stream_listener_get_state(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_no_votes.json', 
                        kind={'Question': Question,'State': State})

        twitter_stream_listener = TwitterStreamListener()

        test_text = "#AL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == "AL"

        test_text = "#no I agree #AL"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == "AL"

        test_text = "#ALAL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "AL #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "#AL #AK #no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None

        test_text = "#no I agree"
        result = twitter_stream_listener.get_state_from_string(test_text)
        assert result == None
示例#5
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"
示例#6
0
    def test_view_tasks_twitter_stream_listener_get_sway(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State})

        twitter_stream_listener = TwitterStreamListener()

        test_text = "#SWAY20 #SWAY10"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO #SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "NO #SWAY20 #SWAY MAN"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 0

        test_text = "#SWAY2 Testing"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 2

        test_text = "Testing #SWAY200"
        result = twitter_stream_listener.get_sway_from_string(test_text)
        assert result == 200
示例#7
0
    def test_view_tasks_twitter_post_status_unstarted(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_unstarted.json", kind={"Question": Question})

        # No last question so should always post something
        # Doesn't post to Twitter - we don't want to overload the API
        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=52560000")
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#8
0
    def test_view_tasks_twitter_post_status_complete(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_complete.json", kind={"Question": Question})

        # No more questions so should never post anything
        # Doesn't post to Twitter - we don't want to overload the API
        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1")
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#9
0
    def test_view_tasks_twitter_post_status(self):
        if os.getenv('TWITTER_TESTS') == "FALSE":
            return # Modify in runtests.sh

        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})
        current_question = Question.get_current_question()

        # Add user with vote so we can test sway points
        vote_current = Vote(
            question = current_question.key,
            replyid = "123",
            state_abbreviation = "CA",
            party = 1,
            sway_points = 20
        )
        user = User(
            userid = "jrgrafton",
            votes = [vote_current],
            # Used 20 sway points upon creation
            sway_points = 30
        )
        user.put()
        original_sway_points = user.sway_points # Store initial sway points

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=1&post_to_twitter=True')
        current_question = Question.get_current_question()
        ndb.get_context().clear_cache()
        assert current_question.twitterid != None
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True

        # Ensure user had sway points returned
        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"])

        # Ensure Twitter timeline has been updated
        twitter_status = twitter_api.get_last_tweet().text
        question_entity = Question.get_current_question()
        assert twitter_status == question_entity.question_text

        # Expect question not to be posted as time is not yet up
        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=52560000')
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#10
0
    def test_view_tasks_is_time_for_new_question_unstarted(self):
        load_fixture("tests/questions_unstarted.json", kind={"Question": Question})

        # 1 minute cadence should be true
        result = tasks.__dict__["__is_time_for_new_question"](1)
        assert result == True

        # Should always be false for 100 years interval!
        result = tasks.__dict__["__is_time_for_new_question"](60 * 24 * 365 * 100)
        assert result == True
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache()  # Prevent data from leaking between tests

        load_fixture('test_data/event_team_status.json',
                      kind={'EventDetails': EventDetails, 'Event': Event, 'Match': Match},
                      post_processor=self.event_key_adder)
        self.event = Event.get_by_id('2016nytr')
        self.assertIsNotNone(self.event)
示例#12
0
    def test_view_tasks_is_time_for_new_question_unstarted(self):
        load_fixture('tests/questions_unstarted.json', 
                        kind={'Question': Question})
        
        # 1 minute cadence should be true
        result = tasks.__dict__["__is_time_for_new_question"](1)
        assert result == True

        # Should always be false for 100 years interval!
        result = \
            tasks.__dict__["__is_time_for_new_question"](60*24*365*100)
        assert result == True
示例#13
0
    def test_model_question_unstarted(self):
        # Load fixtures
        load_fixture('tests/questions_unstarted.json', 
                        kind={'Question': Question})

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

        # Check get current question start time
        current_question_start_time = Question.get_current_question_start_time()
        assert current_question_start_time == None
示例#14
0
    def test_model_question_complete(self):
        # Load fixtures
        load_fixture('tests/questions_complete.json', 
                        kind={'Question': Question})

        # Check get next questions
        question_entity = Question.get_next_question()
        assert question_entity == None

        # Check get current question start  time
        current_question_start_time = Question.get_current_question_start_time()
        assert current_question_start_time == None
示例#15
0
    def test_view_tasks_twitter_post_status_unstarted(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_unstarted.json', 
                        kind={'Question': Question})

        # No last question so should always post something
        # Doesn't post to Twitter - we don't want to overload the API
        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=52560000')
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#16
0
    def test_view_tasks_twitter_post_status_complete(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_complete.json', 
                        kind={'Question': Question})

        # No more questions so should never post anything
        # Doesn't post to Twitter - we don't want to overload the API
        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=1')
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#17
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"])
示例#18
0
    def test_model_question_unstarted(self):
        # Load fixtures
        load_fixture('tests/questions_unstarted.json',
                     kind={'Question': Question})

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

        # Check get current question start time
        current_question_start_time = Question.get_current_question_start_time(
        )
        assert current_question_start_time == None
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_taskqueue_stub(root_path=".")
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache()  # Prevent data from leaking between tests

        load_fixture('test_data/2016cama_no_surrogate.json',
                     kind={'Event': Event, 'Match': Match},
                     post_processor=self.eventKeyAdder)
        self.event = Event.get_by_id('2016cama')
        self.assertIsNotNone(self.event)
示例#20
0
    def test_model_question_complete(self):
        # Load fixtures
        load_fixture('tests/questions_complete.json',
                     kind={'Question': Question})

        # Check get next questions
        question_entity = Question.get_next_question()
        assert question_entity == None

        # Check get current question start  time
        current_question_start_time = Question.get_current_question_start_time(
        )
        assert current_question_start_time == None
示例#21
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.loaded_data = load_fixture('tests/ancestors_and_dogs.json',
                                     {'Person': Person, 'Dog': Dog})
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.loaded_data = load_fixture('tests/persons_children_and_dogs.json',
                                     {'Person': Person, 'Dog': Dog})
示例#23
0
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_taskqueue_stub(root_path=".")
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache(
        )  # Prevent data from leaking between tests

        load_fixture('test_data/2016cama_no_surrogate.json',
                     kind={
                         'Event': Event,
                         'Match': Match
                     },
                     post_processor=self.eventKeyAdder)
        self.event = Event.get_by_id('2016cama')
        self.assertIsNotNone(self.event)
示例#24
0
    def test_view_tasks_twitter_post_status(self):
        if os.getenv("TWITTER_TESTS") == "FALSE":
            return  # Modify in runtests.sh

        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})
        current_question = Question.get_current_question()

        # Add user with vote so we can test sway points
        vote_current = Vote(
            question=current_question.key, replyid="123", state_abbreviation="CA", party=1, sway_points=20
        )
        user = User(
            userid="jrgrafton",
            votes=[vote_current],
            # Used 20 sway points upon creation
            sway_points=30,
        )
        user.put()
        original_sway_points = user.sway_points  # Store initial sway points

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1&post_to_twitter=True")
        current_question = Question.get_current_question()
        ndb.get_context().clear_cache()
        assert current_question.twitterid != None
        assert "Posted new status" in reponse.data
        assert status.is_success(reponse.status_code) == True

        # Ensure user had sway points returned
        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"])

        # Ensure Twitter timeline has been updated
        twitter_status = twitter_api.get_last_tweet().text
        question_entity = Question.get_current_question()
        assert twitter_status == question_entity.question_text

        # Expect question not to be posted as time is not yet up
        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=52560000")
        assert "New question not ready" in reponse.data
        assert status.is_success(reponse.status_code) == True
示例#25
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.loaded_data = load_fixture(
         os.path.join(
             os.path.dirname(__file__), 'persons.json'), Person)
示例#26
0
    def test_view_tasks_twitter_stream_listener_add_vote_for_question(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_no_votes.json', 
                        kind={'Question': Question,'State': State})
        
        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 0

        twitter_stream_listener = TwitterStreamListener()
        twitter_stream_listener.add_vote_for_question(0,
                                                      30,
                                                      "CA",
                                                      current_question)

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 30
        assert current_question.state_scores[0].party_score_sway[1] == 0

        twitter_stream_listener.add_vote_for_question(0,
                                                      50,
                                                      "CA",
                                                      current_question)
        twitter_stream_listener.add_vote_for_question(1,
                                                      20,
                                                      "CA",
                                                      current_question)
        assert current_question.state_scores[0].party_score_votes[0] == 2
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 80
        assert current_question.state_scores[0].party_score_sway[1] == 20

        twitter_stream_listener.add_vote_for_question(1,
                                                      30,
                                                      "WA",
                                                      current_question)
        assert len(current_question.state_scores) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 0
        assert current_question.state_scores[1].party_score_votes[1] == 1
        assert current_question.state_scores[1].party_score_sway[0] == 0
        assert current_question.state_scores[1].party_score_sway[1] == 30
示例#27
0
    def test_view_tasks_twitter_stream_listener_get_party(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_no_votes.json', 
                        kind={'Question': Question,'State': State})

        twitter_stream_listener = TwitterStreamListener()

        # party from string extraction
        current_question = Question.get_current_question()
        current_question_party = current_question.party

        test_text = "#NO #no I disagree"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == 1 - current_question_party # Inverse current party

        test_text = "I disagree #no"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == 1 - current_question_party # Inverse current party

        test_text = "#YeS I agree"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == current_question_party 

        test_text = "#YeSssir I agree"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "#nobody agree(s)"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "NO no I disagree"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "#Yes #Yes #no I agree"
        result = twitter_stream_listener. \
            get_party_from_string_using_question(test_text, current_question)
        assert result == None
示例#28
0
    def test_view_tasks_twitter_stream(self):
        if os.getenv('TWITTER_TESTS') == "FALSE":
            return # Modify in runtests.sh

        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})

        # Start new thread with Twitter Stream listener
        stream_thread = Thread(target=self._start_twitter_stream)
        stream_thread.daemon = True
        stream_thread.start()

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=1&post_to_twitter=True')
        current_question = Question.get_current_question()

        # Post reply
        twitter_status_id = twitter_api.get_last_tweet().id
        twitter_api.update_status("#yes for #WA #sway10", twitter_status_id)

        # Wait for stream to be updated
        time.sleep(2)

        # Test to see reply has been registered
        ndb.get_context().clear_cache()
        users = User.get_all()
        assert len(users) == 1
        users[0].sway_points = User.get_starting_sway_points() - 10
        users[0].votes[0].vote_text == "#yes for #WA"

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "WA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 10
        assert current_question.state_scores[0].party_score_sway[1] == 0
示例#29
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
示例#30
0
    def setUp(self):
        def process(p):
            p.processed = True

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.loaded_data = load_fixture('tests/persons.json',
                                        Person,
                                        post_processor=process)
示例#31
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
示例#32
0
    def test_view_tasks_twitter_stream(self):
        if os.getenv("TWITTER_TESTS") == "FALSE":
            return  # Modify in runtests.sh

        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})

        # Start new thread with Twitter Stream listener
        stream_thread = Thread(target=self._start_twitter_stream)
        stream_thread.daemon = True
        stream_thread.start()

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1&post_to_twitter=True")
        current_question = Question.get_current_question()

        # Post reply
        twitter_status_id = twitter_api.get_last_tweet().id
        twitter_api.update_status("#yes for #WA #sway10", twitter_status_id)

        # Wait for stream to be updated
        time.sleep(2)

        # Test to see reply has been registered
        ndb.get_context().clear_cache()
        users = User.get_all()
        assert len(users) == 1
        users[0].sway_points = User.get_starting_sway_points() - 10
        users[0].votes[0].vote_text == "#yes for #WA"

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "WA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 10
        assert current_question.state_scores[0].party_score_sway[1] == 0
示例#33
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"
    def setUp(self):

        def process(p):
            p.processed = True

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.loaded_data = load_fixture(
            'tests/persons_and_dogs.json',
            {'Person': Person, 'Dog': Dog},
            post_processor=process
        )
示例#35
0
    def test_view_tasks_twitter_stream_listener_get_party(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State})

        twitter_stream_listener = TwitterStreamListener()

        # party from string extraction
        current_question = Question.get_current_question()
        current_question_party = current_question.party

        test_text = "#NO #no I disagree"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == 1 - current_question_party  # Inverse current party

        test_text = "I disagree #no"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == 1 - current_question_party  # Inverse current party

        test_text = "#YeS I agree"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == current_question_party

        test_text = "#YeSssir I agree"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "#nobody agree(s)"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "NO no I disagree"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == None

        test_text = "#Yes #Yes #no I agree"
        result = twitter_stream_listener.get_party_from_string_using_question(test_text, current_question)
        assert result == None
示例#36
0
    def setUp(self):

        def process(p):
            p.processed = True

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.loaded_data = load_fixture(
            os.path.join(
                os.path.dirname(__file__), 'persons.json'),
            Person,
            post_processor=process
        )
    def setUp(self):

        self.counter = 0

        def counted_process(p):
            p.processed = True
            self.counter += 1

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.loaded_data = load_fixture(
            'tests/persons.json',
            Person,
            post_processor=counted_process
        )
示例#38
0
    def test_view_tasks_twitter_stream_listener_process_retweet_from_screename(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})
        twitter_stream_listener = TwitterStreamListener()
        current_question = Question.get_current_question()
        
        # Invalid retweet id
        twitter_stream_listener.\
            process_retweet_from_screename(current_question,
                                            -1,
                                           "jrgrafton",
                                           "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg")
        users = User.get_all()
        assert len(users) == 0

        # Valid retweet id - new user
        twitter_stream_listener.\
            process_retweet_from_screename(current_question,
                                           current_question.twitterid,
                                           "jrgrafton",
                                           "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg")
        users = User.get_all()
        assert len(users) == 1
        assert users[0].sway_points == User.get_starting_sway_points() + \
                                       sway_points_backend["rewteet_poll"]
        assert "https" in users[0].profile_image_url

        # Ensure no extra sway given for retweet
        twitter_stream_listener.\
            process_retweet_from_screename(current_question,
                                           current_question.twitterid,
                                           "jrgrafton",
                                           "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg")
        users = User.get_all()
        assert len(users) == 1
        assert users[0].sway_points == User.get_starting_sway_points() + \
                                       sway_points_backend["rewteet_poll"]

        # Test existing user reweet
        user = User(
            userid = "jrgrafton2",
            sway_points = 30
        )
        user.put()

        # Valid retweet id - new user
        twitter_stream_listener.\
            process_retweet_from_screename(current_question,
                                           current_question.twitterid,
                                           "jrgrafton2",
                                           "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg")
        users = User.get_all()
        assert len(users) == 2
        assert users[1].sway_points == 30 + sway_points_backend["rewteet_poll"]
示例#39
0
    def test_view_tasks_twitter_stream_listener_process_retweet_from_screename(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})
        twitter_stream_listener = TwitterStreamListener()
        current_question = Question.get_current_question()

        # Invalid retweet id
        twitter_stream_listener.process_retweet_from_screename(
            current_question,
            -1,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg",
        )
        users = User.get_all()
        assert len(users) == 0

        # Valid retweet id - new user
        twitter_stream_listener.process_retweet_from_screename(
            current_question,
            current_question.twitterid,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg",
        )
        users = User.get_all()
        assert len(users) == 1
        assert users[0].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"]
        assert "https" in users[0].profile_image_url

        # Ensure no extra sway given for retweet
        twitter_stream_listener.process_retweet_from_screename(
            current_question,
            current_question.twitterid,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg",
        )
        users = User.get_all()
        assert len(users) == 1
        assert users[0].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"]

        # Test existing user reweet
        user = User(userid="jrgrafton2", sway_points=30)
        user.put()

        # Valid retweet id - new user
        twitter_stream_listener.process_retweet_from_screename(
            current_question,
            current_question.twitterid,
            "jrgrafton2",
            "https://pbs.twimg.com// \
                                            profile_images//440698495// \
                                            9929_128442162134_504357134_ \
                                            2556732_5649977_n_bigger.jpg",
        )
        users = User.get_all()
        assert len(users) == 2
        assert users[1].sway_points == 30 + sway_points_backend["rewteet_poll"]
示例#40
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.loaded_data = load_fixture('tests/persons.json', Person)
示例#41
0
    def test_view_tasks_twitter_stream_listener_add_vote_for_screenname(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})
        twitter_stream_listener = TwitterStreamListener()
        current_question = Question.get_current_question()

        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote text #yes",
            "123",
            None,  # Invalid state
            1,
            0,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == False

        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote text #CA",
            "123",
            "CA",
            None,  # Invalid party
            0,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == False

        # First valid user
        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote text #CA #yes",
            "123",
            "CA",
            0,
            100,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == True
        user = User.query_by_userid("jrgrafton").get()
        assert user != None
        assert len(user.votes) == 1
        assert user.votes[0].question.get().key == current_question.key
        assert user.votes[0].vote_text == "Vote text #CA #yes"
        assert user.votes[0].replyid == "123"
        assert user.votes[0].state_abbreviation == "CA"
        assert user.votes[0].party == 0
        assert user.votes[0].sway_points == 100

        # Second vote for user on same question
        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote text #WA #no",
            "123",
            "WA",
            1,
            0,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == False

        # Second valid user
        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote text for user 2 #WA #no",
            "123",
            "WA",
            1,
            50,
            "jrgrafton_test",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == True
        users = User.get_all()
        assert len(users) == 2
        user = users[1]
        assert len(user.votes) == 1
        assert user.votes[0].question.get().key == current_question.key
        assert user.votes[0].vote_text == "Vote text for user 2 #WA #no"
        assert user.votes[0].replyid == "123"
        assert user.votes[0].state_abbreviation == "WA"
        assert user.votes[0].party == 1
        assert user.votes[0].sway_points == 50

        # Go to new question and test valid votes
        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1")
        current_question = Question.get_current_question()

        # Second vote for new question should work
        result = twitter_stream_listener.add_vote_for_screenname(
            current_question,
            "Vote second question text #WA #no",
            "124",
            "WA",
            1,
            10,
            "jrgrafton",
            "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg",
        )
        assert result == True
        user = User.query_by_userid("jrgrafton").get()
        assert user != None
        assert len(user.votes) == 2
        assert user.votes[1].vote_text == "Vote second question text #WA #no"
        assert user.votes[1].question.get().key == current_question.key
        assert user.votes[1].replyid == "124"
        assert user.votes[1].state_abbreviation == "WA"
        assert user.votes[1].party == 1
        assert user.votes[1].sway_points == 10
示例#42
0
    def test_view_tasks_twitter_stream_listener_on_data(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions_no_votes.json', 
                        kind={'Question': Question,'State': State})

        twitter_stream = open(os.path.join(os.path.dirname(__file__),
                                          'twitter_stream.json'))
        twitter_stream = json.load(twitter_stream)
        twitter_stream_listener = TwitterStreamListener()

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_old"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_old"]))
        users = User.get_all()
        assert len(users) == 0
        
        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_current_invalid"]))
        users = User.get_all()
        assert len(users) == 0
        
        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_post"]))
        users = User.get_all()
        assert len(users) == 0
        
        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_mention"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_favourite"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_favourite_reply"]))
        users = User.get_all()
        assert len(users) == 0

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_current_valid_user1"]))
        users = User.get_all()
        assert len(users) == 1
        assert "https" in users[0].profile_image_url
        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 0
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 0
        assert current_question.state_scores[0].party_score_sway[1] == \
            User.get_starting_sway_points()
        assert users[0].sway_points == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_current_valid_user1"]))
        users = User.get_all()
        assert len(users) == 1
        assert len(users[0].votes) == 1
        assert users[0].sway_points == 0

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 0
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 0
        assert current_question.state_scores[0].party_score_sway[1] == \
            User.get_starting_sway_points()
        assert users[0].sway_points == 0

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_current_valid_user2"]))
        users = User.get_all()
        assert len(users) == 2
        assert "https" in users[1].profile_image_url
        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 1
        assert current_question.state_scores[1].party_score_votes[1] == 0
        assert current_question.state_scores[1].party_score_sway[0] == 20
        assert current_question.state_scores[1].party_score_sway[1] == 0
        assert users[1].sway_points == User.get_starting_sway_points() - 20

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_reply_current_valid_user2"]))
        users = User.get_all()
        assert len(users) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 1
        assert current_question.state_scores[1].party_score_votes[1] == 0
        assert current_question.state_scores[1].party_score_sway[0] == 20
        assert current_question.state_scores[1].party_score_sway[1] == 0
        assert users[1].sway_points == User.get_starting_sway_points() - 20

        twitter_stream_listener.on_data(
            json.dumps(twitter_stream["twitter_retweet"]))
        users = User.get_all()
        assert len(users) == 3
        assert users[2].sway_points == User.get_starting_sway_points() + \
                                       sway_points_backend["rewteet_poll"]
        assert "https" in users[2].profile_image_url
示例#43
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"])
示例#44
0
    def test_model_user(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json',
                     kind={
                         'Question': Question,
                         'State': State
                     })

        current_question_entity = Question.get_current_question()
        User.add_user_vote(
            None, "jrgrafton_test",
            "https://pbs.twimg.com//profile_images//440698495// \
                          9929_128442162134_504357134_2556732_5649977_n_\
                          bigger.jpg",
            Vote(question=current_question_entity.key,
                 replyid="692368266292023296",
                 state_abbreviation="CA",
                 party=0,
                 sway_points=40))

        # Ensure user was created
        user_entity = User.query_by_userid("jrgrafton_test").fetch()
        assert len(user_entity) == 1
        assert user_entity[0].sway_points == User.get_starting_sway_points() - \
                                             40
        assert user_entity[0].userid == "jrgrafton_test"
        assert "https" in user_entity[0].profile_image_url
        assert user_entity[0].votes[0].question == current_question_entity.key
        assert user_entity[0].votes[0].replyid == "692368266292023296"
        assert user_entity[0].votes[0].state_abbreviation == "CA"
        assert user_entity[0].votes[0].party == 0

        # Ensure a reply to a different question is tallied
        next_question_entity = Question.get_current_question()
        User.add_user_vote(
            user_entity[0], "jrgrafton_test",
            "https://pbs.twimg.com//profile_images//440698495// \
                          9929_128442162134_504357134_2556732_5649977_n_\
                          bigger.jpg",
            Vote(question=next_question_entity.key,
                 replyid="692368266292023297",
                 state_abbreviation="WA",
                 party=1,
                 sway_points=10))

        # Ensure new vote was collated under existing user
        user_entity = User.query_by_userid("jrgrafton_test").fetch()
        assert len(user_entity) == 1
        assert len(user_entity[0].votes) == 2
        assert user_entity[0].sway_points == User.get_starting_sway_points() - \
                                             (40 + 10)
        # Verify integrity of new vote
        assert user_entity[0].votes[1].question == next_question_entity.key
        assert user_entity[0].votes[1].replyid == "692368266292023297"
        assert user_entity[0].votes[1].state_abbreviation == "WA"
        assert user_entity[0].votes[1].party == 1

        # Verify integrity of old vote
        assert user_entity[0].userid == "jrgrafton_test"
        assert "https" in user_entity[0].profile_image_url
        assert user_entity[0].votes[0].question == current_question_entity.key
        assert user_entity[0].votes[0].replyid == "692368266292023296"
        assert user_entity[0].votes[0].state_abbreviation == "CA"
        assert user_entity[0].votes[0].party == 0
示例#45
0
    def test_model_user(self):
        # Load fixtures
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})

        current_question_entity = Question.get_current_question()
        User.add_user_vote(None, 
                          "jrgrafton_test",
                          "https://pbs.twimg.com//profile_images//440698495// \
                          9929_128442162134_504357134_2556732_5649977_n_\
                          bigger.jpg",
                          Vote(
                            question = current_question_entity.key,
                            replyid = "692368266292023296",
                            state_abbreviation = "CA",
                            party = 0,
                            sway_points = 40
                          ))

        # Ensure user was created
        user_entity = User.query_by_userid("jrgrafton_test").fetch()
        assert len(user_entity) == 1
        assert user_entity[0].sway_points == User.get_starting_sway_points() - \
                                             40
        assert user_entity[0].userid == "jrgrafton_test"
        assert "https" in user_entity[0].profile_image_url
        assert user_entity[0].votes[0].question == current_question_entity.key
        assert user_entity[0].votes[0].replyid == "692368266292023296"
        assert user_entity[0].votes[0].state_abbreviation == "CA"
        assert user_entity[0].votes[0].party == 0

        # Ensure a reply to a different question is tallied
        next_question_entity = Question.get_current_question()
        User.add_user_vote(user_entity[0],
                          "jrgrafton_test",
                          "https://pbs.twimg.com//profile_images//440698495// \
                          9929_128442162134_504357134_2556732_5649977_n_\
                          bigger.jpg",
                          Vote(
                            question = next_question_entity.key,
                            replyid = "692368266292023297",
                            state_abbreviation = "WA",
                            party = 1,
                            sway_points = 10
                          ))

        # Ensure new vote was collated under existing user
        user_entity = User.query_by_userid("jrgrafton_test").fetch()
        assert len(user_entity) == 1
        assert len(user_entity[0].votes) == 2
        assert user_entity[0].sway_points == User.get_starting_sway_points() - \
                                             (40 + 10)
        # Verify integrity of new vote
        assert user_entity[0].votes[1].question == next_question_entity.key
        assert user_entity[0].votes[1].replyid == "692368266292023297"
        assert user_entity[0].votes[1].state_abbreviation == "WA"
        assert user_entity[0].votes[1].party == 1

        # Verify integrity of old vote
        assert user_entity[0].userid == "jrgrafton_test"
        assert "https" in user_entity[0].profile_image_url
        assert user_entity[0].votes[0].question == current_question_entity.key
        assert user_entity[0].votes[0].replyid == "692368266292023296"
        assert user_entity[0].votes[0].state_abbreviation == "CA"
        assert user_entity[0].votes[0].party == 0
示例#46
0
    def test_view_tasks_twitter_stream_listener_add_vote_for_screenname(self):
        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})
        twitter_stream_listener = TwitterStreamListener()
        current_question = Question.get_current_question()
        
        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote text #yes",
                                    "123",
                                    None, # Invalid state
                                    1,
                                    0,
                                    "jrgrafton",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == False

        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote text #CA",
                                    "123",
                                    "CA",
                                    None, # Invalid party
                                    0,
                                    "jrgrafton",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == False

        # First valid user
        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote text #CA #yes",
                                    "123",
                                    "CA",
                                    0,
                                    100,
                                    "jrgrafton",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == True
        user = User.query_by_userid("jrgrafton").get()
        assert user != None
        assert len(user.votes) == 1
        assert user.votes[0].question.get().key == current_question.key
        assert user.votes[0].vote_text == "Vote text #CA #yes"
        assert user.votes[0].replyid == "123"
        assert user.votes[0].state_abbreviation == "CA"
        assert user.votes[0].party == 0
        assert user.votes[0].sway_points == 100

        # Second vote for user on same question
        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote text #WA #no",
                                    "123",
                                    "WA",
                                    1,
                                    0,
                                    "jrgrafton",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == False

        # Second valid user
        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote text for user 2 #WA #no",
                                    "123",
                                    "WA",
                                    1,
                                    50,
                                    "jrgrafton_test",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == True
        users = User.get_all()
        assert len(users) == 2
        user = users[1]
        assert len(user.votes) == 1
        assert user.votes[0].question.get().key == current_question.key
        assert user.votes[0].vote_text == "Vote text for user 2 #WA #no"
        assert user.votes[0].replyid == "123"
        assert user.votes[0].state_abbreviation == "WA"
        assert user.votes[0].party == 1
        assert user.votes[0].sway_points == 50

        # Go to new question and test valid votes
        reponse = self.app.get('/tasks/twitter_post_status' \
                               '?question_cadence_minutes=1')
        current_question = Question.get_current_question()

        # Second vote for new question should work
        result = twitter_stream_listener.\
            add_vote_for_screenname(current_question,
                                    "Vote second question text #WA #no",
                                    "124",
                                    "WA",
                                    1,
                                    10,
                                    "jrgrafton",
                                    "https://pbs.twimg.com// \
                                    profile_images//440698495// \
                                    9929_128442162134_504357134_ \
                                    2556732_5649977_n_bigger.jpg")
        assert result == True
        user = User.query_by_userid("jrgrafton").get()
        assert user != None
        assert len(user.votes) == 2
        assert user.votes[1].vote_text == "Vote second question text #WA #no"
        assert user.votes[1].question.get().key == current_question.key
        assert user.votes[1].replyid == "124"
        assert user.votes[1].state_abbreviation == "WA"
        assert user.votes[1].party == 1
        assert user.votes[1].sway_points == 10
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.loaded_data = load_fixture('tests/hard_coded_id.json', Person)
示例#48
0
    def __init__(self, has_event_details=True, batch_advance=False):
        self._step = 0
        self._substep = 0
        # whether to update rankings and alliance selections
        self._has_event_details = has_event_details
        # whether to update next playoff level all at once, or as winners are determined
        self._batch_advance = batch_advance

        # Load and save complete data
        load_fixture('test_data/fixtures/2016nytr_event_team_status.json',
              kind={'EventDetails': EventDetails, 'Event': Event, 'Match': Match},
              post_processor=self._event_key_adder)
        event = Event.get_by_id('2016nytr')

        # Add 3rd matches that never got played
        unplayed_matches = [
            Match(
                id='2016nytr_qf1m3',
                year=2016,
                event=event.key,
                comp_level='qf',
                set_number=1,
                match_number=3,
                alliances_json=json.dumps({
                    'red': {
                        'teams': ['frc3990', 'frc359', 'frc4508'],
                        'score': -1,
                    },
                    'blue': {
                        'teams': ['frc3044', 'frc4930', 'frc4481'],
                        'score': -1,
                    }
                }),
                time=datetime.datetime(2016, 3, 19, 18, 34),
            ),
            Match(
                id='2016nytr_qf3m3',
                year=2016,
                event=event.key,
                comp_level='qf',
                set_number=3,
                match_number=3,
                alliances_json=json.dumps({
                    'red': {
                        'teams': ['frc20', 'frc5254', 'frc229'],
                        'score': -1,
                    },
                    'blue': {
                        'teams': ['frc3003', 'frc358', 'frc527'],
                        'score': -1,
                    }
                }),
                time=datetime.datetime(2016, 3, 19, 18, 48),
            ),
            Match(
                id='2016nytr_sf1m3',
                year=2016,
                event=event.key,
                comp_level='sf',
                set_number=1,
                match_number=3,
                alliances_json=json.dumps({
                    'red': {
                        'teams': ['frc3990', 'frc359', 'frc4508'],
                        'score': -1,
                    },
                    'blue': {
                        'teams': ['frc5240', 'frc3419', 'frc663'],
                        'score': -1,
                    }
                }),
                time=datetime.datetime(2016, 3, 19, 19, 42),
            )
        ]

        self._event_details = event.details
        self._alliance_selections_without_backup = copy.deepcopy(event.details.alliance_selections)
        self._alliance_selections_without_backup[1]['backup'] = None
        self._played_matches = MatchHelper.organizeMatches(event.matches)
        self._all_matches = MatchHelper.organizeMatches(event.matches + unplayed_matches)

        # Delete data
        event.details.key.delete()
        ndb.delete_multi([match.key for match in event.matches])
        ndb.get_context().clear_cache()

        # Used to keep track of non-batch advancement
        self._advancement_alliances = defaultdict(dict)
示例#49
0
 def test_api_question(self):
     load_fixture('tests/states.json', kind={'State': State})
     load_fixture('tests/questions.json', 
                     kind={'Question': Question,'State': State})
     response = self.app.get('/api/questions')
示例#50
0
    def test_view_tasks_twitter_stream_listener_on_data(self):
        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions_no_votes.json", kind={"Question": Question, "State": State})

        twitter_stream = open(os.path.join(os.path.dirname(__file__), "twitter_stream.json"))
        twitter_stream = json.load(twitter_stream)
        twitter_stream_listener = TwitterStreamListener()

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_old"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_old"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_invalid"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_post"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_mention"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_favourite"]))
        users = User.get_all()
        assert len(users) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_favourite_reply"]))
        users = User.get_all()
        assert len(users) == 0

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user1"]))
        users = User.get_all()
        assert len(users) == 1
        assert "https" in users[0].profile_image_url
        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 0
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 0
        assert current_question.state_scores[0].party_score_sway[1] == User.get_starting_sway_points()
        assert users[0].sway_points == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user1"]))
        users = User.get_all()
        assert len(users) == 1
        assert len(users[0].votes) == 1
        assert users[0].sway_points == 0

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "CA"
        assert current_question.state_scores[0].party_score_votes[0] == 0
        assert current_question.state_scores[0].party_score_votes[1] == 1
        assert current_question.state_scores[0].party_score_sway[0] == 0
        assert current_question.state_scores[0].party_score_sway[1] == User.get_starting_sway_points()
        assert users[0].sway_points == 0

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user2"]))
        users = User.get_all()
        assert len(users) == 2
        assert "https" in users[1].profile_image_url
        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 1
        assert current_question.state_scores[1].party_score_votes[1] == 0
        assert current_question.state_scores[1].party_score_sway[0] == 20
        assert current_question.state_scores[1].party_score_sway[1] == 0
        assert users[1].sway_points == User.get_starting_sway_points() - 20

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_reply_current_valid_user2"]))
        users = User.get_all()
        assert len(users) == 2
        assert current_question.state_scores[1].state_abbreviation == "WA"
        assert current_question.state_scores[1].party_score_votes[0] == 1
        assert current_question.state_scores[1].party_score_votes[1] == 0
        assert current_question.state_scores[1].party_score_sway[0] == 20
        assert current_question.state_scores[1].party_score_sway[1] == 0
        assert users[1].sway_points == User.get_starting_sway_points() - 20

        twitter_stream_listener.on_data(json.dumps(twitter_stream["twitter_retweet"]))
        users = User.get_all()
        assert len(users) == 3
        assert users[2].sway_points == User.get_starting_sway_points() + sway_points_backend["rewteet_poll"]
        assert "https" in users[2].profile_image_url