Skip to content

Commit

Permalink
Revert "cleaned tests"
Browse files Browse the repository at this point in the history
This reverts commit 7a398ee.
  • Loading branch information
beckyfisher authored Aug 23, 2023
1 parent bf5a7ca commit 89a13a2
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 201 deletions.
21 changes: 11 additions & 10 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(bayesnec)
library(brms)
library(dplyr)
library(testthat)

random_filename <- function(nchar) {
Expand All @@ -16,14 +16,15 @@ add_na <- function(x, n = 3) {
data(nec_data)
other_data <- nec_data
colnames(other_data) <- c("a", "b")
nec_data$count <- as.integer(round(nec_data$y * 20))
nec_data$trials <- as.integer(20)
nec_data$log_x <- log(nec_data$x)
nec_data <- nec_data %>%
mutate(count = as.integer(round(y * 20)),
trials = as.integer(20),
log_x = log(x))

data(manec_example)
nec4param <- pull_out(manec_example, model = "nec4param") |>
suppressMessages() |>
suppressWarnings()
ecx4param <- pull_out(manec_example, model = "ecx4param") |>
suppressMessages() |>
suppressWarnings()
nec4param <- pull_out(manec_example, model = "nec4param") %>%
suppressMessages %>%
suppressWarnings
ecx4param <- pull_out(manec_example, model = "ecx4param") %>%
suppressMessages %>%
suppressWarnings
41 changes: 21 additions & 20 deletions tests/testthat/test-amend.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
library(bayesnec)
library(dplyr)

data(manec_example)

test_that("input checks work correctly and return appropriate messages", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
general_error <- paste(
"Nothing to amend, please specify a proper model to either add or drop, or",
"changes to loo_controls;\n Returning original model set."
Expand All @@ -14,27 +15,27 @@ test_that("input checks work correctly and return appropriate messages", {
m_3 <- paste0("No new LOO fitting/weighting arguments have been specified;",
" ignoring argument loo_controls.")
expect_message(amend(manec_example), general_error)
amend(manec_example, drop = "nec3param") |>
expect_message(general_error) |>
amend(manec_example, drop = "nec3param") %>%
expect_message(general_error) %>%
expect_message(m_1)
m_4 <- "loo_controls list names are incorrect. See ?bnec"
manec_example |>
amend(loo_controls = list(method = "somethingwrong")) |>
expect_message(m_4) |>
expect_error()
expect_message(amend(manec_example, drop = "nec4param")) |>
suppressWarnings()
amend(manec_example, add = "nec3param") |>
expect_message() |>
expect_message("Fitted models are: nec4param ecx4param") |>
suppressWarnings()
manec_example %>%
amend(loo_controls = list(method = "somethingwrong")) %>%
expect_message(m_4) %>%
expect_error
expect_message(amend(manec_example, drop = "nec4param")) %>%
suppressWarnings
amend(manec_example, add = "nec3param") %>%
expect_message %>%
expect_message("Fitted models are: nec4param ecx4param") %>%
suppressWarnings
amend(manec_example, add = "nec4param",
loo_controls = list(weights = list(method = "pseudobma"))) |>
expect_message(general_error) |>
expect_message(m_1) |>
loo_controls = list(weights = list(method = "pseudobma"))) %>%
expect_message(general_error) %>%
expect_message(m_1) %>%
expect_message(m_3)
amend(manec_example,
loo_controls = list(weights = list(method = "pseudobma"))) |>
expect_message(general_error) |>
loo_controls = list(weights = list(method = "pseudobma"))) %>%
expect_message(general_error) %>%
expect_message(m_3)
})
27 changes: 9 additions & 18 deletions tests/testthat/test-bayesmanec_methods.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
library(bayesnec)
library(brms)

data(manec_example)

test_that("predict is silent", {
expect_silent(predict(manec_example))
})

test_that("predict/fitted is a matrix of appropriately name elements", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
pred_p <- predict(manec_example)
expect_equal(class(pred_p), c("matrix", "array"))
expect_equal(dim(pred_p), c(100, 4))
Expand All @@ -19,18 +20,14 @@ test_that("predict/fitted is a matrix of appropriately name elements", {
})

test_that("plot returns null, is invisible, and is silent", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
expect_null(plot(manec_example))
expect_silent(plot(manec_example))
expect_invisible(plot(manec_example))
})

test_that("rhat behaves as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
rhat_p <- suppressMessages(rhat(manec_example))
rhat2_p <- rhat(manec_example, rhat_cutoff = 1)
expect_message(rhat(manec_example, rhat_cutoff = 1))
Expand All @@ -39,9 +36,7 @@ test_that("rhat behaves as expected", {
})

test_that("summary behaves as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
summary.p <- suppressWarnings(summary(manec_example))
expect_equal(class(summary.p), "manecsummary")
expect_equal(names(summary.p), c("models", "family", "sample_size",
Expand All @@ -51,9 +46,7 @@ test_that("summary behaves as expected", {
})

test_that("formula behaves as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
expect_error(formula(manec_example))
expect_s3_class(formula(manec_example, "nec4param"), "bayesnecformula")
expect_s3_class(formula(manec_example, model = "nec4param"),
Expand All @@ -64,9 +57,7 @@ test_that("formula behaves as expected", {
})

test_that("model.frame behaves as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
expect_error(model.frame(manec_example))
expect_s3_class(model.frame(manec_example, "nec4param"),
"data.frame")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-bayesnec_methods.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(bayesnec)
library(brms)

test_that("predict is silent", {
expect_silent(predict(nec4param))
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-bnec.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
library(bayesnec)
library(dplyr)

test_that("Check for data when using formula syntax", {
expect_error(bnec(y ~ crf(x, "ecxlin")), "argument \"data\" is missing")
})

test_that("Check models inappropriate for negative x are dropped", {
bnec(y ~ crf(log_x, "nechorme4pwr"), data = nec_data) |>
expect_message("Dropping the model\\(s\\) nechorme4pwr as they are not valid for data with negative predictor \\(x\\) values\\.") |>
bnec(y ~ crf(log_x, "nechorme4pwr"), data = nec_data) %>%
expect_message("Dropping the model\\(s\\) nechorme4pwr as they are not valid for data with negative predictor \\(x\\) values\\.") %>%
expect_error("No valid models have been supplied for this data type.")
})
15 changes: 6 additions & 9 deletions tests/testthat/test-check_chains.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
library(bayesnec)
library(dplyr)

data(manec_example)

test_that("returns null", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
expect_null(check_chains(nec4param))
})

test_that("returns pdf for bayesmanec objects with filename argument", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
filename <- random_filename(15)
check_chains(manec_example, filename = filename) |>
expect_invisible() |>
expect_message()
check_chains(manec_example, filename = filename) %>%
expect_invisible %>%
expect_message
on.exit(file.remove(paste(filename, ".pdf", sep = "")))
})
17 changes: 9 additions & 8 deletions tests/testthat/test-check_formula.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(bayesnec)
library(dplyr)

nec3param <- function(beta, nec, top, x) {
top * exp(-exp(beta) * (x - nec) *
Expand Down Expand Up @@ -33,14 +34,14 @@ test_that("Parameter checks", {
f_2 <- y | trials(tr) ~ crf(x, "nec") + (nec + bot | group_1)
expect_s3_class(check_formula(bnf(f_2), data, run_par_checks = FALSE),
"bayesnecformula")
check_formula(bnf(f_2), data, run_par_checks = TRUE) |>
expect_message("Performing single parameter checks on all models...") |>
expect_message("\"bot\" not valid parameters") |>
expect_message("\"bot\" not valid parameters") |>
expect_message("\"bot\" not valid parameters") |>
expect_message("\"bot\" not valid parameters") |>
expect_message("\"bot\" not valid parameters") |>
expect_message("\"bot\" not valid parameters") |>
check_formula(bnf(f_2), data, run_par_checks = TRUE) %>%
expect_message("Performing single parameter checks on all models...") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters") %>%
expect_message("\"bot\" not valid parameters")
f_3 <- "log(y) | trials(tr) ~ crf(sqrt(x), \"nec3param\")"
expect_s3_class(check_formula(bnf(f_3), data), "bayesnecformula")
Expand Down
9 changes: 3 additions & 6 deletions tests/testthat/test-check_priors.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
library(bayesnec)
library(ggplot2)

data(manec_example)

test_that("returns ggplot for bayesnecfit objects", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
p <- check_priors(nec4param)
expect_error(print(p), NA)
expect_silent(check_priors(nec4param))
expect_equal(class(p), c("gg", "ggplot"))
})

test_that("returns pdf for bayesmanecfit objects", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
filename <- random_filename(15)
expect_invisible(check_priors(manec_example, filename = filename))
on.exit(file.remove(paste(filename, ".pdf", sep = "")))
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(bayesnec)
data("nec_data")

test_that("nec_data are as expected", {
expect_equal(class(nec_data), "data.frame")
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-define_prior.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(bayesnec)
library(brms)

pred_a <- rnorm(100)
pred_na <- add_na(pred_a)
Expand All @@ -7,6 +8,8 @@ resp_na <- add_na(resp_a)
pred_b <- pred_a[-1]
resp_b <- resp_a[-1]

data(manec_example)

test_that("model is always properly specified as character", {
p_a <- define_prior(model = "nec3param", family = gaussian(),
predictor = pred_a, response = resp_a)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-dispersion.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(bayesnec)
data(manec_example)

test_that("dispersion fails because family is gaussian", {
expect_length(dispersion(ecx4param), 0)
Expand Down
39 changes: 12 additions & 27 deletions tests/testthat/test-ecx.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
library(bayesnec)
library(brms)

data(manec_example)

test_that("prob_vals warnings behave as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx(manec_example, prob_vals = c(0.6, 0.1, 0.9), precision = 10,
type = "relative") |>
expect_length(3) |>
Expand All @@ -26,9 +27,7 @@ test_that("prob_vals warnings behave as expected", {
})

test_that("ecx_val warnings behave as expected", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
expect_error(
ecx(manec_example, ecx_val = 0.9, type = "relative", precision = 10)
) |>
Expand All @@ -39,9 +38,7 @@ test_that("ecx_val warnings behave as expected", {
})

test_that("ecx returns expected object types and arguments pass correctly", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ec50_summary <- ecx(manec_example, ecx_val = 50, type = "relative",
precision = 50)
ec50_summary2 <- ecx(manec_example, ecx_val = 50, type = "relative",
Expand All @@ -67,52 +64,40 @@ test_that("ecx returns expected object types and arguments pass correctly", {
})

test_that("works for bayesnecfit", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx1 <- ecx(ecx4param, precision = 10)
expect_equal(length(ecx1), 3)
expect_equal(names(ecx1), c("Q50", "Q2.5", "Q97.5"))
})

test_that("works for bayesmanecfit", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx1 <- ecx(manec_example, precision = 10)
expect_equal(length(ecx1), 3)
expect_equal(names(ecx1), c("Q50", "Q2.5", "Q97.5"))
})

test_that("xform passes correctly", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx1 <- ecx(ecx4param, precision = 10)
ecx2 <- ecx(ecx4param, xform = exp, precision = 10)
expect_gt(ecx2[1], ecx1[2])
})

test_that("posterior passes correctly", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx3 <- ecx(ecx4param, posterior = TRUE, precision = 10)
expect_equal(length(ecx3), 100)
})

test_that("prob_vals passes correctly", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx4 <- ecx(ecx4param, prob_vals = c(0.5, 0.3, 0.7), precision = 10)
expect_equal(names(ecx4), c("Q50", "Q30", "Q70"))
})

test_that("ecx_val passes correctly", {
if (Sys.getenv("NOT_CRAN") == "") {
skip_on_cran()
}
skip_on_cran()
ecx4 <- ecx(ecx4param, prob_vals = c(0.5, 0.3, 0.7), ecx_val = 20,
precision = 10)
expect_equal(names(ecx4), c("Q50", "Q30", "Q70"))
Expand Down
Loading

0 comments on commit 89a13a2

Please sign in to comment.