Skip to content

Commit

Permalink
fix for horizon and read_renewables to return empty list if input mis…
Browse files Browse the repository at this point in the history
…sing
  • Loading branch information
vargastat committed Feb 4, 2025
1 parent 935a48e commit 16eba04
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
15 changes: 1 addition & 14 deletions src/antares/craft/api_conf/request_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,16 @@
]


class EmptyResponse(requests.Response):
"""
Class to simulate an empty requests.Response.
"""

def json(self, **kwargs: Any) -> list:
return []


def _handle_exceptions(response: requests.Response) -> requests.Response:
"""
If an exception occurred, returns APIError exception containing the AntaresWeb error message.
"""
if response.status_code - 200 < 100:
return response
try:
error_data = response.json()
msg = error_data.get("description", response.reason)
if response.status_code == 404 and "renewables" in msg:
return EmptyResponse()
msg = response.json()["description"]
except (json.decoder.JSONDecodeError, KeyError):
msg = response.reason

raise APIError(msg)


Expand Down
8 changes: 7 additions & 1 deletion src/antares/craft/service/api_services/renewable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ def read_renewables(
area_id: str,
) -> List[RenewableCluster]:
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 == []
2 changes: 0 additions & 2 deletions tests/antares/services/api_services/test_renewable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,3 @@ def test_read_renewables_with_404_response_then_return_empty_list(self):
actual_renewable_list = renewable_api.read_renewables(area_id)

assert actual_renewable_list == []

assert len(actual_renewable_list) == 0
2 changes: 0 additions & 2 deletions tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,7 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):
assert moved_study.name == study.name

new_settings_aggregated = StudySettings()
new_settings_aggregated.general_parameters = GeneralParameters(nbYears=4)
new_settings_aggregated.advanced_parameters = AdvancedParameters()
new_settings_aggregated.advanced_parameters.unit_commitment_mode = UnitCommitmentMode.MILP
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")
Expand Down

0 comments on commit 16eba04

Please sign in to comment.