Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPascoli committed Sep 19, 2024
1 parent 9dff527 commit cac1349
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 46 deletions.
84 changes: 53 additions & 31 deletions antarest/study/business/link_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
from antarest.study.business.all_optional_meta import all_optional_model, camel_case_model
from antarest.study.business.utils import execute_or_add_commands
from antarest.study.model import RawStudy
from antarest.study.storage.rawstudy.model.filesystem.config.links import LinkProperties, LinkStyle
from antarest.study.storage.rawstudy.model.filesystem.config.links import (
AssetType,
LinkProperties,
LinkStyle,
TransmissionCapacity,
)
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.model.command.common import FilteringOptions
from antarest.study.storage.variantstudy.model.command.create_link import CreateLink
from antarest.study.storage.variantstudy.model.command.remove_link import RemoveLink
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig

_ALL_LINKS_PATH = "input/links"


class LinkUIDTO(BaseModel):
color: str
width: float
style: LinkStyle
DEFAULT_COLOR = "112"


class LinkInfoDTOBase(BaseModel):
Expand All @@ -41,10 +41,14 @@ class LinkInfoDTOBase(BaseModel):
hurdles_cost: t.Optional[bool] = False
loop_flow: t.Optional[bool] = False
use_phase_shifter: t.Optional[bool] = False
transmission_capacities: t.Optional[str] = "enabled"
asset_type: t.Optional[str] = "ac"
transmission_capacities: t.Optional[str] = TransmissionCapacity.ENABLED
asset_type: t.Optional[str] = AssetType.AC
display_comments: t.Optional[bool] = True
ui: t.Optional[LinkUIDTO] = None
colorr: t.Optional[str] = DEFAULT_COLOR
colorb: t.Optional[str] = DEFAULT_COLOR
colorg: t.Optional[str] = DEFAULT_COLOR
link_width: t.Optional[float] = 1
link_style: t.Optional[str] = LinkStyle.PLAIN


class LinkInfoDTO820(LinkInfoDTOBase):
Expand All @@ -57,10 +61,10 @@ class LinkInfoDTO820(LinkInfoDTOBase):

class LinkInfoFactory:
@staticmethod
def create_link_info(version: int, **kwargs) -> LinkInfoDTOType:
def create_link_info(**kwargs) -> LinkInfoDTOType:
filters_provided = kwargs.get("filter_synthesis") is not None or kwargs.get("filter_year_by_year") is not None

if version >= 820:
if kwargs.get("version") >= 820:
link_info = LinkInfoDTO820(**kwargs)
if link_info.filter_synthesis is None:
link_info.filter_synthesis = FilteringOptions.FILTER_SYNTHESIS
Expand Down Expand Up @@ -91,28 +95,36 @@ def get_all_links(self, study: RawStudy, with_ui: bool = False) -> t.List[LinkIn
for area_id, area in file_study.config.areas.items():
links_config = file_study.tree.get(["input", "links", area_id, "properties"])
for link in area.links:
ui_info: t.Optional[LinkUIDTO] = None
link_properties = links_config[link]
link_info_args = {
"version": int(study.version),
"area1": area_id,
"area2": link,
"hurdles_cost": link_properties.get("hurdles-cost"),
"loop_flow": link_properties.get("loop-flow"),
"use_phase_shifter": link_properties.get("use-phase-shifter"),
"transmission_capacities": link_properties.get("transmission-capacities"),
"asset_type": link_properties.get("asset-type"),
"display_comments": link_properties.get("display-comments"),
"filter_synthesis": link_properties.get("filter-synthesis"),
"filter_year_by_year": link_properties.get("filter-year-by-year"),
}
if with_ui and links_config and link in links_config:
ui_info = LinkUIDTO(
color=f"{links_config[link].get('colorr', '163')},{links_config[link].get('colorg', '163')},{links_config[link].get('colorb', '163')}",
width=links_config[link].get("link-width", 1),
style=links_config[link].get("link-style", "plain"),
link_info_args.update(
{
"colorr": str(link_properties.get("colorr", "112")),
"colorb": str(link_properties.get("colorb", "112")),
"colorg": str(link_properties.get("colorg", "112")),
"link_width": link_properties.get("link-width", 1.0),
"link_style": link_properties.get("link-style", "plain"),
}
)
else:
link_info_args.update(
{"colorr": None, "colorb": None, "colorg": None, "link_width": None, "link_style": None}
)
link_info_dto = LinkInfoFactory.create_link_info(**link_info_args)

link_info_dto = LinkInfoFactory.create_link_info(
version=int(study.version),
area1=area_id,
area2=link,
hurdles_cost=links_config[link].get("hurdles-cost"),
loop_flow=links_config[link].get("loop-flow"),
use_phase_shifter=links_config[link].get("use-phase-shifter"),
transmission_capacities=links_config[link].get("transmission-capacities"),
asset_type=links_config[link].get("asset-type"),
display_comments=links_config[link].get("display-comments"),
ui=ui_info,
filter_synthesis=links_config[link].get("filter-synthesis"),
filter_year_by_year=links_config[link].get("filter-year-by-year"),
)
result.append(link_info_dto)
return result

Expand All @@ -128,6 +140,11 @@ def create_link(self, study: RawStudy, link_creation_info: LinkInfoDTOType) -> L
transmission_capacities=link_creation_info.transmission_capacities,
asset_type=link_creation_info.asset_type,
display_comments=link_creation_info.display_comments,
colorr=link_creation_info.colorr,
colorb=link_creation_info.colorb,
colorg=link_creation_info.colorg,
link_width=link_creation_info.link_width,
link_style=link_creation_info.link_style,
filter_synthesis=link_creation_info.filter_synthesis if study_version >= 820 else None,
filter_year_by_year=link_creation_info.filter_year_by_year if study_version >= 820 else None,
)
Expand Down Expand Up @@ -237,6 +254,11 @@ def create_parameters(study_version: int, link_creation_info: LinkInfoDTOType) -
"transmission-capacities": link_creation_info.transmission_capacities,
"asset-type": link_creation_info.asset_type,
"display-comments": link_creation_info.display_comments,
"colorr": link_creation_info.colorr,
"colorb": link_creation_info.colorb,
"colorg": link_creation_info.colorg,
"link-width": link_creation_info.link_width,
"link-style": link_creation_info.link_style,
}

