Skip to content

Commit

Permalink
replace snapshot tests
Browse files Browse the repository at this point in the history
as they were troublemakers on CRAN. Instead test with local rds files.
  • Loading branch information
mrcaseb committed Dec 22, 2022
1 parent 90f6dcc commit dc1d776
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 396 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nflfastR 4.5.1

* New implementation of tests to be able to identify breaking changes in reverse dependencies
* New implementation of tests to be able to identify breaking changes in reverse dependencies (#396, #406)
* `calculate_standings()` no more freezes when computing standings from schedules where some games are missing results, i.e. upcoming games.
* Bug fix that caused problems with upcoming dplyr and tidyselect updates that weren't reverse compatible.
* Significant performance improvements of internal functions. (#402)
Expand Down
362 changes: 0 additions & 362 deletions tests/testthat/_snaps/build_nflfastR_pbp/pbp.csv

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/_snaps/ep_wp_calculators/ep.csv

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/_snaps/ep_wp_calculators/wp.csv

This file was deleted.

Binary file added tests/testthat/expected_ep.rds
Binary file not shown.
Binary file added tests/testthat/expected_pbp.rds
Binary file not shown.
Binary file added tests/testthat/expected_sc.rds
Binary file not shown.
Binary file added tests/testthat/expected_sc_weekly.rds
Binary file not shown.
Binary file added tests/testthat/expected_wp.rds
Binary file not shown.
37 changes: 35 additions & 2 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ pbp_cache <- tempfile("pbp_cache", fileext = ".rds")

load_test_pbp <- function(pbp = pbp_cache, dir = test_dir){
if (file.exists(pbp) && !is.null(dir)){
cli::cli_alert_info("Will return pbp from cache")
if(interactive()) cli::cli_alert_info("Will return pbp from cache")
return(readRDS(pbp))
}

g <- readRDS(file.path(test_dir, paste0("games.rds")))

# model output differs across machines so we round to 4 significant digits
# to prevent failing tests
pbp_data <- build_nflfastR_pbp(game_ids, dir = dir, games = g)
pbp_data <- build_nflfastR_pbp(game_ids, dir = dir, games = g) %>%
# we gotta round floating point numbers because of different model output
# across platforms
round_double_to_digits()
if(!is.null(dir)) saveRDS(pbp_data, pbp)
pbp_data
}
Expand All @@ -27,3 +30,33 @@ save_test_object <- function(object){
data.table::fwrite(modify_digits, tmp_file, na = "NA")
invisible(tmp_file)
}

load_expectation <- function(type = c("pbp", "sc", "sc_weekly", "ep", "wp"),
dir = test_dir){
type <- match.arg(type)
file_name <- switch (
type,
"pbp" = "expected_pbp.rds",
"sc" = "expected_sc.rds",
"sc_weekly" = "expected_sc_weekly.rds",
"ep" = "expected_ep.rds",
"wp" = "expected_wp.rds",
)
strip_nflverse_attributes(readRDS(file.path(dir, file_name))) %>%
# we gotta round floating point numbers because of different model output
# across platforms
round_double_to_digits()
}

# strip nflverse attributes for tests because timestamp and version cause failures
# .internal.selfref is a data.table attribute that is not necessary in this case
strip_nflverse_attributes <- function(df){
input_attrs <- names(attributes(df))
input_remove <- input_attrs[grepl("nflverse|.internal.selfref|nflfastR", input_attrs)]
attributes(df)[input_remove] <- NULL
df
}

round_double_to_digits <- function(df, digits = 4){
dplyr::mutate_if(df, is.double, signif, digits = digits)
}
10 changes: 6 additions & 4 deletions tests/testthat/test-build_nflfastR_pbp.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ test_that("build_nflfastR_pbp works (on CRAN)", {
# this test will run on everywhere, including CRAN. It uses locally available data
# so it can't break because of failed downloads
pbp <- load_test_pbp(dir = test_dir)

expect_snapshot_file(save_test_object(pbp), "pbp.csv", cran = TRUE)
expect_s3_class(pbp, "nflverse_data")
pbp <- strip_nflverse_attributes(pbp)
exp <- load_expectation("pbp")
expect_equal(pbp, exp)
})

test_that("build_nflfastR_pbp works (outside CRAN)", {
Expand All @@ -13,6 +14,7 @@ test_that("build_nflfastR_pbp works (outside CRAN)", {
skip_on_cran()
skip_if_offline("github.com")
pbp <- load_test_pbp(dir = NULL)
expect_snapshot_file(save_test_object(pbp), "pbp.csv", cran = TRUE)
expect_s3_class(pbp, "nflverse_data")
pbp <- strip_nflverse_attributes(pbp)
exp <- load_expectation("pbp")
expect_equal(pbp, exp)
})
13 changes: 9 additions & 4 deletions tests/testthat/test-calculate_series_conversion_rates.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
test_that("calculate_series_conversion_rates works", {
pbp <- load_test_pbp()

sc <- calculate_series_conversion_rates(pbp = pbp, weekly = FALSE)
sc_weekly <- calculate_series_conversion_rates(pbp = pbp, weekly = TRUE)
sc <- calculate_series_conversion_rates(pbp = pbp, weekly = FALSE) %>%
round_double_to_digits()
sc_weekly <- calculate_series_conversion_rates(pbp = pbp, weekly = TRUE) %>%
round_double_to_digits()

expect_snapshot_file(save_test_object(sc), "sc.csv", cran = TRUE)
expect_snapshot_file(save_test_object(sc_weekly), "sc_weekly.csv", cran = TRUE)
exp_sc <- load_expectation("sc")
exp_sc_weekly <- load_expectation("sc_weekly")

expect_s3_class(sc, "tbl_df")
expect_s3_class(sc_weekly, "tbl_df")

expect_equal(sc, exp_sc)
expect_equal(sc_weekly, exp_sc_weekly)
})
10 changes: 6 additions & 4 deletions tests/testthat/test-ep_wp_calculators.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ test_that("calculate_expected_points works", {
"posteam_timeouts_remaining" = 3,
"defteam_timeouts_remaining" = 3
)
ep <- calculate_expected_points(data)
expect_snapshot_file(save_test_object(ep), "ep.csv", cran = TRUE)
ep <- calculate_expected_points(data) %>% round_double_to_digits()
exp <- load_expectation("ep")
expect_equal(ep, exp)
})

test_that("calculate_expected_points works", {
Expand All @@ -30,6 +31,7 @@ test_that("calculate_expected_points works", {
"posteam_timeouts_remaining" = 3,
"defteam_timeouts_remaining" = 3
)
wp <- calculate_win_probability(data)
expect_snapshot_file(save_test_object(wp), "wp.csv", cran = TRUE)
wp <- calculate_win_probability(data) %>% round_double_to_digits()
exp <- load_expectation("wp")
expect_equal(wp, exp)
})

0 comments on commit dc1d776

Please sign in to comment.