Skip to content

Commit

Permalink
add tests for read_smips functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wasin Pipattungsakul committed Nov 20, 2024
1 parent 1c5d10f commit 0416696
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.DS_Store
.quarto
docs
.Renviron
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Authors@R: c(
comment = c(ORCID = "0000-0002-0061-8359")),
person("Russell", "Edson", , "russell.edson@adelaide.edu.au", role = "aut",
comment = c(ORCID = "0000-0002-4607-5396")),
person("Max", "Moldovan", , "max.moldovan@adelaide.edu.au", role = "ctb")
person("Max", "Moldovan", , "max.moldovan@adelaide.edu.au", role = "ctb"),
person("Wasin", "Pipattungsakul", , "wasin.pipattungsakul@adelaide.edu.au",
role = "aut")
)
Description: Provides access to Australia's TERN (Terrestrial Ecosystem
Research Network) data through the API, <https://tern.org.au>.
Expand Down
5 changes: 4 additions & 1 deletion R/read_smips.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ read_smips <- function(collection = "totalbucket",

while (attempt <= max_tries && !success)
tryCatch({
# TODO: move the url concatenation out
r <- (terra::rast(
paste0(
"/vsicurl/https://",
Expand Down Expand Up @@ -192,7 +193,7 @@ read_smips <- function(collection = "totalbucket",
"bucket1",
"bucket2",
"deepD",
"runnoff")
"runoff")
collection <- rlang::arg_match(.collection, approved_collections)

.check_collection_agreement(.collection = .collection, .day = .day)
Expand All @@ -211,4 +212,6 @@ read_smips <- function(collection = "totalbucket",
collection == "runoff",
paste0("smips_runoff_mm_", url_date, ".tif")
)

return(collection_url)
}
1 change: 1 addition & 0 deletions man/nert-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/testthat/test-.check_collection_agreement.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("A valid collection combination passes", {
expect_null(.check_collection_agreement("totalbucket", as.Date("2007-01-01")))
expect_null(.check_collection_agreement("some_collection", as.Date("2007-01-01")))
expect_null(.check_collection_agreement("some_collection", lubridate::today() - 7))
})

test_that("An invalid collection combination throws an error", {
last_week <- lubridate::today() - 7

error_msg <- paste0("The data are not available before 2005 and roughly much past ", last_week)
expect_error(
.check_collection_agreement("some_collection", lubridate::today()),
error_msg
)
expect_error(
.check_collection_agreement("totalbucket", as.Date("2004-01-01")),
error_msg
)
})
7 changes: 1 addition & 6 deletions tests/testthat/test-.check_not_example_api_key.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
test_that("An error is raised when the API key is the example string", {
# multiple dates
expect_error(
.check_not_example_api_key("your_api_key"),
"You have copied the example code and not provided a proper API key.
An API key may be requested from TERN to access this resource. Please
see the help file for {.fn get_key} for more information."
)
expect_error(.check_not_example_api_key("your_api_key"))
})
30 changes: 30 additions & 0 deletions tests/testthat/test-.make_smips_url.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test_that("A valid url part is created", {
date_ <- as.Date("2020-01-01")

# different collections
expect_equal(.make_smips_url("totalbucket", date_), "smips_totalbucket_mm_20200101.tif")
expect_equal(.make_smips_url("SMindex", date_), "smips_smi_perc_20200101.tif")
expect_equal(.make_smips_url("bucket1", date_), "smips_bucket1_mm_20200101.tif")
expect_equal(.make_smips_url("bucket2", date_), "smips_bucket2_mm_20200101.tif")
expect_equal(.make_smips_url("deepD", date_), "smips_deepD_mm_20200101.tif")

# different date
expect_equal(.make_smips_url("runoff", as.Date("2020-01-02")), "smips_runoff_mm_20200102.tif")
})

test_that("An invalid collection throws an error", {
valid_collections <- c(
"totalbucket",
"SMindex",
"bucket1",
"bucket2",
"deepD",
"runoff"
)
valid_collections_str <- paste(valid_collections[seq_along(valid_collections) - 1], collapse = '", "')
valid_collections_str <- paste0('"', valid_collections_str, '", "', tail(valid_collections, 1), '"')

# TODO: check error message
error_msg <- paste0("`.collection` must be one of ", valid_collections_str, ', not "asdf".')
expect_error(.make_smips_url("asdf", as.Date("2020-01-01")))
})

0 comments on commit 0416696

Please sign in to comment.