Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing 'testthat' functions #2987 #3067

Merged
merged 43 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e1a02a5
Replacing testthat::expect_true with assert_that
Nov 15, 2022
738c741
Merge branch 'develop' into replace_testthat
Its-Maniaco Nov 15, 2022
d74af88
Replacing testthat::expect_true with assert_that
Nov 15, 2022
0a3ffe0
replaced testthat with logger
Nov 30, 2022
173242d
replaced testthat with logger
Nov 30, 2022
a5e2ffb
Update check_met_input.R
Its-Maniaco Nov 30, 2022
b3bd5db
Update check_met_input.R
Its-Maniaco Nov 30, 2022
c3a0e83
Replace testthat in test_dims function
Dec 13, 2022
eb7bb3f
Merge branch 'replace_testthat' of https://github.com/Its-Maniaco/pec…
Dec 13, 2022
af46b79
Moved testthat to Suggests
Dec 13, 2022
e26b17d
Merge branch 'develop' into replace_testthat
infotroph Jan 10, 2023
f05e0cd
Merge branch 'develop' into replace_testthat
Its-Maniaco Jan 17, 2023
288cab2
Merge branch 'develop' of https://github.com/Its-Maniaco/pecan into r…
Jan 18, 2023
8fdcb40
replaced validate_that with assert_that
Jan 18, 2023
bc3edc6
Merge branch 'replace_testthat' of https://github.com/Its-Maniaco/pec…
Jan 18, 2023
4a5636d
Merge branch 'develop' into replace_testthat
Aariq Jan 21, 2023
98340b5
Merge branch 'develop' into replace_testthat
infotroph Feb 6, 2023
fded356
Merge branch 'develop' into replace_testthat
Aariq Feb 10, 2023
feb3f08
Updated Documentation
Feb 12, 2023
e846223
Merge branch 'replace_testthat' of https://github.com/Its-Maniaco/pec…
Feb 12, 2023
f8a85ec
Update modules/data.atmosphere/DESCRIPTION
Its-Maniaco Feb 23, 2023
0cc7d6b
Merge branch 'develop' into replace_testthat
Its-Maniaco Feb 23, 2023
82382d0
Update modules/data.atmosphere/R/check_met_input.R
Aariq Mar 30, 2023
4698d61
Merge branch 'develop' into replace_testthat
Aariq Mar 30, 2023
15d12b8
Merge branch 'develop' into replace_testthat
infotroph Apr 5, 2023
f46c7ee
Merge branch 'develop' into replace_testthat
infotroph May 4, 2023
4d0fcd6
Merge branch 'develop' into replace_testthat
infotroph May 6, 2023
41eefc2
Merge branch 'develop' into replace_testthat
mdietze Jun 12, 2023
b2cee25
Merge branch 'develop' into replace_testthat
infotroph Nov 24, 2023
320eb1e
Merge branch 'PecanProject:develop' into replace_testthat
Its-Maniaco Dec 1, 2023
afa6a3b
Merge branch 'develop' into replace_testthat
infotroph Dec 2, 2023
793ae8b
Merge branch 'develop' into replace_testthat
infotroph Dec 23, 2023
eb65ede
Merge branch 'develop' into replace_testthat
mdietze Jan 18, 2024
9af94f6
Merge branch 'develop' into replace_testthat
infotroph Feb 21, 2024
45c9629
Merge branch 'develop' into replace_testthat
infotroph Mar 17, 2024
a86ac5c
Merge branch 'develop' into replace_testthat
infotroph Mar 26, 2024
8d80b9e
Merge branch 'develop' into replace_testthat
infotroph Apr 9, 2024
3272aa4
update deps
infotroph Apr 10, 2024
1963b3c
Merge branch 'develop' into replace_testthat
infotroph May 10, 2024
9f24aa4
simplify assertion logic to avoid try boilerplate
infotroph May 16, 2024
f2fa36d
Merge branch 'develop' into replace_testthat
infotroph May 16, 2024
ce38f80
these vars were defined twice
infotroph May 16, 2024
443c941
typo
infotroph May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docker/depends/pecan_package_dependencies.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"amerifluxr","*","modules/data.atmosphere","Imports",FALSE
"arrow","*","modules/data.atmosphere","Imports",FALSE
"assertthat","*","models/ed","Imports",FALSE
"assertthat","*","modules/data.atmosphere","Imports",FALSE
"BayesianTools","*","modules/assim.batch","Imports",FALSE
"BayesianTools","*","modules/rtm","Imports",FALSE
"BioCro","*","models/biocro","Suggests",FALSE
Expand Down Expand Up @@ -595,7 +596,7 @@
"testthat",">= 2.0.0","base/utils","Suggests",FALSE
"testthat",">= 2.0.0","models/biocro","Suggests",FALSE
"testthat",">= 2.0.0","modules/benchmark","Suggests",FALSE
"testthat",">= 2.0.0","modules/data.atmosphere","Imports",FALSE
"testthat",">= 2.0.0","modules/data.atmosphere","Suggests",FALSE
"testthat",">= 3.0.0","models/sibcasa","Suggests",FALSE
"testthat",">= 3.0.4","base/qaqc","Suggests",FALSE
"tibble","*","base/db","Imports",FALSE
Expand Down
3 changes: 2 additions & 1 deletion modules/data.atmosphere/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Description: The Predictive Ecosystem Carbon Analyzer (PEcAn) is a scientific
Imports:
abind (>= 1.4.5),
amerifluxr,
assertthat,
arrow,
curl,
dplyr (>= 0.8.1),
Expand Down Expand Up @@ -50,7 +51,6 @@ Imports:
stringr (>= 1.1.0),
suntools,
terra,
testthat (>= 2.0.0),
tibble,
tidyr,
tidyselect,
Expand All @@ -68,6 +68,7 @@ Suggests:
PEcAn.settings,
progress,
reticulate,
testthat (>= 2.0.0),
withr
Remotes:
github::adokter/suntools,
Expand Down
60 changes: 26 additions & 34 deletions modules/data.atmosphere/R/check_met_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,32 @@ check_met_input_file <- function(metfile,

nc <- ncdf4::nc_open(metfile)

dimensions <- nc[["dim"]]
time_regex <- paste0(
"^(seconds|minutes|hours|hours|days) since ",
"[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}",
"T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}Z$"
)

try2 <- purrr::partial(try, silent = TRUE)

dimensions <- nc[["dim"]]
time_regex <- paste0(
"^(seconds|minutes|hours|hours|days) since ",
"[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}",
"T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}Z$"
)
test_dims <- list(
try2(testthat::expect_type(dimensions, "list")),
try2(testthat::expect_equal(length(dimensions), 3)),
try2(testthat::expect_true("time" %in% names(dimensions))),
try2(testthat::expect_match(
ncdf4::ncatt_get(nc, "time", "units")[["value"]],
time_regex
)),
try2(testthat::expect_true("latitude" %in% names(dimensions))),
try2(testthat::expect_equal(
ncdf4::ncatt_get(nc, "latitude", "units")[["value"]],
"degrees_north"
)),
try2(testthat::expect_true("longitude" %in% names(dimensions))),
try2(testthat::expect_equal(
ncdf4::ncatt_get(nc, "longitude", "units")[["value"]],
"degrees_east"
))
assertthat::validate_that(is.list(dimensions)),
assertthat::validate_that(length(dimensions) == 3),
assertthat::validate_that("time" %in% names(dimensions)),
assertthat::validate_that(
grepl(time_regex, ncdf4::ncatt_get(nc, "time", "units")[["value"]])
),
assertthat::validate_that("latitude" %in% names(dimensions)),
assertthat::validate_that(
ncdf4::ncatt_get(nc, "latitude", "units")[["value"]] == "degrees_north"
),
assertthat::validate_that("longitude" %in% names(dimensions)),
assertthat::validate_that(
ncdf4::ncatt_get(nc, "longitude", "units")[["value"]] == "degrees_east"
)
)

dim_errors_lgl <- purrr::map_lgl(test_dims, inherits, "try-error")
# validate_that returns TRUE on success, character on error
dim_errors_lgl <- !purrr::map_lgl(test_dims, isTRUE)
dim_errors <- test_dims[dim_errors_lgl] %>%
purrr::map_chr(as.character) %>%
paste(collapse = "\n\n")

test_dims_summary <- tibble::tibble(
Expand All @@ -88,12 +76,17 @@ check_met_input_file <- function(metfile,
)
)

nonstring_to_missing <- function(x) {
if (is.character(x)) return(x)
NA_character_
}

test_var_units <- tibble::tibble(
test_type = "variable has correct units",
target_variable = nc_vars,
test_raw = purrr::map(nc_vars, check_unit, nc = nc, variable_table = variable_table),
test_passed = !purrr::map_lgl(.data$test_raw, inherits, "try-error"),
test_error_message = purrr::map_chr(.data$test_raw, purrr::possibly(as.character, NA_character_))
test_passed = purrr::map_lgl(.data$test_raw, isTRUE),
test_error_message = purrr::map_chr(.data$test_raw, nonstring_to_missing)
) %>% dplyr::select(-"test_raw")

results_df <- dplyr::bind_rows(test_dims_summary, test_required_vars, test_var_units)
Expand All @@ -118,8 +111,7 @@ check_unit <- function(variable, nc, variable_table, warn_unknown = TRUE) {
dplyr::filter(.data$cf_standard_name == variable) %>%
dplyr::pull(units)
ncvar_unit <- ncdf4::ncatt_get(nc, variable, "units")[["value"]]
try(testthat::expect_true(
PEcAn.utils::units_are_equivalent(ncvar_unit, var_correct_unit),
glue::glue("NetCDF unit '{ncvar_unit}' not equivalent to expected unit '{var_correct_unit}'.")
))
eq_units <- PEcAn.utils::units_are_equivalent(ncvar_unit, var_correct_unit)

eq_units || glue::glue("NetCDF unit '{ncvar_unit}' not equivalent to expected unit '{var_correct_unit}'.")
}
Loading