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

# dat1
wind <- NULL
wind$data <- array(rweibull(n = 100, shape = 2, scale = 6), 
                   c(member = 10, lat = 2, lon = 5))
wind$coords <- list(lat =  c(40, 41), lon = 1:5)
variable <- list(varName = 'sfcWind', 
                 metadata = list(sfcWind = list(level = 'Surface')))
wind$attrs <- list(Variable = variable, Datasets = 'synthetic', 
                   when = Sys.time(), Dates = '1990-01-01 00:00:00')
class(wind) <- 's2dv_cube'

# dat2
dat2 <- array(abs(rnorm(5 * 3 * 214 * 2)*50),
                  c(member = 5, sdate = 3, time = 214, lon = 2)) 
dates0 <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), 
                         as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'),
                     seq(as.Date("01-05-2001", format = "%d-%m-%Y"), 
                         as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'),
                     seq(as.Date("01-05-2002", format = "%d-%m-%Y"), 
                         as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day'))
dates2 <- dates0
dim(dates2) <- c(sdate = 3, time = 214)

###########################################################################
test_that("1. Input checks", {
  # Check 's2dv_cube'
  expect_error(
    CST_WindPowerDensity(wind = 1), 
    "Parameter 'wind' must be of the class 's2dv_cube'."
  )
  # Dates subset
  expect_warning(
    CST_WindPowerDensity(wind = wind, start = list(1,3), end = list(1,7)), 
    paste0("Dimensions in 'wind' element 'attrs$Dates' are missed and ",
           "all data would be used."), 
    fixed = TRUE
  )
  # start and end
  expect_error(
    WindPowerDensity(wind = wind$data, dates = wind$attrs$Dates, 
                     start = c(1,2), end = c(2,3)), 
    paste0("Parameter 'start' and 'end' must be lists indicating the ",
           "day and the month of the period start and end.")
  )
  # start and end when dates is not provided
  expect_warning(
    WindPowerDensity(array(1:10, c(time = 10)), 
                     start = list(05, 02), end = list(05, 09)),
    paste0("Parameter 'dates' is NULL and the average of the full data ", 
           "provided in 'data' is computed.")
  )
})

###########################################################################
test_that("2. Output checks", {
  expect_equal(
    CST_WindPowerDensity(wind = wind)$attrs$Variable$varName, 
    'WindPowerDensity'
  )
  expect_equal(
    dim(CST_WindPowerDensity(wind = wind)$data), 
    c(member = 10, lat = 2, lon = 5)
  )
  # test start and end
  expect_warning(
    WindPowerDensity(wind = dat2, dates = dates0, 
                     start = list(21, 4), end = list(21, 6)),
    paste0("Parameter 'wind' must have named dimensions if 'start' and ",
           "'end' are not NULL. All data will be used.")
  )
  expect_equal(
    dim(WindPowerDensity(wind = dat2, dates = dates2, 
                         start = list(21, 4), end = list(21, 6))),
    c(member = 5, sdate = 3, time = 52, lon = 2)
  )
})

