Skip to content

Commit

Permalink
fix(api-study): check area duplicates on creation (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte authored Mar 4, 2024
1 parent c491baf commit 4201e88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
8 changes: 8 additions & 0 deletions antarest/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ def __init__(self, *area_ids: str) -> None:
super().__init__(HTTPStatus.NOT_FOUND, msg)


class DuplicateAreaName(HTTPException):
"""Exception raised when trying to create an area with an already existing name."""

def __init__(self, area_name: str) -> None:
msg = f"Area '{area_name}' already exists and could not be created"
super().__init__(HTTPStatus.CONFLICT, msg)


class DistrictNotFound(HTTPException):
def __init__(self, *district_ids: str) -> None:
count = len(district_ids)
Expand Down
12 changes: 10 additions & 2 deletions antarest/study/business/area_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pydantic import BaseModel

from antarest.core.exceptions import LayerNotAllowedToBeDeleted, LayerNotFound
from antarest.core.exceptions import DuplicateAreaName, LayerNotAllowedToBeDeleted, LayerNotFound
from antarest.study.business.utils import execute_or_add_commands
from antarest.study.model import Patch, PatchArea, PatchCluster, RawStudy, Study
from antarest.study.repository import StudyMetadataRepository
Expand Down Expand Up @@ -318,12 +318,20 @@ def remove_layer(self, study: RawStudy, layer_id: str) -> None:

def create_area(self, study: Study, area_creation_info: AreaCreationDTO) -> AreaInfoDTO:
file_study = self.storage_service.get_storage(study).get_raw(study)

# check if area already exists
area_id = transform_name_to_id(area_creation_info.name)
if area_id in set(file_study.config.areas):
raise DuplicateAreaName(area_creation_info.name)

# Create area and apply changes in the study
command = CreateArea(
area_name=area_creation_info.name,
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)
execute_or_add_commands(study, file_study, [command], self.storage_service)
area_id = transform_name_to_id(area_creation_info.name)

# Update metadata
patch = self.patch_service.get(study)
patch.areas = patch.areas or {}
patch.areas[area_id] = area_creation_info.metadata or PatchArea()
Expand Down
19 changes: 11 additions & 8 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from antarest.core.model import PublicMode
from antarest.launcher.model import LauncherLoadDTO
from antarest.study.business.adequacy_patch_management import PriceTakingOrder
from antarest.study.business.area_management import AreaType, LayerInfoDTO
from antarest.study.business.area_management import LayerInfoDTO
from antarest.study.business.areas.properties_management import AdequacyPatchMode
from antarest.study.business.areas.renewable_management import TimeSeriesInterpretation
from antarest.study.business.general_management import Mode
Expand Down Expand Up @@ -420,31 +420,34 @@ def test_area_management(client: TestClient, admin_access_token: str, study_id:
headers=admin_headers,
json={
"name": "area 1",
"type": AreaType.AREA.value,
"type": "AREA",
"metadata": {"country": "FR", "tags": ["a"]},
},
)
assert res.status_code == 200, res.json()

# Test area creation with duplicate name
res = client.post(
f"/v1/studies/{study_id}/areas",
headers=admin_headers,
json={
"name": "area 1",
"type": AreaType.AREA.value,
"name": "Area 1", # Same name but with different case
"type": "AREA",
"metadata": {"country": "FR"},
},
)
assert res.status_code == 500
assert res.status_code == 409, res.json()
assert res.json() == {
"description": "Area 'area 1' already exists and could not be created",
"exception": "CommandApplicationError",
"description": "Area 'Area 1' already exists and could not be created",
"exception": "DuplicateAreaName",
}

client.post(
f"/v1/studies/{study_id}/areas",
headers=admin_headers,
json={
"name": "area 2",
"type": AreaType.AREA.value,
"type": "AREA",
"metadata": {"country": "DE"},
},
)
Expand Down

0 comments on commit 4201e88

Please sign in to comment.