Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Update to testthat3 #81

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Authors@R: c(
comment = c(ORCID = "0000-0002-7683-4592")),
person("Kirill", "Müller", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-1416-3412")),
person("Nicole", "Hill", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-7623-2153")),
person("Ayla", "Pearson", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0001-7388-1222")),
person("Nadine", "Hussein", , "[email protected]", role = "ctb",
Expand Down Expand Up @@ -51,7 +53,7 @@ Suggests:
cli,
covr,
jmbr,
testthat
testthat (>= 3.0.0)
RdMacros:
lifecycle
Remotes:
Expand All @@ -64,3 +66,4 @@ Remotes:
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion R/plot-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ggplot_data <- function(x, y, x_name, y_name) {
height <- if (is.numeric(y)) 0 else 0.2

gp <- ggplot2::ggplot(data = data) +
ggplot2::aes_string(x = "x", y = "y") +
ggplot2::aes(x = "x", y = "y") +
ggplot2::xlab(x_name) +
ggplot2::ylab(y_name) +
ggplot2::geom_jitter(alpha = 1 / 3, width = width, height = height)
Expand Down
4 changes: 2 additions & 2 deletions R/residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plot_residuals <- function(x, ...) {

ggplot_residuals_histogram <- function(data, name) {
gp <- ggplot2::ggplot(data = data) +
ggplot2::aes_string(x = "residuals") +
ggplot2::aes(x = "residuals") +
suppressWarnings(ggplot2::geom_histogram()) +
ggplot2::expand_limits(x = 0) +
ggplot2::xlab("residual")
Expand All @@ -37,7 +37,7 @@ ggplot_residuals_histogram <- function(data, name) {

ggplot_residuals <- function(data, name) {
gp <- ggplot2::ggplot(data = data) +
ggplot2::aes_string(x = "x", y = "residuals") +
ggplot2::aes(x = "x", y = "residuals") +
ggplot2::geom_hline(yintercept = 0) +
ggplot2::expand_limits(y = 0) +
ggplot2::xlab(name) +
Expand Down
1 change: 1 addition & 0 deletions man/embr-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-mb-code.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("mb_code", {
expect_warning(mb_code("blah blah"), "template type is unrecognised")
expect_is(mb_code(.tmb_template), "tmb_code")
expect_is(mb_code("function() dpois()"), "lmb_code")
expect_s3_class(mb_code(.tmb_template), "tmb_code")
expect_s3_class(mb_code("function() dpois()"), "lmb_code")
})

test_that("template", {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-mb-null-analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ test_that("mb_null_analysis", {
class(analysis) <- c("mb_null_analysis", "mb_analysis")

coef <- coef(analysis)
expect_is(coef, "tbl")
expect_s3_class(coef, "tbl")
expect_identical(colnames(coef), c("term", "estimate", "sd", "zscore", "lower", "upper", "pvalue"))
expect_identical(nrow(coef), 0L)

glance <- glance(analysis)
expect_is(glance, "tbl")
expect_s3_class(glance, "tbl")
expect_identical(colnames(glance), c("n", "K", "logLik", "IC", "converged"))
expect_identical(nrow(glance), 1L)

tidy <- tidy(analysis)
expect_is(tidy, "tbl")
expect_s3_class(tidy, "tbl")
expect_identical(colnames(tidy), c("term", "estimate", "lower", "upper"))
expect_identical(nrow(tidy), 0L)

Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-new-expr-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test_that("update new expr string expression", {
residual <- res_lnorm(Density, fit, exp(log_sDensity))
}"
)
expect_warning(analysis <- analyse(model, data = data, glance = FALSE))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

expect_identical(pars(analysis, "derived"), "eDensity")
expect_identical(pars(analysis, "random"), "bSiteYear")
Expand Down Expand Up @@ -143,11 +143,11 @@ test_that("update new expr bare expression", {
residual <- res_lnorm(Density, fit, exp(log_sDensity))
}
)
expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand Down Expand Up @@ -223,11 +223,11 @@ test_that("add new_expr_vec argument to update model", {
}",
new_expr_vec = FALSE
)
expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand Down Expand Up @@ -296,11 +296,11 @@ test_that("add new_expr_vec argument to update model and updates original new_ex
model,
new_expr_vec = TRUE
)
expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = TRUE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand Down Expand Up @@ -369,11 +369,11 @@ test_that("cannot undo the vectorization if orignally set in the model", {
model,
new_expr_vec = FALSE
)
expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-new-expr-vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ test_that("vectorize predict string expression", {
random_effects = list(bSiteYear = c("Site", "YearFactor"))
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = TRUE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -65,7 +65,7 @@ test_that("vectorize predict string expression", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
Expand Down Expand Up @@ -130,11 +130,11 @@ test_that("vectorize predict bare expression", {
random_effects = list(bSiteYear = c("Site", "YearFactor"))
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -145,7 +145,7 @@ test_that("vectorize predict bare expression", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
Expand Down Expand Up @@ -213,11 +213,11 @@ test_that("two expressions wrapped even when new_expr_vec = TRUE", {
random_effects = list(bSiteYear = c("Site", "YearFactor"))
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -228,7 +228,7 @@ test_that("two expressions wrapped even when new_expr_vec = TRUE", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-new-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ test_that("predict string expression", {
random_effects = list(bSiteYear = c("Site", "YearFactor"))
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -65,7 +65,7 @@ test_that("predict string expression", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
Expand Down Expand Up @@ -130,11 +130,11 @@ test_that("predict bare expression", {
random_effects = list(bSiteYear = c("Site", "YearFactor"))
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_warning(expect_warning(analysis <- analyse(model, data = data, glance = FALSE)))

year <- predict(analysis, new_data = "Year")

expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -145,7 +145,7 @@ test_that("predict bare expression", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
Expand Down
14 changes: 0 additions & 14 deletions tests/testthat/test-plot-data.R

This file was deleted.

2 changes: 1 addition & 1 deletion tests/testthat/test-predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("lmb", {
# models <- as.models(models)
#
# analyses <- analyse(models, data = data, glance = FALSE, beep = FALSE)
# expect_is(analyses, "mb_analyses")
# expect_s3_class(analyses, "mb_analyses")
#
# feed <- predict(analyses, new_data = "feed")
# expect_identical(as.character(feed$feed), levels(datasets::chickwts$feed))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ test_that("lmb", {
#
# plots <- plot_residuals(analysis)
# expect_identical(names(plots), c("feed", "fit", "weight"))
# expect_is(plots[[1]], "ggplot")
# expect_s3_class(plots[[1]], "ggplot")
})
22 changes: 11 additions & 11 deletions tests/testthat/test-zzz-analyse.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test_that("analyse", {
new_expr = new_expr
)

expect_output(expect_warning(analysis <- analyse(model, data = data)))
expect_output(expect_warning(expect_warning(analysis <- analyse(model, data = data))))

expect_equal(as.data.frame(data_set(analysis)), data)
data2 <- data_set(analysis, marginalize_random_effects = TRUE)
Expand Down Expand Up @@ -93,10 +93,10 @@ test_that("analyse", {
c("bHabitatQuality", "bIntercept", "bSiteYear", "bYear", "eDensity", "log_sDensity", "log_sSiteYear")
)

expect_is(as.mcmcr(analysis), "mcmcr")
expect_s3_class(as.mcmcr(analysis), "mcmcr")

glance <- glance(analysis)
expect_is(glance, "tbl")
expect_s3_class(glance, "tbl")
expect_identical(colnames(glance), c("n", "K", "nchains", "niters", "nthin", "ess", "rhat", "converged"))
expect_identical(glance$n, 300L)
expect_identical(glance$nthin, 1L)
Expand All @@ -108,7 +108,7 @@ test_that("analyse", {

coef <- coef(analysis, simplify = TRUE)

expect_is(coef, "tbl")
expect_s3_class(coef, "tbl")
expect_identical(colnames(coef), c("term", "estimate", "lower", "upper", "svalue"))

expect_identical(coef$term, as.term(c(
Expand All @@ -127,13 +127,13 @@ test_that("analyse", {

ppc <- posterior_predictive_check(analysis)

expect_is(ppc, "tbl_df")
expect_s3_class(ppc, "tbl_df")
expect_identical(colnames(ppc), c("moment", "observed", "median", "lower", "upper", "svalue"))
expect_identical(ppc$moment, structure(1:5, .Label = c(
"zeros", "mean", "variance", "skewness",
"kurtosis"
), class = "factor"))
expect_is(year, "tbl")
expect_s3_class(year, "tbl")
expect_identical(colnames(year), c(
"Site", "HabitatQuality", "Year", "Visit",
"Density", "YearFactor",
Expand All @@ -145,15 +145,15 @@ test_that("analyse", {
dd <- mcmc_derive_data(analysis, new_data = c("Site", "Year"), ref_data = TRUE)
expect_true(mcmcdata::is.mcmc_data(dd))

expect_warning(
expect_warning(expect_warning(expect_warning(
sensitivity <- sd_priors_by(analysis, by = 10, glance = FALSE),
"incomplete adaptation"
)
)))

analyses <- analyses(analysis, sensitivity)

rhat2 <- rhat(analyses)
expect_is(rhat2, "list")
expect_type(rhat2, "list")
expect_identical(names(rhat2), c("mcmcr1", "mcmcr2"))

skip_if_not_installed("mcmcr", "0.6.1.9001")
Expand All @@ -163,15 +163,15 @@ test_that("analyse", {
"`rhat.mcmcrs\\(x, bound = TRUE\\)`"
)

expect_is(rhat2, "list")
expect_type(rhat2, "list")
expect_identical(names(rhat2), c("mcmcr1", "mcmcr2", "bound"))

lifecycle::expect_deprecated(
glance <- glance(analyses, bound = TRUE),
"`rhat.mcmcrs\\(x, bound = TRUE\\)`"
)

expect_is(glance, "tbl")
expect_s3_class(glance, "tbl")
expect_identical(nrow(glance), 1L)
expect_identical(colnames(glance), c("n", "K", "nchains", "niters", "rhat_1", "rhat_2", "rhat_all", "converged"))
})
Loading