diff --git a/src/antares/craft/model/settings/general.py b/src/antares/craft/model/settings/general.py index 7871c45e..de4e991b 100644 --- a/src/antares/craft/model/settings/general.py +++ b/src/antares/craft/model/settings/general.py @@ -10,9 +10,11 @@ # # This file is part of the Antares project. +import typing as t + from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import EnumIgnoreCase -from pydantic import BaseModel, ConfigDict, Field +from pydantic import BaseModel, ConfigDict, Field, field_validator from pydantic.alias_generators import to_camel @@ -97,6 +99,11 @@ class DefaultGeneralParameters(BaseModel, extra="forbid", populate_by_name=True, mc_scenario: bool = False # ? output/storenewset result_format: OutputFormat = Field(default=OutputFormat.TXT, exclude=True) + @field_validator("horizon", mode="before") + def transform_horizon_to_str(cls, val: t.Union[str, int, None]) -> t.Optional[str]: + # horizon can be an int. + return str(val) if val else val # type: ignore + @all_optional_model class GeneralParameters(DefaultGeneralParameters): diff --git a/src/antares/craft/service/api_services/renewable_api.py b/src/antares/craft/service/api_services/renewable_api.py index 7097e995..fe5b6c18 100644 --- a/src/antares/craft/service/api_services/renewable_api.py +++ b/src/antares/craft/service/api_services/renewable_api.py @@ -82,9 +82,21 @@ def read_renewables( self, area_id: str, ) -> List[RenewableCluster]: + """ + read_renewables will return an error if + study settings renewable_generation_modelling is aggregated + an empty list will be returned instead + """ + url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/clusters/renewable" - json_renewables = self._wrapper.get(url).json() + try: + json_renewables = self._wrapper.get(url).json() + except APIError as e: + if e.message == "'renewables' not a child of Input": + json_renewables = [] + else: + raise renewables = [] for renewable in json_renewables: diff --git a/tests/antares/services/api_services/test_area_api.py b/tests/antares/services/api_services/test_area_api.py index 58c5b634..fbc7fc47 100644 --- a/tests/antares/services/api_services/test_area_api.py +++ b/tests/antares/services/api_services/test_area_api.py @@ -391,3 +391,18 @@ def test_read_hydro(self): assert actual_hydro.area_id == expected_hydro.area_id assert actual_hydro.properties == expected_hydro.properties assert actual_hydro.matrices is None + + def test_read_renewables_empty(self): + area = self.area + url_renewable = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{area.id}/clusters/renewable" + + with requests_mock.Mocker() as mocker: + mocker.get( + url_renewable, + status_code=404, + json={"description": "'renewables' not a child of Input", "exception": "ChildNotFoundError"}, + ) + + actual_renewables = area.read_renewables() + + assert actual_renewables == [] diff --git a/tests/integration/test_web_client.py b/tests/integration/test_web_client.py index 2d6b3eba..d3fbfed4 100644 --- a/tests/integration/test_web_client.py +++ b/tests/integration/test_web_client.py @@ -504,6 +504,8 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop): # Really important note. To instance such object with value you must respect camel case. # Another way to do so is to instance the object and then fill its values new_settings.general_parameters = GeneralParameters(nbYears=4) + # To create a variant with horizon as string + new_settings.general_parameters.horizon = "2018" new_settings.advanced_parameters = AdvancedParameters() new_settings.advanced_parameters.unit_commitment_mode = UnitCommitmentMode.MILP new_study.update_settings(new_settings) @@ -658,3 +660,11 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop): moved_study = read_study_api(api_config, study.service.study_id) assert moved_study.path == study.path assert moved_study.name == study.name + + new_settings_aggregated = StudySettings() + new_settings_aggregated.advanced_parameters = AdvancedParameters() + new_settings_aggregated.advanced_parameters.renewable_generation_modelling = "aggregated" + study_aggregated = create_study_api("test_aggregated", "880", api_config, new_settings_aggregated) + study_aggregated.create_area("area_without_renewables") + # read_study_api does not raise an error + read_study_api(api_config, study_aggregated.service.study_id)