context("Generic tests")
test_that("Sanity checks", {
 expect_error(
    CST_WeatherRegimes(data = 1),
    paste0("Parameter 'data' must be of the class 's2dv_cube', as output by ",
           "CSTools::CST_Load."))
  
  data1 <- 1 : 20
  data1 <- list(data = data1)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_WeatherRegimes(data = data1),
    paste0("Parameter 'data' must be an array with named dimensions."))
  
  data1 <- 1 : 20
  dim(data1) <- c(lat = 5, lon=4)
  data1 <- list(data = data1 , lat=1:5)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_WeatherRegimes(data = data1),
    paste0("Parameter 'data' must have temporal dimensions."))
  
  data1 <- 1 : 20
  dim(data1) <- c(time = 20)
  data1 <- list(data = data1)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_WeatherRegimes(data = data1) ,
    paste0("Parameter 'lat' must be specified."))
  
  data1 <- 1 : 400
  dim(data1) <- c(time = 20, lat = 5, lon=4)
  data1 <- list(data = data1, lat=1:5)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_WeatherRegimes(data = data1),
    paste0("Parameter 'ncenters' must be specified."))
  
  expect_error(
    CST_WeatherRegimes(data = data1, ncenters=3),
    paste0("Parameter 'lon' must be specified."))
  
  expect_equal(
    names(dim(CST_WeatherRegimes(data = data1, ncenters=3, EOFS= FALSE)$data)), 
    c('lat', 'lon', 'cluster'))
  
  data1 <- 1 : 400
  dim(data1) <- c(sdate = 2, ftime = 10, lat = 5, lon=4)
  data1 <- list(data = data1, lat=1:5)
  class(data1) <- 's2dv_cube'
  nclusters <- 3 
  
  expect_equal(
    dim(CST_WeatherRegimes(data = data1 ,
                           ncenters = nclusters,
                           EOFS = FALSE)$statistics$frequency),c(2, nclusters))
  expect_equal(
    names(dim(CST_WeatherRegimes(data = data1, nclusters, EOFS= FALSE)$data)), 
    c('lat', 'lon', 'cluster'))
  
  data1 <- 1 : 400
  dim(data1) <- c(sdate = 2, ftime = 10, lat = 5, lon=4)
  data1 <- list(data = data1, lat=1:5 ,lon=1:4)
  class(data1) <- 's2dv_cube'
  
  expect_equal(
    names(CST_WeatherRegimes(data = data1 , ncenters = 4)$statistics),
    c('pvalue', 'cluster', 'frequency', 'persistence'))
  
  expect_equal(
    names(CST_WeatherRegimes(data = data1 , ncenters = 4, method='ward.D')$statistics),
    c('pvalue', 'cluster'))
  
  expect_equal(
    names(dim(CST_WeatherRegimes(data = data1, ncenters=4)$data)), 
    c('lat', 'lon', 'cluster'))
  
  data1 <- 1 : 400
  dim(data1) <- c(time = 20, lat = 5, lon=4)
  data1[4,,] <- NA
  data1 <- list(data = data1, lat=1:5 ,lon=1:4)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_WeatherRegimes(data = data1, ncenters=3, EOFS = FALSE),
    paste0("Parameter 'data' contains NAs in the 'time' dimensions."))
  
  data1 <- 1 : 400
  dim(data1) <- c(time = 20, lat = 5, lon=4)
  data1[,2,3] <- NA
  data1 <- list(data = data1, lat=1:5 ,lon=1:4)
  class(data1) <- 's2dv_cube'
  expect_equal(
    any(is.na(CST_WeatherRegimes(data = data1, ncenters=3, EOFS = FALSE)$data)),
    TRUE)
  expect_equal(
    names(dim(CST_WeatherRegimes(data = data1, ncenters=3, EOFS = FALSE)$data)), 
    c('lat', 'lon', 'cluster'))
})





