# ============================================================================== # tests/testthat/test-brood_plot.R # ============================================================================== make_brood_snapshot <- function() { d <- data.frame( stratum = c("0-17", "18-64", "65+"), reference_date = as.Date(NA), coverage = c(0.62, 0.81, 0.93), stringsAsFactors = FALSE ) class(d) <- c("brood_df", "data.frame") attr(d, "time_series") <- FALSE d } make_brood_timeseries <- function(with_intervention = TRUE) { d <- data.frame( stratum = rep(c("0-17", "18-64"), each = 4), reference_date = rep(seq(as.Date("2024-01-01"), by = "month", length.out = 4), 2), coverage = c(0.10, 0.30, 0.55, 0.70, 0.05, 0.20, 0.45, 0.68), stringsAsFactors = FALSE ) class(d) <- c("brood_df", "data.frame") attr(d, "time_series") <- TRUE if (with_intervention) attr(d, "intervention_date_ts") <- as.Date("2024-02-15") d } test_that("brood_plot() draws a bar chart for single-timepoint data", { p <- brood_plot(make_brood_snapshot()) expect_s3_class(p, "ggplot") built <- ggplot2::layer_data(p, 1) expect_equal(sort(built$y), sort(c(0.62, 0.81, 0.93))) }) test_that("brood_plot() draws a line chart for time-series data", { p <- brood_plot(make_brood_timeseries()) built <- ggplot2::layer_data(p, 1) expect_equal(nrow(built), 8) # 4 time points x 2 strata }) test_that("brood_plot() draws the intervention line when present", { p_with <- brood_plot(make_brood_timeseries(with_intervention = TRUE)) p_without <- brood_plot(make_brood_timeseries(with_intervention = FALSE)) expect_gt(length(p_with$layers), length(p_without$layers)) }) test_that("brood_plot() show_intervention = FALSE suppresses the line even when present", { p <- brood_plot(make_brood_timeseries(with_intervention = TRUE), show_intervention = FALSE) p_without <- brood_plot(make_brood_timeseries(with_intervention = FALSE)) expect_equal(length(p$layers), length(p_without$layers)) }) test_that("brood_plot() draws a target_coverage reference line", { p_with <- brood_plot(make_brood_snapshot(), target_coverage = 0.95) p_without <- brood_plot(make_brood_snapshot()) expect_gt(length(p_with$layers), length(p_without$layers)) }) test_that("brood_plot() rejects an out-of-range target_coverage", { expect_error(brood_plot(make_brood_snapshot(), target_coverage = 1.5), "between 0 and 1") }) test_that("brood_plot() rejects data missing required columns", { d <- data.frame(x = 1:3) expect_error(brood_plot(d), "missing required column") }) test_that("brood_plot() warns (but proceeds) when data lacks the brood_df class", { d <- make_brood_snapshot() class(d) <- "data.frame" expect_warning(p <- brood_plot(d), "brood_df") expect_s3_class(p, "ggplot") }) test_that("brood_plot() time-series without reference_date errors clearly", { d <- make_brood_timeseries() d$reference_date <- NULL expect_error(brood_plot(d), "reference_date") }) test_that("brood_plot() area type is accepted for time-series data", { p <- brood_plot(make_brood_timeseries(), type = "area") expect_s3_class(p, "ggplot") })