Skip to content

Commit

Permalink
unnecessary hassattr removed from create_link method
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Dec 10, 2024
1 parent 5f2ba94 commit 6e68eba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
17 changes: 4 additions & 13 deletions src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,15 @@ def create_link(
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
) -> Link:
missing_areas = [
area for area in [area_from, area_to] if area not in self._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.")
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
(link for link in self._links.values() if link.area_from == area_from and link.area_to == area_to), None
)
if existing_link:
raise LinkCreationError(
area_from, area_to, f"A link from {area_from} to {area_to} already exists"
)
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
Expand Down
25 changes: 19 additions & 6 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_create_link_fails(self):

with pytest.raises(
LinkCreationError,
match=f"Could not create the link area_from / {area_to}: {area_to} does not exist.",
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)

Expand Down Expand Up @@ -288,16 +288,15 @@ def test_create_variant_fails(self):
with pytest.raises(StudyVariantCreationError, match=error_message):
create_variant_api(self.api, self.study_id, variant_name)


def test_create_link_already_created(self):
def test_create_duplicated_link(self):
area_from = "area_1"
area_to = "area_2"

self.study._areas[area_from] = Area(
area_from,
self.study._area_service,
Mock(),
Mock(),
Mock(),
Mock(),
)
self.study._areas[area_to] = Area(
Expand All @@ -310,12 +309,26 @@ def test_create_link_already_created(self):

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


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

def test_create_link_unknown_area(self):
area_from = "area_fr"
area_to = "area_missing"

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

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)

0 comments on commit 6e68eba

Please sign in to comment.