Skip to content

Commit

Permalink
a few more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jgabry committed Nov 22, 2024
1 parent 3af034b commit 4be1269
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/test-fit-laplace.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("fitted-vb")
context("fitted-laplace")

set_cmdstan_path()
fit_laplace <- testing_fit("logistic", method = "laplace", seed = 100)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-fit-shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ test_that("sampling with inits works with include_paths", {
include_paths = test_path("resources", "stan"))

data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
expect_no_error(
expect_no_error(utils::capture.output(
fit <- mod_w_include$sample(
data = data_list,
seed = 123,
Expand All @@ -474,7 +474,7 @@ test_that("sampling with inits works with include_paths", {
list(theta = 0.25),
list(theta = 0.25))
)
)
))
})

test_that("CmdStanModel created with exe_file works", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-model-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ test_that("dirname of stan_file is used as include path if no other paths suppli

mod_tmp <- cmdstan_model(stan_file, compile = FALSE)
expect_true(mod_tmp$check_syntax())
expect_true(mod_tmp$format())
utils::capture.output(expect_true(mod_tmp$format()))
expect_s3_class(mod_tmp$compile(), "CmdStanModel")
})

Expand Down
23 changes: 13 additions & 10 deletions tests/testthat/test-model-laplace.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ test_that("laplace() method errors for any invalid argument before calling cmdst
for (nm in names(bad_arg_values)) {
args <- ok_arg_values
args[[nm]] <- bad_arg_values[[nm]]
expect_error(do.call(mod$laplace, args), regexp = nm, info = nm)
utils::capture.output(
expect_error(do.call(mod$laplace, args), regexp = nm, info = nm)
)
}
args <- ok_arg_values
args$opt_args <- list(iter = "NOT_A_NUMBER")
Expand Down Expand Up @@ -129,14 +131,15 @@ test_that("laplace() errors with bad combinations of arguments", {

test_that("laplace() errors if optimize() fails", {
mod_schools <- testing_model("schools")
expect_error(
expect_warning(
expect_message(
mod_schools$laplace(data = testing_data("schools"), refresh = 0),
"Line search failed to achieve a sufficient decrease"
utils::capture.output(
expect_error(
expect_warning(
expect_message(
mod_schools$laplace(data = testing_data("schools"), refresh = 0),
"Line search failed to achieve a sufficient decrease"
),
"Fitting finished unexpectedly"
),
"Fitting finished unexpectedly"
),
"Optimization failed"
)
"Optimization failed"
))
})
7 changes: 4 additions & 3 deletions tests/testthat/test-model-pathfinder.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data_list <- testing_data("bernoulli")
data_file_r <- test_path("resources", "data", "bernoulli.data.R")
data_file_json <- test_path("resources", "data", "bernoulli.data.json")

# these are all valid for sample()
# these are all valid for pathfinder()
ok_arg_values <- list(
data = data_list,
output_dir = tempdir(),
Expand All @@ -32,9 +32,10 @@ ok_arg_values <- list(
max_lbfgs_iters = 100,
save_single_paths = FALSE,
calculate_lp = TRUE,
psis_resample=TRUE)
psis_resample=TRUE
)

# using any one of these should cause sample() to error
# using any one of these should cause pathfinder() to error
bad_arg_values <- list(
data = "NOT_A_FILE",
output_dir = "NOT_A_DIRECTORY",
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-threads.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,13 @@ test_that("correct output when stan_threads not TRUE", {
fixed = TRUE
)
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE)
expect_warning(
mod$sample(data = data_file_json, threads_per_chain = 4),
"'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!",
expect_output(
expect_warning(
mod$sample(data = data_file_json, threads_per_chain = 4),
"'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!",
fixed = TRUE
),
"Running MCMC with 4 sequential chains",
fixed = TRUE
)
})

0 comments on commit 4be1269

Please sign in to comment.