tests/testthat/test-validation.R

test_that("invalid dimensions and non-finite inputs fail explicitly", {
  z <- small_inputs()
  expect_error(ijse_draws(z$draws[1, , drop = FALSE], z$ll), "at least 2")
  expect_error(ijse_draws(z$draws, z$ll[-1, ]), "same number of rows")
  for (bad in c(NA_real_, NaN, Inf, -Inf)) {
    d <- z$draws
    d[1, 1] <- bad
    expect_error(ijse_draws(d, z$ll), "finite")
    ll <- z$ll
    ll[1, 6] <- bad
    expect_error(ijse_draws(z$draws, ll, block_size = 2), "finite")
  }
  expect_error(ijse_draws(list(a = 1:3), z$ll), "numeric matrix")
  expect_error(ijse_draws(cbind(a = letters[1:3]), z$ll), "numeric matrix")
  expect_error(ijse_draws(data.frame(a = 1:3, b = letters[1:3]), z$ll), "numeric columns")
  expect_error(ijse_draws(z$draws + 1i, z$ll), "numeric matrix")
  for (value in list(NULL, 0, -1, 1.5, NA, Inf, TRUE, c(1, 2), 1 + 1i)) {
    expect_error(ijse_draws(z$draws, z$ll, block_size = value), "block_size")
  }
  expect_error(ijse_draws(z$draws, z$ll, save_influence = 1), "TRUE or FALSE")
  expect_error(ijse_draws(z$draws, z$ll, cluster_var = 1:3), "length 6")
  expect_error(ijse_draws(z$draws, z$ll, cluster_var = list(1, 2, 3, 4, 5, 6)), "atomic")
})

test_that("callbacks have a strict shape and preserve errors", {
  z <- small_inputs()
  expect_error(ijse_draws(z$draws, function(i) z$ll[, i]), "n_obs")
  expect_error(ijse_draws(z$draws, function(i) z$ll[, i], n_obs = 6, block_size = 1),
               "numeric matrix")
  expect_error(ijse_draws(z$draws, function(i) z$ll[, 1:2], n_obs = 6, block_size = 3),
               "one column per requested")
  expect_error(ijse_draws(z$draws, function(i) stop("original provider cause"), n_obs = 6),
               "block starting at 1: original provider cause")
  expect_error(ijse_draws(z$draws, z$ll, n_obs = 7), "number of likelihood columns")
  rownames(z$draws) <- paste0("draw-", seq_len(nrow(z$draws)))
  rownames(z$ll) <- rev(rownames(z$draws))
  expect_error(ijse_draws(z$draws, z$ll), "Row names.*agree")
})

test_that("parameter selection and transforms reject ambiguous results", {
  z <- small_inputs()
  for (variables in list(character(), "missing", c("a", "a"), NA_character_, 1)) {
    expect_error(ijse_draws(z$draws, z$ll, variables = variables), "variables")
  }
  colnames(z$draws) <- c("a", "a")
  expect_error(ijse_draws(z$draws, z$ll), "unique")
  z <- small_inputs()
  expect_error(ijse_draws(z$draws, z$ll, transform = "a * b"), "function")
  expect_error(ijse_draws(z$draws, z$ll, transform = function(d) stop("my cause")), "my cause")
  expect_error(ijse_draws(z$draws, z$ll, transform = function(d) d[-1, ]), "one row")
  expect_error(ijse_draws(z$draws, z$ll, transform = function(d) rep(Inf, nrow(d))), "finite")
  rownames(z$draws) <- paste0("row", seq_len(nrow(z$draws)))
  expect_error(ijse_draws(z$draws, z$ll, transform = function(d) d[nrow(d):1, ]),
               "preserve posterior draw row order")
})

test_that("brms class and streaming metadata errors remain informative", {
  expect_error(IJ_se(list()), "brmsfit")
  expect_error(ijse(matrix(1)), "brmsfit")
  expect_error(IJSE:::brms_stream(function(i) i), "metadata")
  fun <- function(data_i, draws) rep(data_i$i, 3)
  attr(fun, "data") <- data.frame(i = 1:4)
  attr(fun, "draws") <- list(old_order = c(3L, 1L, 4L, 2L))
  stream <- IJSE:::brms_stream(fun)
  expect_equal(dim(stream$read(1:2)), c(3L, 2L))
  expect_equal(stream$read(1:4), matrix(rep(c(3, 1, 4, 2), each = 3), nrow = 3))
  attr(fun, "draws") <- list(old_order = c(1.1, 2, 3, 4))
  expect_error(IJSE:::brms_stream(fun), "Invalid brms observation permutation")
  attr(fun, "draws") <- structure(list(), class = "mvbrmsprep")
  expect_error(IJSE:::brms_stream(fun), "Multivariate streaming")
})

Try the IJSE package in your browser

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

IJSE documentation built on Oct. 22, 2026, 5:18 p.m.