context("CSTools::CST_Calibration tests")

##############################################
# dat1
exp_obs <- lonlat_temp
exp <- exp_obs[[1]]
obs <- exp_obs[[2]]
exp$data <- ClimProjDiags::Subset(exp_obs[[1]]$data, c('member', 'lon', 'lat'), list(1:2, 1:4, 1:4))
obs$data <- ClimProjDiags::Subset(exp_obs[[2]]$data, c('lon', 'lat'), list(1:4, 1:4))
exp$lon <- exp$lon[1:4]
exp$lat <- exp$lat[1:4]
obs$lon <- obs$lon[1:4]
obs$lat <- obs$lat[1:4]

# dat2
exp2 <- exp
exp2$data[1, 2, 1, 1, 1, 1] <- NA
obs2 <- obs
obs2$data[1, 1, 2, 1, 1, 1] <- NA
##############################################

test_that("1. Input checks", {
  expect_error(
    CST_Calibration(exp = 1),
    "Parameter 'exp' and 'obs' must be of the class 's2dv_cube', "
  )
  expect_error(
    CST_Calibration(obs = 1),
    c("argument \"exp\" is missing, with no default")
  )
  cal <- CST_Calibration(exp = exp, obs = obs)
  expect_equal(
    length(cal),
    9
  )
  expect_equal(
    as.numeric(dim(cal$data)),
    as.numeric(dim(exp$data))
  )
  expect_equal(
    cal$lat,
    exp$lat
  )
  expect_equal(
    cal$lat,
    obs$lat
  )
  expect_equal(
    cal$lon,
    exp$lon
  )
  expect_equal(
    cal$lon,
    obs$lon
  )
  expect_warning(
    CST_Calibration(exp = exp2, obs = obs),
    "Parameter 'exp' contains NA values."
  )
  expect_warning(
    CST_Calibration(exp = exp, obs = obs2),
    "Parameter 'obs' contains NA values."
  )
  expect_warning(
    CST_Calibration(exp = exp2, obs = obs2),
    "Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values."
  )
})

##############################################
