Skip to content

Commit

Permalink
Merge pull request #125 from njtierney/add-tile-tests
Browse files Browse the repository at this point in the history
Add more testing
  • Loading branch information
njtierney authored Nov 29, 2024
2 parents 1b7be73 + 32952a3 commit 3f8f1da
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 62 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
^doc$
^Meta$
^codemeta\.json$
^\.covrignore$
1 change: 1 addition & 0 deletions .covrignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
R/release_bullets.R
4 changes: 1 addition & 3 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ tar_terra_rast <- function(name,
preserve_metadata <- rlang::arg_match0(preserve_metadata, c("drop", "zip"))

# ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_rast}")
}
check_user_resources(resources)

name <- targets::tar_deparse_language(substitute(name))

Expand Down
8 changes: 2 additions & 6 deletions R/tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ tar_terra_sprc <- function(name,
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")) {
# ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_sprc}")
}
check_user_resources(resources)

gdal <- gdal %||% character(0)
filetype <- filetype %||% "GTiff"
Expand Down Expand Up @@ -196,9 +194,7 @@ tar_terra_sds <- function(name,
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")) {
# ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_sprc}")
}
check_user_resources(resources)

gdal <- gdal %||% character(0)
filetype <- filetype %||% "GTiff"
Expand Down
4 changes: 1 addition & 3 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ tar_terra_vect <- function(name,
}

# ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_vect}")
}
check_user_resources(resources)

name <- targets::tar_deparse_language(substitute(name))

Expand Down
7 changes: 6 additions & 1 deletion R/tar_terra_tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,12 @@ tile_blocksize <- function(raster, n_blocks_row = 1, n_blocks_col = 1) {
#' @rdname tile_helpers
tile_n <- function(raster, n) {
if (!rlang::is_integerish(n)) {
rlang::abort("`n` must be an integer.")
cli::cli_abort(
c(
"{.val {n}} must be an integer.",
"We see that {.val n} is: {n}"
)
)
}
sq <- sqrt(n)
sq_round <- floor(sq)
Expand Down
19 changes: 13 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ get_gdal_available_driver_list <- function(driver_type) {
drv
}

semicolon_split <- function(env_vars) {
strsplit(env_vars, ";")[[1]]
}

semicolon_paste <- function(vec) {
paste0(vec, collapse = ";")
check_user_resources <- function(resources,
call = rlang::caller_env()){
if ("custom_format" %in% names(resources)) {
cli::cli_abort(
message = c(
"{.val custom_format} cannot be supplied to targets created \\
with {.fn tar_terra_rast}",
"We see in {.code names(resources)}:",
"{.val {names(resources)}}"
),
call = call
)
}
}
32 changes: 16 additions & 16 deletions man/geotargets-options.Rd

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

26 changes: 13 additions & 13 deletions man/tar_stars.Rd

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

26 changes: 13 additions & 13 deletions man/tar_terra_rast.Rd

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

2 changes: 1 addition & 1 deletion man/tar_terra_sds.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/check-user-resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# check_user_resources works

Code
check_user_resources(resources = c(custom_format = 1))
Condition
Error:
! "custom_format" cannot be supplied to targets created with `tar_terra_rast()`
We see in `names(resources)`:
"custom_format"

33 changes: 33 additions & 0 deletions tests/testthat/_snaps/tile-funs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# tile_n fails with non integer

Code
tile_n(r, n = 3.14)
Condition
Error in `tile_n()`:
! 3.14 must be an integer.
We see that "n" is: 3.14

---

Code
tile_n(r, n = 4)
Message
creating 2 * 2 = 4 tile extents
Output
[[1]]
xmin xmax ymin ymax
5.741667 6.141667 49.816667 50.191667
[[2]]
xmin xmax ymin ymax
6.133333 6.533333 49.816667 50.191667
[[3]]
xmin xmax ymin ymax
5.741667 6.141667 49.441667 49.816667
[[4]]
xmin xmax ymin ymax
6.133333 6.533333 49.441667 49.816667

9 changes: 9 additions & 0 deletions tests/testthat/test-check-user-resources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("check_user_resources works", {
expect_snapshot(
error = TRUE,
check_user_resources(resources = c("custom_format" = 1))
)
expect_silent(
check_user_resources(resources = c("anything else" = 1))
)
})
12 changes: 12 additions & 0 deletions tests/testthat/test-tile-funs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("tile_n fails with non integer", {
f <- system.file("ex/elev.tif", package = "terra")
r <- terra::rast(f)
expect_snapshot(
error = TRUE,
tile_n(r, n = 3.14)
)
skip_on_ci()
expect_snapshot(
tile_n(r, n = 4)
)
})

0 comments on commit 3f8f1da

Please sign in to comment.