Skip to content

Commit

Permalink
add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Dec 20, 2024
1 parent 11169e1 commit cf2e4bc
Showing 1 changed file with 248 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <yuni/io/file.h>

#include "antares/antares/constants.h"
#include "antares/study/parts/short-term-storage/container.h"
#include "antares/study/parts/short-term-storage/AdditionalConstraints.h"

Expand Down Expand Up @@ -500,14 +501,24 @@ BOOST_AUTO_TEST_CASE(Validate_InvalidHours)
constraints.variable = "injection";
constraints.operatorType = "less";

ShortTermStorage::SingleAdditionalConstraint constraint;
constraint.hours = {0, 169}; // Invalid hours range (out of 1-168)
constraints.constraints.push_back(constraint);
// Case 1: Empty hours
ShortTermStorage::SingleAdditionalConstraint constraint1;
constraint1.hours = {}; // Invalid: empty
constraints.constraints.push_back(constraint1);

// Case 2: Out of range
ShortTermStorage::SingleAdditionalConstraint constraint2;
constraint2.hours = {120, 169}; // Invalid: out of range
constraints.constraints.push_back(constraint2);

// Case 3: Below minimum
ShortTermStorage::SingleAdditionalConstraint constraint3;
constraint3.hours = {0, 1}; // Invalid: below minimum
constraints.constraints.push_back(constraint3);

auto [ok, error_msg] = constraints.validate();
BOOST_CHECK_EQUAL(ok, false);
BOOST_CHECK_EQUAL(error_msg,
"Hours sets contains invalid values. Must be between 1 and 168.");
BOOST_CHECK_EQUAL(error_msg, "Hours sets contains invalid values. Must be between 1 and 168.");
}

BOOST_AUTO_TEST_CASE(Validate_ValidConstraints)
Expand All @@ -531,5 +542,237 @@ BOOST_AUTO_TEST_CASE(Validate_ValidConstraints)
BOOST_CHECK(error_msg.empty());
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_ValidFile)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=cluster1\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "cluster1";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);

BOOST_CHECK_EQUAL(result, true);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints.size(), 1);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].name,
"constraint1");

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_InvalidHours)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=ClusterA\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[0,1]\n"; // Invalid hours
iniFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "ClusterA";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(result, false);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_ValidHoursFormats)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=cluster1\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1],[1],[3,2,1]\n"; // Valid formats
iniFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "cluster1";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(result, true);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_MissingFile)
{
ShortTermStorage::STStorageInput storageInput;
bool result = storageInput.LoadConstraintsFromIniFile("nonexistent_path");
BOOST_CHECK_EQUAL(result, true);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_InvalidConstraint)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=cluster1\n";
iniFile << "variable=invalid\n"; // Invalid variable
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "cluster1";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(result, false);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_ValidRhs)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=cluster1\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

std::ofstream rhsFile(testPath / "rhs_constraint1.txt");
for (int i = 0; i < HOURS_PER_YEAR; ++i)
{
rhsFile << i * 1.0 << "\n";
}
rhsFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "cluster1";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);

BOOST_CHECK_EQUAL(result, true);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].rhs.size(),
HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].rhs[0], 0.0);
BOOST_CHECK_EQUAL(
storageInput.storagesByIndex[0].additional_constraints[0].rhs[HOURS_PER_YEAR - 1],
HOURS_PER_YEAR - 1);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_MissingRhsFile)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=cluster1\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "cluster1";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);

BOOST_CHECK_EQUAL(result, true);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].rhs.size(),
HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].rhs[0], 0.0);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_MalformedRhsFile)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=ClusterA\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

std::ofstream rhsFile(testPath / "rhs_constraint1.txt");
rhsFile << "1.0\n2.0\ninvalid\n4.0\n"; // Malformed line
rhsFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "ClusterA";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(result, false);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_IncompleteRhsFile)
{
std::filesystem::path testPath = "test_data";
std::filesystem::create_directory(testPath);

std::ofstream iniFile(testPath / "additional-constraints.ini");
iniFile << "[constraint1]\n";
iniFile << "cluster=ClusterA\n";
iniFile << "variable=injection\n";
iniFile << "operator=less\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

std::ofstream rhsFile(testPath / "rhs_constraint1.txt");
for (int i = 0; i < 10; ++i)
{
rhsFile << i * 1.0 << "\n";
}
rhsFile.close();

ShortTermStorage::STStorageInput storageInput;
ShortTermStorage::STStorageCluster cluster;
cluster.id = "ClusterA";
storageInput.storagesByIndex.push_back(cluster);

bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(result, false);

std::filesystem::remove_all(testPath);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit cf2e4bc

Please sign in to comment.