From f23e6bdf07283d2fef13393e7c873bd0f00639e9 Mon Sep 17 00:00:00 2001 From: njtierney Date: Thu, 16 Jan 2025 15:31:37 +1100 Subject: [PATCH] add tests for sprc and sds to check that values are carried through --- tests/testthat/_snaps/tar-terra-sprc.md | 68 +++++++++++++++++++++++++ tests/testthat/test-tar-terra-sprc.R | 33 +++++++++++- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/tests/testthat/_snaps/tar-terra-sprc.md b/tests/testthat/_snaps/tar-terra-sprc.md index f29f87e..15f6bd5 100644 --- a/tests/testthat/_snaps/tar-terra-sprc.md +++ b/tests/testthat/_snaps/tar-terra-sprc.md @@ -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 @@ -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 + diff --git a/tests/testthat/test-tar-terra-sprc.R b/tests/testthat/test-tar-terra-sprc.R index 2debae9..4be6490 100644 --- a/tests/testthat/test-tar-terra-sprc.R +++ b/tests/testthat/test-tar-terra-sprc.R @@ -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", @@ -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( @@ -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", { @@ -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( @@ -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