Skip to content

Commit

Permalink
test: update generator test to reflect where validation is done
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehk committed Dec 20, 2024
1 parent 1c3d201 commit 5058974
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/libecalc/presentation/yaml/ltp_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
45 changes: 14 additions & 31 deletions tests/libecalc/dto/test_generator_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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=[],
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 5058974

Please sign in to comment.