if study_version >= 820 and isinstance(link_creation_info, LinkInfoDTO820):
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,18 @@ def test_area_management(client: TestClient, admin_access_token: str) -> None:
"area1": "area 1",
"area2": "area 2",
"asset_type": "ac",
"colorb": "112",
"colorg": "112",
"colorr": "112",
"display_comments": True,
"filter_synthesis": "hourly, daily, weekly, monthly, annual",
"filter_year_by_year": "hourly, daily, weekly, monthly, annual",
"hurdles_cost": False,
"link_style": "plain",
"link_width": 1.0,
"loop_flow": False,
"transmission_capacities": "enabled",
"use_phase_shifter": False,
"ui": {"color": "112,112,112", "style": "plain", "width": 1.0},
"use_phase_shifter": False
}
]

Expand Down
48 changes: 35 additions & 13 deletions tests/storage/business/test_arealink_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ def test_area_crud(empty_study: FileStudy, matrix_service: SimpleMatrixService):
"hurdles-cost": False,
"loop-flow": False,
"use-phase-shifter": False,
"transmission-capacities": TransmissionCapacity.ENABLED,
"asset-type": AssetType.AC,
"transmission-capacities": "enabled",
"asset-type": "ac",
"display-comments": True,
"colorr": "112",
"colorg": "112",
"colorb": "112",
"link-width": 1.0,
"link-style": "plain",
"filter-synthesis": "hourly, daily, weekly, monthly, annual",
"filter-year-by-year": "hourly, daily, weekly, monthly, annual",
},
Expand Down Expand Up @@ -286,9 +291,14 @@ def test_area_crud(empty_study: FileStudy, matrix_service: SimpleMatrixService):
"hurdles-cost": False,
"loop-flow": False,
"use-phase-shifter": False,
"transmission-capacities": TransmissionCapacity.ENABLED,
"asset-type": AssetType.AC,
"transmission-capacities": "enabled",
"asset-type": "ac",
"display-comments": True,
"colorr": "112",
"colorg": "112",
"colorb": "112",
"link-width": 1.0,
"link-style": "plain",
},
},
),
Expand Down Expand Up @@ -533,40 +543,52 @@ def test_get_all_area():
{
"area1": "a1",
"area2": "a2",
"asset_type": AssetType.DC,
"asset_type": "dc",
"colorb": None,
"colorg": None,
"colorr": None,
"display_comments": False,
"filter_synthesis": "hourly, daily, weekly, monthly, annual",
"filter_year_by_year": "hourly, daily, weekly, monthly, annual",
"hurdles_cost": True,
"link_style": None,
"link_width": None,
"loop_flow": True,
"transmission_capacities": TransmissionCapacity.ENABLED,
"ui": None,
"transmission_capacities": "enabled",
"use_phase_shifter": False,
},
{
"area1": "a1",
"area2": "a3",
"asset_type": AssetType.AC,
"asset_type": "ac",
"colorb": None,
"colorg": None,
"colorr": None,
"display_comments": True,
"filter_synthesis": "hourly, daily, weekly, monthly, annual",
"filter_year_by_year": "hourly, daily, weekly, monthly, annual",
"hurdles_cost": True,
"link_style": None,
"link_width": None,
"loop_flow": False,
"transmission_capacities": TransmissionCapacity.ENABLED,
"ui": None,
"transmission_capacities": "enabled",
"use_phase_shifter": True,
},
{
"area1": "a2",
"area2": "a3",
"asset_type": AssetType.AC,
"asset_type": "ac",
"colorb": None,
"colorg": None,
"colorr": None,
"display_comments": True,
"filter_synthesis": "hourly, daily, weekly, monthly, annual",
"filter_year_by_year": "hourly, daily, weekly, monthly, annual",
"hurdles_cost": True,
"link_style": None,
"link_width": None,
"loop_flow": False,
"transmission_capacities": TransmissionCapacity.ENABLED,
"ui": None,
"transmission_capacities": "enabled",
"use_phase_shifter": True,
},
] == [link.model_dump() for link in links]
Expand Down

0 comments on commit cac1349

Please sign in to comment.