Skip to content

Commit

Permalink
add tests for sprc and sds to check that values are carried through
Browse files Browse the repository at this point in the history
  • Loading branch information
njtierney committed Jan 16, 2025
1 parent bb1bbeb commit f23e6bd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
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

33 changes: 31 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,16 @@ 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::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_sprc(
Expand All @@ -33,6 +43,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 +54,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 +88,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

0 comments on commit f23e6bd

Please sign in to comment.