# ============================================================================== # tests/testthat/test-roost_plot.R # ============================================================================== make_roost_like <- function() { d <- data.frame( month = as.Date(c("2024-01-01", "2024-02-01", "2024-03-01")), n = c(10L, 14L, 9L) ) attr(d, "roost_meta") <- list(time_unit = "month") d } make_roost_like_stacked <- function() { d <- data.frame( month = rep(as.Date(c("2024-01-01", "2024-02-01")), each = 2), hosp_label = rep(c("Community", "Hospitalised"), 2), n = c(8L, 2L, 11L, 3L) ) attr(d, "roost_meta") <- list(time_unit = "month") d } make_roost_like_corrected <- function() { d <- make_roost_like() d$corrected_count <- d$n * 2 d$corrected_count_lower <- d$n * 1.5 d$corrected_count_upper <- d$n * 2.5 d } test_that("roost_plot() auto-detects x from roost_meta and y from 'n'", { p <- roost_plot(make_roost_like()) expect_s3_class(p, "ggplot") built <- ggplot2::layer_data(p, 1) expect_equal(sort(built$y), sort(c(10, 14, 9))) }) test_that("roost_plot() errors when x cannot be auto-detected and isn't supplied", { d <- data.frame(month = as.Date("2024-01-01"), n = 5L) expect_error(roost_plot(d), "auto-detected") }) test_that("roost_plot() requires y when several n_* columns exist and none is 'n'", { d <- data.frame(month = as.Date("2024-01-01"), n_cases = 5L, n_deaths = 1L) attr(d, "roost_meta") <- list(time_unit = "month") expect_error(roost_plot(d), "n_cases, n_deaths") }) test_that("roost_plot() resolves a single n_* candidate automatically", { d <- data.frame(month = as.Date("2024-01-01"), n_cases = 5L) attr(d, "roost_meta") <- list(time_unit = "month") p <- roost_plot(d) expect_s3_class(p, "ggplot") }) test_that("roost_plot() stacks bars by stack_by", { p <- roost_plot(make_roost_like_stacked(), stack_by = "hosp_label", type = "bar") built <- ggplot2::layer_data(p, 1) expect_equal(nrow(built), 4) # 2 months x 2 strata }) test_that("roost_plot() auto-detects and draws a corrected_count overlay", { p <- roost_plot(make_roost_like_corrected()) # bars (layer 1) + ribbon (layer 2) + corrected line (layer 3) expect_gte(length(p$layers), 3) line_layer <- ggplot2::layer_data(p, length(p$layers)) expect_equal(sort(line_layer$y), sort(c(20, 28, 18))) }) test_that("roost_plot() show_corrected = FALSE suppresses the overlay even when present", { p <- roost_plot(make_roost_like_corrected(), show_corrected = FALSE) expect_equal(length(p$layers), 1) # bars only }) test_that("roost_plot() show_corrected = TRUE errors if corrected_count is absent", { expect_error(roost_plot(make_roost_like(), show_corrected = TRUE), "corrected_count") }) test_that("roost_plot() corrected_smooth changes the plotted corrected values", { d <- data.frame( month = seq(as.Date("2024-01-01"), by = "month", length.out = 8), n = c(10L, 50L, 12L, 9L, 45L, 11L, 10L, 48L) # jagged, so smoothing has a visible effect ) attr(d, "roost_meta") <- list(time_unit = "month") d$corrected_count <- d$n * 2 p_raw <- roost_plot(d, corrected_smooth = FALSE) p_smooth <- roost_plot(d, corrected_smooth = TRUE) line_raw <- ggplot2::layer_data(p_raw, length(p_raw$layers)) line_smooth <- ggplot2::layer_data(p_smooth, length(p_smooth$layers)) expect_false(isTRUE(all.equal(line_raw$y, line_smooth$y))) }) test_that("roost_plot() sums the corrected overlay across stack_by groups", { d <- make_roost_like_stacked() d$corrected_count <- d$n * 2 p <- roost_plot(d, stack_by = "hosp_label") line_layer <- ggplot2::layer_data(p, length(p$layers)) # Jan: (8+2)*2 = 20 ; Feb: (11+3)*2 = 28 expect_equal(sort(line_layer$y), sort(c(20, 28))) }) test_that("roost_plot() rejects a non-data.frame", { expect_error(roost_plot(list(a = 1)), "data frame") }) test_that("roost_plot() rejects an unknown stack_by column", { expect_error(roost_plot(make_roost_like(), stack_by = "not_a_col"), "not found") }) test_that("roost_plot() interactive = TRUE without plotly errors informatively", { skip_if(requireNamespace("plotly", quietly = TRUE), "plotly is installed; this test only applies when it's absent") expect_error(roost_plot(make_roost_like(), interactive = TRUE), "plotly") })