tests/testthat/test-codes-sweeps.R

# The refusals of lago_sensitivity() and lago_budget() are LAGO errors with a
# code, an argument and a fix, and a sweep keeps the codes of its failed runs.

sweeps_args <- function(...) {
  utils::modifyList(list(
    data = mtcars,
    outcome_name = "mpg",
    outcome_type = "continuous",
    glm_family = "gaussian",
    link = "identity",
    intervention_components = c("gear", "qsec"),
    intervention_lower_bounds = c(0, 0),
    intervention_upper_bounds = c(10, 350),
    cost_list_of_vectors = list(c(0, 4), c(4, 6)),
    outcome_goal_intention = "maximize"
  ), list(...))
}

sweeps_error <- function(expr) {
  e <- tryCatch(expr, lago_error = function(e) e)
  expect_s3_class(e, "lago_error")
  expect_true(is.character(e$fix) && length(e$fix) == 1 && nzchar(e$fix))
  expect_true(e$code %in% lago_error_codes()$code)
  e
}

sweeps_sens <- function(...) {
  sweeps_error(do.call("lago_sensitivity", list(...)))
}

test_that("lago_sensitivity() refuses a bad parameter with a code", {
  e <- sweeps_sens(parameter = 1, values = 1)
  expect_identical(e$code, "sweep_parameter_invalid")
  expect_identical(e$argument, "parameter")
  e <- sweeps_sens(parameter = "no_such_argument", values = 1)
  expect_identical(e$code, "sweep_parameter_unknown")
  expect_identical(e$argument, "parameter")
  e <- sweeps_sens(parameter = "intervention_lower_bounds", values = 1)
  expect_identical(e$code, "sweep_parameter_not_scalar")
  expect_identical(e$argument, "parameter")
})

test_that("lago_sensitivity() refuses a parameter without effect with a code", {
  cs_only <- LAGOtrials:::.lago_confidence_set_only_formals()[1]
  e <- sweeps_sens(parameter = cs_only, values = 1)
  expect_identical(e$code, "sweep_parameter_no_effect")
  expect_identical(e$argument, "parameter")
  e <- sweeps_sens(parameter = "time_effect_optimization_value", values = 1)
  expect_identical(e$code, "sweep_parameter_no_effect")
  expect_identical(e$argument, "include_time_effects")
  e <- sweeps_sens(parameter = "icc", values = 0.1)
  expect_identical(e$code, "sweep_parameter_no_effect")
  expect_identical(e$argument, "power_goal")
  e <- sweeps_sens(parameter = "shrinkage_threshold", values = 0.1)
  expect_identical(e$code, "sweep_parameter_no_effect")
  expect_identical(e$argument, "parameter")
})

test_that("lago_sensitivity() refuses bad values with a code", {
  e <- sweeps_sens(parameter = "outcome_goal")
  expect_identical(e$code, "sweep_values_invalid")
  expect_identical(e$argument, "values")
  expect_match(e$fix, "numeric vector")
  e <- sweeps_sens(parameter = "outcome_goal", values = "a")
  expect_identical(e$code, "sweep_values_invalid")
  e <- sweeps_sens(
    parameter = "time_effect_optimization_value", values = list(1),
    include_time_effects = TRUE
  )
  expect_identical(e$code, "sweep_values_invalid")
  expect_match(e$fix, "periods")
  e <- sweeps_sens(
    parameter = "time_effect_optimization_value", values = c(1, NA),
    include_time_effects = TRUE
  )
  expect_identical(e$code, "sweep_values_not_finite")
  expect_identical(e$argument, "values")
  e <- sweeps_sens(parameter = "outcome_goal", values = c(1, Inf))
  expect_identical(e$code, "sweep_values_not_finite")
  expect_identical(e$argument, "values")
})

test_that("lago_sensitivity() refuses a bad cost multiplier sweep with a code", {
  e <- sweeps_sens(parameter = "cost_multiplier", values = 1)
  expect_identical(e$code, "sweep_costs_missing")
  expect_identical(e$argument, "unit_costs")
  e <- sweeps_sens(parameter = "cost_multiplier", values = c(1, 0), unit_costs = c(1, 1))
  expect_identical(e$code, "sweep_multiplier_not_positive")
  expect_identical(e$argument, "values")
})

test_that("a component named like a result column is refused with a code", {
  e <- sweeps_sens(
    parameter = "outcome_goal", values = 30,
    intervention_components = c("status", "qsec")
  )
  expect_identical(e$code, "component_name_reserved")
  expect_identical(e$argument, "intervention_components")
  e <- sweeps_error(do.call("lago_budget", c(
    sweeps_args(intervention_components = c("affordable", "qsec")),
    list(budget = 10)
  )))
  expect_identical(e$code, "component_name_reserved")
  expect_identical(e$argument, "intervention_components")
})

