Skip to content

Commit

Permalink
chore: improve error message for extra pfs columns (#541)
Browse files Browse the repository at this point in the history
* chore: improve error message for extra pfs columns

ECALC-1342
  • Loading branch information
frodehk authored Jul 23, 2024
1 parent 212b01f commit 13bac91
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
25 changes: 10 additions & 15 deletions src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
)
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression
from libecalc.presentation.yaml.ltp_validation import (
validate_generator_set_power_from_shore,
)
from libecalc.presentation.yaml.yaml_keywords import EcalcYamlKeywords
from libecalc.presentation.yaml.yaml_types.emitters.yaml_venting_emitter import (
YamlVentingEmitter,
Expand Down Expand Up @@ -323,21 +326,13 @@ def check_mandatory_category_for_generator_set(cls, user_defined_category, info:

@model_validator(mode="after")
def check_power_from_shore(self):
if self.cable_loss is not None or self.max_usage_from_shore is not None:
if isinstance(self.user_defined_category, ConsumerUserDefinedCategoryType):
if self.user_defined_category is not ConsumerUserDefinedCategoryType.POWER_FROM_SHORE:
raise ValueError(
f"{self.model_fields['cable_loss'].title} and {self.model_fields['max_usage_from_shore'].title} "
f"are only valid for the category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE.value}, not for "
f"{self.user_defined_category}."
)
else:
if ConsumerUserDefinedCategoryType.POWER_FROM_SHORE not in self.user_defined_category.values():
raise ValueError(
f"{self.model_fields['cable_loss'].title} and {self.model_fields['max_usage_from_shore'].title} "
f"are only valid for the category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE.value}, not for "
f"{self.user_defined_category[datetime(1900, 1,1)].value}."
)
_check_power_from_shore_attributes = validate_generator_set_power_from_shore(
cable_loss=self.cable_loss,
max_usage_from_shore=self.max_usage_from_shore,
model_fields=self.model_fields,
category=self.user_defined_category,
)

return self

def get_graph(self) -> ComponentGraph:
Expand Down
33 changes: 33 additions & 0 deletions src/libecalc/presentation/yaml/ltp_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime
from typing import Union

from libecalc.dto.base import ConsumerUserDefinedCategoryType
from libecalc.dto.utils.validators import ExpressionType


def validate_generator_set_power_from_shore(
cable_loss: ExpressionType,
max_usage_from_shore: ExpressionType,
model_fields: dict,
category: Union[dict, ConsumerUserDefinedCategoryType],
):
if cable_loss is not None or max_usage_from_shore is not None:
feedback_text = (
f"{model_fields['cable_loss'].title} and " f"{model_fields['max_usage_from_shore'].title} are only valid"
)
if cable_loss is None:
feedback_text = f"{model_fields['max_usage_from_shore'].title} is only valid"
if max_usage_from_shore is None:
feedback_text = f"{model_fields['cable_loss'].title} is only valid"

if isinstance(category, ConsumerUserDefinedCategoryType):
if category is not ConsumerUserDefinedCategoryType.POWER_FROM_SHORE:
message = f"{feedback_text} for the category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE.value}, not for {category}."
raise ValueError(message)
else:
if ConsumerUserDefinedCategoryType.POWER_FROM_SHORE not in category.values():
message = (
f"{feedback_text} for the category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE.value}"
f", not for {category[datetime(1900, 1, 1)].value}."
)
raise ValueError(message)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from libecalc.dto.base import ConsumerUserDefinedCategoryType
from libecalc.dto.utils.validators import ComponentNameStr
from libecalc.expression.expression import ExpressionType
from libecalc.presentation.yaml.ltp_validation import (
validate_generator_set_power_from_shore,
)
from libecalc.presentation.yaml.yaml_types import YamlBase
from libecalc.presentation.yaml.yaml_types.components.legacy.yaml_electricity_consumer import (
YamlElectricityConsumer,
Expand Down Expand Up @@ -68,18 +71,10 @@ class YamlGeneratorSet(YamlBase):

@model_validator(mode="after")
def check_power_from_shore(self):
if self.cable_loss is not None or self.max_usage_from_shore is not None:
if isinstance(self.category, ConsumerUserDefinedCategoryType):
if self.category is not ConsumerUserDefinedCategoryType.POWER_FROM_SHORE:
raise ValueError(
f"{self.model_fields['cable_loss'].title} and {self.model_fields['max_usage_from_shore'].title} are only valid for the "
f"category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE}, not for "
f"{self.category}."
)
else:
if ConsumerUserDefinedCategoryType.POWER_FROM_SHORE not in self.category.values():
raise ValueError(
f"{self.model_fields['cable_loss'].title} and {self.model_fields['max_usage_from_shore'].title} are only valid for the "
f"category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE}."
)
_check_power_from_shore_attributes = validate_generator_set_power_from_shore(
cable_loss=self.cable_loss,
max_usage_from_shore=self.max_usage_from_shore,
model_fields=self.model_fields,
category=self.category,
)
return self
20 changes: 17 additions & 3 deletions src/tests/libecalc/dto/test_generator_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def test_power_from_shore_wrong_category(self):
cable_loss=0,
)

assert (
"CABLE_LOSS and MAX_USAGE_FROM_SHORE are only valid for the category POWER-FROM-SHORE, not for BOILER"
) in str(exc_info.value)
assert ("CABLE_LOSS is only valid for the category POWER-FROM-SHORE, not for BOILER") in str(exc_info.value)

# Check for MAX_USAGE_FROM_SHORE
with pytest.raises(ValueError) as exc_info:
Expand All @@ -124,6 +122,22 @@ def test_power_from_shore_wrong_category(self):
max_usage_from_shore=20,
)

assert ("MAX_USAGE_FROM_SHORE is only valid for the category POWER-FROM-SHORE, not for BOILER") in str(
exc_info.value
)

with pytest.raises(ValueError) as exc_info:
dto.GeneratorSet(
name="Test",
user_defined_category={datetime(1900, 1, 1): ConsumerUserDefinedCategoryType.BOILER},
generator_set_model={},
regularity={datetime(1900, 1, 1): Expression.setup_from_expression(1)},
consumers=[],
fuel={},
max_usage_from_shore=20,
cable_loss=0,
)

assert (
"CABLE_LOSS and MAX_USAGE_FROM_SHORE are only valid for the category POWER-FROM-SHORE, not for BOILER"
) in str(exc_info.value)

0 comments on commit 13bac91

Please sign in to comment.