From 35cd165d3db595d748b992a5ee2070e358c551cb Mon Sep 17 00:00:00 2001 From: Guillaume PIERRE Date: Mon, 20 Jan 2025 15:31:04 +0100 Subject: [PATCH] Data for new modeler (4.1) : mainly testind data series repository --- .../modeler/dataSeries/dataSeriesRepo.cpp | 12 ++- .../solver/modeler/dataSeries/dataSeries.h | 9 +- .../modeler/dataSeries/dataSeriesRepo.h | 1 + .../solver/modeler/dataSeries/timeSeriesSet.h | 1 - .../modeler/dataSeries/timeSeriesSet.cpp | 4 +- .../solver/modeler/dataSeries/CMakeLists.txt | 1 + .../modeler/dataSeries/testDataSeriesRepo.cpp | 87 +++++++++++++++++++ 7 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/tests/src/solver/modeler/dataSeries/testDataSeriesRepo.cpp diff --git a/src/solver/modeler/dataSeries/dataSeriesRepo.cpp b/src/solver/modeler/dataSeries/dataSeriesRepo.cpp index 598006f7b3..68f8c23bb2 100644 --- a/src/solver/modeler/dataSeries/dataSeriesRepo.cpp +++ b/src/solver/modeler/dataSeries/dataSeriesRepo.cpp @@ -6,15 +6,21 @@ namespace Antares::Solver::Modeler::DataSeries { void DataSeriesRepository::addDataSeries(std::unique_ptr dataSeries) { - dataSeries_[dataSeries->name()] = std::move(dataSeries); + std::string name = dataSeries->name(); + dataSeries_[name] = std::move(dataSeries); } IDataSeries& DataSeriesRepository::getDataSeries(std::string setId) { + std::string error_message = err_prefix; + if (dataSeries_.empty()) + { + error_message += "empty"; + throw std::invalid_argument(error_message); + } if (!dataSeries_.contains(setId)) { - std::string error_message = "Data series repo : data series named '" + setId - + "' does not exist."; + error_message += "data series '" + setId + "' does not exist"; throw std::invalid_argument(error_message); } return *(dataSeries_[setId]); diff --git a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeries.h b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeries.h index 254b88c824..b8e6d1c03a 100644 --- a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeries.h +++ b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeries.h @@ -9,13 +9,18 @@ namespace Antares::Solver::Modeler::DataSeries class IDataSeries { public: - std::string name() + IDataSeries(std::string name): + name_(name) { - return name_; } virtual double getData(unsigned int rank, unsigned int hour) = 0; + std::string name() + { + return name_; + } + private: std::string name_; }; diff --git a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeriesRepo.h b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeriesRepo.h index f1dc24f82b..ae47eb25a8 100644 --- a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeriesRepo.h +++ b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/dataSeriesRepo.h @@ -18,6 +18,7 @@ class DataSeriesRepository private: std::map> dataSeries_; + std::string err_prefix = "Data series repo : "; }; } // namespace Antares::Solver::Modeler::DataSeries diff --git a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/timeSeriesSet.h b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/timeSeriesSet.h index bca2df9949..8f259d31d4 100644 --- a/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/timeSeriesSet.h +++ b/src/solver/modeler/dataSeries/include/antares/solver/modeler/dataSeries/timeSeriesSet.h @@ -13,7 +13,6 @@ class TimeSeriesSet: public IDataSeries double getData(unsigned rank, unsigned hour) override; private: - std::string name_; unsigned height_ = 0; std::vector> tsSet_; std::string err_prefix_; diff --git a/src/solver/modeler/dataSeries/timeSeriesSet.cpp b/src/solver/modeler/dataSeries/timeSeriesSet.cpp index 756b46080d..f05c0d1d95 100644 --- a/src/solver/modeler/dataSeries/timeSeriesSet.cpp +++ b/src/solver/modeler/dataSeries/timeSeriesSet.cpp @@ -6,9 +6,9 @@ namespace Antares::Solver::Modeler::DataSeries { TimeSeriesSet::TimeSeriesSet(std::string name, unsigned int height): - name_(name), + IDataSeries::IDataSeries(name), height_(height), - err_prefix_("TS set '" + name_ + "' : ") + err_prefix_("TS set '" + this->name() + "' : ") { } diff --git a/src/tests/src/solver/modeler/dataSeries/CMakeLists.txt b/src/tests/src/solver/modeler/dataSeries/CMakeLists.txt index c52c407102..37f5df6e98 100644 --- a/src/tests/src/solver/modeler/dataSeries/CMakeLists.txt +++ b/src/tests/src/solver/modeler/dataSeries/CMakeLists.txt @@ -5,6 +5,7 @@ add_boost_test(unit-tests-for-modeler-data-series main.cpp testScenarioGroupRepo.cpp testTimeSeriesSet.cpp + testDataSeriesRepo.cpp INCLUDE "${solver_modeler_data_series}" LIBS diff --git a/src/tests/src/solver/modeler/dataSeries/testDataSeriesRepo.cpp b/src/tests/src/solver/modeler/dataSeries/testDataSeriesRepo.cpp new file mode 100644 index 0000000000..34e1006f99 --- /dev/null +++ b/src/tests/src/solver/modeler/dataSeries/testDataSeriesRepo.cpp @@ -0,0 +1,87 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include + +#include + +#include +#include "antares/solver/modeler/dataSeries/timeSeriesSet.h" + +using namespace Antares::Solver::Modeler::DataSeries; + +BOOST_AUTO_TEST_CASE(repo_is_empty__asking_any_data_series_raises_exception) +{ + DataSeriesRepository dataSeriesRepository; + + std::string expected_err_msg = "Data series repo : empty"; + BOOST_CHECK_EXCEPTION(dataSeriesRepository.getDataSeries("dummy name"), + std::invalid_argument, + checkMessage(expected_err_msg)); +} + +BOOST_AUTO_TEST_CASE(repo_not_empty__asking_nonexistent_data_raises_exception) +{ + DataSeriesRepository dataSeriesRepository; + auto some_TS_set = std::make_unique("some TS set", 5); + dataSeriesRepository.addDataSeries(std::move(some_TS_set)); + + std::string expected_err_msg = "Data series repo : data series 'dummy name' does not exist"; + BOOST_CHECK_EXCEPTION(dataSeriesRepository.getDataSeries("dummy name"), + std::invalid_argument, + checkMessage(expected_err_msg)); +} + +BOOST_AUTO_TEST_CASE(ask_a_simple_data_repo_some_data_it_contains___answer_is_correct) +{ + DataSeriesRepository dataSeriesRepository; + + auto some_TS_set = std::make_unique("some TS set", 5); + some_TS_set->add({1., 2., 3., 4., 5.}); + some_TS_set->add({11., 12., 13., 14., 15.}); + + dataSeriesRepository.addDataSeries(std::move(some_TS_set)); + + unsigned rank = 1; + unsigned hour = 3; + BOOST_CHECK_EQUAL(dataSeriesRepository.getDataSeries("some TS set").getData(rank, hour), 14.); +} + +BOOST_AUTO_TEST_CASE(asking_repo_data_for_a_too_big_hour___exception_from_data_series_is_caught) +{ + DataSeriesRepository dataSeriesRepository; + auto some_TS_set = std::make_unique("some TS set", 5); + some_TS_set->add({1., 2., 3., 4., 5.}); + dataSeriesRepository.addDataSeries(std::move(some_TS_set)); + + unsigned rank = 0; + unsigned hour = 100; // Hour too big + std::string expected_err_msg = "TS set 'some TS set' : hour 100 exceeds TS set's height"; + BOOST_CHECK_EXCEPTION(dataSeriesRepository.getDataSeries("some TS set").getData(rank, hour), + std::invalid_argument, + checkMessage(expected_err_msg)); +} + +BOOST_AUTO_TEST_CASE(ask_a_more_complex_data_repo_some_data_it_contains___answer_is_correct) +{ + DataSeriesRepository dataSeriesRepository; + + auto TS_set_1 = std::make_unique("TS set 1", 5); + TS_set_1->add({1., 2., 3., 4., 5.}); + TS_set_1->add({11., 12., 13., 14., 15.}); + + auto TS_set_2 = std::make_unique("TS set 2", 5); + TS_set_2->add({21., 22., 23., 24., 25.}); + TS_set_2->add({31., 32., 33., 34., 35.}); + TS_set_2->add({41., 42., 43., 44., 45.}); + + dataSeriesRepository.addDataSeries(std::move(TS_set_1)); + dataSeriesRepository.addDataSeries(std::move(TS_set_2)); + + unsigned rank = 1; + unsigned hour = 3; + BOOST_CHECK_EQUAL(dataSeriesRepository.getDataSeries("TS set 1").getData(rank, hour), 14.); + + rank = 2; + hour = 1; + BOOST_CHECK_EQUAL(dataSeriesRepository.getDataSeries("TS set 2").getData(rank, hour), 42.); +}