tests/testthat/test-check-settings.R

# lago_check_settings(): a dry run of lago_optimization().

check_base <- function(...) {
  utils::modifyList(list(
    data = mtcars, outcome_name = "mpg", outcome_type = "continuous",
    intervention_components = c("gear", "qsec"),
    intervention_lower_bounds = c(3, 14), intervention_upper_bounds = c(5, 23),
    cost_list_of_vectors = list(c(0, 4), c(4, 6)), outcome_goal = 25
  ), list(...))
}

test_that("good settings are ready, with the defaults filled in", {
  x <- do.call(lago_check_settings, check_base())
  expect_s3_class(x, "lago_check")
  expect_true(x$ok)
  expect_null(x$problem)
  expect_identical(x$settings$family, "gaussian")
  expect_identical(x$settings$link, "identity")
  expect_identical(x$settings$outcome_goal_intention, "maximize")
  expect_identical(x$settings$intervention_upper_bounds, c(gear = 5, qsec = 23))
  expect_identical(x$settings$n_rows_fitted, 32L)
  expect_named(x$settings$coefficients, c("(Intercept)", "gear", "qsec"))
  expect_identical(lago_summary_text(x), "The settings are ready to run, and the outcome model fits on 32 rows.")
  expect_output(print(x), "ready to run")
})

test_that("without the fit only the inputs are checked", {
  x <- do.call(lago_check_settings, c(check_base(unit_costs = c(4, 6), cost_list_of_vectors = NULL), fit_model = FALSE))
  expect_true(x$ok)
  expect_null(x$settings$n_rows_fitted)
  expect_length(x$settings$cost_list_of_vectors$gear, 5)
  expect_identical(lago_summary_text(x), "The settings are ready to run. They give 1 warning.")
})

test_that("two warnings are counted in the summary", {
  x <- do.call(lago_check_settings, check_base(
    unit_costs = c(4, 6), cost_list_of_vectors = NULL, outcome_goal = 10
  ))
  expect_gte(length(x$warnings), 2)
  expect_match(lago_summary_text(x), "warnings.$")
})

test_that("a problem comes with its code, argument and fix", {
  x <- do.call(lago_check_settings, check_base(intervention_lower_bounds = c(5, 14), intervention_upper_bounds = c(3, 23)))
  expect_false(x$ok)
  expect_null(x$settings)
  expect_identical(x$problem$code, "bounds_order")
  expect_identical(x$problem$argument, "intervention_upper_bounds")
  expect_match(lago_summary_text(x), "^The settings have a problem \\(bounds_order\\): ")
  expect_match(lago_summary_text(x), "To fix it: ", fixed = TRUE)
})

test_that("an error without a code is still reported", {
  x <- lago_check_settings(data = stop("no data here"), outcome_name = "y", outcome_type = "binary")
  expect_false(x$ok)
  expect_null(x$problem$code)
  expect_null(x$problem$fix)
  expect_identical(lago_summary_text(x), "The settings have a problem: no data here")
})

test_that("a model that cannot be fitted is a problem only when it is fitted", {
  set.seed(5)
  d <- data.frame(x1 = stats::runif(100, 0, 10), x2 = stats::runif(100, 0, 10))
  d$y <- as.integer(d$x1 > 5)
  args <- list(
    data = d, outcome_name = "y", outcome_type = "binary",
    intervention_components = c("x1", "x2"), intervention_lower_bounds = c(0, 0),
    intervention_upper_bounds = c(10, 10), cost_list_of_vectors = list(c(0, 1), c(0, 1)),
    outcome_goal = 0.6
  )
  expect_true(do.call(lago_check_settings, c(args, fit_model = FALSE))$ok)
  fitted <- do.call(lago_check_settings, args)
  expect_false(fitted$ok)
  expect_identical(fitted$problem$code, "model_not_converged")
})

test_that("notes on how the inputs were read are kept", {
  d <- mtcars
  d$period <- rep(1:4, length.out = 32)
  x <- do.call(lago_check_settings, check_base(data = d, include_time_effects = TRUE, time_effect_optimization_value = 4))
  expect_true(x$ok)
  expect_true(any(grepl("not a factor", x$notes)))
  expect_true(x$settings$include_time_effects)
})

test_that("center weights and main components are in the settings", {
  d <- mtcars
  d$center <- rep(c("a", "b", "c", "d"), length.out = 32)
  x <- do.call(lago_check_settings, check_base(data = d, include_center_effects = TRUE))
  expect_true(x$ok)
  expect_equal(sum(x$settings$center_weights_for_outcome_goal), 1)
  d[["gear:qsec"]] <- d$gear * d$qsec
  y <- do.call(lago_check_settings, check_base(
    data = d, intervention_components = c("gear", "qsec", "gear:qsec"),
    main_components = c("gear", "qsec"), include_interaction_terms = TRUE
  ))
  expect_identical(y$settings$main_components, c("gear", "qsec"))
  js <- lago_as_list(y)
  expect_identical(js$settings$main_components, list("gear", "qsec"))
  expect_true(is.list(lago_as_list(x)$settings$center_weights_for_outcome_goal))
})

test_that("the check becomes a list and JSON with a fixed shape", {
  good <- lago_as_list(do.call(lago_check_settings, check_base()))
  expect_identical(good$kind, "lago_check")
  expect_true(good$ok)
  expect_identical(good$settings$cost_list_of_vectors$qsec, list(4, 6))
  bad <- lago_as_list(do.call(lago_check_settings, check_base(outcome_goal = NULL)))
  expect_false(bad$ok)
  expect_null(bad$settings)
  back <- jsonlite::fromJSON(lago_to_json(do.call(lago_check_settings, check_base(outcome_goal = NULL))))
  expect_identical(back$problem$code, bad$problem$code)
})

test_that("fit_model must be TRUE or FALSE, and the check leaves no state behind", {
  e <- tryCatch(lago_check_settings(fit_model = NA), lago_error = function(e) e)
  expect_identical(e$code, "not_true_false")
  do.call(lago_check_settings, check_base())
  expect_null(LAGOtrials:::.lago_state$check_only)
  r <- suppressWarnings(do.call(lago_optimization, c(check_base(), quiet = TRUE)))
  expect_s3_class(r, "lago")
})

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.