Skip to content

Commit

Permalink
minor fixes, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Dec 26, 2023
1 parent df14659 commit 893f396
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
23 changes: 9 additions & 14 deletions fastf1/ergast/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,52 +453,47 @@ def _build_url(
# therefore, if the specifier is defined, do not add the endpoint
# string additionally
if driver is not None:
if endpoint == 'driver':
endpoint = f"/drivers/{driver}"
if endpoint == 'drivers':
endpoint = f"drivers/{driver}"
else:
selectors.append(f"/drivers/{driver}")

if constructor is not None:
if endpoint == 'constructors':
endpoint = f"/constructors/{constructor}"
endpoint = f"constructors/{constructor}"
else:
selectors.append(f"/constructors/{constructor}")

if circuit is not None:
if endpoint == 'circuits':
endpoint = f"/circuits/{circuit}"
endpoint = f"circuits/{circuit}"
else:
selectors.append(f"/circuits/{circuit}")

if status is not None:
if endpoint == 'status':
endpoint = f"/status/{status}"
endpoint = f"status/{status}"
else:
selectors.append(f"/status/{status}")

if standings_position is not None:
if endpoint == 'driverStandings':
endpoint = f"/driverStandings/{standings_position}"
endpoint = f"driverStandings/{standings_position}"
elif endpoint == 'constructorStandings':
endpoint = f"/constructorStandings/{standings_position}"
endpoint = f"constructorStandings/{standings_position}"

if lap_number is not None:
if endpoint == 'laps':
endpoint = f"/laps/{lap_number}"
endpoint = f"laps/{lap_number}"
else:
selectors.append(f"/laps/{lap_number}")

if stop_number is not None:
if endpoint == 'pitstops':
endpoint = f"/pitstops/{stop_number}"
endpoint = f"pitstops/{stop_number}"
else:
selectors.append(f"/pitstops/{stop_number}")

# Special case for race_schedule
# If no additional filters besides required (season) exclude endpoint
# if endpoint == 'races' and len(selectors) == 1:
# endpoint = None

if endpoint is not None:
selectors.append(f"/{endpoint}")

Expand Down
70 changes: 59 additions & 11 deletions fastf1/tests/test_ergast.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,30 +313,78 @@ def test_merge_dicts_of_lists(data, expected):
@pytest.mark.parametrize(
"endpoint, selectors, expected",
(
# "normal" behaviour for most endpoints
# "simple" behaviour
['seasons', {}, "https://ergast.com/api/f1/seasons.json"],
['circuits', {'driver': 'alonso', 'constructor': 'alpine'},
"https://ergast.com/api/f1/constructors/alpine/"
"drivers/alonso/circuits.json"],
['races', {'season': 2022},
"https://ergast.com/api/f1/2022/races.json"],
['results', {'season': 2022, 'round': '10'},
"https://ergast.com/api/f1/2022/10/results.json"],
['sprint', {'season': 2022, 'round': '4'},
"https://ergast.com/api/f1/2022/4/sprint.json"],
['qualifying', {'season': 2022, 'round': '10'},
"https://ergast.com/api/f1/2022/10/qualifying.json"],
# special cases where endpoint name matches selector and the endpoint
# gets extended with its selection
['drivers', {},
"https://ergast.com/api/f1/drivers.json"],
['drivers', {'driver': 'alonso'},
"https://ergast.com/api/f1/drivers/alonso.json"],
['constructors', {},
"https://ergast.com/api/f1/constructors.json"],
['constructors', {'constructor': 'ferrari'},
"https://ergast.com/api/f1/constructors/ferrari.json"],
['circuits', {},
"https://ergast.com/api/f1/circuits.json"],
['circuits', {'circuit': 'monza'},
"https://ergast.com/api/f1/circuits/monza.json"],
['status', {},
"https://ergast.com/api/f1/status.json"],
['status', {'status': '1'},
"https://ergast.com/api/f1/status/1.json"],
['driverStandings', {'season': 2022},
"https://ergast.com/api/f1/2022/driverStandings.json"],
['driverStandings', {'season': 2022, 'standings_position': '1'},
"https://ergast.com/api/f1/2022/driverStandings/1.json"],
['constructorStandings', {'season': 2022},
"https://ergast.com/api/f1/2022/constructorStandings.json"],
['constructorStandings', {'season': 2022, 'standings_position': '1'},
"https://ergast.com/api/f1/2022/constructorStandings/1.json"],
# special case where endpoint name matches selector
['laps', {'season': 2022, 'round': 10},
"https://ergast.com/api/f1/2022/10/laps.json"],
['laps', {'season': 2022, 'round': 10, 'lap_number': 1},
"https://ergast.com/api/f1/2022/10/laps/1.json"],
['pitstops', {'season': 2022, 'round': 10},
"https://ergast.com/api/f1/2022/10/pitstops.json"],
['pitstops', {'season': 2022, 'round': 10, 'stop_number': '1'},
"https://ergast.com/api/f1/2022/10/pitstops/1.json"],
# endpoint/selector combination in other request
['pitstops', {'season': 2022, 'round': 10, 'lap_number': 1},
"https://ergast.com/api/f1/2022/10/laps/1/pitstops.json"],
# combined selector standings_position
['driverStandings', {'season': 2022, 'standings_position': 1},
"https://ergast.com/api/f1/2022/driverStandings/1.json"],
['constructorStandings', {'season': 2022, 'standings_position': 3},
"https://ergast.com/api/f1/2022/constructorStandings/3.json"]
['circuits', {'driver': 'alonso', 'constructor': 'alpine'},
"https://ergast.com/api/f1/drivers/alonso/"
"constructors/alpine/circuits.json"],
)
)
def test_ergast_build_url(endpoint: str, selectors: dict, expected: str):
Expand Down

0 comments on commit 893f396

Please sign in to comment.