Skip to content

Commit

Permalink
return an object to be more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle authored and skamril committed Jan 22, 2025
1 parent af1402b commit 5ea3723
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
21 changes: 13 additions & 8 deletions antarest/study/business/timeseries_config_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@
#
# This file is part of the Antares project.

from antarest.core.serialization import AntaresBaseModel
from antarest.study.business.all_optional_meta import all_optional_model
from antarest.study.business.utils import GENERAL_DATA_PATH, FormFieldsBaseModel, execute_or_add_commands
from antarest.study.business.utils import GENERAL_DATA_PATH, execute_or_add_commands
from antarest.study.model import Study
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig


class TSConfigFields(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True):
number: int


@all_optional_model
class TSFormFields(FormFieldsBaseModel):
thermal: int
class TSConfigDTO(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True):
thermal: TSConfigFields


class TimeSeriesConfigManager:
def __init__(self, storage_service: StudyStorageService) -> None:
self.storage_service = storage_service

def get_values(self, study: Study) -> TSFormFields:
def get_values(self, study: Study) -> TSConfigDTO:
"""
Get Time-Series generation values
"""
Expand All @@ -35,10 +40,10 @@ def get_values(self, study: Study) -> TSFormFields:
url.extend(["general", "nbtimeseriesthermal"])
nb_ts_gen_thermal = file_study.tree.get(url)

args = {"thermal": nb_ts_gen_thermal}
return TSFormFields.model_validate(args)
args = {"thermal": TSConfigFields(number=nb_ts_gen_thermal)}
return TSConfigDTO.model_validate(args)

def set_values(self, study: Study, field_values: TSFormFields) -> None:
def set_values(self, study: Study, field_values: TSConfigDTO) -> None:
"""
Set Time-Series generation values
"""
Expand All @@ -48,7 +53,7 @@ def set_values(self, study: Study, field_values: TSFormFields) -> None:
url = f"{GENERAL_DATA_PATH}/general/nbtimeseriesthermal"
command = UpdateConfig(
target=url,
data=field_values.thermal,
data=field_values.thermal.number,
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
)
Expand Down
8 changes: 4 additions & 4 deletions antarest/study/web/study_data_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
from antarest.study.business.scenario_builder_management import Rulesets, ScenarioType
from antarest.study.business.table_mode_management import TableDataDTO, TableModeType
from antarest.study.business.thematic_trimming_field_infos import ThematicTrimmingFormFields
from antarest.study.business.timeseries_config_management import TSFormFields
from antarest.study.business.timeseries_config_management import TSConfigDTO
from antarest.study.model import PatchArea, PatchCluster
from antarest.study.service import StudyService
from antarest.study.storage.rawstudy.model.filesystem.config.binding_constraint import (
Expand Down Expand Up @@ -968,13 +968,13 @@ def set_adequacy_patch_form_values(
path="/studies/{uuid}/timeseries/config",
tags=[APITag.study_data],
summary="Gets the TS Generation config",
response_model=TSFormFields,
response_model=TSConfigDTO,
response_model_exclude_none=True,
)
def get_timeseries_form_values(
uuid: str,
current_user: JWTUser = Depends(auth.get_current_user),
) -> TSFormFields:
) -> TSConfigDTO:
logger.info(
msg=f"Getting Time-Series generation config for study {uuid}",
extra={"user": current_user.id},
Expand All @@ -990,7 +990,7 @@ def get_timeseries_form_values(
summary="Sets the TS Generation config",
)
def set_ts_generation_config(
uuid: str, field_values: TSFormFields, current_user: JWTUser = Depends(auth.get_current_user)
uuid: str, field_values: TSConfigDTO, current_user: JWTUser = Depends(auth.get_current_user)
) -> None:
logger.info(
f"Updating Time-Series generation config for study {uuid}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_lifecycle_nominal(self, client: TestClient, user_access_token: str) ->
area2_id = preparer.create_area(study_id, name="Area 2")["id"]
# Set nb timeseries thermal to 3.
nb_years = 3
res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": nb_years})
res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": nb_years}})
assert res.status_code in {200, 201}

# Create 1 cluster in area1
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_advanced_results(self, client: TestClient, user_access_token: str) -> N
preparer = PreparerProxy(client, user_access_token)
study_id = preparer.create_study("foo", version=860)
area_id = preparer.create_area(study_id, name="test")["id"]
res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": 10})
res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": 10}})
cluster_id = "cluster_test"
assert res.status_code in {200, 201}

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,11 +1225,11 @@ def test_area_management(client: TestClient, admin_access_token: str) -> None:

res_ts_config = client.get(f"/v1/studies/{study_id}/timeseries/config")
res_ts_config_json = res_ts_config.json()
assert res_ts_config_json == {"thermal": 1}
client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": 2})
assert res_ts_config_json == {"thermal": {"number": 1}}
client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": 2}})
res_ts_config = client.get(f"/v1/studies/{study_id}/timeseries/config")
res_ts_config_json = res_ts_config.json()
assert res_ts_config_json == {"thermal": 2}
assert res_ts_config_json == {"thermal": {"number": 2}}

# Renewable form

Expand Down
6 changes: 3 additions & 3 deletions tests/storage/business/test_timeseries_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pytest

from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSFormFields
from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSConfigDTO, TSConfigFields
from antarest.study.model import RawStudy
from antarest.study.storage.rawstudy.model.filesystem.config.files import build
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
Expand Down Expand Up @@ -53,9 +53,9 @@ def test_ts_field_values(
study = RawStudy(id="test", path=str(file_study_820.config.path))

# Asserts the get method returns the right value
assert config_manager.get_values(study) == TSFormFields(thermal=1)
assert config_manager.get_values(study) == TSConfigDTO(thermal=TSConfigFields(number=1))

# Modifies the value and asserts the get takes the modification into account
new_value = TSFormFields(thermal=2)
new_value = TSConfigDTO(thermal=TSConfigFields(number=2))
config_manager.set_values(study, new_value)
assert config_manager.get_values(study) == new_value

0 comments on commit 5ea3723

Please sign in to comment.