Skip to content

Commit

Permalink
Attempt to fix test uncoupling by using namespacing instead of packag…
Browse files Browse the repository at this point in the history
…e loading (#361)
  • Loading branch information
rempsyc authored Apr 3, 2023
1 parent 6f38ef5 commit c5446ea
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 48 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ Imports:
bayestestR (>= 0.13.0),
effectsize (> 0.8.2),
insight (>= 0.19.1),
parameters (>= 0.20.0),
performance (>= 0.9.2),
datawizard (>= 0.6.5),
parameters (>= 0.20.2),
performance (>= 0.10.2),
datawizard (>= 0.7.0),
stats,
tools,
utils
Expand Down
14 changes: 0 additions & 14 deletions tests/testthat/helper.R

This file was deleted.

4 changes: 2 additions & 2 deletions tests/testthat/test-format_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("format_model", {


test_that("format_model", {
skip_if_not_or_load_if_installed("lme4")
skip_if_not_installed("lme4")
expect_identical(format_model(lme4::lmer(wt ~ cyl + (1 | gear), data = mtcars)), "linear mixed model")
expect_identical(
format_model(lme4::glmer(vs ~ cyl + (1 | gear), data = mtcars, family = "binomial")),
Expand All @@ -23,7 +23,7 @@ test_that("format_model", {


test_that("format_model", {
skip_if_not_or_load_if_installed("rstanarm")
skip_if_not_installed("rstanarm")
expect_identical(
format_model(suppressWarnings(rstanarm::stan_glm(mpg ~ wt, data = mtcars, refresh = 0, iter = 50))),
"Bayesian linear model"
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-report.bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ test_that("models", {
expect_output(print(r), "Compared to the Species model")
})



test_that("inclusion", {
inc_bf <- bayestestR::bayesfactor_inclusion(BFmodels, prior_odds = c(1, 2, 3), match_models = TRUE)
r <- report(inc_bf)
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-report.brmsfit.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
skip_if_not_or_load_if_installed("brms")
skip_if_not_installed("brms")

test_that("report.brms", {
testthat::skip_if_not(packageVersion("rstan") >= "2.26.0")
testthat::skip_if_not_installed("rstan", "2.26.0")

set.seed(333)
model <- suppressWarnings(brm(mpg ~ qsec + wt, data = mtcars, refresh = 0, iter = 300, seed = 333))
model <- suppressMessages(suppressWarnings(brms::brm(
mpg ~ qsec + wt,
data = mtcars, refresh = 0, iter = 300, seed = 333
)))
r <- report(model, verbose = FALSE)

expect_s3_class(summary(r), "character")
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-report.data.frame.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
skip_if_not(getRversion() <= "4.2.1")
# skip_if_not(getRversion() <= "4.2.1")
# This skip does not seem necessary??

test_that("report.numeric", {
r <- report(seq(0, 1, length.out = 100))
Expand Down Expand Up @@ -43,7 +44,7 @@ test_that("report.factor", {
})

test_that("report.data.frame", {
skip_if_not_or_load_if_installed("dplyr")
skip_if_not_installed("dplyr")

r <- report(iris)
expect_equal(nrow(as.data.frame(r)), 7, tolerance = 0)
Expand All @@ -60,26 +61,26 @@ test_that("report.data.frame", {
expect_equal(nrow(as.data.frame(r)), 7, tolerance = 0)
expect_equal(mean(as.data.frame(r)$n_Obs), 107, tolerance = 0.01)

r <- report(group_by(iris, Species))
r <- report(dplyr::group_by(iris, Species))
expect_equal(nrow(as.data.frame(r)), 8, tolerance = 0)
expect_equal(mean(as.data.frame(r)$n_Obs), 50, tolerance = 0)

expect_snapshot(variant = "windows", r)
})

test_that("report.data.frame - with NAs", {
skip_if_not_or_load_if_installed("dplyr")
skip_if_not_installed("dplyr")

df <- mtcars
df[1, 2] <- NA
df[1, 6] <- NA

report_grouped_df <- report(group_by(df, cyl))
report_grouped_df <- report(dplyr::group_by(df, cyl))
expect_snapshot(variant = "windows", report_grouped_df)
})

test_that("report.data.frame - with list columns", {
skip_if_not_or_load_if_installed("dplyr")
skip_if_not_installed("dplyr")

set.seed(123)
expect_snapshot(variant = "windows", report(dplyr::starwars))
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-report.ivreg.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
skip_if_not_or_load_if_installed("ivreg")
skip_if_not_installed("ivreg")

test_that("report-survreg", {
data("CigaretteDemand", package = "ivreg")

# model
set.seed(123)
ivr <-
ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
data = CigaretteDemand
ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
data = ivreg::CigaretteDemand
)

expect_snapshot(variant = "windows", report(ivr))
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-report.lavaan.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
skip_if_not_or_load_if_installed("lavaan")
skip_if_not_installed("lavaan")

structure <- " ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3
dem60 ~ ind60 "

set.seed(123)
model <- lavaan::sem(structure, data = PoliticalDemocracy)
model <- lavaan::sem(structure, data = lavaan::PoliticalDemocracy)

# Specific reports
test_that("model-lavaan detailed report", {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-report.lm.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
skip_if_not(getRversion() <= "4.2.1")
# This skip does not seem necessary??
# Readding back because of a .1 decimal difference in snapshots

test_that("report.lm - lm", {
# lm -------
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-report.lmer.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
skip_if_not_or_load_if_installed("lme4")
skip_if_not_installed("lme4")

test_that("report-lmer", {
df <- lme4::sleepstudy
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-report.stanreg.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
skip_if_not_or_load_if_installed("rstanarm")
skip_if_not_installed("rstanarm")

set.seed(123)
model <- suppressWarnings(stan_glm(mpg ~ qsec + wt, data = mtcars, refresh = 0, iter = 300))
model <- suppressWarnings(rstanarm::stan_glm(mpg ~ qsec + wt, data = mtcars, refresh = 0, iter = 300))

test_that("model-stanreg", {
r <- report(model, centrality = "mean")
Expand Down
11 changes: 9 additions & 2 deletions tests/testthat/test-report.survreg.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
test_that("report-survreg", {
skip_if_not_or_load_if_installed("survival")
skip_if_not_installed("survival")
require("survival", quietly = TRUE)

# TODO: Use namespace when https://github.com/easystats/datawizard/issues/401 is resolved
set.seed(123)
mod_survreg <- survival::survreg(
mod_survreg <- survreg(
formula = Surv(futime, fustat) ~ ecog.ps + rx,
data = ovarian,
dist = "logistic"
)

expect_snapshot(variant = "windows", report(mod_survreg))

unloadNamespace("rstanarm")
unloadNamespace("multcomp")
unloadNamespace("TH.data")
unloadNamespace("survival")
})
34 changes: 26 additions & 8 deletions tests/testthat/test-report_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test_that("report_performance", {
)

# Mixed models
skip_if_not_or_load_if_installed("lme4")
skip_if_not_installed("lme4")

x <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
expect_identical(
Expand Down Expand Up @@ -61,11 +61,10 @@ test_that("report_performance", {
)
)


# Mixed models
skip_if_not_or_load_if_installed("lme4")
skip_if_not_installed("lme4")

x <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
x <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
expect_identical(
as.character(report_performance(x)),
paste(
Expand Down Expand Up @@ -98,9 +97,12 @@ test_that("report_performance", {
)

# Bayesian
skip_if_not_or_load_if_installed("rstanarm")
skip_if_not_installed("rstanarm")

x <- stan_glm(Sepal.Length ~ Species, data = iris, refresh = 0, iter = 1000, seed = 333)
x <- rstanarm::stan_glm(
Sepal.Length ~ Species,
data = iris, refresh = 0, iter = 1000, seed = 333
)
expect_snapshot(
variant = "windows",
report_performance(x)
Expand All @@ -110,7 +112,10 @@ test_that("report_performance", {
summary(report_performance(x))
)

x <- stan_glm(vs ~ disp, data = mtcars, family = "binomial", refresh = 0, iter = 1000, seed = 333)
x <- rstanarm::stan_glm(vs ~ disp,
data = mtcars, family = "binomial",
refresh = 0, iter = 1000, seed = 333
)
expect_snapshot(
variant = "windows",
report_performance(x)
Expand All @@ -120,7 +125,19 @@ test_that("report_performance", {
summary(report_performance(x))
)

x <- stan_lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris, refresh = 0, iter = 1000, seed = 333)
suppressWarnings(suppressMessages(library(rstanarm)))
# Using namespace instead of loading the package throws an error:
# could not find function "stan_glmer"
# But we don't call "stan_glmer" directly, I suppose it must be called internally
# Even defining it manually, however, we get another error:
# stan_glmer <- rstanarm::stan_glmer
# Unable to refit the model with standardized data.
# Try instead to standardize the data (standardize(data)) and refit the
# model manually.

x <- rstanarm::stan_lmer(Sepal.Length ~ Petal.Length + (1 | Species),
data = iris, refresh = 0, iter = 1000, seed = 333
)
expect_snapshot(
variant = "windows",
report_performance(x)
Expand All @@ -129,4 +146,5 @@ test_that("report_performance", {
variant = "windows",
summary(report_performance(x))
)
unloadNamespace("rstanarm")
})
1 change: 1 addition & 0 deletions tests/testthat/test-report_sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test_that("report_sample weights, coorect weighted N", {
})

test_that("report_sample check input", {
skip_if(packageVersion("parameters") < "0.20.3")
data(iris)
expect_error(report_sample(lm(Sepal.Length ~ Species, data = iris)))
expect_silent(report_sample(iris$Species))
Expand Down

0 comments on commit c5446ea

Please sign in to comment.