From cf2e4bcf51fea52f880138d452f80b8661a181a9 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 20 Dec 2024 16:09:07 +0100 Subject: [PATCH] add UT --- .../short-term-storage-input-output.cpp | 253 +++++++++++++++++- 1 file changed, 248 insertions(+), 5 deletions(-) diff --git a/src/tests/src/libs/antares/study/short-term-storage-input/short-term-storage-input-output.cpp b/src/tests/src/libs/antares/study/short-term-storage-input/short-term-storage-input-output.cpp index 16451dc2e5..c5e74d4d8d 100644 --- a/src/tests/src/libs/antares/study/short-term-storage-input/short-term-storage-input-output.cpp +++ b/src/tests/src/libs/antares/study/short-term-storage-input/short-term-storage-input-output.cpp @@ -29,6 +29,7 @@ #include +#include "antares/antares/constants.h" #include "antares/study/parts/short-term-storage/container.h" #include "antares/study/parts/short-term-storage/AdditionalConstraints.h" @@ -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) @@ -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()