Skip to content

Commit

Permalink
create_link modified to use area names instead of Area object
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Dec 6, 2024
1 parent b5bf3a2 commit 51bc156
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 111 deletions.
11 changes: 5 additions & 6 deletions src/antares/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from pydantic import BaseModel

from antares.model.area import Area
from antares.model.commons import FilterOption, sort_filter_values
from antares.tools.alias_generators import to_kebab
from antares.tools.all_optional_meta import all_optional_model
Expand Down Expand Up @@ -132,8 +131,8 @@ def yield_link_ui(self) -> LinkUi:
class Link:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
self,
area_from: Area,
area_to: Area,
area_from: str,
area_to: str,
link_service,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
Expand All @@ -146,14 +145,14 @@ def __init__( # type: ignore # TODO: Find a way to avoid circular imports

@property
def name(self) -> str:
return self._area_from.id + " / " + self._area_to.id
return self._area_from + " / " + self._area_to

@property
def area_from(self) -> Area:
def area_from(self) -> str:
return self._area_from

@property
def area_to(self) -> Area:
def area_to(self) -> str:
return self._area_to

@property
Expand Down
4 changes: 2 additions & 2 deletions src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ def delete_area(self, area: Area) -> None:
def create_link(
self,
*,
area_from: Area,
area_to: Area,
area_from: str,
area_to: str,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
existing_areas: Optional[MappingProxyType[str, Area]] = None,
Expand Down
18 changes: 10 additions & 8 deletions src/antares/service/api_services/link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def __init__(self, config: APIconf, study_id: str):

def create_link(
self,
area_from: Area,
area_to: Area,
area_from: str,
area_to: str,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
existing_areas: Optional[MappingProxyType[str, Area]] = None,
Expand All @@ -60,7 +60,9 @@ def create_link(
"""
base_url = f"{self._base_url}/studies/{self.study_id}"
# TODO: Currently, AntaresWeb does not have a specific endpoint for links. Once it will, we should change this logic.
area1_id, area2_id = sorted([area_from.id, area_to.id])

area1_id, area2_id = sorted([area_from, area_to])

raw_url = f"{base_url}/raw?path=input/links/{area1_id}/properties/{area2_id}"

try:
Expand Down Expand Up @@ -94,13 +96,13 @@ def create_link(
created_properties = LinkProperties.model_validate(json_properties)

except APIError as e:
raise LinkCreationError(area_from.id, area_to.id, e.message) from e
raise LinkCreationError(area_from, area_to, e.message) from e

return Link(area_from, area_to, self, created_properties, ui)

def delete_link(self, link: Link) -> None:
area_from_id = link.area_from.id
area_to_id = link.area_to.id
area_from_id = link.area_from
area_to_id = link.area_to
url = f"{self._base_url}/studies/{self.study_id}/links/{area_from_id}/{area_to_id}"
try:
self._wrapper.delete(url)
Expand All @@ -109,7 +111,7 @@ def delete_link(self, link: Link) -> None:

def update_link_properties(self, link: Link, properties: LinkProperties) -> LinkProperties:
# todo: change this code when AntaresWeb will have a real endpoint
area1_id, area2_id = sorted([link.area_from.id, link.area_to.id])
area1_id, area2_id = sorted([link.area_from, link.area_to])
raw_url = f"{self._base_url}/studies/{self.study_id}/raw?path=input/links/{area1_id}/properties/{area2_id}"
try:
new_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
Expand Down Expand Up @@ -141,7 +143,7 @@ def update_link_properties(self, link: Link, properties: LinkProperties) -> Link

def update_link_ui(self, link: Link, ui: LinkUi) -> LinkUi:
# todo: change this code when AntaresWeb will have a real endpoint
area1_id, area2_id = sorted([link.area_from.id, link.area_to.id])
area1_id, area2_id = sorted([link.area_from, link.area_to])
raw_url = f"{self._base_url}/studies/{self.study_id}/raw?path=input/links/{area1_id}/properties/{area2_id}"
try:
new_ui = ui.model_dump(mode="json", by_alias=True, exclude_none=True)
Expand Down
4 changes: 2 additions & 2 deletions src/antares/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ class BaseLinkService(ABC):
@abstractmethod
def create_link(
self,
area_from: Area,
area_to: Area,
area_from: str,
area_to: str,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
existing_areas: Optional[MappingProxyType[str, Area]] = None,
Expand Down
22 changes: 11 additions & 11 deletions src/antares/service/local_services/link_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __init__(self, config: LocalConfiguration, study_name: str, **kwargs: Any) -

def create_link(
self,
area_from: Area,
area_to: Area,
area_from: str,
area_to: str,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
existing_areas: Optional[MappingProxyType[str, Area]] = None,
Expand All @@ -53,18 +53,18 @@ def create_link(
Raises:
LinkCreationError if an area doesn't exist or existing areas have not been provided
"""
areas = dict(sorted({area_from.name: area_from, area_to.name: area_to}.items()))
areas = dict(sorted({area_from: area_from, area_to: area_to}.items()))

if existing_areas is not None:
for area in areas.keys():
if area not in existing_areas:
raise LinkCreationError(area_from.name, area_to.name, f"{area} does not exist.")
raise LinkCreationError(area_from, area_to, f"{area} does not exist.")
else:
raise LinkCreationError(area_from.name, area_to.name, "Cannot verify existing areas.")
raise LinkCreationError(area_from, area_to, "Cannot verify existing areas.")

area_from, area_to = areas.values()

link_dir = self.config.study_path / "input/links" / area_from.name
link_dir = self.config.study_path / "input/links" / area_from
os.makedirs(link_dir, exist_ok=True)

local_properties = (
Expand All @@ -81,16 +81,16 @@ def create_link(
with open(properties_ini_file, "r") as ini_file:
properties_ini.read_file(ini_file)
try:
properties_ini.add_section(area_to.name)
properties_ini.add_section(area_to)
except DuplicateSectionError:
raise LinkCreationError(
area_from=area_from.name,
area_to=area_to.name,
message=f"Link exists already between '{area_from.name}' and '{area_to.name}'.",
area_from=area_from,
area_to=area_to,
message=f"Link exists already between '{area_from}' and '{area_to}'.",
)
ini_dict = dict(local_properties.ini_fields)
ini_dict.update(local_ui.ini_fields)
properties_ini[area_to.name] = self.sort_link_properties_dict(ini_dict)
properties_ini[area_to] = self.sort_link_properties_dict(ini_dict)

properties_ini = sort_ini_sections(properties_ini)

Expand Down
6 changes: 3 additions & 3 deletions tests/antares/delete/test_delete_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ def test_delete_area_fails(self):

def test_delete_link_success(self):
with requests_mock.Mocker() as mocker:
link = Link(self.area_be, self.area_fr, self.link_service)
link = Link(self.area_be.id, self.area_fr.id, self.link_service)
url = f"https://antares.com/api/v1/studies/{self.study_id}/links/{self.area_be.id}/{self.area_fr.id}"
mocker.delete(url, status_code=200)
self.link_service.delete_link(link)

def test_delete_link_fails(self):
with requests_mock.Mocker() as mocker:
link = Link(self.area_fr, self.area_be, self.link_service)
link = Link(self.area_fr.name, self.area_be.name, self.link_service)
url = f"https://antares.com/api/v1/studies/{self.study_id}/links/{self.area_fr.id}/{self.area_be.id}"
mocker.delete(url, json={"description": self.antares_web_description_msg}, status_code=404)
with pytest.raises(
LinkDeletionError,
match=f"Could not delete the link {self.area_fr.id} / {self.area_be.id}: {self.antares_web_description_msg}",
match=f"Could not delete the link {self.area_fr.name} / {self.area_be.name}: {self.antares_web_description_msg}",
):
self.link_service.delete_link(link)

Expand Down
3 changes: 2 additions & 1 deletion tests/antares/services/api_services/test_link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TestCreateAPI:
)
area_to = Area(name="area_to", area_service=api, storage_service=api, thermal_service=api, renewable_service=api)
antares_web_description_msg = "Mocked Server KO"
link = Link(area_from, area_to, ServiceFactory(api, study_id).create_link_service())
link = Link(area_from.id, area_to.id, ServiceFactory(api, study_id).create_link_service())

def test_update_links_properties_success(self):
with requests_mock.Mocker() as mocker:
Expand Down Expand Up @@ -64,6 +64,7 @@ def test_update_links_properties_fails(self):
)
antares_web_description_msg = "Server KO"
mocker.get(raw_url, json={"description": antares_web_description_msg}, status_code=404)

with pytest.raises(
LinkPropertiesUpdateError,
match=f"Could not update properties for link {self.link.name}: {antares_web_description_msg}",
Expand Down
36 changes: 6 additions & 30 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,10 @@ def test_create_link_success(self):
base_url = f"https://antares.com/api/v1/studies/{self.study_id}"
url = f"{base_url}/links"
mocker.post(url, status_code=200)
area = Area(
name="area",
area_service=self.api,
storage_service=self.api,
thermal_service=self.api,
renewable_service=self.api,
)
area_to = Area(
name="area_to",
area_service=self.api,
storage_service=self.api,
thermal_service=self.api,
renewable_service=self.api,
)
area = "area"
area_to = "area_to"

raw_url = f"{base_url}/raw?path=input/links/{area.id}/properties/{area_to.id}"
raw_url = f"{base_url}/raw?path=input/links/{area}/properties/{area_to}"
json_response = {**LinkProperties().model_dump(by_alias=True), **LinkUi().model_dump(by_alias=True)}
mocker.get(raw_url, json=json_response, status_code=200)
link = self.study.create_link(area_from=area, area_to=area_to)
Expand All @@ -158,24 +146,12 @@ def test_create_link_fails(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/links"
mocker.post(url, json={"description": self.antares_web_description_msg}, status_code=404)
area_from = Area(
name="area_from",
area_service=self.api,
storage_service=self.api,
thermal_service=self.api,
renewable_service=self.api,
)
area_to = Area(
name="area_to",
area_service=self.api,
storage_service=self.api,
thermal_service=self.api,
renewable_service=self.api,
)
area_from = "area_from"
area_to = "area_to"

with pytest.raises(
LinkCreationError,
match=f"Could not create the link {area_from.id} / {area_to.id}: {self.antares_web_description_msg}",
match=f"Could not create the link {area_from} / {area_to}: {self.antares_web_description_msg}",
):
self.study.create_link(area_from=area_from, area_to=area_to)

Expand Down
5 changes: 2 additions & 3 deletions tests/antares/services/local_services/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ def local_study_w_links(tmp_path, local_study_w_areas):
for link in links_to_create:
area_from, area_to = link.split("_")
local_study_w_areas.create_link(
area_from=local_study_w_areas.get_areas()[area_from],
area_to=local_study_w_areas.get_areas()[area_to],
existing_areas=local_study_w_areas.get_areas(),
area_from=area_from, area_to=area_to, existing_areas=local_study_w_areas.get_areas()
)

return local_study_w_areas


Expand Down
Loading

0 comments on commit 51bc156

Please sign in to comment.