Skip to content

Commit

Permalink
feat(api): add read hydro method (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdiwahada authored Nov 29, 2024
1 parent 7344e80 commit b7cf90d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def create_renewable_cluster(
def create_st_storage(self, st_storage_name: str, properties: Optional[STStorageProperties] = None) -> STStorage:
storage = self._area_service.create_st_storage(self.id, st_storage_name, properties)
self._st_storages[storage.id] = storage

return storage

def get_load_matrix(self) -> pd.DataFrame:
Expand Down Expand Up @@ -374,3 +375,7 @@ def read_thermal_clusters(
) -> List[ThermalCluster]:
return self._thermal_service.read_thermal_clusters(area_id)

def read_hydro(
self,
) -> Hydro:
return self._area_service.read_hydro(self.id)
11 changes: 10 additions & 1 deletion src/antares/service/api_services/area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def create_area(
ui_response = AreaUiResponse.model_validate(json_ui)
ui_properties = AreaUi.model_validate(ui_response.to_craft())

hydro = self.read_hydro(area_id)

except APIError as e:
raise AreaCreationError(area_name, e.message) from e

Expand All @@ -135,6 +137,7 @@ def create_area(
self.renewable_service,
properties=area_properties,
ui=ui_properties,
hydro=hydro,
)

def create_thermal_cluster(
Expand Down Expand Up @@ -411,7 +414,13 @@ def read_hydro(
self,
area_id: str,
) -> Hydro:
raise NotImplementedError
url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/hydro/form"
json_hydro = self._wrapper.get(url).json()

hydro_props = HydroProperties(**json_hydro)
hydro = Hydro(self, area_id, hydro_props)

return hydro

def _create_hydro_series(self, area_id: str, matrices: Dict[HydroMatrixName, pd.DataFrame]) -> None:
command_body = []
Expand Down
31 changes: 31 additions & 0 deletions tests/antares/services/api_services/test_area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,34 @@ def test_read_areas(self):
assert actual_thermals[thermal_id].name == expected_area.get_thermals()[thermal_id].name
assert actual_renewables[renewable_id].name == expected_area.get_renewables()[renewable_id].name
assert actual_storages[storage_id].name == expected_area.get_st_storages()[storage_id].name

def test_read_hydro(self):
json_hydro = {
"interDailyBreakdown": 1,
"intraDailyModulation": 24,
"interMonthlyBreakdown": 1,
"reservoir": "false",
"reservoirCapacity": 0,
"followLoad": "true",
"useWater": "false",
"hardBounds": "false",
"initializeReservoirDate": 0,
"useHeuristic": "true",
"powerToLevel": "false",
"useLeeway": "false",
"leewayLow": 1,
"leewayUp": 1,
"pumpingEfficiency": 1,
}
url = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{self.area.id}/hydro/form"

with requests_mock.Mocker() as mocker:
mocker.get(url, json=json_hydro)
hydro_props = HydroProperties(**json_hydro)

actual_hydro = Hydro(self.api, self.area.id, hydro_props)
expected_hydro = self.area.read_hydro()

assert actual_hydro.area_id == expected_hydro.area_id
assert actual_hydro.properties == expected_hydro.properties
assert actual_hydro.matrices is None
4 changes: 3 additions & 1 deletion tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
from antares.model.area import Area, AreaProperties, AreaUi
from antares.model.binding_constraint import BindingConstraint, BindingConstraintProperties
from antares.model.hydro import HydroProperties
from antares.model.link import Link, LinkProperties, LinkUi
from antares.model.settings.general import GeneralParameters
from antares.model.settings.study_settings import StudySettings
Expand Down Expand Up @@ -108,9 +109,10 @@ def test_create_area_success(self):
}
mocker.get(url1, json={area_name: area_ui}, status_code=201)
url2 = f"{base_url}/studies/{self.study_id}/areas/{area_name}/properties/form"
url3 = f"{base_url}/studies/{self.study_id}/areas/{area_name}/hydro/form"
mocker.put(url2, status_code=201)
mocker.get(url2, json=AreaProperties().model_dump(), status_code=200)

mocker.get(url3, json=HydroProperties().model_dump())
area = self.study.create_area(area_name)
assert isinstance(area, Area)

Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):
actual_storage = storage_list[0]
assert actual_storage.id == storage_fr.id

# test actual_hydro has the same datas (id, properties and matrices) than area_fr hydro
actual_hydro = area_fr.read_hydro()
assert actual_hydro.area_id == area_fr.id
assert actual_hydro.properties == area_fr.hydro.properties

# test short term storage creation with properties
st_storage_name = "wind_onshore"
storage_properties = STStorageProperties(reservoir_capacity=0.5)
Expand Down

0 comments on commit b7cf90d

Please sign in to comment.