diff --git a/src/libecalc/dto/components.py b/src/libecalc/dto/components.py index 9e8bb75ec1..5fcedb7743 100644 --- a/src/libecalc/dto/components.py +++ b/src/libecalc/dto/components.py @@ -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, @@ -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: diff --git a/src/libecalc/presentation/yaml/ltp_validation.py b/src/libecalc/presentation/yaml/ltp_validation.py new file mode 100644 index 0000000000..7ecce2ec1f --- /dev/null +++ b/src/libecalc/presentation/yaml/ltp_validation.py @@ -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) 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 bf0aa3e65b..0d3eb03dde 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 @@ -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, @@ -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 diff --git a/src/tests/libecalc/dto/test_generator_set.py b/src/tests/libecalc/dto/test_generator_set.py index aea05578d0..3af56fbdb2 100644 --- a/src/tests/libecalc/dto/test_generator_set.py +++ b/src/tests/libecalc/dto/test_generator_set.py @@ -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: @@ -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)