Ejemplo n.º 1
0
    def test_extractTrainingExample_instantiatesCorrectGameHome(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        game = ws.extractTrainingExample(endpoint_game, hitters_stats,
                                         pitchers_stats, soup, 'home',
                                         team_home, team_away, 2015)

        expected_id = '/boxes/CHN/CHN201504050.shtml-H'
        expected_len_hitters = 11
        expected_len_pitchers = 4
        expected_outcome = {
            '/players/f/fowlede01.shtml': 1 / 5,
            '/players/c/castrst01.shtml': 1 / 5,
            '/players/c/coghlch01.shtml': 1 / 5,
            '/players/r/rossda01.shtml': 1 / 5,
            '/players/l/lasteto01.shtml': 1 / 5
        }
        isCorrect = True
        for outcome in expected_outcome:
            if game.outcome[outcome] != expected_outcome[outcome]:
                isCorrect = False
        self.assertTrue(expected_id == game.id
                        and expected_len_hitters == len(game.hitters)
                        and expected_len_pitchers == len(game.pitchers)
                        and isCorrect)
Ejemplo n.º 2
0
    def test_extractTrainingExample_instantiatesCorrectGameAway(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        game = ws.extractTrainingExample(endpoint_game, hitters_stats,
                                         pitchers_stats, soup, 'away',
                                         team_away, team_home, 2015)

        expected_id = '/boxes/CHN/CHN201504050.shtml-A'
        expected_len_hitters = 10
        expected_len_pitchers = 6
        expected_outcome = {
            '/players/c/carpema01.shtml': 2 / 10,
            '/players/h/heywaja01.shtml': 3 / 10,
            '/players/h/hollima01.shtml': 2 / 10,
            '/players/p/peraljh01.shtml': 1 / 10,
            '/players/j/jayjo02.shtml': 2 / 10
        }
        isCorrect = True
        for outcome in expected_outcome:
            if game.outcome[outcome] != expected_outcome[outcome]:
                isCorrect = False
        self.assertTrue(expected_id == game.id
                        and expected_len_hitters == len(game.hitters)
                        and expected_len_pitchers == len(game.pitchers)
                        and isCorrect)
Ejemplo n.º 3
0
    def test_extractSeasonStatsFromTable_raisesExceptionIfInvalidArgument(
            self):
        table = ws.BeautifulSoup("", 'html.parser')

        with self.assertRaises(Exception) as ctx:
            output = ws.extractSeasonStatsFromTable(table, '', 2010)

        self.assertIsInstance(ctx.exception, ValueError)
Ejemplo n.º 4
0
    def test_searchForTable_returnsNoneIfNotFound(self):
        endpoint = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "this_is_a_test_id"

        table = ws.searchForTable(soup, search_id)

        self.assertEqual(None, table)
Ejemplo n.º 5
0
    def test_extractPlayerName_extractsCorrectName(self):
        endpoint = "/players/p/peraljh01.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        selector = "#meta div[itemtype='https://schema.org/Person']"
        div = soup.select(selector)[0]

        name = ws.extractPlayerName(div)

        self.assertEqual("Jhonny Peralta", name)
Ejemplo n.º 6
0
    def test_searchForTable_searchesTableWithID(self):
        endpoint = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "StLouisCardinalsbatting"

        table = ws.searchForTable(soup, search_id)

        caption = table.find("caption").string
        self.assertEqual("St. Louis Cardinals Table", caption)
Ejemplo n.º 7
0
    def test_extractPlayerEndpointsFromTable_extractsCorrectEndpoint(self):
        endpoint = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "StLouisCardinalsbatting"
        table = ws.searchForTable(soup, search_id)

        endpoint_players = ws.extractPlayerEndpointsFromTable(table, 'hitter')

        self.assertEqual("/players/c/carpema01.shtml", endpoint_players[0])
Ejemplo n.º 8
0
    def test_extractPlayerEndpointsFromTable_extractsAllEndpointsHitter(self):
        endpoint = "/boxes/LAN/LAN201509150.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "ColoradoRockiesbatting"
        table = ws.searchForTable(soup, search_id)

        endpoint_players = ws.extractPlayerEndpointsFromTable(table, 'hitter')

        self.assertEqual(15, len(endpoint_players))
Ejemplo n.º 9
0
    def test_extractPlayerEndpointsFromTable_extractsAllEndpointsPitcher(self):
        endpoint = "/boxes/LAN/LAN201509150.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "LosAngelesDodgerspitching"
        table = ws.searchForTable(soup, search_id)

        endpoint_players = ws.extractPlayerEndpointsFromTable(table, 'pitcher')

        self.assertEqual(11, len(endpoint_players))
Ejemplo n.º 10
0
    def test_extractSeasonStatsFromTable_extractsStatsPitcher(self):
        endpoint = "/players/w/wainwad01.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'pitching_standard')

        output = ws.extractSeasonStatsFromTable(table, 'pitcher', 2010)

        expected = {'SHO': 2, 'IP': 230.1, 'H': 186, 'SO': 213, 'BF': 910}
        self.assertEqual(expected, output)
Ejemplo n.º 11
0
    def test_extractSeasonStatsFromTable_extractsStatsHitter(self):
        endpoint = "/players/w/wainwad01.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'batting_standard')

        output = ws.extractSeasonStatsFromTable(table, 'hitter', 2007)

        expected = {'PA': 74, 'H': 18, 'SO': 18}
        self.assertEqual(expected, output)
Ejemplo n.º 12
0
    def test_extractPlayerHand_extractsPitcherLeft(self):
        endpoint = "/players/c/cokeph01.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        selector = "#meta div[itemtype='https://schema.org/Person']"
        div = soup.select(selector)[0]

        r, l = ws.extractPlayerHand(div, 'pitcher')

        self.assertTrue(r == 0 and l == 1)
Ejemplo n.º 13
0
    def test_extractTeams_extractsTeams(self):
        endpoint_game = "/boxes/ARI/ARI201504060.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')

        team_away, team_home = ws.extractTeams(soup)

        isAway = team_away == "San Francisco Giants"
        isHome = team_home == "Arizona Diamondbacks"
        self.assertTrue(isAway and isHome)
Ejemplo n.º 14
0
    def test_extractPlayerHand_raisesExceptionIfInvalidArgument(self):
        endpoint = "/players/c/cokeph01.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        selector = "#meta div[itemtype='https://schema.org/Person']"
        div = soup.select(selector)[0]

        with self.assertRaises(Exception) as ctx:
            r, l = ws.extractPlayerHand(div, '')

        self.assertIsInstance(ctx.exception, ValueError)
Ejemplo n.º 15
0
    def test_extractHitterOutcome_extractsCorrectOutcome(self):
        endpoint = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        search_id = "StLouisCardinalsbatting"
        table = ws.searchForTable(soup, search_id)
        endpoint_player = "/players/h/heywaja01.shtml"

        h_contribution = ws.extractHitterOutcome(table, endpoint_player)

        self.assertEqual(3 / 10, h_contribution)
Ejemplo n.º 16
0
    def test_extractPlayerGamePerformance_extractsPitcherData_noSHO(self):
        endpoint_player = "/players/w/wainwad01.shtml"
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'StLouisCardinalspitching')

        output = ws.extractPlayerGamePerformance(table, endpoint_player,
                                                 'pitcher')

        expected = {'IP': 6, 'H': 5, 'SO': 6, 'BF': 23, 'SHO': 0}
        self.assertEqual(expected, output)
Ejemplo n.º 17
0
    def test_extractPlayerGamePerformance_extractsHitterData(self):
        endpoint_player = "/players/c/carpema01.shtml"
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'StLouisCardinalsbatting')

        output = ws.extractPlayerGamePerformance(table, endpoint_player,
                                                 'hitter')

        expected = {'PA': 5, 'H': 2, 'SO': 0}
        self.assertEqual(expected, output)
