Skip to content

Commit

Permalink
test(commands): improve unit tests for remove_area command
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed Mar 15, 2024
1 parent 8b8f40d commit b8bc294
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ def _remove_area_from_binding_constraints(self, study_data: FileStudy) -> None:
Instead, we decide to remove the binding constraints that are related to the area.
"""
# See also `RemoveArea`
# noinspection SpellCheckingInspection
url = ["input", "bindingconstraints", "bindingconstraints"]
binding_constraints = study_data.tree.get(url)

# Collect the binding constraints that are related to the area to remove
# by searching the terms that contain the ID of the area.
bc_to_remove = {}
lower_area_id = self.id.lower()
for bc_index, bc in list(binding_constraints.items()):
for key in bc:
# Term IDs are in the form `area1%area2` or `area.cluster`
Expand All @@ -110,7 +112,8 @@ def _remove_area_from_binding_constraints(self, study_data: FileStudy) -> None:
else:
# This key belongs to the set of properties, it isn't a term ID, so we skip it
continue
if self.id.lower() in related_areas:
related_areas = [area.lower() for area in related_areas]
if lower_area_id in related_areas:
bc_to_remove[bc_index] = binding_constraints.pop(bc_index)
break

Expand Down
Binary file added tests/variantstudy/assets/empty_study_810.zip
Binary file not shown.
Binary file added tests/variantstudy/assets/empty_study_840.zip
Binary file not shown.
15 changes: 3 additions & 12 deletions tests/variantstudy/model/command/test_remove_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from antarest.study.storage.rawstudy.model.filesystem.config.binding_constraint import BindingConstraintFrequency
from antarest.study.storage.rawstudy.model.filesystem.config.model import transform_name_to_id
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.study_upgrader import upgrade_study
from antarest.study.storage.variantstudy.model.command.common import BindingConstraintOperator
from antarest.study.storage.variantstudy.model.command.create_area import CreateArea
from antarest.study.storage.variantstudy.model.command.create_binding_constraint import CreateBindingConstraint
Expand All @@ -18,13 +17,8 @@


class TestRemoveArea:
@pytest.mark.parametrize("version", [810, 840])
def test_apply(
self,
empty_study: FileStudy,
command_context: CommandContext,
version: int,
):
@pytest.mark.parametrize("empty_study", ["empty_study_810.zip", "empty_study_840.zip"], indirect=True)
def test_apply(self, empty_study: FileStudy, command_context: CommandContext):
# noinspection SpellCheckingInspection
empty_study.tree.save(
{
Expand Down Expand Up @@ -72,10 +66,8 @@ def test_apply(

########################################################################################

upgrade_study(empty_study.config.study_path, str(version))

empty_study_cfg = empty_study.tree.get(depth=999)
if version >= 830:
if empty_study.config.version >= 830:
empty_study_cfg["input"]["areas"][area_id]["adequacy_patch"] = {
"adequacy-patch": {"adequacy-patch-mode": "outside"}
}
Expand All @@ -84,7 +76,6 @@ def test_apply(
area_name2 = "Area2"
area_id2 = transform_name_to_id(area_name2)

empty_study.config.version = version
create_area_command: ICommand = CreateArea.parse_obj(
{
"area_name": area_name2,
Expand Down

0 comments on commit b8bc294

Please sign in to comment.