context("CSTools::PlotWeeklyClim tests")

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

# dat1
dat1 <- array(rnorm(1*7), dim = c(dat = 1, var = 1, sdate = 1, time = 7))

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

test_that("1. Input checks", {
  # data
  expect_error(
    PlotWeeklyClim(data = array(1:92), first_date = '2020-03-01', 
                       ref_period_ini = 1993, ref_period_end = 2021),
    "Parameter 'data' must have named dimensions."
  )
  expect_error(
    PlotWeeklyClim(data = data.frame(week = 1:92), first_date = '2020-03-01', 
                       ref_period_ini = 1993, ref_period_end = 2021),
    paste0("If parameter 'data' is a data frame, it must contain the ",
           "following column names: 'week', 'clim', 'p10', 'p90', 'p33', ", 
           "'p66', 'week_mean', 'day' and 'data'.")
  )
  expect_error(
    PlotWeeklyClim(data = 1:92, first_date = '2020-03-01', 
                       ref_period_ini = 1993, ref_period_end = 2021),
    "Parameter 'data' must be an array or a data frame."
  )
  # time_dim
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = '2020-03-01', 
                       ref_period_ini = 2020, ref_period_end = 2020, time_dim = 1),
    "Parameter 'time_dim' must be a character string."
  )
  expect_error(
    PlotWeeklyClim(data = array(rnorm(1), dim = c(dat = 1)), 
                       first_date = '2020-03-01', ref_period_ini = 2020, 
                       ref_period_end = 2020),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    PlotWeeklyClim(data = array(rnorm(1*7), dim = c(time = 6)), 
                       first_date = '2020-03-01', ref_period_ini = 2020, 
                       ref_period_end = 2020),
    paste0("Parameter 'data' must have the dimension 'time_dim' of length ",
           "equal or grater than 7 to compute the weekly means.")
  )
  # sdate_dim
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = '2020-03-01', 
                       ref_period_ini = 2020, ref_period_end = 2020, 
                       sdate_dim = 1),
    "Parameter 'sdate_dim' must be a character string."
  )
  expect_warning(
    PlotWeeklyClim(data = array(rnorm(7), dim = c(time = 7)), 
                       first_date = '2020-03-01', ref_period_ini = 2020, 
                       ref_period_end = 2020),
    paste0("Parameter 'sdate_dim' is not found in 'data' dimension. ",
           "A dimension of length 1 has been added.")
  )
  # ref_period_ini
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = '2020-03-01', 
                       ref_period_ini = "2020", ref_period_end = 2020),
    "Parameters 'ref_period_ini' and 'ref_period_end' must be numeric."
  )
  # first_date
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = 2020-03-01, 
                       ref_period_ini = 2020, ref_period_end = 2020),
    paste0("Parameter 'first_date' must be a character string ", 
           "indicating the date in the format 'yyyy-mm-dd', 'POSIXct' ", 
           "or 'Dates' class.")
  )
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = 'a', 
                       ref_period_ini = 2020, ref_period_end = 2020),
    paste0("Parameter 'first_date' must be a character string ", 
           "indicating the date in the format 'yyyy-mm-dd', 'POSIXct' ", 
           "or 'Dates' class.")
  )
  expect_error(
    PlotWeeklyClim(data = dat1, first_date = '2020-03-01', ref_period_ini = 2021,
                       ref_period_end = 2022),
    "Parameter 'first_date' must be a date included in the reference period."
  )
})

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