def test_mlb_boxscore_string_representation(self): expected = ('Boxscore for San Francisco Giants at ' 'Los Angeles Angels (Monday, August 17, 2020)') boxscore = Boxscore(BOXSCORE) assert boxscore.__repr__() == expected
def test_mlb_boxscore_string_representation(self): expected = ('Boxscore for Detroit Tigers at Boston Red Sox (Thursday, ' 'June 7, 2018)') boxscore = Boxscore(BOXSCORE) assert boxscore.__repr__() == expected
def test_mlb_boxscore_player(self): boxscore = Boxscore(BOXSCORE) assert len(boxscore.home_players) == 15 assert len(boxscore.away_players) == 15 for player in boxscore.home_players: assert not player.dataframe.empty for player in boxscore.away_players: assert not player.dataframe.empty
def test_invalid_url_yields_empty_class(self): flexmock(Boxscore) \ .should_receive('_retrieve_html_page') \ .and_return(None) boxscore = Boxscore(BOXSCORE) for key, value in boxscore.__dict__.items(): if key == '_uri': continue assert value is None
def test_game_summary_with_no_scores_returns_none(self): result = Boxscore(None)._parse_summary( pq("""<table class="linescore nohover stats_table no_freeze"> <tbody> <tr> <td class="center"></td> <td class="center"></td> <td class="center"></td> <td class="center"></td> </tr> <tr> <td class="center"></td> <td class="center"></td> <td class="center"></td> <td class="center"></td> </tr> </tbody> </table>""")) assert result == {'away': [None], 'home': [None]}
def setup_method(self, *args, **kwargs): self.results = { 'date': 'Monday, August 17, 2020', 'time': '6:40 p.m. Local', 'venue': 'Angel Stadium of Anaheim', 'time_of_day': NIGHT, 'duration': '3:12', 'winner': HOME, 'winning_name': 'Los Angeles Angels', 'winning_abbr': 'LAA', 'losing_name': 'San Francisco Giants', 'losing_abbr': 'SFG', 'away_at_bats': 35, 'away_runs': 6, 'away_hits': 10, 'away_rbi': 6, 'away_earned_runs': 6.0, 'away_bases_on_balls': 1, 'away_strikeouts': 5, 'away_plate_appearances': 38, 'away_batting_average': .286, 'away_on_base_percentage': .316, 'away_slugging_percentage': .457, 'away_on_base_plus': .773, 'away_pitches': 140, 'away_strikes': 100, 'away_win_probability_for_offensive_player': .283, 'away_average_leverage_index': 1.15, 'away_win_probability_added': .893, 'away_win_probability_subtracted': -.608, 'away_base_out_runs_added': 1.1, 'away_putouts': 25, 'away_assists': 5, 'away_innings_pitched': 8.1, 'away_home_runs': 2, 'away_strikes_by_contact': 63, 'away_strikes_swinging': 14, 'away_strikes_looking': 23, 'away_grounded_balls': 10, 'away_fly_balls': 21, 'away_line_drives': 10, 'away_unknown_bat_type': 0, 'away_game_score': 38, 'away_inherited_runners': 0, 'away_inherited_score': 0, 'away_win_probability_by_pitcher': -.783, 'away_average_leverage_index': 1.75, 'away_base_out_runs_saved': -2.4, 'home_at_bats': 35, 'home_runs': 7, 'home_hits': 12, 'home_rbi': 7, 'home_earned_runs': 7.56, 'home_bases_on_balls': 2, 'home_strikeouts': 10, 'home_plate_appearances': 38, 'home_batting_average': .343, 'home_on_base_percentage': .368, 'home_slugging_percentage': .600, 'home_on_base_plus': .968, 'home_pitches': 159, 'home_strikes': 99, 'home_win_probability_for_offensive_player': .784, 'home_average_leverage_index': 1.75, 'home_win_probability_added': 1.842, 'home_win_probability_subtracted': -1.060, 'home_base_out_runs_added': 2.4, 'home_putouts': 27, 'home_assists': 8, 'home_innings_pitched': 9, 'home_home_runs': 1, 'home_strikes_by_contact': 50, 'home_strikes_swinging': 14, 'home_strikes_looking': 35, 'home_grounded_balls': 7, 'home_fly_balls': 19, 'home_line_drives': 9, 'home_unknown_bat_type': 0, 'home_game_score': 42, 'home_inherited_runners': 5, 'home_inherited_score': 2, 'home_win_probability_by_pitcher': -.283, 'home_average_leverage_index': 1.15, 'home_base_out_runs_saved': -1.1 } flexmock(utils) \ .should_receive('_todays_date') \ .and_return(MockDateTime(YEAR, MONTH)) self.boxscore = Boxscore(BOXSCORE)
def setup_method(self, *args, **kwargs): flexmock(Boxscore) \ .should_receive('_parse_game_data') \ .and_return(None) self.boxscore = Boxscore(None)
class TestMLBBoxscore: @patch('requests.get', side_effect=mock_pyquery) def setup_method(self, *args, **kwargs): flexmock(Boxscore) \ .should_receive('_parse_game_data') \ .and_return(None) self.boxscore = Boxscore(None) def test_away_team_wins(self): fake_away_runs = PropertyMock(return_value=6) fake_home_runs = PropertyMock(return_value=3) type(self.boxscore)._away_runs = fake_away_runs type(self.boxscore)._home_runs = fake_home_runs assert self.boxscore.winner == AWAY def test_home_team_wins(self): fake_away_runs = PropertyMock(return_value=3) fake_home_runs = PropertyMock(return_value=6) type(self.boxscore)._away_runs = fake_away_runs type(self.boxscore)._home_runs = fake_home_runs assert self.boxscore.winner == HOME def test_winning_name_is_home(self): expected_name = 'Home Name' fake_winner = PropertyMock(return_value=HOME) fake_home_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_name = fake_home_name assert self.boxscore.winning_name == expected_name def test_winning_name_is_away(self): expected_name = 'Away Name' fake_winner = PropertyMock(return_value=AWAY) fake_away_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_name = fake_away_name assert self.boxscore.winning_name == expected_name def test_winning_abbr_is_home(self): expected_name = 'HOME' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=HOME) fake_home_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_abbr = fake_home_abbr assert self.boxscore.winning_abbr == expected_name def test_winning_abbr_is_away(self): expected_name = 'AWAY' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=AWAY) fake_away_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_abbr = fake_away_abbr assert self.boxscore.winning_abbr == expected_name def test_losing_name_is_home(self): expected_name = 'Home Name' fake_winner = PropertyMock(return_value=AWAY) fake_home_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_name = fake_home_name assert self.boxscore.losing_name == expected_name def test_losing_name_is_away(self): expected_name = 'Away Name' fake_winner = PropertyMock(return_value=HOME) fake_away_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_name = fake_away_name assert self.boxscore.losing_name == expected_name def test_losing_abbr_is_home(self): expected_name = 'HOME' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=AWAY) fake_home_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_abbr = fake_home_abbr assert self.boxscore.losing_abbr == expected_name def test_losing_abbr_is_away(self): expected_name = 'AWAY' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=HOME) fake_away_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_abbr = fake_away_abbr assert self.boxscore.losing_abbr == expected_name def test_game_summary_with_no_scores_returns_none(self): result = Boxscore(None)._parse_summary( pq("""<table class="linescore nohover stats_table no_freeze"> <tbody> <tr> <td class="center"></td> <td class="center"></td> <td class="center"></td> <td class="center"></td> </tr> <tr> <td class="center"></td> <td class="center"></td> <td class="center"></td> <td class="center"></td> </tr> </tbody> </table>""")) assert result == {'away': [None], 'home': [None]} @patch('requests.get', side_effect=mock_pyquery) def test_invalid_url_returns_none(self, *args, **kwargs): result = Boxscore(None)._retrieve_html_page('') assert result is None def test_mlb_game_info(self): fields = { 'attendance': 26340, 'date': 'Monday, July 9, 2018', 'time': '4:05 p.m. ET', 'venue': 'Oriole Park at Camden Yards', 'duration': '2:55', 'time_of_day': 'Night' } mock_field = """Monday, July 9, 2018 Start Time: 4:05 p.m. ET Attendance: 26,340 Venue: Oriole Park at Camden Yards Game Duration: 2:55 Night Game, on grass """ m = MockBoxscoreData(MockField(mock_field)) self.boxscore._parse_game_date_and_location(m) for field, value in fields.items(): assert getattr(self.boxscore, field) == value def test_mlb_first_game_double_header_info(self): fields = { 'attendance': None, 'date': 'Monday, July 9, 2018', 'time': '4:05 p.m. ET', 'venue': 'Oriole Park at Camden Yards', 'duration': '2:55', 'time_of_day': 'Night' } mock_field = """Monday, July 9, 2018 Start Time: 4:05 p.m. ET Venue: Oriole Park at Camden Yards Game Duration: 2:55 Night Game, on grass First game of doubleheader """ m = MockBoxscoreData(MockField(mock_field)) self.boxscore._parse_game_date_and_location(m) for field, value in fields.items(): assert getattr(self.boxscore, field) == value def test_mlb_second_game_double_header_info(self): fields = { 'attendance': 26340, 'date': 'Monday, July 9, 2018', 'attendance': 26340, 'venue': 'Oriole Park at Camden Yards', 'duration': '3:13', 'time_of_day': 'Night' } mock_field = """Monday, July 9, 2018 Attendance: 26,340 Venue: Oriole Park at Camden Yards Game Duration: 3:13 Night Game, on grass Second game of doubleheader """ m = MockBoxscoreData(MockField(mock_field)) self.boxscore._parse_game_date_and_location(m) for field, value in fields.items(): assert getattr(self.boxscore, field) == value def test_mlb_limited_game_info(self): fields = { 'attendance': 19043, 'date': 'Sunday, June 28, 1970', 'time': None, 'venue': 'Robert F. Kennedy Stadium', 'duration': '3:43', 'time_of_day': 'Day' } mock_field = """Sunday, June 28, 1970 Attendance: 19,043 Venue: Robert F. Kennedy Stadium Game Duration: 3:43 Day Game, on grass """ m = MockBoxscoreData(MockField(mock_field)) self.boxscore._parse_game_date_and_location(m) for field, value in fields.items(): assert getattr(self.boxscore, field) == value def test_invalid_away_inherited_runners_returns_default(self): mock_runners = PropertyMock(return_value='') type(self.boxscore)._away_inherited_runners = mock_runners assert self.boxscore.away_inherited_runners is None def test_invalid_away_inherited_score_returns_default(self): mock_score = PropertyMock(return_value='') type(self.boxscore)._away_inherited_score = mock_score assert self.boxscore.away_inherited_score is None def test_invalid_home_inherited_runners_returns_default(self): mock_runners = PropertyMock(return_value='') type(self.boxscore)._home_inherited_runners = mock_runners assert self.boxscore.home_inherited_runners is None def test_invalid_home_inherited_score_returns_default(self): mock_score = PropertyMock(return_value='') type(self.boxscore)._home_inherited_score = mock_score assert self.boxscore.home_inherited_score is None def test_no_class_information_returns_dataframe_of_none(self): mock_runs = PropertyMock(return_value=None) type(self.boxscore)._away_runs = mock_runs type(self.boxscore)._home_runs = mock_runs assert self.boxscore.dataframe is None def test_attendance_with_empty_string(self): fake_attendance = PropertyMock(return_value='') type(self.boxscore)._attendance = fake_attendance assert self.boxscore.attendance is None def test_night_game_returns_night(self): fake_time_of_day = PropertyMock(return_value='night game on grass') type(self.boxscore)._time_of_day = fake_time_of_day assert self.boxscore.time_of_day == NIGHT def test_night_game_returns_night(self): fake_time_of_day = PropertyMock(return_value='day game on grass') type(self.boxscore)._time_of_day = fake_time_of_day assert self.boxscore.time_of_day == DAY
def test_invalid_url_returns_none(self, *args, **kwargs): result = Boxscore(None)._retrieve_html_page('') assert result is None
def setup_method(self, *args, **kwargs): self.results = { 'date': 'Thursday, June 7, 2018', 'time': '7:10 p.m. ET', 'attendance': 36556, 'venue': 'Fenway Park', 'time_of_day': NIGHT, 'duration': '2:55', 'winner': AWAY, 'winning_name': 'Detroit Tigers', 'winning_abbr': 'DET', 'losing_name': 'Boston Red Sox', 'losing_abbr': 'BOS', 'away_at_bats': 37, 'away_runs': 7, 'away_hits': 10, 'away_rbi': 7, 'away_earned_runs': 7.0, 'away_bases_on_balls': 3, 'away_strikeouts': 11, 'away_plate_appearances': 40, 'away_batting_average': .270, 'away_on_base_percentage': .325, 'away_slugging_percentage': .486, 'away_on_base_plus': .811, 'away_pitches': 141, 'away_strikes': 109, 'away_win_probability_for_offensive_player': .315, 'away_average_leverage_index': .46, 'away_win_probability_added': .551, 'away_win_probability_subtracted': -.236, 'away_base_out_runs_added': 2.2, 'away_putouts': 27, 'away_assists': 9, 'away_innings_pitched': 9, 'away_home_runs': 1, 'away_strikes_by_contact': 68, 'away_strikes_swinging': 14, 'away_strikes_looking': 27, 'away_grounded_balls': 13, 'away_fly_balls': 13, 'away_line_drives': 6, 'away_unknown_bat_type': 0, 'away_game_score': 59, 'away_inherited_runners': 1, 'away_inherited_score': 0, 'away_win_probability_by_pitcher': .184, 'away_average_leverage_index': .71, 'away_base_out_runs_saved': 2.8, 'home_at_bats': 33, 'home_runs': 2, 'home_hits': 7, 'home_rbi': 2, 'home_earned_runs': 2.0, 'home_bases_on_balls': 5, 'home_strikeouts': 8, 'home_plate_appearances': 38, 'home_batting_average': .212, 'home_on_base_percentage': .316, 'home_slugging_percentage': .364, 'home_on_base_plus': .679, 'home_pitches': 157, 'home_strikes': 83, 'home_win_probability_for_offensive_player': -.184, 'home_average_leverage_index': .71, 'home_win_probability_added': .368, 'home_win_probability_subtracted': -.552, 'home_base_out_runs_added': -2.8, 'home_putouts': 27, 'home_assists': 9, 'home_innings_pitched': 9, 'home_home_runs': 1, 'home_strikes_by_contact': 45, 'home_strikes_swinging': 12, 'home_strikes_looking': 26, 'home_grounded_balls': 9, 'home_fly_balls': 16, 'home_line_drives': 7, 'home_unknown_bat_type': 0, 'home_game_score': 25, 'home_inherited_runners': 0, 'home_inherited_score': 0, 'home_win_probability_by_pitcher': -.317, 'home_average_leverage_index': .46, 'home_base_out_runs_saved': -2.2 } flexmock(utils) \ .should_receive('_todays_date') \ .and_return(MockDateTime(YEAR, MONTH)) self.boxscore = Boxscore(BOXSCORE)
def boxscore(self): """ Returns an instance of the Boxscore class containing more detailed stats on the game. """ return Boxscore(self._boxscore)