Skip to content

Commit

Permalink
feat(tests): add tests on matrix index
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Oct 9, 2024
1 parent 430e70b commit 830bd83
Showing 1 changed file with 156 additions and 2 deletions.
158 changes: 156 additions & 2 deletions tests/storage/business/test_study_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TimeSeriesData,
)
from antarest.study.storage.study_download_utils import StudyDownloader
from antarest.study.storage.utils import get_start_date
from antarest.study.storage.utils import DAY_NAMES, get_start_date


def test_output_downloads_export(tmp_path: Path):
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_output_downloads_export(tmp_path: Path):
),
],
)
def test_create_matrix_index(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
config_mock = Mock()
config_mock.archived = False
output_id = "some output"
Expand All @@ -236,3 +236,157 @@ def test_create_matrix_index(config: Dict[str, Any], level: StudyDownloadLevelDT
file_study.config.outputs = {output_id: config_mock}

assert get_start_date(file_study, output_id, level) == expected


@pytest.mark.parametrize(
"config,level,expected",
[
(
{
"first-month-in-year": "january",
"january.1st": "Monday",
"leapyear": True,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 354,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2024, 1, 1)),
steps=53,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "january",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 354,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2018, 1, 1)),
steps=53,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Wednesday",
"simulation.start": 5,
"simulation.end": 340,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
steps=53,
first_week_size=4,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 200,
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 100,
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 5,
"simulation.end": 100,
},
StudyDownloadLevelDTO.HOURLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 1)),
steps=8760,
first_week_size=5,
level=StudyDownloadLevelDTO.HOURLY,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 5,
"simulation.end": 100,
},
StudyDownloadLevelDTO.ANNUAL,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 1)),
steps=1,
first_week_size=5,
level=StudyDownloadLevelDTO.ANNUAL,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Sunday",
"leapyear": False,
"first.weekday": "Friday",
"simulation.start": 3,
"simulation.end": 100,
},
StudyDownloadLevelDTO.DAILY,
MatrixIndex(
start_date=str(datetime.datetime(2022, 3, 1)),
steps=365,
first_week_size=3,
level=StudyDownloadLevelDTO.DAILY,
),
),
],
)
def test_create_matrix_index_input(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
file_study = Mock()
file_study.tree.get.return_value = {"general": config}
# Asserts the content are the same
actual = get_start_date(file_study, None, level)
assert actual == expected
# Asserts the returned 1st January corresponds to the chosen one
actual_datetime = datetime.datetime.strptime(actual.start_date, "%Y-%m-%d %H:%M:%S")
next_year = str(actual_datetime.year + (actual_datetime.month != 1))
first_january = datetime.datetime.strptime(next_year, "%Y").weekday()
assert first_january == DAY_NAMES.index(config["january.1st"])

0 comments on commit 830bd83

Please sign in to comment.