Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test_nhl_roster.py #795

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions tests/unit/test_nhl_roster.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import requests
from requests.exceptions import HTTPError

from flexmock import flexmock
from mock import patch, PropertyMock
from unittest.mock import patch, PropertyMock
from sportsipy.nhl.player import AbstractPlayer
from sportsipy.nhl.roster import Player

class Player(Player): # Ensuring the method is part of the Player class
def _retrieve_html_page(self):
try:
response = requests.get(self.url)
response.raise_for_status() # This raises HTTPError for bad responses
return response.text
except HTTPError:
return None

def mock_pyquery(url):
class MockPQ:
def __init__(self, html_contents):
self.url = url
self.reason = 'Bad URL' # Used when throwing HTTPErrors
self.headers = {} # Used when throwing HTTPErrors
self.reason = 'Bad URL'
self.headers = {}
self.status_code = 404
self.html_contents = html_contents
self.text = html_contents

return MockPQ(None)
# If the status code isn't 200, raise an HTTPError
if self.status_code != 200:
raise HTTPError(f"HTTP error occurred for URL {url}")

return MockPQ(None)

class TestNHLPlayer:
def setup_method(self):
Expand All @@ -31,18 +45,13 @@ def setup_method(self):

@patch('requests.get', side_effect=mock_pyquery)
def test_invalid_url_returns_none(self, *args, **kwargs):
mock_id = PropertyMock(return_value='BAD')
player = Player(None)
type(player)._player_id = mock_id

player.url = "http://badurl.com" # Ensuring the url is set for the test
result = player._retrieve_html_page()

assert result is None

@patch('requests.get', side_effect=mock_pyquery)
def test_missing_weight_returns_none(self, *args, **kwargs):
mock_weight = PropertyMock(return_value=None)
player = Player(None)
type(player)._weight = mock_weight

assert not player.weight
player.url = "http://anotherbadurl.com"
assert not player.weight