Skip to content

Commit

Permalink
chore: align cli validation for power from shore (#513)
Browse files Browse the repository at this point in the history
* chore: align cli validation for power from shore
  • Loading branch information
frodehk authored Jun 5, 2024
1 parent 7b4ea11 commit 94e46b8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,25 @@ def check_mandatory_category_for_generator_set(cls, user_defined_category, info:

return user_defined_category

@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}."
)
return self

def get_graph(self) -> ComponentGraph:
graph = ComponentGraph()
graph.add_node(self)
Expand Down
9 changes: 3 additions & 6 deletions src/libecalc/presentation/yaml/mappers/component_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from libecalc import dto
from libecalc.common.logger import logger
from libecalc.common.time_utils import Period, define_time_model_for_period
from libecalc.dto.base import ComponentType, ConsumerUserDefinedCategoryType
from libecalc.dto.base import ComponentType
from libecalc.dto.types import ConsumerType, ConsumptionType, EnergyModelType
from libecalc.dto.utils.validators import convert_expression
from libecalc.expression import Expression
Expand Down Expand Up @@ -219,12 +219,9 @@ def from_yaml_to_dto(
user_defined_category = define_time_model_for_period(
data.get(EcalcYamlKeywords.user_defined_tag), target_period=self._target_period
)
cable_loss = None
max_usage_from_shore = None

if ConsumerUserDefinedCategoryType.POWER_FROM_SHORE in user_defined_category.values():
cable_loss = convert_expression(data.get(EcalcYamlKeywords.cable_loss))
max_usage_from_shore = convert_expression(data.get(EcalcYamlKeywords.max_usage_from_shore))
cable_loss = convert_expression(data.get(EcalcYamlKeywords.cable_loss))
max_usage_from_shore = convert_expression(data.get(EcalcYamlKeywords.max_usage_from_shore))

try:
generator_set_name = data.get(EcalcYamlKeywords.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def check_power_from_shore(self):
if isinstance(self.category, ConsumerUserDefinedCategoryType):
if self.category is not ConsumerUserDefinedCategoryType.POWER_FROM_SHORE:
raise ValueError(
f"{self.model_fields['cable_loss'].alias} and {self.model_fields['max_usage_from_shore'].alias} are only valid for the "
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'].alias} and {self.model_fields['max_usage_from_shore'].alias} are only valid for the "
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}."
)
return self
37 changes: 37 additions & 0 deletions src/tests/libecalc/dto/test_generator_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,40 @@ def test_genset_should_fail_with_fuel_consumer(self):
consumers=[fuel_consumer],
fuel={},
)

def test_power_from_shore_wrong_category(self):
"""
Check that CABLE_LOSS and MAX_USAGE_FROM_SHORE are only allowed if generator set category is POWER-FROM-SHORE
"""

# Check for CABLE_LOSS
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={},
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)

# Check for MAX_USAGE_FROM_SHORE
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,
)

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 94e46b8

Please sign in to comment.