def testParseTourneyScores_updateDate(self, mock_add_queue): # Page with two teams, one of which has been added to the DB. self.SetHtmlResponse(FAKE_TOURNEY_SCORES_PAGE) params = { 'url_suffix': 'schedule/Men/College-Men/', 'name': 'my_tourney', 'division': 'OPEN', 'age_bracket': 'COLLEGE' } # Both teams, the tournament, and the game have already been added to the database. key = game_model.tourney_key_full(params['name']) now = datetime.utcnow() tourney = game_model.Tournament( last_modified_at=datetime(2016, 5, 25, 0, 0), key=key, has_started=False, start_date=datetime(2016, 5, 31, 0, 0), end_date=datetime(2016, 5, 31, 0, 0), sub_tournaments=[ game_model.SubTournament( division=scores_messages.Division.OPEN, age_bracket=scores_messages.AgeBracket.COLLEGE) ], url='%s%s' % (score_reporter_handler.USAU_URL_PREFIX, params.get('name', '')), id_str=params.get('name', ''), name='my tourney') tourney.put() game_model.TeamIdLookup(score_reporter_id='123', score_reporter_tourney_id=['8%3d']).put() game_model.TeamIdLookup(score_reporter_id='456', score_reporter_tourney_id=['g%3d']).put() game_info = score_reporter_crawler.GameInfo( '71984', 'my_tourney', 'my_tourney', scores_messages.Division.OPEN, scores_messages.AgeBracket.COLLEGE) game_info.status = 'Unknown' game = game_model.Game.FromGameInfo(game_info, {}) self.assertEquals(scores_messages.GameStatus.UNKNOWN, game.game_status) game.put() response = self.testapp.get('/tasks/sr/crawl_tournament', params=params) self.assertEqual(200, response.status_int) calls = mock_add_queue.mock_calls self.assertEquals(0, len(calls)) db_game = game_model.game_key(game).get() self.assertEquals(scores_messages.GameStatus.FINAL, db_game.game_status) db_tourney = key.get() # Tournament details should have been updated. self.assertEquals(True, db_tourney.has_started) self.assertTrue(db_tourney.last_modified_at >= now)
def testParseTeamInfo_teamMapExistsWithoutTourney(self): """TeamIdLookup exists, but doesn't contain the crawled tourney.""" id = 'njcj4s6Ct8EmLJyC98tkMEP3YQC5QiKs33MnNEu9jp0%3d' id_map = game_model.TeamIdLookup(score_reporter_id=id, score_reporter_tourney_id=[]) id_map.put() self._runParseTeamTest()
def testParseTeamInfo_teamMapExists(self): """If TeamIdLookup exists, nothing is changed.""" id = 'njcj4s6Ct8EmLJyC98tkMEP3YQC5QiKs33MnNEu9jp0%3d' tourney_id = 'g%3d' id_map = game_model.TeamIdLookup( score_reporter_id=id, score_reporter_tourney_id=[tourney_id]) id_map.put() self._runParseTeamTest()
def testParseTourneyScores_newTourneyBothTeamsKnown(self, mock_add_queue): # Page with two teams, both of which have been added to the DB. self.SetHtmlResponse(FAKE_TOURNEY_SCORES_PAGE) params = { 'url_suffix': 'schedule/Men/College-Men/', 'name': 'my-tourney', 'division': 'OPEN', 'age_bracket': 'COLLEGE' } # One team has already been added to the database, but one is new. game_model.TeamIdLookup(score_reporter_id='123', score_reporter_tourney_id=['8%3d']).put() game_model.Team(twitter_id=5, score_reporter_id='123').put() game_model.TeamIdLookup(score_reporter_id='456', score_reporter_tourney_id=['g%3d']).put() response = self.testapp.get('/tasks/sr/crawl_tournament', params=params) self.assertEqual(200, response.status_int) calls = mock_add_queue.mock_calls self.assertEquals(0, len(calls)) full_url = '%s%s' % (score_reporter_crawler.EVENT_PREFIX, 'my_tourney/schedule/Men/College-Men/') game_query = game_model.Game.query() games = game_query.fetch(1000) self.assertEqual(1, len(games)) self.assertEqual(scores_messages.Division.OPEN, games[0].division) self.assertEqual(scores_messages.AgeBracket.COLLEGE, games[0].age_bracket) self.assertEqual('71984', games[0].id_str) self.assertEqual('my-tourney', games[0].tournament_id) self.assertEqual('my tourney', games[0].tournament_name) self.assertEqual([15, 13], games[0].scores) self.assertEqual('456', games[0].teams[0].score_reporter_id) self.assertEqual('123', games[0].teams[1].score_reporter_id) self.assertEqual(None, games[0].teams[0].twitter_id) self.assertEqual(5, games[0].teams[1].twitter_id)
def testParseTourneyScores_noTeamUrls(self, mock_add_queue): # Page with two teams, both of which have been added to the DB. self.SetHtmlResponse(FAKE_TOURNEY_NO_TEAM_URLS) params = { 'url_suffix': 'schedule/Men/College-Men/', 'name': 'my_tourney', 'division': 'OPEN', 'age_bracket': 'COLLEGE' } # One team has already been added to the database, but one is new. game_model.TeamIdLookup(score_reporter_id='123', score_reporter_tourney_id=['8%3d']).put() game_model.Team(twitter_id=5, score_reporter_id='123').put() game_model.TeamIdLookup(score_reporter_id='456', score_reporter_tourney_id=['g%3d']).put() response = self.testapp.get('/tasks/sr/crawl_tournament', params=params) self.assertEqual(200, response.status_int) calls = mock_add_queue.mock_calls self.assertEquals(0, len(calls)) game_query = game_model.Game.query() games = game_query.fetch(1000) self.assertEqual(0, len(games))
def _PossiblyAddTeamLookup(self, team_id, tourney_id): """Add the mapping between tourney ID and full team ID if needed. Args: team_id: ID for team page with all games. tourney_id: ID for tournament-specific team page. """ query = game_model.TeamIdLookup.query( game_model.TeamIdLookup.score_reporter_id == team_id) teams = query.fetch(1) if not teams: id_map = game_model.TeamIdLookup( score_reporter_id=team_id, score_reporter_tourney_id=[tourney_id]) id_map.put() return # If the team entry exists, make sure this tourney is associated with it. if tourney_id in teams[0].score_reporter_tourney_id: return teams[0].score_reporter_tourney_id.append(tourney_id) teams[0].put()
def testParseTourneyScores_newTourneyOneTeamKnown(self, mock_add_queue): # Page with two teams, one of which has been added to the DB. self.SetHtmlResponse(FAKE_TOURNEY_SCORES_PAGE) params = { 'url_suffix': 'schedule/Men/College-Men/', 'name': 'my_tourney', 'division': 'OPEN', 'age_bracket': 'COLLEGE' } # One team has already been added to the database, but one is new. game_model.TeamIdLookup(score_reporter_id='123', score_reporter_tourney_id=['8%3d']).put() response = self.testapp.get('/tasks/sr/crawl_tournament', params=params) self.assertEqual(200, response.status_int) calls = mock_add_queue.mock_calls # Team page should be crawled for the unknown team. self.assertEquals(1, len(calls)) self.assertEquals( calls[0], mock.call(url='/tasks/sr/crawl_team', method='GET', params={ 'id': 'g%3d', 'division': 'OPEN', 'age_bracket': 'COLLEGE' }, queue_name='score-reporter')) full_url = '%s%s' % (score_reporter_crawler.EVENT_PREFIX, 'my_tourney/schedule/Men/College-Men/') game_query = game_model.Game.query() games = game_query.fetch(1000) self.assertEqual(0, len(games))