Skip to content

Commit

Permalink
Add fallback for missing ergast drivers in round
Browse files Browse the repository at this point in the history
  • Loading branch information
SmCTwelve committed Sep 17, 2023
1 parent 68e24ae commit a1965c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"editor.formatOnSave": true,
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],
"[python]": {},
"python.analysis.typeCheckingMode": "off"
}
20 changes: 15 additions & 5 deletions f1/api/ergast.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@ async def get_all_drivers(season=None, round=None) -> list[dict]:
Raises `MissingDataError`.
"""
round_url = f'{BASE_URL}/{season}/{round}/drivers.json?limit=1000'
season_url = f'{BASE_URL}/{season}/drivers.json?limit=1000'
all_time_url = f'{BASE_URL}/drivers.json?limit=1000'

# Get JSON data as dict
if season and round:
url = f'{BASE_URL}/{season}/{round}/drivers.json?limit=1000'
res = await fetch(round_url)
elif season:
url = f'{BASE_URL}/{season}/drivers.json?limit=1000'
res = await fetch(season_url)
else:
url = f'{BASE_URL}/drivers.json?limit=1000'
# Get JSON data as dict
res = await fetch(url)
res = await fetch(all_time_url)

if res is None:
raise MissingDataError()

# Fallback to season only if data for the round is unavailable
if round and not res['MRData']['DriverTable']['Drivers']:
logger.warning("Driver data for specified round is missing, falling back to season list.")
res = await fetch(season_url)

return res['MRData']['DriverTable']['Drivers']


Expand Down
48 changes: 27 additions & 21 deletions f1/tests/mock_response/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,25 +386,31 @@ def result_wrapper(body, quali=False):
]
}

all_drivers = [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http:\/\/en.wikipedia.org\/wiki\/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "max_verstappen",
"permanentNumber": "1",
"code": "VER",
"url": "http:\/\/en.wikipedia.org\/wiki\/Max_Verstappen",
"givenName": "Max",
"familyName": "Verstappen",
"dateOfBirth": "1981-07-29",
"nationality": "Dutch"
all_drivers = {
"MRData": {
"DriverTable": {
"Drivers": [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http:\/\/en.wikipedia.org\/wiki\/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "max_verstappen",
"permanentNumber": "1",
"code": "VER",
"url": "http:\/\/en.wikipedia.org\/wiki\/Max_Verstappen",
"givenName": "Max",
"familyName": "Verstappen",
"dateOfBirth": "1981-07-29",
"nationality": "Dutch"
}
]
}
}
]
}
17 changes: 17 additions & 0 deletions f1/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,23 @@ async def test_get_driver_career(self, mock_fetch):
self.check_total_and_num_results(data['Seasons']['total'], data['Seasons']['years'])
self.check_total_and_num_results(data['Teams']['total'], data['Teams']['names'])

@patch(fetch_path)
@async_test
async def test_get_all_drivers(self, mock_fetch):
mock_fetch.return_value = models.all_drivers
res = await ergast.get_all_drivers()
self.assertEqual(res[0]["code"], "ALO")
self.assertEqual(res[1]["code"], "VER")

@patch(fetch_path)
@async_test
async def test_get_all_drivers_missing_round(self, mock_fetch):
empty_data = {"MRData": {"DriverTable": {"Drivers": []}}}
mock_fetch.side_effect = [empty_data, models.all_drivers]
res = await ergast.get_all_drivers(round=100)
self.assertEqual(mock_fetch.call_count, 2)
self.assertEqual(len(res), 2)

# boundary tests


Expand Down

0 comments on commit a1965c3

Please sign in to comment.