Skip to content

Commit

Permalink
fix(api): horizon to be accepted as str and read_renewables doesn't…
Browse files Browse the repository at this point in the history
… fail when settings are `aggregated` (#74)
  • Loading branch information
vargastat authored Feb 5, 2025
1 parent dfc0a0e commit 9eef4cf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/antares/craft/model/settings/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down
14 changes: 13 additions & 1 deletion src/antares/craft/service/api_services/renewable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/antares/services/api_services/test_area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
10 changes: 10 additions & 0 deletions tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 9eef4cf

Please sign in to comment.