Skip to content

Commit

Permalink
more updates and fixes of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Mar 4, 2021
1 parent f6fe29b commit a58b05d
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 29 deletions.
13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@ def pytest_addoption(parser):
"--prj-doc", action="store_true", default=False,
help="run only tests for general project structure and documentation"
)
parser.addoption(
"--slow", action="store_true", default=False,
help="run very slow tests too: this may take 30 minutes or more and will may multiple"
"hundred requests to the api server - usage is highly discouraged"
)


def pytest_configure(config):
config.addinivalue_line("markers", "f1telapi: test connects to the f1 telemetry api")
config.addinivalue_line("markers", "ergastapi: test connects to the ergast api")
config.addinivalue_line("markers", "prjdoc: general non-code tests for project and structure")
config.addinivalue_line("markers", "slow: extremely slow tests (multiple minutes)")


def pytest_collection_modifyitems(config, items):
# cli conditional skip extremely slow tests
if not config.getoption("--slow"):
skip_slow = pytest.mark.skip(reason="need --slow option to run; usage highly discouraged")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)

# cli conditional skip test that connect to the f1 telemetry api
if not config.getoption("--f1-tel-api"):
skip_f1_tel = pytest.mark.skip(reason="need --f1-tel-api option to run")
Expand Down
Binary file modified fastf1/tests/mpl-baseline/test_doc_example_delta_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified fastf1/tests/mpl-baseline/test_doc_example_fast_lec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified fastf1/tests/mpl-baseline/test_readme_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified fastf1/tests/mpl-baseline/test_speed_trace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions fastf1/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def test_enable_cache(tmpdir):
fastf1.Cache.enable_cache(tmpdir)


# TODO: use some workaround/fixture/whatever to ingest a valid response and make thís test run!
@pytest.mark.f1telapi
@pytest.mark.skip(reason="required data not available")
def test_cache_used_and_clear(tmpdir):
fastf1.Cache.enable_cache(tmpdir)

Expand Down
25 changes: 14 additions & 11 deletions fastf1/tests/test_example_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import fastf1 as ff1
from fastf1 import plotting, utils
from matplotlib import pyplot as plt

plotting.setup_mpl()

# generate baseline with
# >pytest tests --mpl-generate-path=tests/mpl-baseline

ff1.Cache.enable_cache("test_cache/")


@pytest.mark.f1telapi
@pytest.mark.mpl_image_compare
@pytest.mark.mpl_image_compare(style='default')
def test_readme_example():
ff1.Cache.enable_cache("test_cache/")
race = ff1.get_session(2020, 'Belgian', 'R')
laps = race.load_laps()

Expand All @@ -31,18 +30,20 @@ def test_readme_example():

@pytest.mark.f1telapi
def test_doc_example_pronto_seb():
monza_quali = ff1.get_session(2020, 'Belgian', 'R')
ff1.Cache.enable_cache("test_cache/")
session = ff1.get_session(2020, 'Belgian', 'R')

vettel = monza_quali.get_driver('VET')
vettel = session.get_driver('VET')
assert f"Pronto {vettel.name}?" == "Pronto Sebastian?"


@pytest.mark.f1telapi
@pytest.mark.mpl_image_compare
@pytest.mark.mpl_image_compare(style='default')
def test_doc_example_fast_lec():
monza_quali = ff1.get_session(2020, 'Belgian', 'R')
ff1.Cache.enable_cache("test_cache/")
session = ff1.get_session(2020, 'Belgian', 'R')

laps = monza_quali.load_laps()
laps = session.load_laps()
fast_leclerc = laps.pick_driver('LEC').pick_fastest()
t = fast_leclerc.telemetry['Time']
vCar = fast_leclerc.telemetry['Speed']
Expand All @@ -58,8 +59,9 @@ def test_doc_example_fast_lec():


