Skip to content

Commit

Permalink
Upgrade tests to use testthat edition 3
Browse files Browse the repository at this point in the history
- close #191

- remove defunct functions, e.g., `context()`
- update deprecated functions, e.g., `expect_equivalent()` -> `expect_equal()`
- handle cases where functions issue multiple warnings/messages but testthat is now capturing only the first warning/message
  • Loading branch information
dschlaep committed Dec 13, 2022
1 parent 3a2a5b8 commit 5097833
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 197 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Suggests:
rSW2exter (>= 0.1.0),
soilDB,
spelling (>= 2.1.0),
testthat
testthat (>= 3.0.0)
Remotes:
github::DrylandEcology/rSW2utils,
github::DrylandEcology/rSW2data,
Expand All @@ -46,6 +46,7 @@ URL: https://github.com/DrylandEcology/rSOILWAT2
BugReports: https://github.com/DrylandEcology/rSOILWAT2/issues
Encoding: UTF-8
RoxygenNote: 7.2.2
Config/testthat/edition: 3
LazyData: true
Language: en-US
VignetteBuilder: knitr
2 changes: 0 additions & 2 deletions tests/testthat/test_DataAccess.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("rSOILWAT2 data access functions")


#--- Tests
test_that("set_requested_flags:", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_OutputDerived.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Derived output functions")


# Tests
test_that("Derived output: transpiration and evaporation", {
Expand Down
27 changes: 15 additions & 12 deletions tests/testthat/test_SegmentationFault_v3.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("rSOILWAT2 segfault")

# nolint start: line_length_linter, commented_code_linter.
# Running from command line --> segfaults
Expand All @@ -15,20 +14,24 @@ context("rSOILWAT2 segfault")
#gctorture(FALSE) #https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-gctorture
# nolint end

# Create reference objects
path_demo <- system.file("extdata", "example1", package = "rSOILWAT2")

a <- sw_inputDataFromFiles(dir = path_demo,
files.in = file.path(path_demo, "files.in"))
test_that("Test for segfault", {
# Create reference objects
path_demo <- system.file("extdata", "example1", package = "rSOILWAT2")

a <- sw_inputDataFromFiles(
dir = path_demo,
files.in = file.path(path_demo, "files.in")
)

# Location of segfaults with upper bound of b
# - (eo/debug/!interactive()): step 2 >= 558151 > step 3 >= 558105 >
# step 4 >= 555193 > no segfault
b <- 1:1e6
b2 <- 1:1e7
b3 <- 1:1e7

# Location of segfaults with upper bound of b
# - (eo/debug/!interactive()): step 2 >= 558151 > step 3 >= 558105 >
# step 4 >= 555193 > no segfault
b <- 1:1e6
b2 <- 1:1e7
b3 <- 1:1e7

test_that("Test for segfault", {
expect_silent({
elems <- ls(envir = .GlobalEnv)
if (length(elems) > 0) for (i in seq_along(elems)) x <- get(elems[i])
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_Soils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Manipulate soil")


test_that("Manipulate soils", {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test_Vegetation.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Vegetation functions")

# Inputs
utils::data("weatherData", package = "rSOILWAT2")
Expand Down Expand Up @@ -339,7 +338,7 @@ test_that("Vegetation: estimate land cover composition", {
)

expect_pnv(pnv[1:2])
expect_equivalent(pnv[["Rel_Abundance_L0"]][ibar], 1)
expect_equal(pnv[["Rel_Abundance_L0"]][ibar], 1, ignore_attr = "names")

# Make sure `SOILWAT2` throws a warning that R, we use `sw_verbosity()`
# to do that
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test_WaterBalance.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("rSOILWAT2 water balance")

# The 8 checks, implemented below, correspond to the checks in
# \var{SOILWAT/test/test_WaterBalance.cc}
Expand All @@ -13,7 +12,10 @@ dir_test_data <- file.path("..", "test_data")
temp <- list.files(dir_test_data, pattern = "Ex")
temp <- sapply(strsplit(temp, "_", fixed = TRUE), function(x) x[[1]])
tests <- unique(temp)
test_that("Test data availability", expect_gt(length(tests), 0))
test_that("Test data availability", {
expect_gt(length(tests), 0)
})


aggregate_for_each_timestep <- function(x, dyt) {
nid <- 1:2
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_WeatherData.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("rSOILWAT2 weather data")


test_that("Weather data check", {
Expand Down
90 changes: 50 additions & 40 deletions tests/testthat/test_WeatherGenerator_functionality.R
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
context("rSOILWAT2 weather generator")

#---INPUTS
path_extdata <- file.path("..", "..", "inst", "extdata")
if (!dir.exists(path_extdata)) {
path_extdata <- system.file("extdata", package = "rSOILWAT2")
}


dir_test_data <- file.path("..", "test_data")
temp <- list.files(dir_test_data, pattern = "Ex")
temp <- sapply(strsplit(temp, "_", fixed = TRUE), function(x) x[[1]])
tests <- unique(temp)
test_that("Test data availability", expect_gt(length(tests), 0))
test_that("Test data availability", {
expect_gt(length(tests), 0)
})

sw_weather <- lapply(
tests,
function(it) readRDS(file.path(dir_test_data, paste0(it, "_weather.rds")))
)


#---TESTS


test_that("Weather generator: estimate input parameters", {
for (k in seq_along(tests)) {
test_dat <- sw_weather[[k]]
test_dat <- readRDS(
file.path(dir_test_data, paste0(tests[k], "_weather.rds"))
)

test_df <- data.frame(dbW_weatherData_to_dataframe(test_dat, valNA = NULL))

if (anyNA(test_df)) {
expect_warning(
res <- dbW_estimate_WGen_coefs(
test_df,
propagate_NAs = TRUE
),
"Insufficient weather data to estimate values"
# We have NAs that propagate
# --> warnings: "Insufficient weather data to estimate values [...]"
res <- suppressWarnings(
dbW_estimate_WGen_coefs(test_df, propagate_NAs = TRUE)
)
expect_true(all(is.na(res[["mkv_woy"]][, -1])))
expect_true(all(is.na(res[["mkv_doy"]][, -1])))

expect_message(
res <- dbW_estimate_WGen_coefs(
# We have NAs that we impute
# --> messages: "Impute missing [...]"
res <- suppressMessages(
dbW_estimate_WGen_coefs(
test_df,
propagate_NAs = FALSE,
imputation_type = "mean"
),
"Impute missing"
)
)

} else {
Expand Down Expand Up @@ -70,25 +63,31 @@ test_that("Weather generator: estimate input parameters", {

test_that("Weather generator: generate weather", {
for (k in seq_along(tests)) {
test_dat <- sw_weather[[k]]
test_dat <- readRDS(
file.path(dir_test_data, paste0(tests[k], "_weather.rds"))
)
years <- get_years_from_weatherData(test_dat)
n <- length(test_dat)

wout <- list()

# Case 1: generate weather for dataset and impute missing values
wout[[1]] <- dbW_generateWeather(
test_dat,
imputation_type = "mean",
imputation_span = 5
wout[[1]] <- suppressMessages(
dbW_generateWeather(
test_dat,
imputation_type = "mean",
imputation_span = 5
)
)

# Case 2: generate weather based on partial dataset,
# use estimated weather generator coefficients from full dataset
wgen_coeffs <- dbW_estimate_WGen_coefs(
test_dat,
imputation_type = "mean",
imputation_span = 5
wgen_coeffs <- suppressMessages(
dbW_estimate_WGen_coefs(
test_dat,
imputation_type = "mean",
imputation_span = 5
)
)

wout[[2]] <- dbW_generateWeather(
Expand Down Expand Up @@ -143,6 +142,8 @@ test_that("Weather generator (integration tests): compare input/output", {
)
)

tag <- "IntegrationTest-WeatherGenerator"

dir_inttests <- file.path("..", "rSOILWAT_IntegrationTestOutput")
dir.create(dir_inttests, showWarnings = FALSE)

Expand Down Expand Up @@ -197,12 +198,21 @@ test_that("Weather generator (integration tests): compare input/output", {


#--- Comparison
compare_weather(
ref_weather = obs_df,
weather = wgen_df,
N = N,
path = dir_inttests,
tag = "IntegrationTest-WeatherGenerator"
suppressMessages(
compare_weather(
ref_weather = obs_df,
weather = wgen_df,
N = N,
path = dir_inttests,
tag = tag
)
)

expect_length(
list.files(
path = dir_inttests,
pattern = tag
),
n = 4L
)
})
1 change: 0 additions & 1 deletion tests/testthat/test_class_swCarbon.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Carbon dioxide class")

#---TESTS
test_that("Manipulate swCarbon", {
Expand Down
45 changes: 23 additions & 22 deletions tests/testthat/test_class_swProd.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
context("Vegetation parameters class")

ids_VegType <- rSW2_glovars[["kSOILWAT2"]][["VegTypes"]]
names_VegTypes <- tolower(
gsub(
"SW_",
"",
names(rSW2_glovars[["kSOILWAT2"]][["VegTypes"]]),
fixed = TRUE
)
)

names_VegTypes2 <- sapply(
names_VegTypes,
function(x) {
if (endsWith(x, "s")) {
if (endsWith(x, "ss")) x else substr(x, 1, nchar(x) - 1)
} else {
x
}
}
)



#---TESTS
test_that("Manipulate 'swProd' class", {
ids_VegType <- rSW2_glovars[["kSOILWAT2"]][["VegTypes"]]
names_VegTypes <- tolower(
gsub(
"SW_",
"",
names(rSW2_glovars[["kSOILWAT2"]][["VegTypes"]]),
fixed = TRUE
)
)

names_VegTypes2 <- sapply(
names_VegTypes,
function(x) {
if (endsWith(x, "s")) {
if (endsWith(x, "ss")) x else substr(x, 1, nchar(x) - 1)
} else {
x
}
}
)


x <- new("swProd")
expect_s4_class(x, "swProd")

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test_class_swSite.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
context("Site parameters class")

dir_test_data <- file.path("..", "test_data")
temp <- list.files(dir_test_data, pattern = "Ex")
temp <- sapply(strsplit(temp, "_", fixed = TRUE), function(x) x[[1]])
tests <- unique(temp)

test_that("Test data availability", expect_gt(length(tests), 0))
test_that("Test data availability", {
expect_gt(length(tests), 0)
})


#---TESTS
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_climate_functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Calculations of climate variables")

# Inputs
weatherList_year1980 <- readRDS(
Expand Down
Loading

0 comments on commit 5097833

Please sign in to comment.