test_that("sens_anova shares sum to 100", { set.seed(1) d <- expand.grid(A = c("a1","a2"), B = c("b1","b2","b3"), year = 1:10) d$y <- 10 + 3*(d$A=="a2") + 5*(d$B=="b3") + rnorm(nrow(d)) s <- sens_anova(d, "y", c("A","B"), order = 2) expect_equal(sum(s$si), 100, tolerance = 1e-6) expect_true(s$term[1] %in% c("A","B")) expect_true(all(c("main","interaction","residual") %in% s$kind)) }) test_that("sens_oat elasticity is signed correctly", { d <- data.frame(N = rep(c("0","100"), each = 5), y = c(rep(50,5), rep(100,5)), year = rep(1:5, 2), stringsAsFactors = FALSE) s <- sens_oat(d, "y", "N", baseline = list(N = "0")) expect_equal(s$pct, 100) expect_true(is.na(s$elasticity)) # baseline of 0 gives no percent change in x }) test_that("split_phase assigns both phases by date", { d <- data.frame(Date = as.Date("2000-01-01") + c(0, 100, 200, 300), obs = 1:4, pred = 1:4) p <- split_phase(d, "date", at = "2000-05-01") expect_equal(levels(p$phase), c("Calibration","Validation")) expect_equal(as.integer(table(p$phase)), c(2L, 2L)) }) test_that("split_phase by fraction respects date order", { d <- data.frame(Date = as.Date("2000-01-01") + (10:1), obs = 1:10, pred = 1:10) p <- split_phase(d, "fraction", at = 0.6) expect_equal(sum(p$phase == "Calibration"), 6) expect_true(max(p$Date[p$phase == "Calibration"]) <= min(p$Date[p$phase == "Validation"])) }) test_that("suggest_cutoff falls between the calibration and validation seasons", { d <- data.frame(Date = as.Date(c("2016-08-01","2016-10-01", "2017-08-01","2017-10-01"))) cut <- suggest_cutoff(d) expect_true(cut > as.Date("2016-10-01")) expect_true(cut < as.Date("2017-08-01")) expect_equal(sum(d$Date <= cut), 2L) }) test_that("suggest_cutoff honours a multi-season calibration", { d <- data.frame(Date = as.Date(paste0(2016:2020, "-10-01"))) expect_equal(sum(d$Date <= suggest_cutoff(d, seasons = 3)), 3L) }) test_that("a cutoff outside the range leaves the series unsplit", { sim <- data.frame(Date = as.Date("2016-01-01") + 0:100, value = 1:101) p <- plot_ts(sim, cutoff = as.Date("2030-01-01")) # no phase band means no fill layer was added expect_false(any(vapply(p$layers, function(l) inherits(l$geom, "GeomRect"), logical(1)))) }) test_that("plot_ts keeps one panel when a cutoff is given", { sim <- data.frame(Date = as.Date("2016-01-01") + 0:400, value = 1:401) obs <- sim[c(50, 300), ] p <- plot_ts(sim, obs = obs, cutoff = as.Date("2016-08-01")) expect_equal(nlevels(ggplot2::ggplot_build(p)$data[[1]]$PANEL), 1L) expect_true(any(vapply(p$layers, function(l) inherits(l$geom, "GeomVline"), logical(1)))) }) test_that("colour mode changes the palette and restores", { old <- colour_mode() on.exit(set_colour_mode(old)) set_colour_mode("colour") set_palette(c(a = "#0072B2", b = "#D55E00")) expect_equal(unname(get_pal(c("a","b"))), c("#0072B2","#D55E00")) set_colour_mode("grey") expect_true(all(grepl("^#([0-9A-F]{2})\\1\\1$", get_pal(c("a","b")), ignore.case = TRUE))) set_colour_mode("mono") expect_equal(unname(get_pal(c("a","b"), "colour")), c("black","black")) # fills still vary, since fill is the only channel a bar has expect_equal(length(unique(get_pal(c("a","b"), "fill"))), 2L) }) test_that("mono keeps lines darker than fills", { old <- colour_mode(); on.exit(set_colour_mode(old)) set_colour_mode("grey") lum <- function(x) mean(col2rgb(x)) expect_lt(mean(vapply(get_pal(letters[1:3], "colour"), lum, numeric(1))), mean(vapply(get_pal(letters[1:3], "fill"), lum, numeric(1)))) }) test_that("accent and heat colours follow the mode", { old <- colour_mode(); on.exit(set_colour_mode(old)) set_colour_mode("colour"); expect_equal(accent_col(), "#0072B2") set_colour_mode("grey"); expect_equal(accent_col(), "black") expect_false(grepl("#B2182B", heat_cols()$low)) })