tests/testthat/test-reference.R

# The payload of the case study is a fixed artefact. These digests are the ones
# the package produced when the case study was built, so a change to the
# encoder, to the transfer or to the manifest layout is caught here and not by a
# reader noticing that a page looks different.

REFERENCE <- list(created = "2026-01-01T00:00:00+00:00")

test_that("the case study builds the same payload every time", {
    skip_on_cran()
    e <- new.env()
    utils::data("med_mhw", package = "volumr", envir = e)
    a <- vol_payload(e$med_mhw, tempfile("pl"), created = REFERENCE$created,
                     display = list(vmin = 15L, vmax = 60L, cmap = "matter",
                                    gamma = 1))
    b <- vol_payload(e$med_mhw, tempfile("pl"), created = REFERENCE$created,
                     display = list(vmin = 15L, vmax = 60L, cmap = "matter",
                                    gamma = 1))
    for (n in a$files)
        expect_identical(
            digest::digest(file.path(a$dir, n), algo = "sha256", file = TRUE),
            digest::digest(file.path(b$dir, n), algo = "sha256", file = TRUE))
    expect_identical(a$manifest$nz, 21L)
    expect_identical(a$manifest$depths, seq(0, 250, by = 12.5))
})

test_that("the packaged field is the native payload field at every third point", {
    # The native payload records the transfer range of the full field; the
    # packaged field keeps every third point of the same grid, so its range lies
    # inside that one and its corners are corners of the native grid.
    e <- new.env()
    utils::data("med_mhw", package = "volumr", envir = e)
    f <- e$med_mhw
    man <- jsonlite::fromJSON(file.path(vol_example_payload(), "manifest.json"),
                              simplifyVector = FALSE)
    expect_identical(f$source$step, 3L)
    expect_identical(dim(f$values), c(15L, 127L, 339L))
    expect_equal(f$lon[1], man$lon0)
    expect_equal(f$lat[1], man$lat0)
    # the native coordinates were stored in single precision
    expect_equal(f$lon[2] - f$lon[1], 3 * (man$lon1 - man$lon0) / (man$nx - 1L),
                 tolerance = 1e-6)
    r <- range(f$values, na.rm = TRUE)
    expect_gte(r[1], man$transfer$lo)
    expect_lte(r[2], man$transfer$hi)
})

test_that("the shipped payload matches the digests in its manifest", {
    dir <- vol_example_payload()
    man <- jsonlite::fromJSON(file.path(dir, "manifest.json"), simplifyVector = FALSE)
    files <- c(man$slices, list(man$land, man$colormap_file))
    for (s in files)
        expect_identical(digest::digest(file.path(dir, s$file), algo = "sha256",
                                        file = TRUE), s$sha256)
})

test_that("the packaged field carries its provenance", {
    e <- new.env()
    utils::data("med_mhw", package = "volumr", envir = e)
    src <- e$med_mhw$source
    expect_identical(src$product, "MEDSEA_MULTIYEAR_PHY_006_004")
    expect_identical(src$reference_period, "1993-2022")
    expect_identical(length(src$sha256), 15L)
    expect_true(all(nchar(src$sha256) == 64L))
})

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.