Ejemplo n.º 18
0
    def test_extractPlayerGamePerformance_extractsPitcherData_SHO(self):
        endpoint_player = "/players/t/tanakma01.shtml"
        endpoint_game = "/boxes/TBA/TBA201807240.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'NewYorkYankeespitching')

        output = ws.extractPlayerGamePerformance(table, endpoint_player,
                                                 'pitcher')

        expected = {'IP': 9, 'H': 3, 'SO': 9, 'BF': 29, 'SHO': 1}
        self.assertEqual(expected, output)
Ejemplo n.º 19
0
    def test_extractPlayerGamePerformance_raisesExceptionIfInvalidArgument(
            self):
        endpoint_player = "/players/t/tanakma01.shtml"
        endpoint_game = "/boxes/TBA/TBA201807240.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        table = ws.searchForTable(soup, 'NewYorkYankeespitching')

        with self.assertRaises(Exception) as ctx:
            output = ws.extractPlayerGamePerformance(table, endpoint_player,
                                                     '')

        self.assertIsInstance(ctx.exception, ValueError)
Ejemplo n.º 20
0
    def test_extractTrainingExample_raisesExceptionIfInvalidArgument(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        with self.assertRaises(Exception) as ctx:
            ws.extractTrainingExample(endpoint_game, hitters_stats,
                                      pitchers_stats, soup, '', team_away,
                                      team_home, 2015)

        self.assertIsInstance(ctx.exception, ValueError)
Ejemplo n.º 21
0
    def test_extractTrainingExample_makesDeepCopyPitchers(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        game = ws.extractTrainingExample(endpoint_game, hitters_stats,
                                         pitchers_stats, soup, 'away',
                                         team_away, team_home, 2015)
        pitcher = game.pitchers[0]
        print(pitcher, pitchers_stats[pitcher.endpoint])
        self.assertNotEqual(pitcher, pitchers_stats[pitcher.endpoint])
Ejemplo n.º 22
0
    def test_extractTrainingExample_updatesHittersStats_firstGame(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        game = ws.extractTrainingExample(endpoint_game, hitters_stats,
                                         pitchers_stats, soup, 'away',
                                         team_away, team_home, 2015)

        expected = ws.Hitter('Matt Carpenter', '/players/c/carpema01.shtml',
                             709 + 5, 162 + 2, 111, 0, 1)
        self.assertEqual(expected, hitters_stats['/players/c/carpema01.shtml'])
Ejemplo n.º 23
0
    def test_extractTrainingExample_updatesPitchersStats_firstGame(self):
        endpoint_game = "/boxes/CHN/CHN201504050.shtml"
        page = ws.requests.get(ws.URL + endpoint_game)
        soup = ws.BeautifulSoup(page.content, 'html.parser')
        team_away, team_home = ws.extractTeams(soup)
        hitters_stats = {}
        pitchers_stats = {}

        game = ws.extractTrainingExample(endpoint_game, hitters_stats,
                                         pitchers_stats, soup, 'away',
                                         team_away, team_home, 2015)

        expected = ws.Pitcher('Phil Coke', '/players/c/cokeph01.shtml', 0,
                              58 + .2, 69, 41 + 2, 257 + 3, 0, 1)
        self.assertEqual(expected, pitchers_stats['/players/c/cokeph01.shtml'])