Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refacto[Link]: id added instead of name #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/antares/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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
from antares.tools.contents_tool import transform_name_to_id


class TransmissionCapacities(Enum):
Expand Down Expand Up @@ -143,16 +144,16 @@ def __init__( # type: ignore # TODO: Find a way to avoid circular imports
self._ui = ui or LinkUi()

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

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

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

@property
def properties(self) -> LinkProperties:
Expand Down
16 changes: 10 additions & 6 deletions src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,27 @@ def create_link(
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
) -> Link:
if area_from == area_to:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should compare ids and not names :/

raise LinkCreationError(area_from, area_to, "A link cannot start and end at the same area")

sorted_areas = sorted([area_from, area_to])
area_from, area_to = sorted_areas

missing_areas = [area for area in [area_from, area_to] if area not in self._areas]
if missing_areas:
raise LinkCreationError(area_from, area_to, f"{', '.join(missing_areas)} does not exist")

existing_link = next(
(link for link in self._links.values() if link.area_from == area_from and link.area_to == area_to), None
)
if existing_link:
temp_link = Link(area_from, area_to, link_service=None)
if temp_link.id in self._links:
raise LinkCreationError(area_from, area_to, f"A link from {area_from} to {area_to} already exists")

link = self._link_service.create_link(area_from, area_to, properties, ui)
self._links[link.name] = link
self._links[link.id] = link
return link

def delete_link(self, link: Link) -> None:
self._link_service.delete_link(link)
self._links.pop(link.name)
self._links.pop(link.id)

def create_binding_constraint(
self,
Expand Down
18 changes: 9 additions & 9 deletions src/antares/service/api_services/link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ def create_link(
return Link(area_from, area_to, self, created_properties, ui)

def delete_link(self, link: Link) -> None:
area_from_id = link.area_from
area_to_id = link.area_to
area_from_id = link.area_from_id
area_to_id = link.area_to_id
url = f"{self._base_url}/studies/{self.study_id}/links/{area_from_id}/{area_to_id}"
try:
self._wrapper.delete(url)
except APIError as e:
raise LinkDeletionError(link.name, e.message) from e
raise LinkDeletionError(link.id, e.message) from e

def update_link_properties(self, link: Link, properties: LinkProperties) -> LinkProperties:
# todo: change this code when AntaresWeb will have a real endpoint
area1_id = link.area_from
area2_id = link.area_to
area1_id = link.area_from_id
area2_id = link.area_to_id
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 All @@ -132,14 +132,14 @@ def update_link_properties(self, link: Link, properties: LinkProperties) -> Link
link_properties = LinkProperties.model_validate(json_response)

except APIError as e:
raise LinkPropertiesUpdateError(link.name, e.message) from e
raise LinkPropertiesUpdateError(link.id, e.message) from e

return link_properties

def update_link_ui(self, link: Link, ui: LinkUi) -> LinkUi:
# todo: change this code when AntaresWeb will have a real endpoint
area1_id = link.area_from
area2_id = link.area_to
area1_id = link.area_from_id
area2_id = link.area_to_id
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 All @@ -158,7 +158,7 @@ def update_link_ui(self, link: Link, ui: LinkUi) -> LinkUi:
link_ui = LinkUi.model_validate(json_response)

except APIError as e:
raise LinkUiUpdateError(link.name, e.message) from e
raise LinkUiUpdateError(link.id, e.message) from e

return link_ui

Expand Down
1 change: 0 additions & 1 deletion src/antares/service/local_services/link_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def create_link(
Raises:
LinkCreationError if an area doesn't exist or existing areas have not been provided
"""
area_from, area_to = sorted([area_from, area_to])

link_dir = self.config.study_path / "input/links" / area_from
os.makedirs(link_dir, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/antares/services/api_services/test_link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_update_links_properties_fails(self):

with pytest.raises(
LinkPropertiesUpdateError,
match=f"Could not update properties for link {self.link.name}: {antares_web_description_msg}",
match=f"Could not update properties for link {self.link.id}: {antares_web_description_msg}",
):
self.link.update_properties(properties)

Expand Down Expand Up @@ -99,6 +99,6 @@ def test_update_links_ui_fails(self):
mocker.get(raw_url, json={"description": antares_web_description_msg}, status_code=404)
with pytest.raises(
LinkUiUpdateError,
match=f"Could not update ui for link {self.link.name}: {antares_web_description_msg}",
match=f"Could not update ui for link {self.link.id}: {antares_web_description_msg}",
):
self.link.update_ui(ui)
56 changes: 27 additions & 29 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,6 @@ def test_create_link_success(self):
link = self.study.create_link(area_from="area", area_to="area_to")
assert isinstance(link, Link)

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)

self.study._areas["area_from"] = Area(
"area_from",
Mock(),
Mock(),
Mock(),
Mock(),
)
area_to = "area_final"

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

def test_create_binding_constraint_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/bindingconstraints"
Expand Down Expand Up @@ -289,31 +269,32 @@ def test_create_variant_fails(self):
create_variant_api(self.api, self.study_id, variant_name)

def test_create_duplicated_link(self):
vargastat marked this conversation as resolved.
Show resolved Hide resolved
area_from = "area_1"
area_to = "area_2"
area_a = "area_a"
area_b = "area_b"

self.study._areas[area_from] = Area(
area_from,
self.study._areas[area_a] = Area(
area_a,
self.study._area_service,
Mock(),
Mock(),
Mock(),
)
self.study._areas[area_to] = Area(
area_to,
self.study._areas[area_b] = Area(
area_b,
self.study._area_service,
Mock(),
Mock(),
Mock(),
)

self.study._links[f"{area_from}/{area_to}"] = Link(area_from, area_to, Mock())
existing_link = Link(area_a, area_b, Mock())
self.study._links[existing_link.id] = existing_link

with pytest.raises(
LinkCreationError,
match=f"Could not create the link {area_from} / {area_to}: A link from {area_from} to {area_to} already exists",
match=f"Could not create the link {area_a} / {area_b}: A link from {area_a} to {area_b} already exists",
):
self.study.create_link(area_from=area_from, area_to=area_to)
self.study.create_link(area_from=area_b, area_to=area_a)

def test_create_link_unknown_area(self):
area_from = "area_fr"
Expand All @@ -332,3 +313,20 @@ def test_create_link_unknown_area(self):
match=f"Could not create the link {area_from} / {area_to}: {area_to} does not exist",
):
self.study.create_link(area_from=area_from, area_to=area_to)

def test_create_link_same_area(self):
area = "area_1"

self.study._areas[area] = Area(
area,
self.study._area_service,
Mock(),
Mock(),
Mock(),
)

with pytest.raises(
LinkCreationError,
match=f"Could not create the link {area} / {area}: A link cannot start and end at the same area",
):
self.study.create_link(area_from=area, area_to=area)
4 changes: 2 additions & 2 deletions tests/antares/services/local_services/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,8 +1653,8 @@ def test_create_link_alphabetically(self, tmp_path, local_study):
area_to=area_to,
)

assert link_created.area_from == "at"
assert link_created.area_to == "fr"
assert link_created.area_from_id == "at"
assert link_created.area_to_id == "fr"

def test_create_link_sets_ini_content(self, tmp_path, local_study_w_areas):
# Given
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):

# tests link creation with default values
link_de_fr = study.create_link(area_from=area_de.id, area_to=area_fr.id)
assert link_de_fr.area_from == area_de.id
assert link_de_fr.area_to == area_fr.id
assert link_de_fr.name == f"{area_de.id} / {area_fr.id}"
assert link_de_fr.area_from_id == area_de.id
assert link_de_fr.area_to_id == area_fr.id
assert link_de_fr.id == f"{area_de.id} / {area_fr.id}"

# tests link creation with ui and properties
link_ui = LinkUi(colorr=44)
Expand All @@ -120,7 +120,7 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):

# asserts study contains all links and areas
assert study.get_areas() == {area_be.id: area_be, area_fr.id: area_fr, area_de.id: area_de}
assert study.get_links() == {link_be_fr.name: link_be_fr, link_de_fr.name: link_de_fr}
assert study.get_links() == {link_be_fr.id: link_be_fr, link_de_fr.id: link_de_fr}

# test thermal cluster creation with default values
thermal_name = "Cluster_test %?"
Expand Down Expand Up @@ -407,7 +407,7 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):

# tests link deletion
study.delete_link(link_de_fr)
assert link_de_fr.name not in study.get_links()
assert link_de_fr.id not in study.get_links()

# tests thermal cluster deletion
area_be.delete_thermal_cluster(thermal_be)
Expand Down