Skip to content

Commit

Permalink
Merge pull request #140 from njtierney/improve-testing-i139
Browse files Browse the repository at this point in the history
add more tests

Resolves #139
  • Loading branch information
njtierney authored Jan 17, 2025
2 parents 71ece03 + 9027153 commit 56c84fe
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 41 deletions.
13 changes: 5 additions & 8 deletions R/tar_terra_tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ set_window <- function(raster, window) {
#' )
#' }
tile_grid <- function(raster, ncol, nrow) {
check_is_integerish(ncol)
check_is_integerish(nrow)
template <- terra::rast(
x = terra::ext(raster),
ncol = ncol,
Expand All @@ -333,6 +335,8 @@ tile_grid <- function(raster, ncol, nrow) {
#' @export
#' @rdname tile_helpers
tile_blocksize <- function(raster, n_blocks_row = 1, n_blocks_col = 1) {
check_is_integerish(n_blocks_row)
check_is_integerish(n_blocks_col)
tile_ext <-
terra::getTileExtents(
raster,
Expand All @@ -349,14 +353,7 @@ tile_blocksize <- function(raster, n_blocks_row = 1, n_blocks_col = 1) {
#' @export
#' @rdname tile_helpers
tile_n <- function(raster, n) {
if (!rlang::is_integerish(n)) {
cli::cli_abort(
c(
"{.val {n}} must be an integer.",
"We see that {.val n} is: {n}"
)
)
}
check_is_integerish(n)
sq <- sqrt(n)
sq_round <- floor(sq)
quotient <- n/sq_round
Expand Down
75 changes: 46 additions & 29 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# helper for adding "geotargets." when options missing that.
geotargets_repair_option_name <- function(option_name) {
if (!startsWith(option_name, "geotargets.")) {
option_name <- paste0("geotargets.", option_name)
}
gsub("_", ".", tolower(option_name))
if (!startsWith(option_name, "geotargets.")) {
option_name <- paste0("geotargets.", option_name)
}
gsub("_", ".", tolower(option_name))
}

check_pkg_installed <- function(pkg, call = rlang::caller_env()) {
if (!requireNamespace(pkg, quietly = TRUE)) {
cli::cli_abort(
message = "package {.pkg {pkg}} is required",
call = call
)
}
if (!requireNamespace(pkg, quietly = TRUE)) {
cli::cli_abort(
message = "package {.pkg {pkg}} is required",
call = call
)
}
}

gdal_version <- function() {
numeric_version(terra::gdal(lib = "gdal"))
numeric_version(terra::gdal(lib = "gdal"))
}

check_gdal_shz <- function(min_version = "3.1",
call = rlang::caller_env()) {
if (gdal_version() < numeric_version(min_version)) {
cli::cli_abort(
message = c(
"Must have {.pkg GDAL} version {.val {min_version}} or greater",
"i" = "To write a {.val .shz} file requires GDAL version \\
if (gdal_version() < numeric_version(min_version)) {
cli::cli_abort(
message = c(
"Must have {.pkg GDAL} version {.val {min_version}} or greater",
"i" = "To write a {.val .shz} file requires GDAL version \\
{.val {min_version}} or greater"
),
call = call
)
}
),
call = call
)
}
}

get_gdal_available_driver_list <- function(driver_type) {
# get list of drivers available for writing depending on what the user's GDAL supports
drv <- terra::gdal(drivers = TRUE)
if (utils::packageVersion("terra") > "1.7-74") {
drv <- drv[drv[[driver_type]] & grepl("write", drv$can), ]
} else {
drv <- drv[drv$type == driver_type & grepl("write", drv$can), ]
}
drv
# get list of drivers available for writing depending on what the user's GDAL supports
drv <- terra::gdal(drivers = TRUE)
if (utils::packageVersion("terra") > "1.7-74") {
drv <- drv[drv[[driver_type]] & grepl("write", drv$can), ]
} else {
drv <- drv[drv$type == driver_type & grepl("write", drv$can), ]
}
drv
}

check_user_resources <- function(resources,
Expand All @@ -53,7 +53,24 @@ check_user_resources <- function(resources,
with {.fn tar_terra_rast}",
"We see in {.code names(resources)}:",
"{.val {names(resources)}}"
),
),
call = call
)
}
}

check_is_integerish <- function(x,
call = rlang::caller_env(),
arg = rlang::caller_arg(x)){

not_integerish <- !rlang::is_integerish(x)

if (not_integerish) {
cli::cli_abort(
c(
"{.arg {arg}} must be an integer.",
"We see that {.arg {arg}} is: {.val {x}}"
),
call = call
)
}
Expand Down
68 changes: 68 additions & 0 deletions tests/testthat/_snaps/tar-terra-sprc.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,40 @@
crs (first) : lon/lat WGS 84 (EPSG:4326)
names : raster_elevs, raster_elevs

---

Code
x[1]
Output
class : SpatRaster
dimensions : 90, 95, 1 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : raster_elevs
name : elevation
min value : 141
max value : 547
unit : m
time (days) : 2025-01-15

---

Code
x[2]
Output
class : SpatRaster
dimensions : 115, 114, 1 (nrow, ncol, nlyr)
resolution : 683.4048, 683.4048 (x, y)
extent : 1480982, 1558890, 5478149, 5556741 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=igh +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : raster_elevs
name : elevation
min value : 282.8344
max value : 1087.3800
unit : m
time (days) : 2025-01-15

# tar_terra_sds() works

Code
Expand All @@ -27,3 +61,37 @@
source(s) : raster_elevs
names : raster_elevs, raster_elevs

---

Code
x[1]
Output
class : SpatRaster
dimensions : 90, 95, 1 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : raster_elevs
name : elevation
min value : 141
max value : 547
unit : m
time (days) : 2025-01-15

---

Code
x[2]
Output
class : SpatRaster
dimensions : 90, 95, 1 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : raster_elevs
name : elevation
min value : 282
max value : 1094
unit : m
time (days) : 2025-01-15

61 changes: 59 additions & 2 deletions tests/testthat/_snaps/tile-funs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
tile_n(r, n = 3.14)
Condition
Error in `tile_n()`:
! 3.14 must be an integer.
We see that "n" is: 3.14
! `n` must be an integer.
We see that `n` is: 3.14

---

Expand All @@ -31,3 +31,60 @@
6.141667 6.533333 49.441667 49.816667

# tile_grid fails with non integer

Code
tile_grid(r, ncol = 1.5, nrow = 2.5)
Condition
Error in `tile_grid()`:
! `ncol` must be an integer.
We see that `ncol` is: 1.5

---

Code
tile_grid(r, ncol = 1, nrow = 2.5)
Condition
Error in `tile_grid()`:
! `nrow` must be an integer.
We see that `nrow` is: 2.5

---

Code
tile_grid(r, ncol = 1.5, nrow = 2)
Condition
Error in `tile_grid()`:
! `ncol` must be an integer.
We see that `ncol` is: 1.5

---

Code
tile_grid(r, ncol = 2, nrow = 3)
Output
[[1]]
xmin xmax ymin ymax
5.741667 6.141667 49.941667 50.191667
[[2]]
xmin xmax ymin ymax
6.141667 6.533333 49.941667 50.191667
[[3]]
xmin xmax ymin ymax
5.741667 6.141667 49.691667 49.941667
[[4]]
xmin xmax ymin ymax
6.141667 6.533333 49.691667 49.941667
[[5]]
xmin xmax ymin ymax
5.741667 6.141667 49.441667 49.691667
[[6]]
xmin xmax ymin ymax
6.141667 6.533333 49.441667 49.691667

31 changes: 29 additions & 2 deletions tests/testthat/test-tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ targets::tar_test("tar_terra_sprc() works", {
)
targets::tar_script({
elev_scale <- function(z = 1, projection = "EPSG:4326") {
terra::project(
rast_elev_scale <- terra::project(
terra::rast(
system.file(
"ex",
Expand All @@ -16,6 +16,14 @@ targets::tar_test("tar_terra_sprc() works", {
) * z,
projection
)
# test to ensure metadata comes along.
# e.g., layer names, units, time, variables
# The snapshots for these tests do not include those metadata
# so this may be as easy as assigning some of these attributes in the
# test SPRC/SDS and adding those fields in the snapshot.
terra::units(rast_elev_scale) <- "m"
terra::time(rast_elev_scale) <- as.Date("2025-01-15")
rast_elev_scale
}
list(
geotargets::tar_terra_sprc(
Expand All @@ -33,6 +41,8 @@ targets::tar_test("tar_terra_sprc() works", {
x <- targets::tar_read(raster_elevs)
expect_s4_class(x, "SpatRasterCollection")
expect_snapshot(x)
expect_snapshot(x[1])
expect_snapshot(x[2])
})

targets::tar_test("tar_terra_sds() works", {
Expand All @@ -42,13 +52,24 @@ targets::tar_test("tar_terra_sds() works", {
)
targets::tar_script({
elev_scale <- function(z = 1) {
terra::rast(
rast_elev_scale <- terra::rast(
system.file(
"ex",
"elev.tif",
package = "terra"
)
) * z

# test to ensure metadata comes along.
# e.g., layer names, units, time, variables
# The snapshots for these tests do not include those metadata
# so this may be as easy as assigning some of these attributes in the
# test SPRC/SDS and adding those fields in the snapshot.
terra::units(rast_elev_scale) <- "m"
terra::varnames(rast_elev_scale) <- "elev"
terra::longnames(rast_elev_scale) <- "really-long-name"
terra::time(rast_elev_scale) <- as.Date("2025-01-15")
rast_elev_scale
}
list(
geotargets::tar_terra_sds(
Expand All @@ -65,6 +86,12 @@ targets::tar_test("tar_terra_sds() works", {
x <- targets::tar_read(raster_elevs)
expect_s4_class(x, "SpatRasterDataset")
expect_snapshot(x)
expect_snapshot(x[1])
expect_equal(terra::units(x[1]), "m")
expect_equal(terra::time(x[1]), as.Date("2025-01-15"))
expect_snapshot(x[2])
expect_equal(terra::units(x[2]), "m")
expect_equal(terra::time(x[2]), as.Date("2025-01-15"))
})

# difficult to test for this warning from tar_terra_sprc() because it doesn't end
Expand Down
Loading

0 comments on commit 56c84fe

Please sign in to comment.