sheet <- function() data.frame( Sampling_Date = rep(c("2016-09-15", "2016-10-20"), each = 2), Treatment = c("AWD_N120", "CF_N120"), Replication = 1, Biomass = c(6000, 5800, 11000, 10500), Grain_yield = c(2500, 2400, 6000, 5700), stringsAsFactors = FALSE) simlong <- function() { d <- do.call(rbind, lapply(c("AWD_N120", "CF_N120"), function(s) data.frame(sim = s, Date = as.Date("2016-06-01") + 0:200, variable = rep(c("wagt", "wrr"), each = 201), value = c(seq(0, 12000, length.out = 201), seq(0, 6000, length.out = 201)), unit = "kg/ha", stringsAsFactors = FALSE))) d } test_that("var_map renames observed columns to APSIM variable names", { ob <- map_obs(sheet(), "Sampling_Date", c("Biomass", "Grain_yield"), treatment_col = "Treatment", var_map = c(Biomass = "wagt", Grain_yield = "wrr")) expect_equal(sort(unique(ob$variable)), c("wagt", "wrr")) expect_equal(sort(unique(ob$source_column)), c("Biomass", "Grain_yield")) }) test_that("check_variables finds the mismatch and suggests a target", { ob <- map_obs(sheet(), "Sampling_Date", c("Biomass", "Grain_yield"), treatment_col = "Treatment") cv <- check_variables(ob, simlong()) expect_setequal(cv$unmatched, c("Biomass", "Grain_yield")) expect_length(cv$matched, 0L) expect_true(all(vapply(cv$suggestions, function(z) z[1] %in% c("wagt", "wrr"), logical(1)))) }) test_that("unmapped names pair with nothing and say why", { ob <- map_obs(sheet(), "Sampling_Date", "Biomass", treatment_col = "Treatment") expect_warning(pair_obs(simlong(), ob, tol_days = 3), "No observed variable name matches") }) test_that("a treatment mismatch is reported as such, not as a date problem", { ob <- map_obs(sheet(), "Sampling_Date", "Biomass", treatment_col = "Treatment", var_map = c(Biomass = "wagt")) ob$sim <- "NOT_A_SIM" expect_warning(pair_obs(simlong(), ob, tol_days = 3), "No observed treatment label matches") }) test_that("a genuine tolerance failure is reported as such", { ob <- map_obs(sheet(), "Sampling_Date", "Biomass", treatment_col = "Treatment", var_map = c(Biomass = "wagt")) ob$Date <- as.Date("2030-01-01") expect_warning(pair_obs(simlong(), ob, tol_days = 3), "within 3 day") }) test_that("mapped observations reach every treatment and both variables", { ob <- map_obs(sheet(), "Sampling_Date", c("Biomass", "Grain_yield"), treatment_col = "Treatment", var_map = c(Biomass = "wagt", Grain_yield = "wrr")) pr <- pair_obs(simlong(), ob, tol_days = 3) expect_equal(nrow(pr), 8L) expect_equal(as.integer(table(pr$variable)), c(4L, 4L)) expect_equal(length(unique(pr$sim)), 2L) }) test_that("pair_obs carries the simulated side's treatment columns", { sim <- data.frame(sim = "T1", Date = as.Date("2016-01-01") + 0:50, variable = "wrr", value = 1:51, water = "AWD", N = "N120", stringsAsFactors = FALSE) obs <- data.frame(sim = "T1", Date = as.Date("2016-01-10"), variable = "wrr", value = 10, stringsAsFactors = FALSE) pr <- pair_obs(sim, obs, tol_days = 2) expect_true(all(c("water", "N") %in% names(pr))) expect_equal(pr$N, "N120") # and the observed side is not overwritten by it expect_equal(pr$obs, 10) }) test_that("a missing grouping column gives a clear error", { d <- data.frame(obs = 1:5, pred = 1:5) expect_error(plot_one2one(d, group = "N"), "Column\\(s\\) not found") expect_error(plot_taylor(d, group = "N"), "Column\\(s\\) not found") })