Skip to content

Commit fed5b26

Browse files
authored
Merge pull request #8 from M-Colley/codex/fix-test-failures-in-rscript-execution
Add mockable wrappers for external dependencies
2 parents 32bbca6 + 08c566c commit fed5b26

2 files changed

Lines changed: 45 additions & 36 deletions

File tree

r_functionality.R

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ n_fun <- function(x){
220220
#' @export
221221
#'
222222
#' @examples
223-
havingIP <- function() {
224-
if (requireNamespace("curl", quietly = TRUE)) {
225-
return(isTRUE(tryCatch(curl::has_internet(), error = function(...) FALSE)))
226-
}
223+
havingIP <- function() {
224+
if (requireNamespace("curl", quietly = TRUE)) {
225+
return(isTRUE(tryCatch(curl_has_internet(), error = function(...) FALSE)))
226+
}
227227

228228
warning("Package 'curl' is not available; assuming no internet connection.")
229229
FALSE
@@ -411,7 +411,7 @@ ggwithinstatsWithPriorNormalityCheck <- function(data, x, y, ylab, xlabels = NUL
411411
sufficient_sample <- length(subset_data) >= 3
412412

413413
if (sufficient_sample && has_variation) {
414-
normality_test[[group]] <- shapiro.test(subset_data)
414+
normality_test[[group]] <- shapiro_test_wrapper(subset_data)
415415
} else {
416416
normality_test[[group]] <- NULL
417417
if (!sufficient_sample) {
@@ -441,7 +441,7 @@ ggwithinstatsWithPriorNormalityCheck <- function(data, x, y, ylab, xlabels = NUL
441441

442442

443443

444-
plot <- ggstatsplot::ggwithinstats(
444+
plot <- ggwithinstats_wrapper(
445445
data = data, x = !!x, y = !!y, type = type, centrality.type = "p", ylab = ylab, xlab = "", pairwise.comparisons = showPairwiseComp, var.equal = group_all_data_equal,
446446
centrality.point.args = list(size = 5, alpha = 0.5, color = "darkblue"), package = "pals", palette = "glasbey",
447447
p.adjust.method = "holm", ggplot.component = list(theme(text = element_text(size = 16), plot.subtitle = element_text(size = 17, face = "bold"))), ggsignif.args = list(textsize = 4, tip_length = 0.01)
@@ -499,7 +499,7 @@ ggbetweenstatsWithPriorNormalityCheck <- function(data, x, y, ylab, xlabels, sho
499499
sufficient_sample <- length(subset_data) >= 3
500500

501501
if (sufficient_sample && has_variation) {
502-
normality_test[[group]] <- shapiro.test(subset_data)
502+
normality_test[[group]] <- shapiro_test_wrapper(subset_data)
503503
} else {
504504
normality_test[[group]] <- NULL
505505
if (!sufficient_sample) {
@@ -528,7 +528,7 @@ ggbetweenstatsWithPriorNormalityCheck <- function(data, x, y, ylab, xlabels, sho
528528
type <- ifelse(normallyDistributed, "p", "np")
529529

530530
# if one group_all_data_equal then we use the var.equal = TRUE, see here: https://github.com/IndrajeetPatil/ggstatsplot/issues/880
531-
ggstatsplot::ggbetweenstats(
531+
ggbetweenstats_wrapper(
532532
data = data, x = !!x, y = !!y, type = type, centrality.type = "p", ylab = ylab, xlab = "", pairwise.comparisons = showPairwiseComp, var.equal = group_all_data_equal,
533533
centrality.point.args = list(size = 5, alpha = 0.5, color = "darkblue"), package = "pals", palette = "glasbey", plot.type = plotType,
534534
p.adjust.method = "holm", ggplot.component = list(theme(text = element_text(size = 16), plot.subtitle = element_text(size = 17, face = "bold"))), ggsignif.args = list(textsize = 4, tip_length = 0.01)
@@ -580,7 +580,7 @@ ggbetweenstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlab
580580
sufficient_sample <- length(subset_data) >= 3
581581

582582
if (sufficient_sample && has_variation) {
583-
normality_test[[group]] <- shapiro.test(subset_data)
583+
normality_test[[group]] <- shapiro_test_wrapper(subset_data)
584584
} else {
585585
normality_test[[group]] <- NULL
586586
if (!sufficient_sample) {
@@ -609,7 +609,7 @@ ggbetweenstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlab
609609

610610

611611
(df <-
612-
pairwise_comparisons(data = data, x = !!x, y = !!y, type = type, p.adjust.method = "holm") %>%
612+
pairwise_comparisons_wrapper(data = data, x = !!x, y = !!y, type = type, p.adjust.method = "holm") %>%
613613
dplyr::mutate(groups = purrr::pmap(.l = list(group1, group2), .f = c)) %>%
614614
dplyr::arrange(group1) %>%
615615
dplyr::mutate(asterisk_label = ifelse(`p.value` < 0.05 & `p.value` > 0.01, "*", ifelse(`p.value` < 0.01 & `p.value` > 0.001, "**", ifelse(`p.value` < 0.001, "***", NA)))))
@@ -632,13 +632,13 @@ ggbetweenstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlab
632632
}
633633

634634

635-
p <- ggstatsplot::ggbetweenstats(
635+
p <- ggbetweenstats_wrapper(
636636
data = data, x = !!x, y = !!y, type = type, centrality.type = "p", ylab = ylab, xlab = "", pairwise.display = "none", var.equal = group_all_data_equal,
637637
centrality.point.args = list(size = 5, alpha = 0.5, color = "darkblue"), package = "pals", palette = "glasbey", plot.type = plotType,
638638
p.adjust.method = "holm", ggplot.component = list(theme(text = element_text(size = 16), plot.subtitle = element_text(size = 17, face = "bold"))), ggsignif.args = list(textsize = 4, tip_length = 0.01)
639639
) + scale_x_discrete(labels = xlabels)
640640

641-
p + ggsignif::geom_signif(
641+
p + geom_signif_wrapper(
642642
comparisons = df$groups,
643643
map_signif_level = TRUE,
644644
annotations = df$asterisk_label,
@@ -677,7 +677,7 @@ ggwithinstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlabe
677677
sufficient_sample <- length(subset_data) >= 3
678678

679679
if (sufficient_sample && has_variation) {
680-
normality_test[[group]] <- shapiro.test(subset_data)
680+
normality_test[[group]] <- shapiro_test_wrapper(subset_data)
681681
} else {
682682
normality_test[[group]] <- NULL
683683
normality_assessable <- FALSE
@@ -700,7 +700,7 @@ ggwithinstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlabe
700700

701701

702702
(df <-
703-
pairwise_comparisons(data = data, x = !!x, y = !!y, type = type, p.adjust.method = "holm") %>%
703+
pairwise_comparisons_wrapper(data = data, x = !!x, y = !!y, type = type, p.adjust.method = "holm") %>%
704704
dplyr::mutate(groups = purrr::pmap(.l = list(group1, group2), .f = c)) %>%
705705
dplyr::arrange(group1) %>%
706706
dplyr::mutate(asterisk_label = ifelse(`p.value` < 0.05 & `p.value` > 0.01, "*", ifelse(`p.value` < 0.01 & `p.value` > 0.001, "**", ifelse(`p.value` < 0.001, "***", NA)))))
@@ -723,13 +723,13 @@ ggwithinstatsWithPriorNormalityCheckAsterisk <- function(data, x, y, ylab, xlabe
723723
}
724724

725725

726-
p <- ggstatsplot::ggwithinstats(
726+
p <- ggwithinstats_wrapper(
727727
data = data, x = !!x, y = !!y, type = type, centrality.type = "p", ylab = ylab, xlab = "", pairwise.display = "none",
728728
centrality.point.args = list(size = 5, alpha = 0.5, color = "darkblue"), package = "pals", palette = "glasbey", plot.type = plotType,
729729
p.adjust.method = "holm", ggplot.component = list(theme(text = element_text(size = 16), plot.subtitle = element_text(size = 17, face = "bold"))), ggsignif.args = list(textsize = 4, tip_length = 0.01)
730730
) + scale_x_discrete(labels = xlabels)
731731

732-
p + ggsignif::geom_signif(
732+
p + geom_signif_wrapper(
733733
comparisons = df$groups,
734734
map_signif_level = TRUE,
735735
annotations = df$asterisk_label,
@@ -1878,7 +1878,7 @@ reportggstatsplot <- function(p, iv = "independent", dv = "Testdependentvariable
18781878
not_empty(dv)
18791879
not_empty(iv)
18801880

1881-
stats <- extract_stats(p)$subtitle_data
1881+
stats <- extract_stats_wrapper(p)$subtitle_data
18821882
resultString <- ""
18831883

18841884
effectSize <- round(stats$estimate, digits = 2)
@@ -2687,7 +2687,7 @@ reportggstatsplotPostHoc <- function(data, p, iv = "testiv", dv = "testdv", labe
26872687
not_empty(dv)
26882688

26892689
# Extract stats from the ggstatsplot object
2690-
stats <- extract_stats(p)$pairwise_comparisons_data
2690+
stats <- extract_stats_wrapper(p)$pairwise_comparisons_data
26912691

26922692
if (!any(stats$p.value < 0.05, na.rm = TRUE)) {
26932693
cat(paste0("A post-hoc test found no significant differences for ", dv, ". "))
@@ -2757,3 +2757,12 @@ reportggstatsplotPostHoc <- function(data, p, iv = "testiv", dv = "testdv", labe
27572757

27582758

27592759

2760+
# Helper wrappers around external functions so they can be mocked in tests
2761+
curl_has_internet <- function(...) curl::has_internet(...)
2762+
ggwithinstats_wrapper <- function(...) ggstatsplot::ggwithinstats(...)
2763+
ggbetweenstats_wrapper <- function(...) ggstatsplot::ggbetweenstats(...)
2764+
pairwise_comparisons_wrapper <- function(...) ggstatsplot::pairwise_comparisons(...)
2765+
geom_signif_wrapper <- function(...) ggsignif::geom_signif(...)
2766+
shapiro_test_wrapper <- function(...) stats::shapiro.test(...)
2767+
extract_stats_wrapper <- function(...) ggstatsplot::extract_stats(...)
2768+

tests/testthat/test_r_functionality.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ posthoc_stats <- list(
7474
basic_plot <- ggplot2::ggplot(sample_df, ggplot2::aes(x = ConditionID, y = value)) + ggplot2::geom_point()
7575

7676
# Helper wrapper to avoid relying on pkgload/devtools metadata when mocking
77-
with_mock <- function(..., .env = parent.frame()) {
78-
testthat::with_mocked_bindings(..., .package = "base", .env = .env)
77+
with_mock <- function(..., .env = globalenv()) {
78+
testthat::with_mocked_bindings(..., .env = .env)
7979
}
8080

8181

@@ -97,7 +97,7 @@ test_that("basic utility helpers behave", {
9797
expect_s3_class(n_result, "data.frame")
9898
expect_equal(n_result$label, paste0("n = ", length(sample_df$value)))
9999

100-
with_mock(`curl::has_internet` = function(...) TRUE, {
100+
with_mock(curl_has_internet = function(...) TRUE, {
101101
expect_true(havingIP())
102102
})
103103

@@ -136,19 +136,19 @@ test_that("within and between wrappers choose correct type", {
136136
expect_equal(result$type, "p")
137137

138138
np_result <- with_mock(
139-
`ggstatsplot::ggwithinstats` = function(..., type) list(type = type),
140-
shapiro.test = function(...) list(p.value = 0.001),
139+
ggwithinstats_wrapper = function(..., type) list(type = type),
140+
shapiro_test_wrapper = function(...) list(p.value = 0.001),
141141
{
142142
ggwithinstatsWithPriorNormalityCheck(data, "group", "value", "Value")
143143
}
144144
)
145145
expect_equal(np_result$type, "np")
146146

147147
between <- with_mock(
148-
`ggstatsplot::ggbetweenstats` = function(..., type) list(type = type),
149-
shapiro.test = function(...) list(p.value = 0.001),
150-
pairwise_comparisons = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
151-
`ggsignif::geom_signif` = function(...) ggplot2::geom_blank(),
148+
ggbetweenstats_wrapper = function(..., type) list(type = type),
149+
shapiro_test_wrapper = function(...) list(p.value = 0.001),
150+
pairwise_comparisons_wrapper = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
151+
geom_signif_wrapper = function(...) ggplot2::geom_blank(),
152152
{
153153
ggbetweenstatsWithPriorNormalityCheck(data, "group", "value", "Value", c("A", "B"))
154154
}
@@ -157,9 +157,9 @@ test_that("within and between wrappers choose correct type", {
157157

158158
expect_s3_class(
159159
with_mock(
160-
`ggstatsplot::ggbetweenstats` = function(...) ggplot2::ggplot(),
161-
pairwise_comparisons = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
162-
`ggsignif::geom_signif` = function(...) ggplot2::geom_blank(),
160+
ggbetweenstats_wrapper = function(...) ggplot2::ggplot(),
161+
pairwise_comparisons_wrapper = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
162+
geom_signif_wrapper = function(...) ggplot2::geom_blank(),
163163
{
164164
ggbetweenstatsWithPriorNormalityCheckAsterisk(data, "group", "value", "Value", c("A", "B"))
165165
}
@@ -169,10 +169,10 @@ test_that("within and between wrappers choose correct type", {
169169

170170
expect_s3_class(
171171
with_mock(
172-
`ggstatsplot::ggwithinstats` = function(...) ggplot2::ggplot(),
173-
pairwise_comparisons = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
174-
`ggsignif::geom_signif` = function(...) ggplot2::geom_blank(),
175-
shapiro.test = function(...) list(p.value = 0.2),
172+
ggwithinstats_wrapper = function(...) ggplot2::ggplot(),
173+
pairwise_comparisons_wrapper = function(...) data.frame(group1 = "A", group2 = "B", `p.value` = 0.01, stringsAsFactors = FALSE),
174+
geom_signif_wrapper = function(...) ggplot2::geom_blank(),
175+
shapiro_test_wrapper = function(...) list(p.value = 0.2),
176176
{
177177
ggwithinstatsWithPriorNormalityCheckAsterisk(data, "group", "value", "Value", c("A", "B"))
178178
}
@@ -222,7 +222,7 @@ test_that("reporting helpers include effect sizes", {
222222
expect_match(
223223
capture.output(
224224
with_mock(
225-
extract_stats = function(...) posthoc_stats,
225+
extract_stats_wrapper = function(...) posthoc_stats,
226226
{
227227
reportggstatsplot(basic_plot, iv = "group", dv = "score")
228228
}
@@ -233,7 +233,7 @@ test_that("reporting helpers include effect sizes", {
233233

234234
expect_output(
235235
with_mock(
236-
extract_stats = function(...) posthoc_stats,
236+
extract_stats_wrapper = function(...) posthoc_stats,
237237
{
238238
reportggstatsplotPostHoc(report_data, basic_plot, iv = "group", dv = "score")
239239
}

0 commit comments

Comments
 (0)