Skip to content

Commit 5a05326

Browse files
committed
Move edge-case tests to existing test files
1 parent ed8b058 commit 5a05326

9 files changed

Lines changed: 92 additions & 198 deletions

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# bayesplot (development version)
22

3-
* Added dedicated edge-case tests for all exported `_data()` functions.
3+
* Added singleton-dimension edge-case tests for exported `_data()` functions.
44
* Added unit tests for `mcmc_areas_ridges_data()`, `mcmc_parcoord_data()`, and `mcmc_trace_data()`.
55
* Added unit tests for `ppc_error_data()` and `ppc_loo_pit_data()` covering output structure, argument handling, and edge cases.
66
* Added vignette sections demonstrating `*_data()` companion functions for building custom ggplot2 visualizations (#435)

tests/testthat/test-data-functions-edge-cases.R

Lines changed: 0 additions & 197 deletions
This file was deleted.

tests/testthat/test-ppc-discrete.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ test_that("ppc_bars_data includes all levels", {
7777
expect_equal(d3$h[2], 0, ignore_attr = TRUE)
7878
})
7979

80+
test_that("ppc_bars_data handles single observation and single draw", {
81+
y1 <- 2L
82+
yrep1 <- matrix(c(1L, 2L, 3L, 2L, 2L), ncol = 1)
83+
d <- ppc_bars_data(y1, yrep1)
84+
expect_s3_class(d, "data.frame")
85+
expect_equal(d$y_obs[d$x == 2], 1)
86+
87+
# single draw: interval collapses to a point
88+
y_s <- c(1L, 2L, 3L, 2L)
89+
yrep_s <- matrix(c(1L, 2L, 2L, 3L), nrow = 1)
90+
d2 <- ppc_bars_data(y_s, yrep_s)
91+
expect_equal(d2$l, d2$m, ignore_attr = TRUE)
92+
expect_equal(d2$m, d2$h, ignore_attr = TRUE)
93+
})
94+
95+
test_that("ppc_bars_data prob = 0 collapses interval to median", {
96+
d <- ppc_bars_data(y_ord, yrep_ord, prob = 0)
97+
expect_equal(d$l, d$m, ignore_attr = TRUE)
98+
expect_equal(d$m, d$h, ignore_attr = TRUE)
99+
})
100+
80101

81102
# rootograms -----------------------------------------------------------
82103
yrep3 <- matrix(yrep2, nrow = 5, ncol = ncol(yrep2), byrow = TRUE)

tests/testthat/test-ppc-distributions.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ test_that("ppd_data handles a single replicate matrix", {
221221
expect_equal(d$value, c(11, 21))
222222
})
223223

224+
test_that("ppd_data handles single observation (single column)", {
225+
ypred <- matrix(c(1, 2, 3), ncol = 1)
226+
d <- ppd_data(ypred)
227+
expect_equal(nrow(d), 3)
228+
expect_true(all(d$y_id == 1))
229+
expect_equal(d$value, c(1, 2, 3))
230+
})
231+
224232

225233
# Visual tests -----------------------------------------------------------------
226234

tests/testthat/test-ppc-errors.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ test_that("ppc_error_data with group returns exact structure", {
8585
expect_equal(d$group[d$rep_id == 1], group)
8686
})
8787

88+
test_that("ppc_error_data handles single observation", {
89+
y1 <- 5
90+
yrep1 <- matrix(c(4, 6, 5), ncol = 1)
91+
d <- ppc_error_data(y1, yrep1)
92+
expect_equal(nrow(d), 3)
93+
expect_equal(d$value, y1 - yrep1[, 1])
94+
expect_true(all(d$y_obs == 5))
95+
})
96+
8897

8998
# Visual tests -----------------------------------------------------------------
9099

tests/testthat/test-ppc-intervals.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ test_that("ppd_intervals_data + y_obs column same as ppc_intervals_data", {
7272
expect_equal(tibble::add_column(d_group2, y_obs = d_group$y_obs, .after = "y_id"), d_group)
7373
})
7474

75+
test_that("ppd_intervals_data handles single observation and single draw", {
76+
yrep_1obs <- matrix(rnorm(25), ncol = 1)
77+
d <- ppd_intervals_data(yrep_1obs)
78+
expect_equal(nrow(d), 1)
79+
expect_true(d$ll <= d$l && d$l <= d$m && d$m <= d$h && d$h <= d$hh)
80+
81+
# single draw: all quantiles collapse to the value
82+
yrep_1draw <- matrix(rnorm(10), nrow = 1)
83+
d2 <- ppd_intervals_data(yrep_1draw)
84+
expect_equal(d2$ll, d2$m)
85+
expect_equal(d2$hh, d2$m)
86+
})
87+
7588
test_that("ppc_intervals_data does math correctly", {
7689
d <- ppc_intervals_data(y, yrep, prob = .4, prob_outer = .8)
7790
qs <- unname(quantile(yrep[, 1], c(.1, .3, .5, .7, .9)))

tests/testthat/test-ppc-loo.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,10 @@ test_that("ppc_loo_pit_data returns the expected structure for both boundary mod
399399
expect_equal(nrow(yrep_rows), grid_len * n_samples)
400400
expect_false(anyNA(d_bc$x))
401401
})
402+
403+
test_that("ppc_loo_pit_data works with a single pit value", {
404+
d <- suppressMessages(ppc_loo_pit_data(pit = 0.5, boundary_correction = FALSE, samples = 3))
405+
y_rows <- d[d$is_y, ]
406+
expect_equal(nrow(y_rows), 1)
407+
expect_equal(y_rows$value, 0.5)
408+
})

tests/testthat/test-ppc-scatterplots.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ test_that("ppc_scatter_avg_data can take a custom fun_avg", {
3434
expect_equal(sums$value, colSums(yrep))
3535
})
3636

37+
test_that("ppc_scatter_data handles single observation and single draw", {
38+
y1 <- 5
39+
yrep1 <- matrix(c(4, 6, 5), ncol = 1)
40+
d <- ppc_scatter_data(y1, yrep1)
41+
expect_equal(nrow(d), 3)
42+
expect_true(all(d$y_obs == 5))
43+
expect_equal(d$value, c(4, 6, 5))
44+
45+
# single draw
46+
d2 <- ppc_scatter_data(y, yrep[1, , drop = FALSE])
47+
expect_equal(nrow(d2), length(y))
48+
expect_equal(d2$value, yrep[1, ])
49+
expect_equal(d2$y_obs, y)
50+
})
51+
52+
test_that("ppc_scatter_avg_data handles single observation", {
53+
y1 <- 5
54+
yrep1 <- matrix(c(4, 6, 5), ncol = 1)
55+
d <- ppc_scatter_avg_data(y1, yrep1)
56+
expect_equal(nrow(d), 1)
57+
expect_equal(d$value, mean(c(4, 6, 5)))
58+
expect_equal(d$y_obs, 5)
59+
})
3760

3861

3962
# Visual tests ------------------------------------------------------------

tests/testthat/test-ppc-test-statistics.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ test_that("ppc_stat_data and ppd_stat_data throw correct errors", {
129129
"object 'not_a_known_function' of mode 'function' was not found")
130130
})
131131

132+
test_that("ppd_stat_data handles single draw and single observation", {
133+
yrep_single <- matrix(rnorm(10), nrow = 1)
134+
d <- ppd_stat_data(yrep_single, stat = "mean")
135+
expect_equal(nrow(d), 1)
136+
137+
yrep_1obs <- matrix(rnorm(5), ncol = 1)
138+
d2 <- ppd_stat_data(yrep_1obs, stat = "mean")
139+
expect_s3_class(d2, "data.frame")
140+
})
141+
132142

133143
# Visual tests ------------------------------------------------------------
134144

0 commit comments

Comments
 (0)