diff --git a/tests/testthat/test-SeriesAggreg.R b/tests/testthat/test-SeriesAggreg.R index f54edc9cf49a4d2ba1a2a326a210b3493d874610..cc6e0c2bc56fb4c1e7f38ba839ed352b00a56122 100644 --- a/tests/testthat/test-SeriesAggreg.R +++ b/tests/testthat/test-SeriesAggreg.R @@ -15,10 +15,11 @@ test_that("No warning with InputsModel Cemaneige'", { NLayers = 5 ) # Expect no warning: https://stackoverflow.com/a/33638939/5300212 - expect_warning(SeriesAggreg(InputsModel, "%m"), + expect_warning(SeriesAggreg(InputsModel, Format = "%m"), regexp = NA) }) + test_that("Warning: deprecated 'TimeFormat' argument", { InputsModel <- CreateInputsModel( FUN_MOD = RunModel_GR4J, @@ -30,6 +31,7 @@ test_that("Warning: deprecated 'TimeFormat' argument", { regexp = "deprecated 'TimeFormat' argument") }) + test_that("Warning: deprecated 'NewTimeFormat' argument: please use 'Format' instead", { InputsModel <- CreateInputsModel( @@ -42,6 +44,7 @@ test_that("Warning: deprecated 'NewTimeFormat' argument: please use 'Format' ins regexp = "deprecated 'NewTimeFormat' argument: please use 'Format' instead") }) + test_that("Warning: deprecated 'NewTimeFormat' argument: 'Format' argument is used instead", { InputsModel <- CreateInputsModel( @@ -54,6 +57,7 @@ test_that("Warning: deprecated 'NewTimeFormat' argument: 'Format' argument is us regexp = "deprecated 'NewTimeFormat' argument: 'Format' argument is used instead") }) + test_that("Check SeriesAggreg output values on yearly aggregation", { TabSeries <- data.frame( DatesR = BasinObs$DatesR, @@ -72,6 +76,7 @@ test_that("Check SeriesAggreg output values on yearly aggregation", { expect_equal(GoodValues, TestedValues) }) + test_that("Regime calculation should switch ConvertFun to 'mean' for InputsModel", { InputsModel <- CreateInputsModel( FUN_MOD = RunModel_GR4J, @@ -79,11 +84,11 @@ test_that("Regime calculation should switch ConvertFun to 'mean' for InputsModel Precip = BasinObs$P, PotEvap = BasinObs$E ) - - expect_equal(SeriesAggreg(InputsModel, "%m")$Precip, - SeriesAggreg(BasinObs[, c("DatesR", "P")], "%m", ConvertFun = "mean")$P) + expect_equal(SeriesAggreg(InputsModel, Format = "%m")$Precip, + SeriesAggreg(BasinObs[, c("DatesR", "P")], Format = "%m", ConvertFun = "mean")$P) }) + test_that("No DatesR should warning", { TabSeries <- list( Dates = BasinObs$DatesR, @@ -92,11 +97,12 @@ test_that("No DatesR should warning", { Qmm = BasinObs$Qmm ) expect_warning( - SeriesAggreg(TabSeries, "%Y%m", ConvertFun = "sum"), + SeriesAggreg(TabSeries, Format = "%Y%m", ConvertFun = "sum"), regexp = "has been automatically chosen" ) }) + test_that("Check SeriesAggreg.list 'DatesR' argument", { InputsModel <- CreateInputsModel( FUN_MOD = RunModel_GR4J, @@ -107,16 +113,17 @@ test_that("Check SeriesAggreg.list 'DatesR' argument", { DatesR <- InputsModel$DatesR # No InputsModel$DatesR InputsModel$DatesR <- NULL - expect_error(SeriesAggreg(InputsModel, "%Y%m"), regexp = "'POSIXt'") + expect_error(SeriesAggreg(InputsModel, Format = "%Y%m"), regexp = "'POSIXt'") # Other list item chosen InputsModel$SuperDates <- DatesR - expect_warning(SeriesAggreg(InputsModel, "%Y%m"), regexp = "SuperDates") + expect_warning(SeriesAggreg(InputsModel, Format = "%Y%m"), regexp = "SuperDates") # Wrong InputsModel$DatesR InputsModel$DatesR <- BasinObs$P - expect_error(SeriesAggreg(InputsModel, "%Y%m"), regexp = "'POSIXt'") + expect_error(SeriesAggreg(InputsModel, Format = "%Y%m"), regexp = "'POSIXt'") }) + test_that("Check SeriesAggreg.list with embedded lists", { InputsModel <- CreateInputsModel( @@ -128,38 +135,36 @@ test_that("Check SeriesAggreg.list with embedded lists", { HypsoData = BasinInfo$HypsoData, NLayers = 5 ) - I2 <- SeriesAggreg(InputsModel, "%Y%m") + I2 <- SeriesAggreg(InputsModel, Format = "%Y%m") expect_equal(length(I2$ZLayers), 5) expect_null(I2$LayerPrecip$DatesR) expect_equal(length(I2$DatesR), length(I2$LayerPrecip$L1)) }) + test_that("Check SeriesAggreg.outputsModel", { - InputsModel <- - CreateInputsModel( - FUN_MOD = RunModel_CemaNeigeGR4J, - DatesR = BasinObs$DatesR, - Precip = BasinObs$P, - PotEvap = BasinObs$E, - TempMean = BasinObs$T, - ZInputs = median(BasinInfo$HypsoData), - HypsoData = BasinInfo$HypsoData, - NLayers = 5 - ) + InputsModel <- CreateInputsModel( + FUN_MOD = RunModel_CemaNeigeGR4J, + DatesR = BasinObs$DatesR, + Precip = BasinObs$P, + PotEvap = BasinObs$E, + TempMean = BasinObs$T, + ZInputs = median(BasinInfo$HypsoData), + HypsoData = BasinInfo$HypsoData, + NLayers = 5 + ) ## run period selection - Ind_Run <- - seq(which(format(BasinObs$DatesR, format = "%Y-%m-%d") == "1990-01-01"), - which(format(BasinObs$DatesR, format = "%Y-%m-%d") == "1999-12-31")) + Ind_Run <- seq(which(format(BasinObs$DatesR, format = "%Y-%m-%d") == "1990-01-01"), + which(format(BasinObs$DatesR, format = "%Y-%m-%d") == "1999-12-31")) ## preparation of the RunOptions object suppressWarnings( - RunOptions <- - CreateRunOptions( - FUN_MOD = RunModel_CemaNeigeGR4J, - InputsModel = InputsModel, - IndPeriod_Run = Ind_Run - ) + RunOptions <- CreateRunOptions( + FUN_MOD = RunModel_CemaNeigeGR4J, + InputsModel = InputsModel, + IndPeriod_Run = Ind_Run + ) ) ## simulation @@ -175,12 +180,13 @@ test_that("Check SeriesAggreg.outputsModel", { RunOptions = RunOptions, Param = Param) - O2 <- SeriesAggreg(OutputsModel, "%Y%m") + O2 <- SeriesAggreg(OutputsModel, Format = "%Y%m") expect_equal(length(O2$StateEnd), 3) expect_equal(length(O2$DatesR), length(O2$CemaNeigeLayers$Layer01$Pliq)) }) + test_that("Check data.frame handling in SeriesAggreg.list", { InputsModelDown1 <- CreateInputsModel( FUN_MOD = RunModel_GR4J, @@ -193,15 +199,16 @@ test_that("Check data.frame handling in SeriesAggreg.list", { # Distance between upstream catchment outlet and the downstream one in km BasinAreas = c(180, 180) # Upstream and downstream areas in km² ) - expect_warning(SeriesAggreg(InputsModelDown1, "%Y%m"), + expect_warning(SeriesAggreg(InputsModelDown1, Format = "%Y%m"), regexp = NA) - I2 <- SeriesAggreg(InputsModelDown1, "%Y%m") + I2 <- SeriesAggreg(InputsModelDown1, Format = "%Y%m") expect_equal(length(I2$DatesR), nrow(I2$Qupstream)) InputsModelDown1$Qupstream <- InputsModelDown1$Qupstream[-1, , drop = FALSE] - expect_warning(SeriesAggreg(InputsModelDown1, "%Y%m"), + expect_warning(SeriesAggreg(InputsModelDown1, Format = "%Y%m"), regexp = "it will be ignored in the aggregation") }) + test_that("SeriesAggreg from and to the same time step should return initial time series", { InputsModel <- CreateInputsModel( FUN_MOD = RunModel_GR4J, @@ -209,42 +216,45 @@ test_that("SeriesAggreg from and to the same time step should return initial tim Precip = BasinObs$P, PotEvap = BasinObs$E ) - I2 <- SeriesAggreg(InputsModel, "%Y%m") - expect_warning(SeriesAggreg(I2, "%Y%m"), regexp = "No time-step conversion was performed") - expect_equal(I2, suppressWarnings(SeriesAggreg(I2, "%Y%m"))) + I2 <- SeriesAggreg(InputsModel, Format = "%Y%m") + expect_warning(SeriesAggreg(I2, Format = "%Y%m"), regexp = "No time-step conversion was performed") + expect_equal(I2, suppressWarnings(SeriesAggreg(I2, Format = "%Y%m"))) }) -test_that("SeriesAggreg.data.frame with first column not named DatesR should work", - { + +test_that("SeriesAggreg.data.frame with first column not named DatesR should work", { expect_warning(SeriesAggreg( data.frame(BasinObs$DatesR, BasinObs$Qmm), Format = "%Y%m", ConvertFun = "sum" ), regexp = NA) - }) +}) + test_that("SeriesAggreg should work with ConvertFun 'min', 'max' and 'median'", { Qls <- BasinObs[, c("DatesR", "Qls")] - test_ConvertFunRegime <- function(x, ConvertFun, TimeFormat) { - expect_equal(nrow(SeriesAggreg(x, TimeFormat, ConvertFun = ConvertFun)), - length(unique(format(BasinObs$DatesR, "%Y")))) + test_ConvertFunRegime <- function(x, ConvertFun, Format) { + expect_equal(nrow(SeriesAggreg(x, Format, ConvertFun = ConvertFun)), + length(unique(format(BasinObs$DatesR, format = "%Y")))) } - lapply(c("max", "min", "median"), function(x) {test_ConvertFunRegime(Qls, x, "%Y")}) + lapply(c("max", "min", "median"), function(x) {test_ConvertFunRegime(Qls, x, Format = "%Y")}) }) + test_that("Error on convertFun Q without 0-100", { Qls <- BasinObs[, c("DatesR", "Qls")] - expect_error(SeriesAggreg(Qls, "%Y", "q101")) - expect_error(SeriesAggreg(Qls, "%Y", "q-2")) - expect_error(SeriesAggreg(Qls, "%Y", "q12.5")) + expect_error(SeriesAggreg(Qls, Format = "%Y", "q101")) + expect_error(SeriesAggreg(Qls, Format = "%Y", "q-2")) + expect_error(SeriesAggreg(Qls, Format = "%Y", "q12.5")) }) + test_that("ConvertFun q50 should be equal to median", { Qls <- BasinObs[, c("DatesR", "Qls")] - expect_equal(SeriesAggreg(Qls, "%Y", "q50"), - SeriesAggreg(Qls, "%Y", "median")) - expect_equal(SeriesAggreg(Qls, "%Y", "q50"), - SeriesAggreg(Qls, "%Y", "q050")) + expect_equal(SeriesAggreg(Qls, Format = "%Y", "q50"), + SeriesAggreg(Qls, Format = "%Y", "median")) + expect_equal(SeriesAggreg(Qls, Format = "%Y", "q50"), + SeriesAggreg(Qls, Format = "%Y", "q050")) })