library(testthat)
test_that("A few combinations", {
 source("tools/check_recipe.R")
 recipe <- list(Analysis = 
                list(Variables = list(ECVs = list(
                                  list(name = 'tas', freq = 'daily_mean')), 
                     Indicators = list(list(name = 'gdd'))),
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
 conf <- list(code_dir = "./")
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = NULL,
                   dependent = list(list(list(name = 'tas', freq = 'daily_mean'),
                                    name = 'gdd'))))
 recipe <- list(Analysis = 
                list(Variables = list(ECVs = list(
                                 list(name = 'tas', freq = 'daily_mean')),
                                      Indicators = list(name = 'gdd')),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC'))) 
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tas', freq = 'daily_mean'), 
                               name = 'gdd'), dependent = NULL))

 recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                          list(name = 'tas', freq = 'daily_mean'))),
                     Workflow = list(Indicators = FALSE, Calibration = 'SBC')))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tas', freq = 'daily_mean')),
                   dependent = NULL))

 recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                         list(name = 'tas', freq = 'daily_mean'), 
                         list(name = 'tasmax', freq = 'daily_mean'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tas', freq = 'daily_mean'),
                                    list(name = 'tasmax', freq = 'daily_mean')),
                   dependent = NULL))

 recipe <- list(Analysis =
                list(Variables = list(Indicators = list(list(name = 'gdd'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'gdd')), dependent = NULL))
 
 recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                                   list(name = 'tas', freq = 'daily_mean')), 
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'))), 
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tas', freq = 'daily_mean'),
                       list(name =  'gdd'), list(name = 'gst')), dependent = NULL))

 recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                                    list(name = 'tas', freq = 'daily_mean')), 
                                      Indicators = list(list(name = 'gdd'),
                                                        list(name = 'gst'))),  
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = NULL,
                   dependent = list(list(list(name = 'tas', freq = 'daily_mean'),
                                         name = 'gdd', name = 'gst'))))
# Dependent workflow: cal & ind
  recipe <- list(Analysis =
                list(Variables = list(
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'))),
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = NULL, dependent = list('gdd', 'gst')))

  recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                                    list(name = 'tasmax', freq = 'daily_mean')),
                                      Indicators = list(list(name = 'gdd'),
                                                        list(name = 'gst'))),
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tasmax', freq = 'daily_mean')),
                   dependent = list('gdd', 'gst')))

  recipe <- list(Analysis =
                 list(Variables = list(ECVs = list(
                                    list(name = 'tasmax', freq = 'daily_mean'), 
                                    list(name = 'tas', freq = 'daily_mean')),
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'))),
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tasmax', freq = 'daily_mean')),
                   dependent = list(list(list(name = 'tas', freq = 'daily_mean'),
                                     name = 'gdd', name = 'gst'))))

  recipe <- list(Analysis =
                 list(Variables = list(ECVs = list(
                                  list(name = 'tasmax', freq = 'daily_mean'),
                                  list(name = 'tas', freq = 'daily_mean')),
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'), 
                                                        list(name = 'spr32'))),
                     Workflow = list(Calibration = 'SBC', Indicators = TRUE)))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = NULL, 
                   dependent = list(list(list(name = 'tasmax', freq = 'daily_mean'),
                                         name = 'spr32'), 
                                    list(list(name = 'tas', freq = 'daily_mean'),
                                         name = 'gdd', name ='gst'))))
# Independent workflow: ind & cal
  recipe <- list(Analysis =
                list(Variables = list(
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
 expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'gdd'), list(name = 'gst')),
                   dependent = NULL))

  recipe <- list(Analysis =
                list(Variables = list(ECVs = list(
                       list(name ='tasmax', freq = 'daily_freq')),
                                      Indicators = list(list(name = 'gdd'), 
                                                        list(name = 'gst'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tasmax', freq = 'daily_freq'),
                                   list(name = 'gdd'), list(name = 'gst')),
                   dependent = NULL))

  recipe <- list(Analysis =
                 list(Variables = list(ECVs = list(
                           list(name = 'tasmax', freq = 'daily_mean'),
                           list(name = 'tas', freq = 'daily_mean')),
                                      Indicators = list(list(name = 'gdd'),
                                                        list(name = 'gst'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tasmax', freq = 'daily_mean'),
                                      list(name = 'tas', freq = 'daily_mean'),
                                      list(name = 'gdd'), 
                                      list(name = 'gst')), dependent = NULL))

  recipe <- list(Analysis =
                 list(Variables = list(ECVs = list(
                                      list(name = 'tasmax', freq = 'daily_mean'),
                                       list(name = 'tas', freq = 'daily_mean')),
                                      Indicators = list(list(name = 'gdd'),
                                                        list(name = 'gst'),
                                                        list(name = 'spr32'))),
                     Workflow = list(Indicators = TRUE, Calibration = 'SBC')))
  expect_equal(check_number_of_dependent_verifications(recipe, conf),
              list(independent = list(list(name = 'tasmax', freq = 'daily_mean'),
                                      list(name = 'tas', freq = 'daily_mean'),
                                      list(name = 'gdd'), list(name = 'gst'), 
                                      list(name = 'spr32')),
                   dependent = NULL))
})