test_that("a bad object is refused with a code", {
  e <- sweeps_error(lago_sensitivity(mtcars, parameter = "outcome_goal", values = 30))
  expect_identical(e$code, "object_is_data_frame")
  expect_identical(e$argument, "object")
  e <- sweeps_error(lago_sensitivity(list(1), parameter = "outcome_goal", values = 30))
  expect_identical(e$code, "not_a_lago_result")
  expect_identical(e$argument, "object")
  old <- structure(list(), class = "lago")
  e <- sweeps_error(lago_budget(old, budget = 10))
  expect_identical(e$code, "object_without_call_args")
  expect_identical(e$argument, "object")
  stored <- structure(list(), class = "lago", lago_call_args = sweeps_args())
  e <- sweeps_error(lago_sensitivity(stored, 30, parameter = "outcome_goal", values = 30))
  expect_identical(e$code, "dots_unnamed")
  expect_identical(e$argument, "...")
})

test_that("lago_budget() refuses bad inputs with a code", {
  e <- sweeps_error(do.call("lago_budget", c(sweeps_args(), list(budget = -1))))
  expect_identical(e$code, "budget_invalid")
  expect_identical(e$argument, "budget")
  e <- sweeps_error(do.call("lago_budget", c(sweeps_args(), list(budget = 10, n_grid = 2.5))))
  expect_identical(e$code, "n_grid_invalid")
  expect_identical(e$argument, "n_grid")
  e <- sweeps_error(lago_budget(budget = 10))
  expect_identical(e$code, "budget_inputs_missing")
  expect_null(e$argument)
  e <- sweeps_error(do.call("lago_budget", c(sweeps_args(outcome_name = "nope"), list(budget = 10))))
  expect_identical(e$code, "budget_outcome_not_in_data")
  expect_identical(e$argument, "outcome_name")
})

# lago_optimization() with the same arguments that first runs `first`, so a
# sweep sees its formals and can be made to fail
sweeps_failing_fit <- function(first) {
  fake <- LAGOtrials::lago_optimization
  body(fake) <- call("{", first, body(fake))
  fake
}

test_that("lago_sensitivity() keeps the code of each failed run", {
  local_mocked_bindings(lago_optimization = sweeps_failing_fit(quote({
    if (identical(outcome_goal, 31)) lago_abort("internal", "a coded failure", fix = "Do this.")
    if (identical(outcome_goal, 32)) stop("a plain failure")
  })))
  sens <- suppressWarnings(do.call("lago_sensitivity", c(
    sweeps_args(), list(parameter = "outcome_goal", values = c(30, 31, 32))
  )))
  expect_identical(sens$status, c("ok", "error", "error"))
  expect_identical(attr(sens, "error_codes"), c(`31` = "internal", `32` = NA_character_))
  expect_identical(names(attr(sens, "error_codes")), names(attr(sens, "error_messages")))
  ok <- do.call("lago_sensitivity", c(sweeps_args(), list(parameter = "outcome_goal", values = 30)))
  expect_null(attr(ok, "error_codes"))
})

test_that("lago_budget() keeps the code of the first failed run", {
  local_mocked_bindings(lago_optimization = sweeps_failing_fit(quote(
    lago_abort("internal", "a coded failure", fix = "Do this.")
  )))
  b <- suppressWarnings(do.call("lago_budget", c(sweeps_args(), list(budget = 10, n_grid = 3))))
  expect_false(b$feasible)
  expect_identical(b$run_error, "a coded failure")
  expect_identical(b$run_error_code, "internal")
})

test_that("lago_budget() gives NA for a failed run that is not a LAGO error", {
  local_mocked_bindings(lago_optimization = sweeps_failing_fit(quote(stop("a plain failure"))))
  b <- suppressWarnings(do.call("lago_budget", c(sweeps_args(), list(budget = 10, n_grid = 3))))
  expect_identical(b$run_error, "a plain failure")
  expect_identical(b$run_error_code, NA_character_)
  expect_true("run_error_code" %in% names(b))
})

test_that("a feasible lago_budget() has no run error code", {
  b <- suppressWarnings(do.call("lago_budget", c(sweeps_args(), list(budget = 100, n_grid = 4))))
  expect_null(b$run_error_code)
})

Try the LAGOtrials package in your browser

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

LAGOtrials documentation built on Dec. 4, 2026, 5:10 p.m.