def test_get_mulitple_teams_by_standing_and_points(self): gameday = DBSetup().g72_qualify_finished() all_games = Gameinfo.objects.filter(gameday=gameday) for game in all_games: update_gameresults(game) gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_teams_by(standing='HF', points=3) == ['B2', 'B1']
def test_get_qualify_table(self): gameday = DBSetup().g62_qualify_finished() gmw = GamedayModelWrapper(gameday.pk) expected_qualify_table = get_df_from_json('ts_qualify_table') assert_frame_equal(gmw.get_qualify_table(), expected_qualify_table, check_dtype=False)
def get_standing(self, season=None, league=None): if season is None: season = Season.objects.last() all_gamedays = Gameday.objects.filter(season__name=season) all_standings = pd.DataFrame() for gameday in all_gamedays: try: gmw = GamedayModelWrapper(gameday.pk) all_standings = all_standings.append(gmw.get_final_table(), ignore_index=True) except Gameinfo.DoesNotExist: pass if all_standings.empty: return all_standings table_standings = self._calculate_standings(all_standings) if league is not None: season_league_team_mapping = pd.DataFrame( SeasonLeagueTeam.objects.filter( season__name__iexact=season, league__name__iexact=league).values(TEAM_NAME)) if season_league_team_mapping.empty: return season_league_team_mapping table_standings = pd.merge(table_standings, season_league_team_mapping, how='right') print( json.dumps(json.loads(table_standings.to_json(orient='table')), indent=2)) return table_standings
def test_get_team_by_points(self): gameday = DBSetup().g62_finalround(sf='beendet') gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_team_by_points(place=1, standing='HF', points=0) == 'B2' assert gmw.get_team_by_points(place=1, standing='HF', points=3) == 'A1' assert gmw.get_team_by_points(place=2, standing='HF', points=0) == 'A2' assert gmw.get_team_by_points(place=2, standing='HF', points=3) == 'B1'
def test_get_final_table_for_main_round(self): gameday = DBSetup().create_main_round_gameday(status='beendet', number_teams=4) expected_table = get_df_from_json('ts_final_table_4_teams') gmw = GamedayModelWrapper(gameday.pk) assert_frame_equal(gmw.get_final_table(), expected_table, check_dtype=False)
def test_get_all_second_places_for_prelim(self): gameday = DBSetup().g62_qualify_finished() all_prelim_games = Gameinfo.objects.filter(gameday=gameday) for game in all_prelim_games: update_gameresults(game) gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_team_by_qualify_for(place=2, index=0) == 'B2' assert gmw.get_team_by_qualify_for(place=2, index=1) == 'A2'
def test_get_games_to_whistle_for_all_teams(self): gameday = DBSetup().g62_status_empty() first_game = Gameinfo.objects.first() first_team = Team.objects.first() Gameinfo.objects.filter(id=first_game.pk).update(gameFinished='12:00') Gameinfo.objects.filter(id=first_game.pk + 1).update(officials=first_team.pk + 1) gmw = GamedayModelWrapper(gameday.pk) assert len(gmw.get_games_to_whistle('').index) == 10
def test_is_not_finished(self): gameday = DBSetup().g62_qualify_finished() Gameinfo.objects.filter(standing='Gruppe 1').update( status='some_state') Gameinfo.objects.filter(standing='HF').update(status='beendet') gmw = GamedayModelWrapper(gameday.pk) assert not gmw.is_finished('Vorrunde') assert gmw.is_finished('HF')
def test_get_final_table(self): gameday = DBSetup().g62_finalround(sf='beendet', p5='beendet', p3='beendet', p1='beendet') expected_final_table = get_df_from_json('ts_final_table_6_teams') gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_final_table().to_json() == expected_final_table.to_json( )
def test_update_9_teams_3_fields(self): gameday = DBSetup().create_empty_gameday() gameday.format = "9_3" gameday.save() group_A = DBSetup().create_teams('A', 3) group_B = DBSetup().create_teams('B', 3) group_C = DBSetup().create_teams('C', 3) groups = [group_A, group_B, group_C] DBSetup().create_playoff_placeholder_teams() sc = ScheduleCreator(gameday=Gameday.objects.get(pk=gameday.pk), schedule=Schedule(gameday.format, groups)) sc.create() qualify_games = Gameinfo.objects.filter(stage='Vorrunde') for game in qualify_games: update_gameresults(game) Gameinfo.objects.filter(stage='Vorrunde').update(status='beendet') su = ScheduleUpdate(gameday.pk, gameday.format) su.update() check_if_first_standing('PO').is_updated_as_expected_with( 'B2', 'C2', 'C3') check_if_first_standing('P7').is_updated_as_expected_with( 'A1', 'B1', 'B3') update_gameresults_by_standing_and_finish_game_for('PO') update_gameresults_and_finish_first_game_for_P7() su.update() check_if_first_standing('HF').is_updated_as_expected_with( 'C2', 'C3', 'B2') check_if_first_standing('P7').is_updated_as_expected_with( 'B1', 'C1', 'A2') check_if_first_standing('P5').is_updated_as_expected_with( 'A2', 'B2', 'B1') update_gameresults_by_standing_and_finish_game_for('HF') update_gameresults_and_finish_first_game_for_P7() su.update() check_if_first_standing('P7').is_updated_as_expected_with( 'C1', 'A1', 'C2') check_if_first_standing('P1').is_updated_as_expected_with( 'B3', 'C3', 'A1') check_if_first_standing('P3').is_updated_as_expected_with( 'A3', 'C2', 'C1') update_gameresults_by_standing_and_finish_game_for('P5') update_gameresults_and_finish_first_game_for_P7() update_gameresults_by_standing_and_finish_game_for('P3') update_gameresults_by_standing_and_finish_game_for('P1') gmw = GamedayModelWrapper(gameday.pk) DataFrameAssertion.expect( gmw.get_final_table()).to_equal_json('final_table_9_teams.json') DataFrameAssertion.expect( gmw.get_schedule()).to_equal_json('schedule_9_teams_3_fields.json')
def test_get_final_table_for_7_teams(self): gameday = DBSetup().g72_finished() expected_table = get_df_from_json('ts_final_table_7_teams') gmw = GamedayModelWrapper(gameday.pk) print( json.dumps(json.loads( gmw.get_final_table().to_json(orient='table')), indent=2)) assert_frame_equal(gmw.get_final_table(), expected_table, check_dtype=False)
def test_get_schedule(self): gameday = DBSetup().g62_qualify_finished() expected_schedule = get_df_from_json('schedule_g62_qualify_finished') schedule = GamedayModelWrapper(gameday.pk).get_schedule() columns = [ SCHEDULED, FIELD, HOME, POINTS_HOME, POINTS_AWAY, AWAY, OFFICIALS_NAME, STANDING, STAGE, STATUS ] schedule = schedule[columns] expected_schedule = expected_schedule[columns] # del expected_schedule['scheduled'] # del schedule['scheduled'] # del schedule['in_possession'] assert schedule.to_json() == expected_schedule.to_json()
def test_team_aggregation(self): gameday = DBSetup().g72_qualify_finished() all_games = Gameinfo.objects.filter(gameday=gameday) for game in all_games: update_gameresults(game) gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_team_aggregate_by( aggregate_standings=['Gruppe 1', 'Gruppe 2'], aggregate_place=2, place=1) == 'A3' assert gmw.get_team_aggregate_by( aggregate_standings=['Gruppe 1', 'Gruppe 2'], aggregate_place=2, place=2) == 'B2'
def get_all_schedules(self): season = Season.objects.last() all_gamedays = Gameday.objects.filter(season__name=season) all_schedules = pd.DataFrame() for gameday in all_gamedays: try: gmw = GamedayModelWrapper(gameday.pk, ['gameday__name', 'gameday__date']) all_schedules = all_schedules.append(gmw.get_schedule(), ignore_index=True) except Gameinfo.DoesNotExist: pass columns = [ GAMEDAY_NAME, GAMEDAY_ID, SCHEDULED, OFFICIALS_NAME, GAMEINFO_ID, HOME, AWAY, STANDING, STAGE ] return all_schedules[columns]
class GamedayService: @classmethod def create(cls, gameday_pk): try: return cls(gameday_pk) except Gameinfo.DoesNotExist: return EmptyGamedayService def __init__(self, pk): self.gmw = GamedayModelWrapper(pk) def get_schedule(self): schedule = self.gmw.get_schedule() columns = [SCHEDULED, FIELD, HOME, POINTS_HOME, POINTS_AWAY, AWAY, OFFICIALS_NAME, STANDING, STAGE, STATUS] schedule = schedule[columns] schedule[OFFICIALS_NAME] = schedule[OFFICIALS_NAME].apply('<i>{}</i>'.format) schedule[SCHEDULED] = pd.to_datetime(schedule[SCHEDULED], format='%H:%M:%S').dt.strftime('%H:%M') schedule = schedule.rename(columns=SCHEDULE_TABLE_HEADERS) return schedule def get_qualify_table(self): qualify_table = self.gmw.get_qualify_table() if qualify_table is '': return EmptyQualifyTable qualify_table = qualify_table[[STANDING, TEAM_NAME, POINTS, PF, PA, DIFF]] qualify_table = qualify_table.rename(columns=TABLE_HEADERS) return qualify_table def get_final_table(self): final_table = self.gmw.get_final_table() if final_table.empty: return final_table final_table = final_table[[DFFL, TEAM_NAME, POINTS, PF, PA, DIFF]] final_table = final_table.rename(columns=TABLE_HEADERS) return final_table def get_games_to_whistle(self, team): if team == '*': team = '' games_to_whistle = self.gmw.get_games_to_whistle(team) columns = [SCHEDULED, FIELD, OFFICIALS_NAME, STAGE, STANDING, HOME, POINTS_HOME, POINTS_AWAY, AWAY, STATUS, ID_HOME, ID_AWAY, 'id'] games_to_whistle = games_to_whistle[columns] games_to_whistle = games_to_whistle.rename(columns={OFFICIALS_NAME: OFFICIALS}) return games_to_whistle
def test_has_finalround(self): gameday_with_finalround = DBSetup().g62_finalround() gameday_with_main_round = DBSetup().create_main_round_gameday() gmw = GamedayModelWrapper(gameday_with_finalround.pk) assert gmw.has_finalround() gmw = GamedayModelWrapper(gameday_with_main_round.pk) assert not gmw.has_finalround()
def test_empty_get_qualify_table(self): gameday = DBSetup().create_main_round_gameday() gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_qualify_table() == ''
def test_no_gameinfos_for_gameday(self): gameday = DBSetup().create_empty_gameday() with self.assertRaises(Gameinfo.DoesNotExist): GamedayModelWrapper(gameday.pk)
def __init__(self, pk): self.gmw = GamedayModelWrapper(pk)
def test_get_qualify_team_by(self): gameday = DBSetup().g62_qualify_finished() gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_qualify_team_by(place=1, standing='Gruppe 1') == 'A1' assert gmw.get_qualify_team_by(place=3, standing='Gruppe 2') == 'B3'
def update(self): gmw = GamedayModelWrapper(self.gameday_id) for update_entry in self.data: if gmw.is_finished( update_entry['pre_finished']) and not gmw.is_finished( update_entry['name']): entry = UpdateEntry(update_entry) qs = Gameinfo.objects.filter(gameday_id=self.gameday_id, standing=entry.get_name()) game: UpdateGameEntry gi: Gameinfo for gi, game in zip(qs, entry): if game.home.stage: home = gmw.get_team_by_qualify_for( game.home.place, game.home.index) elif game.home.aggregate_standings: home = gmw.get_team_aggregate_by( game.home.aggregate_standings, game.home.aggregate_place, game.home.place) elif game.home.first_match: teams = gmw.get_teams_by(game.home.standing, game.home.points) for entry_to_match in game.home.first_match: if entry_to_match.aggregate_standings: potential_team = gmw.get_team_aggregate_by( entry_to_match.aggregate_standings, entry_to_match.aggregate_place, entry_to_match.place) else: potential_team = gmw.get_team_by( entry_to_match.place, entry_to_match.standing, entry_to_match.points) if potential_team in teams: home = potential_team break else: home = gmw.get_team_by(game.home.place, game.home.standing, game.home.points) if game.away.stage: away = gmw.get_team_by_qualify_for( game.away.place, game.away.index) elif game.away.aggregate_standings: away = gmw.get_team_aggregate_by( game.away.aggregate_standings, game.away.aggregate_place, game.away.place) elif game.away.first_match: teams = gmw.get_teams_by(game.away.standing, game.away.points) for entry_to_match in game.away.first_match: if entry_to_match.aggregate_standings: potential_team = gmw.get_team_aggregate_by( entry_to_match.aggregate_standings, entry_to_match.aggregate_place, entry_to_match.place) else: potential_team = gmw.get_team_by( entry_to_match.place, entry_to_match.standing, entry_to_match.points) if potential_team in teams: away = potential_team break else: away = gmw.get_team_by(game.away.place, game.away.standing, game.away.points) self._update_gameresult(gi, home, True) self._update_gameresult(gi, away, False) if gmw.is_finished(game.officials.pre_finished): if game.officials.stage: officialsTeamName = gmw.get_team_by_qualify_for( game.officials.place, game.officials.index) else: officialsTeamName = gmw.get_team_by( game.officials.place, game.officials.standing, game.officials.points) officials = Team.objects.get(name=officialsTeamName) if gi.officials != officials: gi.officials = officials gi.save()
def test_empty_get_final_table(self): gameday = DBSetup().g62_qualify_finished() gmw = GamedayModelWrapper(gameday.pk) assert gmw.get_final_table().empty