# Tests for zarrs_create_array — Rust-side array creation. # All tests gated on zarrs availability (skip on CRAN / pure-R tier). skip_if_no_zarrs <- function() { skip_if(!.pizzarr_env$zarrs_available, "zarrs backend not available") } # -- V3 creation tests ------------------------------------------------------- test_that("zarrs_create_array V3 float64 no compression", { skip_if_no_zarrs() d <- tempfile("zarrs_create_v3_none_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) meta <- zarrs_create_array(d, "", c(10L, 20L), c(5L, 10L), "float64", "none", 0.0, "{}", 3L) expect_equal(meta$shape, c(10L, 20L)) expect_equal(meta$chunks, c(5L, 10L)) expect_equal(meta$dtype, "float64") expect_equal(meta$r_type, "double") expect_equal(meta$zarr_format, 3L) # Metadata file should exist expect_true(file.exists(file.path(d, "zarr.json"))) zarrs_close_store(d) }) test_that("zarrs_create_array V3 float64 gzip roundtrip", { skip_if_no_zarrs() d <- tempfile("zarrs_create_v3_gzip_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) zarrs_create_array(d, "", 10L, 5L, "float64", "gzip", 0.0, "{}", 3L) # Write and read back zarrs_set_subset(d, "", list(c(0L, 10L)), as.double(1:10), NULL) result <- zarrs_get_subset(d, "", list(c(0L, 10L)), NULL) expect_equal(result$data, as.double(1:10)) zarrs_close_store(d) }) test_that("zarrs_create_array V3 int32 zstd roundtrip", { skip_if_no_zarrs() skip_if(!("zstd" %in% pizzarr_compiled_features()), "zstd not compiled") d <- tempfile("zarrs_create_v3_zstd_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) zarrs_create_array(d, "", 10L, 5L, "int32", "zstd", 0L, "{}", 3L) zarrs_set_subset(d, "", list(c(0L, 10L)), 1:10, NULL) result <- zarrs_get_subset(d, "", list(c(0L, 10L)), NULL) expect_equal(result$data, 1:10) zarrs_close_store(d) }) # -- V2 creation tests ------------------------------------------------------- test_that("zarrs_create_array V2 float64 no compression", { skip_if_no_zarrs() d <- tempfile("zarrs_create_v2_none_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) meta <- zarrs_create_array(d, "", c(10L, 20L), c(5L, 10L), "float64", "none", 0.0, "{}", 2L) expect_equal(meta$shape, c(10L, 20L)) expect_equal(meta$chunks, c(5L, 10L)) expect_equal(meta$dtype, "float64") expect_equal(meta$zarr_format, 2L) # V2 metadata file should exist expect_true(file.exists(file.path(d, ".zarray"))) zarrs_close_store(d) }) test_that("zarrs_create_array V2 float64 gzip roundtrip", { skip_if_no_zarrs() d <- tempfile("zarrs_create_v2_gzip_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) zarrs_create_array(d, "", 10L, 5L, "float64", "gzip", 0.0, "{}", 2L) zarrs_set_subset(d, "", list(c(0L, 10L)), as.double(1:10), NULL) result <- zarrs_get_subset(d, "", list(c(0L, 10L)), NULL) expect_equal(result$data, as.double(1:10)) zarrs_close_store(d) }) test_that("zarrs_create_array V2 zstd rejected", { skip_if_no_zarrs() d <- tempfile("zarrs_create_v2_zstd_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) expect_error( zarrs_create_array(d, "", 10L, 5L, "float64", "zstd", 0.0, "{}", 2L), "zstd.*zarr_format" ) zarrs_close_store(d) }) # -- All dtypes --------------------------------------------------------------- test_that("zarrs_create_array V3 supports all numeric dtypes", { skip_if_no_zarrs() dtypes <- c("float64", "float32", "int32", "int16", "int8", "uint8", "uint16", "uint32", "int64", "uint64", "bool") for (dt in dtypes) { d <- tempfile(paste0("zarrs_create_dtype_", dt, "_")) dir.create(d) meta <- zarrs_create_array(d, "", 10L, 5L, dt, "none", 0L, "{}", 3L) expect_equal(meta$dtype, dt, info = paste("dtype:", dt)) expect_true(meta$r_type %in% c("double", "integer", "logical"), info = paste("dtype:", dt)) zarrs_close_store(d) unlink(d, recursive = TRUE) } }) # -- End-to-end roundtrip ----------------------------------------------------- test_that("zarrs create + write + read full roundtrip", { skip_if_no_zarrs() d <- tempfile("zarrs_roundtrip_") dir.create(d) on.exit(unlink(d, recursive = TRUE)) # Create 2D V3 array with gzip zarrs_create_array(d, "", c(4L, 6L), c(2L, 3L), "float64", "gzip", 0.0, "{}", 3L) # Write a subset zarrs_set_subset(d, "", list(c(0L, 4L), c(0L, 6L)), as.double(1:24), NULL) # Read back result <- zarrs_get_subset(d, "", list(c(0L, 4L), c(0L, 6L)), NULL) expect_equal(result$data, as.double(1:24)) expect_equal(result$shape, c(4L, 6L)) zarrs_close_store(d) }) # -- Transparent dispatch via zarr_create() ----------------------------------- test_that("zarr_create transparently uses zarrs backend for DirectoryStore", { skip_if_no_zarrs() d <- tempfile("zarrs_dispatch_") on.exit(unlink(d, recursive = TRUE)) z <- zarr_create(shape = c(10L, 20L), chunks = c(5L, 10L), dtype = "