1010import requests
1111import pytest
1212
13- from mlbstatsapi import MlbDataAdapter , MlbResult , TheMlbStatsApiException
13+ from mlbstatsapi import (
14+ MlbDataAdapter ,
15+ MlbDecodeError ,
16+ MlbHttpError ,
17+ MlbResult ,
18+ MlbTransportError ,
19+ TheMlbStatsApiException ,
20+ )
1421
1522
1623BASE_URL = "https://statsapi.mlb.com/api/v1/"
@@ -172,7 +179,7 @@ def test_non_json_404_returns_empty_data(adapter, requests_mock):
172179 assert result .data == {}
173180
174181
175- def test_json_500_raises_the_mlb_stats_api_exception (adapter , requests_mock ):
182+ def test_json_500_raises_mlb_http_error (adapter , requests_mock ):
176183 requests_mock .get (
177184 f"{ BASE_URL } teams/133/stats" ,
178185 json = {
@@ -185,16 +192,19 @@ def test_json_500_raises_the_mlb_stats_api_exception(adapter, requests_mock):
185192 reason = "Internal Server Error" ,
186193 )
187194
188- with pytest .raises (TheMlbStatsApiException , match = r"^500: Internal Server Error$" ) as exc_info :
195+ with pytest .raises (MlbHttpError , match = r"^500: Internal Server Error$" ) as exc_info :
189196 adapter .get (
190197 endpoint = "teams/133/stats" ,
191198 ep_params = {"stats" : "standard" , "group" : "hitting" },
192199 )
193200
201+ assert isinstance (exc_info .value , TheMlbStatsApiException )
194202 assert str (exc_info .value ) == "500: Internal Server Error"
203+ assert exc_info .value .status_code == 500
204+ assert exc_info .value .reason == "Internal Server Error"
195205
196206
197- def test_html_502_raises_the_mlb_stats_api_exception (adapter , requests_mock ):
207+ def test_html_502_raises_mlb_http_error (adapter , requests_mock ):
198208 requests_mock .get (
199209 f"{ BASE_URL } sports" ,
200210 text = "<html><body>Bad Gateway</body></html>" ,
@@ -203,26 +213,30 @@ def test_html_502_raises_the_mlb_stats_api_exception(adapter, requests_mock):
203213 headers = {"Content-Type" : "text/html" },
204214 )
205215
206- with pytest .raises (TheMlbStatsApiException , match = r"^502: Bad Gateway$" ) as exc_info :
216+ with pytest .raises (MlbHttpError , match = r"^502: Bad Gateway$" ) as exc_info :
207217 adapter .get (endpoint = "sports" )
208218
219+ assert isinstance (exc_info .value , TheMlbStatsApiException )
209220 assert str (exc_info .value ) == "502: Bad Gateway"
221+ assert exc_info .value .status_code == 502
222+ assert exc_info .value .reason == "Bad Gateway"
210223
211224
212- def test_connection_failure_raises_request_failed (adapter , requests_mock ):
225+ def test_connection_failure_raises_mlb_transport_error (adapter , requests_mock ):
213226 requests_mock .get (
214227 f"{ BASE_URL } sports" ,
215228 exc = requests .exceptions .ConnectionError ("connection refused" ),
216229 )
217230
218- with pytest .raises (TheMlbStatsApiException , match = r"^Request failed$" ) as exc_info :
231+ with pytest .raises (MlbTransportError , match = r"^Request failed$" ) as exc_info :
219232 adapter .get (endpoint = "sports" )
220233
234+ assert isinstance (exc_info .value , TheMlbStatsApiException )
221235 assert str (exc_info .value ) == "Request failed"
222236 assert isinstance (exc_info .value .__cause__ , requests .exceptions .ConnectionError )
223237
224238
225- def test_invalid_json_on_successful_response_raises_bad_json (adapter , requests_mock ):
239+ def test_invalid_json_on_successful_response_raises_mlb_decode_error (adapter , requests_mock ):
226240 requests_mock .get (
227241 f"{ BASE_URL } teams/133/stats" ,
228242 text = '{"some bad json": sdfsd' ,
@@ -231,13 +245,15 @@ def test_invalid_json_on_successful_response_raises_bad_json(adapter, requests_m
231245 headers = {"Content-Type" : "application/json" },
232246 )
233247
234- with pytest .raises (TheMlbStatsApiException , match = r"^Bad JSON in response$" ) as exc_info :
248+ with pytest .raises (MlbDecodeError , match = r"^Bad JSON in response$" ) as exc_info :
235249 adapter .get (
236250 endpoint = "teams/133/stats" ,
237251 ep_params = {"stats" : "season" , "group" : "hitting" },
238252 )
239253
254+ assert isinstance (exc_info .value , TheMlbStatsApiException )
240255 assert str (exc_info .value ) == "Bad JSON in response"
256+ assert isinstance (exc_info .value .__cause__ , ValueError )
241257
242258
243259def test_constructor_does_not_change_logger_level ():
0 commit comments