Skip to content

Commit

Permalink
test: add test for blank fuel
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehk committed Jan 7, 2025
1 parent f6a0b55 commit 3d94919
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ def __init__(self, errors: list[ModelValidationError]):

def errors(self) -> list[ModelValidationError]:
return self._errors


class ComponentDtoValidationError(Exception):
def __init__(self, errors: list[ModelValidationError]):
self.errors = errors
messages = [str(error) for error in errors]
super().__init__("\n".join(messages))

def error_count(self):
return len(self.errors)
15 changes: 13 additions & 2 deletions src/libecalc/presentation/yaml/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from libecalc.application.energy.energy_model import EnergyModel
from libecalc.common.time_utils import Frequency, Period
from libecalc.common.variables import ExpressionEvaluator, VariablesMap
from libecalc.domain.infrastructure.energy_components.component_validation_error import (
ComponentDtoValidationError,
ComponentValidationException,
)
from libecalc.dto import ResultOptions
from libecalc.dto.component_graph import ComponentGraph
from libecalc.expression import Expression
Expand All @@ -20,7 +24,11 @@
from libecalc.presentation.yaml.mappers.variables_mapper.get_global_time_vector import get_global_time_vector
from libecalc.presentation.yaml.model_validation_exception import ModelValidationException
from libecalc.presentation.yaml.resource_service import ResourceService
from libecalc.presentation.yaml.validation_errors import DtoValidationError, Location, ModelValidationError
from libecalc.presentation.yaml.validation_errors import (
DtoValidationError,
Location,
ModelValidationError,
)
from libecalc.presentation.yaml.yaml_models.exceptions import DuplicateKeyError, YamlError
from libecalc.presentation.yaml.yaml_models.yaml_model import YamlValidator
from libecalc.presentation.yaml.yaml_validation_context import (
Expand Down Expand Up @@ -104,7 +112,10 @@ def dto(self):
references=self._get_reference_service(),
target_period=self.period,
)
return model_mapper.from_yaml_to_dto(configuration=self._configuration)
try:
return model_mapper.from_yaml_to_dto(configuration=self._configuration)
except ComponentValidationException as e:
raise ComponentDtoValidationError(errors=e.errors()) from e

@property
def period(self) -> Period:
Expand Down
49 changes: 47 additions & 2 deletions tests/libecalc/dto/test_fuel_consumer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from datetime import datetime
from io import StringIO

import pytest
from pydantic import ValidationError

import libecalc.dto.fuel_type
import libecalc.dto.types
from ecalc_cli.types import Frequency
from libecalc import dto
from libecalc.application.energy_calculator import EnergyCalculator
from libecalc.common.variables import VariablesMap
from libecalc.domain.infrastructure import FuelConsumer, Installation
from libecalc.common.component_type import ComponentType
from libecalc.common.energy_usage_type import EnergyUsageType
from libecalc.common.time_utils import Period
from libecalc.domain.infrastructure.energy_components.component_validation_error import ComponentValidationException
from libecalc.domain.infrastructure.energy_components.component_validation_error import (
ComponentValidationException,
ComponentDtoValidationError,
)
from libecalc.expression import Expression
from libecalc.presentation.yaml.yaml_entities import ResourceStream
from libecalc.presentation.yaml.yaml_models.pyyaml_yaml_model import PyYamlYamlModel
from libecalc.testing.yaml_builder import YamlAssetBuilder, YamlInstallationBuilder, YamlFuelConsumerBuilder

regularity = {Period(datetime(2000, 1, 1)): Expression.setup_from_expression(1)}

Expand Down Expand Up @@ -109,3 +118,39 @@ def test_missing_fuel(self):
user_defined_category="category",
)
assert "Name: test\nMessage: Missing fuel for fuel consumer" in str(exc_info.value.errors()[0])

def test_blank_fuel_name_and_missing_fuel(self, yaml_model_factory, request):
time_vector = [datetime(2027, 1, 1), datetime(2028, 1, 1), datetime(2029, 1, 1)]
variables = VariablesMap(time_vector=time_vector, variables={})

fuel_consumer = YamlFuelConsumerBuilder().with_test_data().validate()

# Setting fuel reference name to blank
fuel_consumer.fuel = ""

installation = (
YamlInstallationBuilder().with_name("Installation 1").with_fuel_consumers([fuel_consumer])
).validate()

# No fuel is set for the asset (means None)
asset = (
YamlAssetBuilder().with_installations([installation]).with_start(time_vector[0]).with_end(time_vector[-1])
).validate()

yaml_model_factory = request.getfixturevalue("yaml_model_factory")
asset_dict = asset.model_dump(
serialize_as_any=True,
mode="json",
exclude_unset=True,
by_alias=True,
)

yaml_string = PyYamlYamlModel.dump_yaml(yaml_dict=asset_dict)
stream = ResourceStream(name="", stream=StringIO(yaml_string))

asset_yaml = yaml_model_factory(resource_stream=stream, resources={}, frequency=Frequency.YEAR)
energy_calculator = EnergyCalculator(energy_model=asset_yaml, expression_evaluator=variables)

with pytest.raises(ComponentDtoValidationError) as exc_info:
energy_calculator.evaluate_energy_usage()
assert "Name: flare\nMessage: Missing fuel for fuel consumer" in str(exc_info.value.errors[0])

0 comments on commit 3d94919

Please sign in to comment.