def test_aggregator_errors_on_invalid_matchday(self): results = get_matches("England Premier League", "2014/2015") aggregator = ResultsAggregator(results) self.assertRaises(ValueError, aggregator.get_league_table_at_matchday, -1) self.assertRaises(ValueError, aggregator.get_league_table_at_matchday, 39)
def test_get_matches(self): actual_results = get_matches("England Premier League", "2014/2015") expected_results = [ MatchResult(date="2014-08-16 00:00:00", home_team="Arsenal", home_team_goals=2, away_team="Crystal Palace", away_team_goals=1), MatchResult(date="2014-08-16 00:00:00", home_team="Leicester", home_team_goals=2, away_team="Everton", away_team_goals=2), MatchResult(date="2014-08-16 00:00:00", home_team="Manchester United", home_team_goals=1, away_team="Swansea", away_team_goals=2), MatchResult(date="2014-08-16 00:00:00", home_team="Queens Park Rangers", home_team_goals=0, away_team="Hull", away_team_goals=1), MatchResult(date="2014-08-16 00:00:00", home_team="Stoke", home_team_goals=0, away_team="Aston Villa", away_team_goals=1), MatchResult(date="2014-08-16 00:00:00", home_team="West Bromwich Albion", home_team_goals=2, away_team="Sunderland", away_team_goals=2) ] self.assertEqual(actual_results, expected_results)
def test_aggregator_aggregates_midpoint_league_table_correctly(self): results = get_matches("England Premier League", "2014/2015") aggregator = ResultsAggregator(results) midpoint_league_table = aggregator.get_league_table_at_matchday(18) expected_league_leader = LeagueResult(team="Chelsea", games=18, points=45, goal_difference=27) self.assertEqual(midpoint_league_table[0], expected_league_leader) expected_league_trailer = LeagueResult(team="Leicester", games=18, points=10, goal_difference=-15) self.assertEqual(midpoint_league_table[-1], expected_league_trailer)
def test_aggregator_aggregates_final_league_table_correctly(self): results = get_matches("England Premier League", "2014/2015") aggregator = ResultsAggregator(results) final_league_table = aggregator.get_final_league_table() expected_league_leader = LeagueResult(team="Chelsea", games=38, points=87, goal_difference=41) self.assertEqual(final_league_table[0], expected_league_leader) expected_league_trailer = LeagueResult(team="Queens Park Rangers", games=38, points=30, goal_difference=-31) self.assertEqual(final_league_table[-1], expected_league_trailer)
def index(): leagues = models.get_all_leagues() seasons = json.dumps( {league: models.get_seasons_by_league(league) for league in leagues}) results = None selected_league = None selected_season = None selected_matchday = None prev_url = None next_url = None if request.args: selected_league = request.args['leagues_selector'] selected_season = request.args['seasons_selector'] results = models.ResultsAggregator( models.get_matches(selected_league, selected_season)) if 'selected_matchday' not in request.args: selected_matchday = results.NumberOfMatchdays else: selected_matchday = int(request.args['selected_matchday']) prev_url = url_for('homepage.index', leagues_selector=selected_league, seasons_selector=selected_season, selected_matchday=selected_matchday - 1) if selected_matchday > 1 else None next_url = url_for( 'homepage.index', leagues_selector=selected_league, seasons_selector=selected_season, selected_matchday=selected_matchday + 1) if selected_matchday < results.NumberOfMatchdays else None return render_template( 'index.html', leagues=leagues, seasons=seasons, selected_league=selected_league, selected_season=selected_season, results=results.get_league_table_at_matchday(selected_matchday) if results else None, prev_url=prev_url, next_url=next_url)
def test_results_aggregator_aggregates_one_round_of_fixtures_correctly( self): match_results = get_matches("England Premier League", "2014/2015") aggregator = ResultsAggregator(match_results) expected_results = [ LeagueResult(team="Arsenal", games=1, points=3, goal_difference=1), LeagueResult(team="Aston Villa", games=1, points=3, goal_difference=1), LeagueResult(team="Hull", games=1, points=3, goal_difference=1), LeagueResult(team="Swansea", games=1, points=3, goal_difference=1), LeagueResult(team="Everton", games=1, points=1, goal_difference=0), LeagueResult(team="Leicester", games=1, points=1, goal_difference=0), LeagueResult(team="Sunderland", games=1, points=1, goal_difference=0), LeagueResult(team="West Bromwich Albion", games=1, points=1, goal_difference=0), LeagueResult(team="Crystal Palace", games=1, points=0, goal_difference=-1), LeagueResult(team="Manchester United", games=1, points=0, goal_difference=-1), LeagueResult(team="Queens Park Rangers", games=1, points=0, goal_difference=-1), LeagueResult(team="Stoke", games=1, points=0, goal_difference=-1) ] self.assertEqual(aggregator.get_final_league_table(), expected_results)
def test_aggregator_returns_correct_number_of_matchdays(self): aggregator = ResultsAggregator( get_matches("England Premier League", "2014/2015")) self.assertEqual(aggregator.NumberOfMatchdays, 38)
def test_aggregator_returns_correct_number_of_matches(self): results = get_matches("England Premier League", "2014/2015") self.assertEqual(len(results), 380)