From 13dcd18331df778e5ae2b51f36ffe3075c2f9cd4 Mon Sep 17 00:00:00 2001 From: Matt Williamson Date: Wed, 24 Aug 2022 18:10:51 +0000 Subject: [PATCH] Minimal test added... more would be good but would need a lot of mocks... or maybe TrivialForcingProvider could be used? --- .../catchment/Catchment_Formulation.hpp | 8 +++---- .../catchments/Bmi_Multi_Formulation_Test.cpp | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/realizations/catchment/Catchment_Formulation.hpp b/include/realizations/catchment/Catchment_Formulation.hpp index e84529421d..9305ed1173 100644 --- a/include/realizations/catchment/Catchment_Formulation.hpp +++ b/include/realizations/catchment/Catchment_Formulation.hpp @@ -17,13 +17,13 @@ namespace realization { : Formulation(id), HY_CatchmentArea(forcing, output_stream) { // Assume the catchment ID is equal to or embedded in the formulation `id` size_t idx = id.find("."); - cat_id = ( idx == std::string::npos ? id : id.substr(0, idx) ); + set_catchment_id( idx == std::string::npos ? id : id.substr(0, idx) ); }; Catchment_Formulation(std::string id) : Formulation(id){ // Assume the catchment ID is equal to or embedded in the formulation `id` size_t idx = id.find("."); - cat_id = ( idx == std::string::npos ? id : id.substr(0, idx) ); + set_catchment_id( idx == std::string::npos ? id : id.substr(0, idx) ); }; /** @@ -95,11 +95,11 @@ namespace realization { protected: std::string get_catchment_id() override { - return id; + return this->cat_id; } void set_catchment_id(std::string cat_id) override { - id = cat_id; + this->cat_id = cat_id; } //TODO: VERY BAD JUJU...the following two members are an ugly hack to avoid having to gut the legacy C/C++ realizations for now. diff --git a/test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp b/test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp index 9a40887c8f..d2fa5e20b8 100644 --- a/test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp +++ b/test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp @@ -63,6 +63,16 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test { return nested->get_var_value_as_double(var_name); } + static std::string get_friend_catchment_id(Bmi_Multi_Formulation& formulation){ + return formulation.get_catchment_id(); + } + + template + static std::string get_friend_nested_catchment_id(const Bmi_Multi_Formulation& formulation, const int mod_index) { + std::shared_ptr nested = std::static_pointer_cast(formulation.modules[mod_index]); + return nested->get_catchment_id(); + } + /* static std::vector get_friend_nested_formulations(Bmi_Multi_Formulation& formulation) { return formulation.get_bmi_model(); @@ -787,6 +797,19 @@ TEST_F(Bmi_Multi_Formulation_Test, GetOutputLineForTimestep_3_a) { ASSERT_EQ(output, "0.000001112,199280.000000000,199240.000000000,199280.000000000,0.000000000,0.000001001"); } +/** + * Test if Catchment Ids of submodules correctly trim any suffix + */ +TEST_F(Bmi_Multi_Formulation_Test, GetIdAndCatchmentId) { + int ex_index = 3; + + Bmi_Multi_Formulation formulation(catchment_ids[ex_index], std::make_unique(*forcing_params_examples[ex_index]), utils::StreamHandler()); + formulation.create_formulation(config_prop_ptree[ex_index]); + ASSERT_EQ(formulation.get_id(), "cat-27"); + ASSERT_EQ(get_friend_catchment_id(formulation), "cat-27"); + ASSERT_EQ(get_friend_nested_catchment_id(formulation, 0), "cat-27"); + //ASSERT_EQ(formulation.get_catchment_id(), "id"); +} #endif // NGEN_BMI_C_LIB_ACTIVE || NGEN_BMI_FORTRAN_ACTIVE || ACTIVATE_PYTHON #endif // NGEN_BMI_MULTI_FORMULATION_TEST_CPP