From 50589742a6db19a51106bc7e5a1ce6addf9c941b Mon Sep 17 00:00:00 2001 From: Frode Helgetun Krogh <70878501+frodehk@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:52:39 +0100 Subject: [PATCH] test: update generator test to reflect where validation is done equinor/ecalc-internal#291 --- .../presentation/yaml/ltp_validation.py | 2 +- tests/libecalc/dto/test_generator_set.py | 45 ++++++------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/libecalc/presentation/yaml/ltp_validation.py b/src/libecalc/presentation/yaml/ltp_validation.py index da8308b64..f57dd3013 100644 --- a/src/libecalc/presentation/yaml/ltp_validation.py +++ b/src/libecalc/presentation/yaml/ltp_validation.py @@ -22,7 +22,7 @@ def validate_generator_set_power_from_shore( 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}." + message = f"{feedback_text} for the category {ConsumerUserDefinedCategoryType.POWER_FROM_SHORE.value}, not for {category.value}." raise ValueError(message) else: if ConsumerUserDefinedCategoryType.POWER_FROM_SHORE not in category.values(): diff --git a/tests/libecalc/dto/test_generator_set.py b/tests/libecalc/dto/test_generator_set.py index c3ef375c4..4cfe71eb4 100644 --- a/tests/libecalc/dto/test_generator_set.py +++ b/tests/libecalc/dto/test_generator_set.py @@ -15,6 +15,8 @@ from libecalc.common.time_utils import Period from libecalc.dto.types import ConsumerUserDefinedCategoryType from libecalc.expression import Expression +from libecalc.presentation.yaml.yaml_types.components.yaml_generator_set import YamlGeneratorSet +from libecalc.testing.yaml_builder import YamlGeneratorSetBuilder class TestGeneratorSetSampled: @@ -73,6 +75,7 @@ def test_valid(self): } def test_genset_should_fail_with_fuel_consumer(self): + """This validation is done in the dto layer""" fuel = libecalc.dto.fuel_type.FuelType( name="fuel", emissions=[], @@ -105,52 +108,32 @@ def test_genset_should_fail_with_fuel_consumer(self): 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 + This validation is done in the yaml layer. """ # Check for CABLE_LOSS with pytest.raises(ValueError) as exc_info: - GeneratorSet( - name="Test", - user_defined_category={Period(datetime(1900, 1, 1)): ConsumerUserDefinedCategoryType.BOILER}, - generator_set_model={}, - regularity={Period(datetime(1900, 1, 1)): Expression.setup_from_expression(1)}, - consumers=[], - fuel={}, - cable_loss=0, - component_type=ComponentType.GENERATOR_SET, - ) + YamlGeneratorSetBuilder().with_test_data().with_category("BOILER").with_cable_loss(0).validate() 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: - GeneratorSet( - name="Test", - user_defined_category={Period(datetime(1900, 1, 1)): ConsumerUserDefinedCategoryType.BOILER}, - generator_set_model={}, - regularity={Period(datetime(1900, 1, 1)): Expression.setup_from_expression(1)}, - consumers=[], - fuel={}, - max_usage_from_shore=20, - component_type=ComponentType.GENERATOR_SET, - ) + YamlGeneratorSetBuilder().with_test_data().with_category("BOILER").with_max_usage_from_shore(20).validate() assert ("MAX_USAGE_FROM_SHORE is only valid for the category POWER-FROM-SHORE, not for BOILER") in str( exc_info.value ) + # Check for CABLE_LOSS and MAX_USAGE_FROM_SHORE with pytest.raises(ValueError) as exc_info: - GeneratorSet( - name="Test", - user_defined_category={Period(datetime(1900, 1, 1)): ConsumerUserDefinedCategoryType.BOILER}, - generator_set_model={}, - regularity={Period(datetime(1900, 1, 1)): Expression.setup_from_expression(1)}, - consumers=[], - fuel={}, - max_usage_from_shore=20, - cable_loss=0, - component_type=ComponentType.GENERATOR_SET, - ) + ( + YamlGeneratorSetBuilder() + .with_test_data() + .with_category("BOILER") + .with_cable_loss(0) + .with_max_usage_from_shore(20) + ).validate() assert ( "CABLE_LOSS and MAX_USAGE_FROM_SHORE are only valid for the category POWER-FROM-SHORE, not for BOILER"