Skip to content

Commit c5e702b

Browse files
committed
consolidate new tests
1 parent 65ca627 commit c5e702b

2 files changed

Lines changed: 91 additions & 71 deletions

File tree

tests/testthat/test-ppc-distributions.R

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,97 @@ test_that("ppc_violin_grouped returns a ggplot object", {
129129
expect_gg(ppc_violin_grouped(y, yrep, group, y_draw = "both", y_jitter = 0.3))
130130
})
131131

132+
# ppc_data / ppd_data tests -----------------------------------------------
133+
134+
test_that("ppc_data returns the correct structure", {
135+
y_small <- c(10, 20)
136+
yrep_small <- rbind(c(11, 21), c(12, 22))
137+
138+
d <- ppc_data(y_small, yrep_small)
139+
140+
expect_s3_class(d, "data.frame")
141+
expect_named(d, c("y_id", "y_name", "rep_id", "rep_label",
142+
"is_y", "is_y_label", "value"))
143+
expect_equal(nrow(d), length(y_small) * (nrow(yrep_small) + 1))
144+
expect_equal(d$y_id, c(1L, 1L, 2L, 2L, 1L, 2L))
145+
expect_equal(as.character(d$y_name), c("1", "1", "2", "2", "1", "2"))
146+
expect_equal(d$rep_id, c(1L, 2L, 1L, 2L, NA, NA))
147+
expect_equal(d$is_y, c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE))
148+
expect_equal(d$value, c(11, 12, 21, 22, 10, 20))
149+
expect_equal(d$value[d$is_y], y_small)
150+
151+
first_level <- levels(d$rep_label)[1]
152+
expect_true(all(as.character(d$rep_label[d$is_y]) == first_level))
153+
expect_true(all(as.character(d$rep_label[!d$is_y]) != first_level))
154+
})
155+
156+
test_that("ppc_data carries group through correctly", {
157+
y_small <- c(10, 20)
158+
yrep_small <- rbind(c(11, 21), c(12, 22))
159+
group_small <- factor(c("a", "b"))
160+
161+
d <- ppc_data(y_small, yrep_small, group = group_small)
162+
163+
expect_named(d, c("group", "y_id", "y_name", "rep_id", "rep_label",
164+
"is_y", "is_y_label", "value"))
165+
expect_equal(as.character(d$group), c("a", "a", "b", "b", "a", "b"))
166+
expect_equal(as.character(d$group[d$is_y]), as.character(group_small))
167+
})
168+
169+
test_that("ppc_data handles a single replicate matrix", {
170+
y_small <- c(10, 20)
171+
yrep_small <- matrix(c(11, 21), nrow = 1)
172+
173+
d <- ppc_data(y_small, yrep_small)
174+
175+
expect_equal(sum(!d$is_y), length(y_small))
176+
expect_equal(d$rep_id[!d$is_y], c(1L, 1L))
177+
expect_equal(d$value[!d$is_y], c(11, 21))
178+
})
179+
180+
test_that("ppd_data returns the correct structure", {
181+
yrep_small <- rbind(c(11, 21), c(12, 22))
182+
183+
d <- ppd_data(yrep_small)
184+
185+
expect_s3_class(d, "data.frame")
186+
expect_named(d, c("y_id", "y_name", "rep_id", "rep_label", "value"))
187+
expect_equal(nrow(d), nrow(yrep_small) * ncol(yrep_small))
188+
expect_equal(d$y_id, c(1L, 1L, 2L, 2L))
189+
expect_equal(as.character(d$y_name), c("1", "1", "2", "2"))
190+
expect_equal(d$rep_id, c(1L, 2L, 1L, 2L))
191+
expect_equal(d$value, c(11, 12, 21, 22))
192+
expect_true(all(grepl("pred", levels(d$rep_label), fixed = TRUE)))
193+
})
194+
195+
test_that("ppd_data carries group through correctly", {
196+
yrep_small <- rbind(c(11, 21), c(12, 22))
197+
group_small <- factor(c("a", "b"))
198+
199+
d <- ppd_data(yrep_small, group = group_small)
200+
201+
expect_named(d, c("group", "y_id", "y_name", "rep_id", "rep_label", "value"))
202+
expect_equal(as.character(d$group), c("a", "a", "b", "b"))
203+
})
204+
205+
test_that("ppd_data carries observation names through to y_name", {
206+
yrep_named <- rbind(c(11, 21), c(12, 22))
207+
colnames(yrep_named) <- c("obs_a", "obs_b")
208+
209+
d <- ppd_data(yrep_named)
210+
211+
expect_equal(as.character(d$y_name), c("obs_a", "obs_a", "obs_b", "obs_b"))
212+
})
213+
214+
test_that("ppd_data handles a single replicate matrix", {
215+
yrep_small <- matrix(c(11, 21), nrow = 1)
216+
217+
d <- ppd_data(yrep_small)
218+
219+
expect_equal(nrow(d), ncol(yrep_small))
220+
expect_equal(d$rep_id, c(1L, 1L))
221+
expect_equal(d$value, c(11, 21))
222+
})
132223

