From 918ee8cecab49a202263e8541e6546ac2f664ce4 Mon Sep 17 00:00:00 2001
From: David <david.dorchies@inrae.fr>
Date: Mon, 29 Aug 2022 13:36:03 +0200
Subject: [PATCH] fix(CreateInputsCrit): model of Apriori node should be the
 same of the current one

Fix #93
---
 R/CreateInputsCrit.GRiwrmInputsModel.R |  4 ++++
 tests/testthat/test-CreateInputsCrit.R | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/R/CreateInputsCrit.GRiwrmInputsModel.R b/R/CreateInputsCrit.GRiwrmInputsModel.R
index 1430274..8cef430 100644
--- a/R/CreateInputsCrit.GRiwrmInputsModel.R
+++ b/R/CreateInputsCrit.GRiwrmInputsModel.R
@@ -61,6 +61,10 @@ CreateInputsCrit.GRiwrmInputsModel <- function(InputsModel,
         stop("'AprioriIds': the node \"", AprioriIds[id],
              "\" is an ungauged upstream node of the node \"", id,"\"")
       }
+      if (!identical(InputsModel[[id]]$FUN_MOD, InputsModel[[AprioriIds[id]]]$FUN_MOD)) {
+        stop("'AprioriIds': the node \"", AprioriIds[id],
+             "\" must use the same hydrological model as the node \"", id,"\"")
+      }
     })
   }
 
diff --git a/tests/testthat/test-CreateInputsCrit.R b/tests/testthat/test-CreateInputsCrit.R
index f5b55d6..3d02d28 100644
--- a/tests/testthat/test-CreateInputsCrit.R
+++ b/tests/testthat/test-CreateInputsCrit.R
@@ -109,6 +109,18 @@ test_that("Lavenne criterion: wrong sub-catchment order should throw error", {
   )
 })
 
+test_that("Lavenne criterion: current node and a priori node must use the same model", {
+  InputsModel[["54032"]]$FUN_MOD <- RunModel_GR6J
+  expect_error(
+    CreateInputsCrit(InputsModel = InputsModel,
+                     RunOptions = RunOptions,
+                     Obs = Qobs[IndPeriod_Run,],
+                     AprioriIds = AprioriIds,
+                     transfo = "sqrt"),
+    regexp = "must use the same hydrological model"
+  )
+})
+
 test_that("Ungauged node as Apriori node should throw an error", {
   nodes$model[nodes$gauge_id == "54001"] <- "Ungauged"
   griwrm <- CreateGRiwrm(
-- 
GitLab