context("Generic tests")
test_that("Sanity checks", {
library(qmap)
  expect_error(
    CST_QuantileMapping(exp = 1),
    paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))

  exp <- 1 : 20
  dim(exp) <- c(time = 20)
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp),
    'argument "obs" is missing, with no default')
  expect_error(
    CST_QuantileMapping(exp = exp, obs = 1),
    paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))

  obs <- 1 : 20
  dim(obs) <- c(time = 20)
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, exp_cor = 1),
    paste0("Parameter 'exp_cor' must be of the class 's2dv_cube', as output ",
    "by CSTools::CST_Load."))
  
  exp <- 1 : 20
  dim(exp) <- 20
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    "Parameter 'exp' must have dimension names.")
  
  exp <- 1 : 20
  dim(exp) <- c(time = 20)
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  obs <- 1 : 20
  dim(obs) <- 20
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    "Parameter 'obs' must have dimension names.")

  obs <- 1 : 20
  dim(obs) <- c(time = 20)
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  exp_cor <- 1 : 20
  dim(exp_cor) <- 20
  exp_cor <- list(data = exp_cor)
  class(exp_cor) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, exp_cor = exp_cor),
    "Parameter 'exp_cor' must have dimension names.")
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    paste0("Parameter 'sample_dims' must be a vector of string character ",
           "indicating names of exiting dimension in parameter 'exp'."))
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', method = 'x'),
    paste0("Parameter 'method' must be one of the following methods: ",
           "'PTF','DIST','RQUANT','QUANT','SSPLIN'."))
  expect_warning(
    CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', sample_length = "month"),
    paste0("Parameter 'sample_length' has not been correctly defined and ",
           "the whole length of the timeseries will be used."))

  exp$data[1] <- NA
  expect_warning(
    CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time'),
    "Parameter 'exp' contains NA values.")

  exp$data[1] <- 1
  obs$data[1] <- NA
    expect_warning(
    CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time'),
    "Parameter 'obs' contains NA values.")
  obs$data[1] <- 1
  expect_equal(CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time'), exp)
  expect_equal(CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', 
                                   method = 'PTF'), exp)
  expect_equal(CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', 
                                   method = 'RQUANT'), exp)
  expect_equal(CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', 
                                   method = 'SSPLIN'), exp)
  library(CSTools)
  expect_equal(CST_QuantileMapping(exp = lonlat_data$exp, obs = lonlat_data$obs),
               CST_QuantileMapping(exp = lonlat_data$exp, obs = lonlat_data$obs,
                                   exp_cor = lonlat_data$exp))

})