133224

134225
# Visual tests -----------------------------------------------------------------
@@ -422,54 +513,3 @@ test_that("ppc_pit_ecdf, ppc_pit_ecdf_grouped renders correctly", {
422513
vdiffr::expect_doppelganger("ppc_pit_ecdf (diff)", p_diff)
423514
vdiffr::expect_doppelganger("ppc_pit_ecdf_grouped (diff)", g_diff)
424515
})
425-
426-
427-
# ppc_data / ppd_data tests -----------------------------------------------
428-
429-
test_that("ppc_data returns correct structure", {
430-
d <- ppc_data(y, yrep)
431-
expect_s3_class(d, "data.frame")
432-
expect_true(all(c("y_id", "rep_id", "rep_label", "is_y", "value") %in% names(d)))
433-
})
434-
435-
test_that("ppc_data includes y and yrep rows", {
436-
d <- ppc_data(y, yrep)
437-
y_rows <- d[d$is_y, ]
438-
yrep_rows <- d[!d$is_y, ]
439-
expect_equal(nrow(y_rows), length(y))
440-
expect_equal(nrow(yrep_rows), length(y) * nrow(yrep))
441-
expect_equal(y_rows$value, y)
442-
})
443-
444-
test_that("ppc_data with group adds group column", {
445-
d <- ppc_data(y, yrep, group = group)
446-
expect_true("group" %in% names(d))
447-
expect_equal(nlevels(factor(d$group)), nlevels(group))
448-
})
449-
450-
test_that("ppc_data works with single replicate", {
451-
d <- ppc_data(y, yrep[1, , drop = FALSE])
452-
yrep_rows <- d[!d$is_y, ]
453-
expect_equal(nrow(yrep_rows), length(y))
454-
})
455-
456-
test_that("ppd_data returns correct structure", {
457-
d <- ppd_data(yrep)
458-
expect_s3_class(d, "data.frame")
459-
expect_true(all(c("y_id", "rep_id", "rep_label", "value") %in% names(d)))
460-
})
461-
462-
test_that("ppd_data returns correct number of rows", {
463-
d <- ppd_data(yrep)
464-
expect_equal(nrow(d), nrow(yrep) * ncol(yrep))
465-
})
466-
467-
test_that("ppd_data with group adds group column", {
468-
d <- ppd_data(yrep, group = group)
469-
expect_true("group" %in% names(d))
470-
})
471-
472-
test_that("ppd_data works with single replicate", {
473-
d <- ppd_data(yrep[1, , drop = FALSE])
474-
expect_equal(nrow(d), ncol(yrep))
475-
})

tests/testthat/test-ppc-intervals.R

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,3 @@ test_that("ppc_ribbon_grouped renders correctly", {
224224
group = vdiff_group)
225225
vdiffr::expect_doppelganger("ppd_ribbon_grouped (x values)", p_x)
226226
})
227-
228-
229-
# ppc_ribbon_data / ppd_ribbon_data alias tests ----------------------------
230-
231-
test_that("ppc_ribbon_data is identical to ppc_intervals_data", {
232-
expect_identical(ppc_ribbon_data, ppc_intervals_data)
233-
y_test <- rnorm(20)
234-
yrep_test <- matrix(rnorm(200), ncol = 20)
235-
d1 <- ppc_intervals_data(y_test, yrep_test, prob = 0.5, prob_outer = 0.9)
236-
d2 <- ppc_ribbon_data(y_test, yrep_test, prob = 0.5, prob_outer = 0.9)
237-
expect_identical(d1, d2)
238-
})
239-
240-
test_that("ppd_ribbon_data is identical to ppd_intervals_data", {
241-
expect_identical(ppd_ribbon_data, ppd_intervals_data)
242-
yrep_test <- matrix(rnorm(200), ncol = 20)
243-
d1 <- ppd_intervals_data(yrep_test, prob = 0.5, prob_outer = 0.9)
244-
d2 <- ppd_ribbon_data(yrep_test, prob = 0.5, prob_outer = 0.9)
245-
expect_identical(d1, d2)
246-
})

0 commit comments

Comments
 (0)