test_client.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import pytest
  2. import requests
  3. from mock import patch
  4. from pysportsdb.exceptions import PateronOnlyEndpoint
  5. @pytest.mark.usefixtures("unpaid_client")
  6. class TestUnpaidClient:
  7. def test_default_client_version(self, unpaid_client):
  8. assert unpaid_client.API_VERSION is "v1"
  9. @patch("pysportsdb.TheSportsDbClient._make_request")
  10. def test_build_url_unsupported_endpoint(
  11. self, mock_make_request, unpaid_client, caplog
  12. ):
  13. mock_make_request.response = {}
  14. with pytest.raises(PateronOnlyEndpoint):
  15. response = unpaid_client.lookup_event(event_id="0")
  16. @pytest.mark.usefixtures("paid_client")
  17. class TestPaidClient:
  18. """A paid client is only distinguised by having an API key other tha '2'"""
  19. @patch("pysportsdb.TheSportsDbClient._make_request")
  20. def test_build_url_supported_endpoint(
  21. self, mock_make_request, paid_client, caplog
  22. ):
  23. mock_make_request.return_value = {}
  24. response = paid_client.lookup_event(event_id="0")
  25. assert len(caplog.records) is 0
  26. assert response == {}
  27. def test_make_request_bad_url(self, paid_client):
  28. with pytest.raises(requests.exceptions.MissingSchema) as error:
  29. paid_client._make_request("bad-url")
  30. def test_make_request_html_response(self, paid_client, caplog):
  31. request = paid_client._make_request("https://www.google.com")
  32. assert caplog.records[0].levelname == "ERROR"
  33. assert (
  34. caplog.records[0].msg
  35. == "Received a non-JSON repsonse from request, check API key"
  36. )
  37. assert request == {}