Skip to content

Commit

Permalink
Data for new modeler (4.1) : mainly testind data series repository
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpier-code committed Jan 20, 2025
1 parent 9d4d566 commit 35cd165
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/solver/modeler/dataSeries/dataSeriesRepo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ namespace Antares::Solver::Modeler::DataSeries
{
void DataSeriesRepository::addDataSeries(std::unique_ptr<IDataSeries> 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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DataSeriesRepository

private:
std::map<std::string, std::unique_ptr<IDataSeries>> dataSeries_;
std::string err_prefix = "Data series repo : ";
};

} // namespace Antares::Solver::Modeler::DataSeries
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class TimeSeriesSet: public IDataSeries
double getData(unsigned rank, unsigned hour) override;

private:
std::string name_;
unsigned height_ = 0;
std::vector<std::vector<double>> tsSet_;
std::string err_prefix_;
Expand Down
4 changes: 2 additions & 2 deletions src/solver/modeler/dataSeries/timeSeriesSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "' : ")
{
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/src/solver/modeler/dataSeries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions src/tests/src/solver/modeler/dataSeries/testDataSeriesRepo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#define WIN32_LEAN_AND_MEAN
#include <memory>
#include <unit_test_utils.h>

#include <boost/test/unit_test.hpp>

#include <antares/solver/modeler/dataSeries/dataSeriesRepo.h>
#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<TimeSeriesSet>("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<TimeSeriesSet>("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<TimeSeriesSet>("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<TimeSeriesSet>("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<TimeSeriesSet>("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.);
}

0 comments on commit 35cd165

Please sign in to comment.