Skip to content

Commit

Permalink
Merge pull request #261 from pbulsink/test-updates
Browse files Browse the repository at this point in the history
Test updates
  • Loading branch information
SCasanova authored Aug 14, 2024
2 parents 142845b + a8f22a3 commit af14c96
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 49 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: f1dataR
Title: Access Formula 1 Data
Version: 1.5.3
Version: 1.5.3.9000
Authors@R: c(
person("Santiago", "Casanova", , "[email protected]", role = c("aut", "cre", "cph")),
person("Philip", "Bulsink", , "[email protected]", role = "aut",
Expand Down Expand Up @@ -43,7 +43,6 @@ Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0),
usethis
VignetteBuilder: knitr
URL: https://scasanova.github.io/f1dataR/, https://github.com/SCasanova/f1dataR
BugReports: https://github.com/SCasanova/f1dataR/issues
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# f1dataR dev
* Removed `usethis` from Suggests (only needed when changing data)
* Updated tests to avoid API failure for 2022 season & better skip-on-CRAN for internet resources

# f1dataR 1.5.3
* Enabled Sprint Qualifying "SQ" as a session type in line with FastF1 v3.3.5.

Expand Down
9 changes: 8 additions & 1 deletion R/load_race_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ load_race_session <- function(obj_name = "session", season = get_current_season(

# fastf1.get_session([args]) runs even if there's no internet connection,
# the schedule of sessions is built into the package
session <- reticulate::py_run_string(py_string)
tryCatch(
session <- reticulate::py_run_string(py_string),
error = function(e) {
cli::cli_abort(c("Error loading FastF1 session.",
"x" = as.character(e)
))
}
)

# Check for fastf1 (F1timing/internet) connection
status <- check_ff1_network_connection(session$session$api_path)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-clear_f1_cache.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("Cache Clearing works for memoised functions to file", {
skip_if_no_ergast()
# Note: cache clearing for fastf1 is not our responsibility, it's performed
# by a call to fastf1 itself.

Expand All @@ -19,6 +20,7 @@ test_that("Cache Clearing works for memoised functions to file", {


test_that("load_ciruits (off cache) works", {
skip_if_no_ergast()
# Set testing specific parameters - this disposes after the test finishes
change_cache("off", persist = FALSE)

Expand All @@ -31,6 +33,8 @@ test_that("load_ciruits (off cache) works", {


test_that("load_ciruits (memory cache) works", {
skip_if_no_ergast()

# Set testing specific parameters - this disposes after the test finishes
withr::local_options("f1dataR.cache" = NULL)
change_cache("memory", persist = TRUE)
Expand All @@ -51,6 +55,8 @@ test_that("load_ciruits (bad path cache) works", {


test_that("load_ciruits (filesystem cache) works", {
skip_if_no_ergast()

# Set testing specific parameters - this disposes after the test finishes
withr::local_options("f1dataR.cache" = NULL)
change_cache(cache = "filesystem")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-load_circuit_details.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("load circuit details works", {
testthat::skip_if_offline("livetiming.formula1.com")
skip_if_no_py()
skip_if_no_ff1()

Expand Down
27 changes: 14 additions & 13 deletions tests/testthat/test-load_driver_telemetry.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("driver telemetry", {
testthat::skip_if_offline("livetiming.formula1.com")
skip_if_no_py()
skip_if_no_ff1()

Expand All @@ -17,46 +18,46 @@ test_that("driver telemetry", {
ff1_ver <- get_fastf1_version()
if (ff1_ver < "3.1") {
expect_error(
telem <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = "all"),
telem <- load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = "all"),
"An old version of FastF1 is in use"
)
skip("Skipping load_driver_telemetry tests as FastF1 is out of date.")
}

telem <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = "all")
telem_fast <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = "fastest")
telem <- load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = "all")
telem_fast <- load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = "fastest")

expect_true(nrow(telem) > nrow(telem_fast))
expect_true(ncol(telem) == ncol(telem_fast))

expect_equal(telem_fast$session_time[[1]], 3518.641)
expect_equal(round(telem_fast$time[[2]], 3), 0.086)
expect_equal(telem_fast$session_time[[1]], 3517.868)
expect_equal(round(telem_fast$time[[2]], 3), 0.082)

telem_lap <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = 1)
telem_lap <- load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = 1)
expect_equal(telem_lap$time[[1]], 0)
expect_equal(telem_lap$speed[[1]], 0)
expect_error(
load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = 1.5),
load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = 1.5),
"* must be one of `fastest`, `all` or an integer value"
)

expect_error(
load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", fastest_only = TRUE),
load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", fastest_only = TRUE),
"The `fastest_only` argument of `load_driver_telemetry\\(\\)` was deprecated in f1dataR 1.4.0 and is now defunct.*"
)

expect_error(
load_driver_telemetry(season = 2022, race = "Brazil", session = "S", driver = "HAM"),
load_driver_telemetry(season = 2023, race = "Brazil", session = "S", driver = "HAM"),
"The `race` argument of `load_driver_telemetry\\(\\)` was deprecated in f1dataR 1.4.0 and is now defunct.*"
)

expect_error(
get_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM"),
get_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM"),
"`get_driver_telemetry\\(\\)` was deprecated in f1dataR 1.4.0 and is now defunct.*"
)

expect_error(
get_driver_telemetry(season = 2022, race = "Brazil", session = "S", driver = "HAM", fastest_only = TRUE),
get_driver_telemetry(season = 2023, race = "Brazil", session = "S", driver = "HAM", fastest_only = TRUE),
"`get_driver_telemetry\\(\\)` was deprecated in f1dataR 1.4.0 and is now defunct.*"
)
})
Expand Down Expand Up @@ -89,10 +90,10 @@ test_that("Load Driver Telemetry works without internet", {
suppressMessages({
httptest2::without_internet({
expect_message(
load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = "all"),
load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = "all"),
"f1dataR: Can't connect to F1 Live Timing for FastF1 data download"
)
expect_null(load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = "all"))
expect_null(load_driver_telemetry(season = 2023, round = "Brazil", session = "S", driver = "HAM", laps = "all"))
})
})
})
Expand Down
31 changes: 17 additions & 14 deletions tests/testthat/test-load_race_session.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("Load Session (file cached) Works", {
testthat::skip_if_offline("livetiming.formula1.com")
skip_if_no_py()
skip_if_no_ff1()

Expand All @@ -18,19 +19,19 @@ test_that("Load Session (file cached) Works", {
ff1_ver <- get_fastf1_version()
if (ff1_ver < "3.1") {
expect_error(
session <- load_race_session(season = 2022, round = 1),
session <- load_race_session(season = 2023, round = 1),
"An old version of FastF1 is in use"
)
skip("Skipping load_race_session as FastF1 is out of date.")
}

# test with all parameters but session provided
expect_invisible(load_race_session(season = 2022, round = 1))
expect_invisible(load_race_session(season = 2023, round = 1))
# validate the cache is there now
expect_true(file.exists(file.path(tempdir(), "tst_session", "fastf1_http_cache.sqlite")))

# test without race provided - loads from cache
expect_invisible(load_race_session(season = 2022, session = "R"))
expect_invisible(load_race_session(season = 2023, session = "R"))

# test without season provided - default is current year assigned at argument
session1 <- load_race_session(session = "R")
Expand All @@ -39,10 +40,10 @@ test_that("Load Session (file cached) Works", {
expect_equal(session1$api_path, session2$api_path)

# verify character and numeric race can draw the same endpoint
session1 <- load_race_session(season = 2022, round = 1, session = "R")
session2 <- load_race_session(season = 2022, round = "Bahrain", session = "R")
session1 <- load_race_session(season = 2023, round = 1, session = "R")
session2 <- load_race_session(season = 2023, round = "Bahrain", session = "R")
expect_equal(session1$api_path, session2$api_path)
expect_equal(session1$event$OfficialEventName, "FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2022")
expect_equal(session1$event$OfficialEventName, "FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2023")

expect_error(
load_race_session(season = 2017),
Expand All @@ -52,12 +53,14 @@ test_that("Load Session (file cached) Works", {
load_race_session(session = "ZZZ"),
'`session` must be one of "FP1", "FP2", "FP3", "Q", "SQ", "SS", "S", or "R"'
)
expect_error(load_race_session(season = 2022, round = 1, session = "R", log_level = "ZZZ"))
expect_error(load_race_session(season = 2023, round = 1, session = "R", log_level = "ZZZ"))

expect_error(load_race_session(season = 2022, race = "Bahrain", session = "R"))
expect_error(load_race_session(season = 2023, race = "Bahrain", session = "R"))

expect_error(load_race_session(season = 2023, round = 100, session = "R"))

expect_message(
load_race_session(season = 2022, round = 1, session = "R", log_level = "INFO"),
load_race_session(season = 2023, round = 1, session = "R", log_level = "INFO"),
"The first time a session is loaded, some time is required. Please*"
)
})
Expand All @@ -77,14 +80,14 @@ test_that("Load Session (memory cached) Works", {
ff1_ver <- get_fastf1_version()
if (ff1_ver < "3.1") {
expect_error(
session <- load_race_session(season = 2022, round = 1),
session <- load_race_session(season = 2023, round = 1),
"An old version of FastF1 is in use"
)
skip("Skipping load_race_session (memory cache) test as FastF1 is out of date.")
}

session1 <- load_race_session(season = 2022, round = 1, session = "R")
expect_equal(session1$event$OfficialEventName, "FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2022")
session1 <- load_race_session(season = 2023, round = 1, session = "R")
expect_equal(session1$event$OfficialEventName, "FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2023")
})

test_that("Load Session works without internet", {
Expand Down Expand Up @@ -114,8 +117,8 @@ test_that("Load Session works without internet", {
suppressWarnings({
suppressMessages({
httptest2::without_internet({
expect_message(load_race_session(season = 2022, round = 1), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(load_race_session(season = 2022, round = 1))
expect_message(load_race_session(season = 2023, round = 1), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(load_race_session(season = 2023, round = 1))
})
})
})
Expand Down
19 changes: 10 additions & 9 deletions tests/testthat/test-load_session_laps.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("load session laps works", {
testthat::skip_if_offline("livetiming.formula1.com")
skip_if_no_py()
skip_if_no_ff1()

Expand All @@ -16,16 +17,16 @@ test_that("load session laps works", {
ff1_ver <- get_fastf1_version()
if (ff1_ver < "3.1") {
expect_error(
laps <- load_session_laps(season = 2022, round = "bahrain"),
laps <- load_session_laps(season = 2023, round = "bahrain"),
"An old version of FastF1 is in use"
)
skip("Skipping load_session_laps tests as FastF1 is out of date.")
}

laps <- load_session_laps(season = 2022, round = "bahrain")
laps2 <- load_session_laps(season = 2022, round = "bahrain", add_weather = TRUE)
lapsq <- load_session_laps(season = 2022, round = "bahrain", session = "Q")
lapsqw <- load_session_laps(season = 2022, round = "bahrain", session = "Q", add_weather = TRUE)
laps <- load_session_laps(season = 2023, round = "bahrain")
laps2 <- load_session_laps(season = 2023, round = "bahrain", add_weather = TRUE)
lapsq <- load_session_laps(season = 2023, round = "bahrain", session = "Q")
lapsqw <- load_session_laps(season = 2023, round = "bahrain", session = "Q", add_weather = TRUE)
lapssq <- load_session_laps(season = 2024, round = "china", session = "SQ")

expect_true("tbl" %in% class(laps))
Expand All @@ -38,13 +39,13 @@ test_that("load session laps works", {
expect_true(all(c("Q1", "Q2", "Q3") %in% unique(lapsq$session_type)))
expect_true(all(c("SQ1", "SQ2", "SQ3") %in% unique(lapssq$session_type)))
expect_true(!is.na(lapsq$time[1]))
expect_equal(min(lapsq$lap_time, na.rm = TRUE), 90.558)
expect_equal(min(lapsq$lap_time, na.rm = TRUE), 89.708)
expect_equal(nrow(lapsq), nrow(lapsqw))
expect_equal(min(lapsq$lap_time, na.rm = TRUE), min(lapsqw$lap_time, na.rm = TRUE))
expect_lt(ncol(lapsq), ncol(lapsqw))
expect_true("wind_speed" %in% colnames(lapsqw))

expect_error(load_session_laps(season = 2022, race = "bahrain", session = "Q"))
expect_error(load_session_laps(season = 2023, race = "bahrain", session = "Q"))
})

test_that("Load Session Laps works without internet", {
Expand Down Expand Up @@ -74,8 +75,8 @@ test_that("Load Session Laps works without internet", {
suppressWarnings({
suppressMessages({
httptest2::without_internet({
expect_message(load_session_laps(season = 2022, round = "bahrain"), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(load_session_laps(season = 2022, round = "bahrain"))
expect_message(load_session_laps(season = 2023, round = "bahrain"), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(load_session_laps(season = 2023, round = "bahrain"))
})
})
})
Expand Down
21 changes: 11 additions & 10 deletions tests/testthat/test-plot_fastest.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("graphics work", {
testthat::skip_if_offline("livetiming.formula1.com")
skip_if_no_py()
skip_if_no_ff1()

Expand All @@ -16,7 +17,7 @@ test_that("graphics work", {
# Ensure caught failure if no ggplot2, then skip remainder
if (!require("ggplot2", quietly = TRUE)) {
expect_error(
plot_fastest(season = 2022, round = 1, session = "R", driver = "HAM", color = "gear"),
plot_fastest(season = 2023, round = 1, session = "R", driver = "HAM", color = "gear"),
"f1dataR::plot_fastest() requires ggplot2 package installation"
)
testthat::skip("ggplot2 not available for testing graphics")
Expand All @@ -26,31 +27,31 @@ test_that("graphics work", {
ff1_ver <- get_fastf1_version()
if (ff1_ver < "3.1") {
expect_error(
session <- load_race_session(season = 2022, round = 1),
session <- load_race_session(season = 2023, round = 1),
"An old version of FastF1 is in use"
)
skip("Skipping graphics tests as FastF1 is out of date.")
}

# Check deprecation
expect_error(
plot_fastest(season = 2022, race = 1, session = "R", driver = "HAM", color = "gear"),
plot_fastest(season = 2023, race = 1, session = "R", driver = "HAM", color = "gear"),
"was deprecated in f1dataR"
)

# Snapshot Tests of graphics
suppressMessages({
suppressWarnings({
gear <- plot_fastest(2022, 1, "R", "HAM", "gear")
speed <- plot_fastest(2022, 1, "R", "HAM", "speed")
qualiplot <- plot_fastest(2022, 1, "Q", "HAM", "gear")
gear <- plot_fastest(2023, 1, "R", "HAM", "gear")
speed <- plot_fastest(2023, 1, "R", "HAM", "speed")
qualiplot <- plot_fastest(2023, 1, "Q", "HAM", "gear")
})
})
expect_equal(gear$label$title, speed$label$title)
expect_equal(gear$label$caption, "Generated by {f1dataR} package")
expect_equal(gear$label$subtitle, "HAM Fastest Lap | 1:36.228")
expect_equal(gear$label$subtitle, "HAM Fastest Lap | 1:36.546")

expect_equal(qualiplot$label$title, "2022 Bahrain Grand Prix Qualifying")
expect_equal(qualiplot$label$title, "2023 Bahrain Grand Prix Qualifying")

qp_axis <- qualiplot + theme_dark_f1(axis_marks = TRUE)

Expand Down Expand Up @@ -100,8 +101,8 @@ test_that("graphics works without internet", {
suppressWarnings({
suppressMessages({
httptest2::without_internet({
expect_message(plot_fastest(2022, 1, "R", "HAM", "gear"), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(plot_fastest(2022, 1, "R", "HAM", "gear"))
expect_message(plot_fastest(2023, 1, "R", "HAM", "gear"), "f1dataR: Can't connect to F1 Live Timing for FastF1 data download")
expect_null(plot_fastest(2023, 1, "R", "HAM", "gear"))
})
})
})
Expand Down

0 comments on commit af14c96

Please sign in to comment.