@pytest.mark.f1telapi
@pytest.mark.mpl_image_compare
@pytest.mark.mpl_image_compare(style='default')
def test_doc_example_delta_time():
ff1.Cache.enable_cache("test_cache/")
quali = ff1.get_session(2020, 'Belgian', 'R')
laps = quali.load_laps()
lec = laps.pick_driver('LEC').pick_fastest()
Expand All @@ -77,8 +79,9 @@ def test_doc_example_delta_time():


@pytest.mark.f1telapi
@pytest.mark.mpl_image_compare
@pytest.mark.mpl_image_compare(style='default')
def test_speed_trace():
ff1.Cache.enable_cache("test_cache/")
session = ff1.get_session(2020, 'Belgian', 'R')
session.load_laps()

Expand Down
24 changes: 9 additions & 15 deletions fastf1/tests/test_input_data_handling.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# test some known special cases

import pytest
import fastf1 as ff1
from fastf1 import core
import logging


@pytest.mark.f1telapi
@pytest.mark.skip(reason="required data not available")
def test_partial_position_data(caplog):
ff1.Cache.enable_cache("test_cache/")
# RUS is missing the first half of the position data because F1 somehow
# switches from development driver to RUS mid-session
# this requires recreating missing data (empty) so that the data has the correct size
Expand All @@ -20,7 +23,9 @@ def test_partial_position_data(caplog):


@pytest.mark.f1telapi
@pytest.mark.skip(reason="required data not available")
def test_history_mod_1(caplog):
ff1.Cache.enable_cache("test_cache/")
# api data sometimes goes back in time
caplog.set_level(logging.INFO)

Expand All @@ -31,29 +36,18 @@ def test_history_mod_1(caplog):
assert "Laps loaded and saved!" in caplog.text # indicates success


@pytest.mark.f1telapi
def test_history_mod_2(caplog):
# api data sometimes goes back in time
caplog.set_level(logging.INFO)

session = core.get_session(2020, 3, 'FP2')
session.load_laps()

assert "The api attempted to rewrite history" in caplog.text # the error
assert "Laps loaded and saved!" in caplog.text # indicates success


@pytest.mark.f1telapi
def test_ergast_lookup_fail(caplog):
ff1.Cache.enable_cache("test_cache/")
# ergast lookup fails if data is requested to soon after a session ends
caplog.set_level(logging.INFO)

def fail_load():
def fail_load(*args, **kwargs):
raise Exception
core.ergast.load = fail_load # force function call to fail

session = core.get_session(2020, 3, 'FP2') # rainy and short session, good for fast test/quick loading
session.load_laps()

assert "Ergast lookup failed" in caplog.text # the warning
assert "Laps loaded and saved!" in caplog.text # indicates success
assert "Failed to load data from Ergast API!" in caplog.text # the warning
assert "Loaded data for" in caplog.text # indicates success
6 changes: 4 additions & 2 deletions fastf1/tests/test_laps_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@


@pytest.mark.f1telapi
@pytest.mark.slow
def test_2019():
for evn in range(1, 22): # 21 races
for ses in ('FP1', 'FP2', 'FP3', 'Q', 'R'):
if evn == 17 and ses == 'FP3':
continue # session did not take place

session = ff1.get_session(2019, evn, ses)
session._load_summary()
session.load_laps(with_telemetry=False)


@pytest.mark.f1telapi
@pytest.mark.slow
def test_2020():
for evn in range(1, 20): # 19 races
for ses in ('FP1', 'FP2', 'FP3', 'Q', 'R'):
session = ff1.get_session(2020, evn, ses)
session._load_summary()
session.load_laps(with_telemetry=False)
2 changes: 1 addition & 1 deletion fastf1/tests/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_slice_by_lap(reference_laps_data):
session, laps = reference_laps_data
drv = list(session.car_data.keys())[1] # some driver
test_data = session.car_data[drv]
test_laps = session.pick_driver(drv)
test_laps = laps.pick_driver(drv)

lap2 = test_laps[test_laps['LapNumber'] == 2].iloc[0]
lap3 = test_laps[test_laps['LapNumber'] == 3].iloc[0]
Expand Down

0 comments on commit a58b05d

Please sign in to comment.