From 94e46b8a3d4bd08c624cc1bbb1e1c962f4bbd52c Mon Sep 17 00:00:00 2001 From: Frode Helgetun Krogh <70878501+frodehk@users.noreply.github.com> Date: Wed, 5 Jun 2024 18:41:18 +0200 Subject: [PATCH] chore: align cli validation for power from shore (#513) * chore: align cli validation for power from shore --- src/libecalc/dto/components.py | 19 ++++++++++ .../yaml/mappers/component_mapper.py | 9 ++--- .../components/yaml_generator_set.py | 4 +- src/tests/libecalc/dto/test_generator_set.py | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/libecalc/dto/components.py b/src/libecalc/dto/components.py index 4e7a663d4c..9e8bb75ec1 100644 --- a/src/libecalc/dto/components.py +++ b/src/libecalc/dto/components.py @@ -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) diff --git a/src/libecalc/presentation/yaml/mappers/component_mapper.py b/src/libecalc/presentation/yaml/mappers/component_mapper.py index d1fa7156dd..077b6742e8 100644 --- a/src/libecalc/presentation/yaml/mappers/component_mapper.py +++ b/src/libecalc/presentation/yaml/mappers/component_mapper.py @@ -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 @@ -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) diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py index 3843d1f778..3cf38d82ae 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py @@ -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 diff --git a/src/tests/libecalc/dto/test_generator_set.py b/src/tests/libecalc/dto/test_generator_set.py index e509180b69..aea05578d0 100644 --- a/src/tests/libecalc/dto/test_generator_set.py +++ b/src/tests/libecalc/dto/test_generator_set.py @@ -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)