def testGetGames_gameTweet(self, mock_add_queue, mock_app_identity):
        """Verify the API handles the case where a game is returned."""
        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        user = self.CreateUser(2, 'bob')
        user.put()

        twt = web_test_base.WebTestBase.CreateTweet(
            1, ('bob', 2), created_at=datetime.utcnow())
        teams = [game_model.Team(twitter_id=2), game_model.Team(twitter_id=3)]
        game = game_model.Game.FromTweet(
            twt, teams, [0, 0], scores_messages.Division.OPEN,
            scores_messages.AgeBracket.NO_RESTRICTION,
            scores_messages.League.USAU)
        game.put()
        self.assertGameDbSize(1)

        # Request with all operators
        request = scores_messages.GamesRequest()
        request.league = scores_messages.League.USAU
        request.division = scores_messages.Division.OPEN
        request.age_bracket = scores_messages.AgeBracket.NO_RESTRICTION

        response = self.api.GetGames(request)
        self.assertEquals(1, len(response.games))
        self.assertEquals(2, len(response.games[0].teams))
        self.assertEquals(
            'bob', response.games[0].teams[0].twitter_account.screen_name)
        self.assertEquals(
            'bob',
            response.games[0].last_update_source.twitter_account.screen_name)

        # Request with no operators
        request = scores_messages.GamesRequest()
        response = self.api.GetGames(request)
        self.assertEquals(1, len(response.games))

        # Request with wrong division operator
        request = scores_messages.GamesRequest()
        request.division = scores_messages.Division.MIXED
        response = self.api.GetGames(request)
        self.assertEquals(0, len(response.games))

        # Request with wrong league operator
        request = scores_messages.GamesRequest()
        request.league = scores_messages.League.AUDL
        response = self.api.GetGames(request)
        self.assertEquals(0, len(response.games))

        # Request with wrong age bracket operator
        request = scores_messages.GamesRequest()
        request.age_bracket = scores_messages.AgeBracket.MASTERS
        response = self.api.GetGames(request)
        self.assertEquals(0, len(response.games))
    def testGetGames_gameTweetNoKnownTeams(self, mock_add_queue,
                                           mock_app_identity):
        """A game is returned with no known teams."""
        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        twt = web_test_base.WebTestBase.CreateTweet(
            1, ('bob', 2), created_at=datetime.utcnow())
        teams = [
            game_model.Team(twitter_id=2),
            game_model.Team(score_reporter_id='unknown')
        ]
        game = game_model.Game.FromTweet(
            twt, teams, [0, 0], scores_messages.Division.OPEN,
            scores_messages.AgeBracket.NO_RESTRICTION,
            scores_messages.League.USAU)
        game.put()
        self.assertGameDbSize(1)

        request = scores_messages.GamesRequest()
        request.league = scores_messages.League.USAU
        request.division = scores_messages.Division.OPEN
        request.age_bracket = scores_messages.AgeBracket.NO_RESTRICTION

        response = self.api.GetGames(request)
        self.assertEquals(1, len(response.games))
        self.assertEquals(2, len(response.games[0].teams))
    def testGetGames_noTweetstriggerCrawl(self, mock_add_queue,
                                          mock_app_identity):
        """Ensure crawl is triggered when there are no tweets."""
        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        self.assertEqual(scores_messages.GamesResponse(),
                         self.api.GetGames(scores_messages.GamesRequest()))

        calls = mock_add_queue.mock_calls
        self.assertEquals(1, len(calls))
    def testGetGames_noTriggerCrawl(self, mock_add_queue, mock_app_identity):
        """Ensure crawl is not triggered if the datebase is up-to-date."""
        # Add a tweet to the database that was recently crawled.
        twt = self.CreateTweet('2', ['bob', '5'])
        twt.put()

        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        self.assertEqual(scores_messages.GamesResponse(),
                         self.api.GetGames(scores_messages.GamesRequest()))

        calls = mock_add_queue.mock_calls
        self.assertEquals(0, len(calls))
    def testGetGames_triggerCrawl(self, mock_add_queue, mock_app_identity):
        """Ensure crawl is triggered if the database is stale."""
        # Add a tweet to the database that was crawled yesterday.
        yesterday_date = datetime.now() - timedelta(days=1)

        # Subtract the delta that's used in the API logic.
        yesterday_date = yesterday_date - timedelta(
            hours=scores_api.MAX_HOURS_CRAWL_LATENCY)
        twt = self.CreateTweet('2', ['bob', '5'], created_at=yesterday_date)
        twt.put()

        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        self.assertEqual(scores_messages.GamesResponse(),
                         self.api.GetGames(scores_messages.GamesRequest()))

        calls = mock_add_queue.mock_calls
        self.assertEquals(1, len(calls))
 def testSanityGetGames(self):
     """Ensure no exceptions are thrown on simple requests to GetGames."""
     self.assertEqual(scores_messages.GamesResponse(),
                      self.api.GetGames(scores_messages.GamesRequest()))
    def testGetGames_scoreReporterGame(self, mock_add_queue,
                                       mock_app_identity):
        """Verify the API handles the case where a SR game is returned."""
        mock_app_identity.get_default_version_hostname = mock.MagicMock()
        mock_app_identity.get_default_version_hostname.return_value = 'production host'

        user = self.CreateUser(2, 'bob')
        user.put()

        teams = [
            game_model.Team(score_reporter_id='a'),
            game_model.Team(score_reporter_id='b')
        ]
        info = score_reporter_crawler.GameInfo(
            'a', 'b', 'name', scores_messages.Division.WOMENS,
            scores_messages.AgeBracket.NO_RESTRICTION)
        info.home_team_link = 'c'
        info.away_team_link = 'd'
        team_tourney_map = {
            'c': 'e',
            'd': 'f',
        }
        game = game_model.Game.FromGameInfo(info, team_tourney_map)
        game.put()
        self.assertGameDbSize(1)

        team_info = game_model.FullTeamInfo(
            key=game_model.full_team_info_key('e'),
            id='e',
            name='name',
            age_bracket=scores_messages.AgeBracket.NO_RESTRICTION,
            division=scores_messages.Division.WOMENS,
            website='website',
            screen_name='twitter_screenname',
            facebook_url='facebook_url',
            image_link='image_link',
            coach='coach',
            asst_coach='asst_coach')
        team_info.put()

        # Request with all operators
        request = scores_messages.GamesRequest()
        request.league = scores_messages.League.USAU
        request.division = scores_messages.Division.WOMENS
        request.age_bracket = scores_messages.AgeBracket.NO_RESTRICTION

        response = self.api.GetGames(request)
        self.assertEquals(1, len(response.games))
        self.assertEquals(2, len(response.games[0].teams))

        account = scores_messages.ScoreReporterAccount(
            id='e',
            name='name',
            team_website='website',
            facebook_url='facebook_url',
            screen_name='twitter_screenname',
            profile_image_url_https='%s%s' %
            (scores_api.USAU_PREFIX, 'image_link'),
            coach='coach',
            asst_coach='asst_coach')
        self.assertEquals(account,
                          response.games[0].teams[0].score_reporter_account)
        account = scores_messages.ScoreReporterAccount(id='f')
        self.assertEquals(account,
                          response.games[0].teams[1].score_reporter_account)
 def testSanityMessages(self):
   """Just verify there are no syntax errors in the protocol definitions."""
   scores_messages.GamesRequest()
   scores_messages.GamesResponse()
   scores_messages.GameInfoRequest()
   scores_messages.GameInfoResponse()