Skip to content

Commit

Permalink
chore: move tests outside src directory
Browse files Browse the repository at this point in the history
Use importlib import-mode for pytest. This requires shared code to be
installed, which we solve by having a testing module in libecalc here.
  • Loading branch information
jsolaas committed Nov 8, 2024
1 parent 1980b11 commit 1fca841
Show file tree
Hide file tree
Showing 161 changed files with 57 additions and 89 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ build-backend = "poetry.core.masonry.api"

# plugins, plugin config etc
[tool.pytest.ini_options]
testpaths = [
"tests",
]
addopts = "--import-mode=importlib"
filterwarnings = [
"error::DeprecationWarning", # Treat all DeprecationWarnings as errors and ignore explicitly below if needed
"ignore:Avoid using the dto.*:DeprecationWarning", # Ignore internal deprecation warnings
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import abc
from enum import Enum
from inspect import isclass
from typing import List, Self, TypeVar, Generic, Any, get_origin, get_args, Type, cast
from typing import List, Self, TypeVar, Generic, get_args, cast

from pydantic import TypeAdapter, BaseModel
from typing_extensions import get_original_bases

from libecalc.dto.types import ConsumerUserDefinedCategoryType
Expand Down Expand Up @@ -35,7 +33,7 @@


class Builder(abc.ABC, Generic[T]):
__model__: type[YamlBase]
__model__: type[T]

def __init_subclass__(cls, **kwargs):
# When this class is subclassed we parse the type to be able to use the pydantic model for build and validate
Expand All @@ -46,11 +44,9 @@ def __init_subclass__(cls, **kwargs):
original_base = original_bases[0]

# Get the type of T in Builder[T], assuming a single class, i.e. no unions
model_type = [arg for arg in get_args(original_base)][0]
model_type: type[T] = [arg for arg in get_args(original_base)][0]

# Type should be a YamlBase
assert isinstance(model_type, type(YamlBase))
cls.__model__ = cast(type(YamlBase), model_type)
cls.__model__ = model_type

@classmethod
def get_model_fields(cls) -> list[str]:
Expand Down Expand Up @@ -115,32 +111,6 @@ def with_consumption_rate_type(self, consumption_rate_type: ConsumptionRateType)
TYamlClass = TypeVar("TYamlClass", bound=YamlBase)


class YamlModelContainer(Generic[TYamlClass]):
"""
Consumer, Installation wrapper that can be used to bundle the component with its dependencies.
A fuel consumer that uses a model reference can then use this class to keep the referenced model in a single class.
"""

def __init__(self, component: TYamlClass):
self.component = component
self.models = None
self.time_series = None
self.facility_inputs = None

def with_models(self, models) -> Self:
self.models = models
return self

def with_time_series(self, time_series):
self.time_series = time_series
return self

def with_facility_inputs(self, facility_inputs) -> Self:
self.facility_inputs = facility_inputs
return self


class YamlFuelConsumerBuilder(Builder[YamlFuelConsumer]):
def __init__(self):
self.name = None
Expand Down Expand Up @@ -192,42 +162,24 @@ def with_name(self, name: str) -> Self:
self.name = name
return self

def with_fuel_consumers(
self, fuel_consumers: list[YamlFuelConsumer | YamlModelContainer[YamlFuelConsumer]]
) -> Self:
def with_fuel_consumers(self, fuel_consumers: list[YamlFuelConsumer]) -> Self:
new_fuel_consumers = []
for fuel_consumer in fuel_consumers:
if isinstance(fuel_consumer, YamlModelContainer):
self._yaml_model_containers.append(fuel_consumer)
new_fuel_consumers.append(fuel_consumer.component)
else:
new_fuel_consumers.append(fuel_consumer)
new_fuel_consumers.append(fuel_consumer)
self.fuel_consumers = new_fuel_consumers
return self

def with_venting_emitters(
self, venting_emitters: list[YamlVentingEmitter | YamlModelContainer[YamlVentingEmitter]]
) -> Self:
def with_venting_emitters(self, venting_emitters: list[YamlVentingEmitter]) -> Self:
new_venting_emitters = []
for venting_emitter in venting_emitters:
if isinstance(venting_emitter, YamlModelContainer):
self._yaml_model_containers.append(venting_emitter)
new_venting_emitters.append(venting_emitter.component)
else:
new_venting_emitters.append(venting_emitter)
new_venting_emitters.append(venting_emitter)
self.venting_emitters = new_venting_emitters
return self

def with_generator_sets(
self, generator_sets: list[YamlGeneratorSet | YamlModelContainer[YamlGeneratorSet]]
) -> Self:
def with_generator_sets(self, generator_sets: list[YamlGeneratorSet]) -> Self:
new_generator_sets = []
for generator_set in generator_sets:
if isinstance(generator_set, YamlModelContainer):
self._yaml_model_containers.append(generator_set)
new_generator_sets.append(generator_set.component)
else:
new_generator_sets.append(generator_set)
new_generator_sets.append(generator_set)
self.generator_sets = new_generator_sets
return self

Expand Down Expand Up @@ -313,20 +265,14 @@ def with_variables(self, variables: YamlVariables):
self.variables = variables
return self

def with_installations(self, installations: List[YamlInstallation | YamlModelContainer[YamlInstallation]]):
def with_installations(self, installations: List[YamlInstallation]):
if len(installations) == 0:
self.installations = []
return self

new_installations = []
for installation in installations:
if isinstance(installation, YamlModelContainer):
self.models.extend(installation.models)
self.facility_inputs.extend(installation.facility_inputs)
self.time_series.extend(installation.time_series)
new_installations.append(installation.component)
else:
new_installations.append(installation)
new_installations.append(installation)

self.installations = new_installations
return self
Expand Down
2 changes: 1 addition & 1 deletion src/tests/conftest.py → tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from libecalc.presentation.yaml.yaml_entities import MemoryResource, ResourceStream
from libecalc.presentation.yaml.yaml_models.yaml_model import ReaderType, YamlConfiguration, YamlValidator
from libecalc.presentation.yaml.yaml_types.components.yaml_asset import YamlAsset
from tests.libecalc.yaml_builder import (
from libecalc.testing.yaml_builder import (
YamlAssetBuilder,
YamlEnergyUsageModelDirectBuilder,
YamlFuelConsumerBuilder,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
yaml_fluid_model = """
import pytest


@pytest.fixture
def yaml_fluid_model():
return """
- NAME: fluid_model
TYPE: FLUID
FLUID_MODEL_TYPE: PREDEFINED
EOS_MODEL: SRK
GAS_TYPE: MEDIUM
"""

yaml_chart_single_speed = """

@pytest.fixture
def yaml_chart_single_speed():
return """
- NAME: single_speed_compressor_chart
TYPE: COMPRESSOR_CHART
CHART_TYPE: SINGLE_SPEED
Expand All @@ -21,7 +29,10 @@
EFFICIENCY: [0.72, 0.75, 0.74, 0.70]
"""

yaml_chart_variable_speed = """

@pytest.fixture
def yaml_chart_variable_speed():
return """
- NAME: variable_speed_compressor_chart
TYPE: COMPRESSOR_CHART
CHART_TYPE: VARIABLE_SPEED
Expand All @@ -44,7 +55,10 @@
EFFICIENCY: [0.72, 0.73, 0.74, 0.74, 0.72, 0.70]
"""

yaml_chart_generic_from_input = """

@pytest.fixture
def yaml_chart_generic_from_input():
return """
- NAME: generic_from_input_compressor_chart
TYPE: COMPRESSOR_CHART
CHART_TYPE: GENERIC_FROM_INPUT
Expand All @@ -53,7 +67,10 @@
EFFICIENCY: FRACTION
"""

yaml_simplified_train_wrong_chart = f"""

@pytest.fixture
def yaml_simplified_train_wrong_chart(yaml_fluid_model, yaml_chart_single_speed):
return f"""
MODELS:
{yaml_fluid_model}
{yaml_chart_single_speed}
Expand All @@ -66,7 +83,10 @@
COMPRESSOR_CHART: single_speed_compressor_chart
"""

yaml_simplified_train_with_control_margin_and_pressure_drop = f"""

@pytest.fixture
def yaml_simplified_train_with_control_margin_and_pressure_drop(yaml_fluid_model, yaml_chart_generic_from_input):
return f"""
MODELS:
{yaml_fluid_model}
{yaml_chart_generic_from_input}
Expand All @@ -82,7 +102,10 @@
COMPRESSOR_CHART: generic_from_input_compressor_chart
"""

yaml_single_speed_train_without_control_margin = f"""

@pytest.fixture
def yaml_single_speed_train_without_control_margin(yaml_fluid_model, yaml_chart_single_speed):
return f"""
MODELS:
{yaml_fluid_model}
{yaml_chart_single_speed}
Expand All @@ -96,7 +119,10 @@
PRESSURE_DROP_AHEAD_OF_STAGE: 2
"""

yaml_variable_speed_train_without_control_margin = f"""

@pytest.fixture
def yaml_variable_speed_train_without_control_margin(yaml_fluid_model, yaml_chart_variable_speed):
return f"""
MODELS:
{yaml_fluid_model}
{yaml_chart_variable_speed}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

import pytest

from conftest import (
yaml_simplified_train_with_control_margin_and_pressure_drop,
yaml_simplified_train_wrong_chart,
)
from ecalc_cli.infrastructure.file_resource_service import FileResourceService
from libecalc.common.time_utils import Frequency
from libecalc.presentation.yaml.model import YamlModel
Expand All @@ -15,7 +11,9 @@
from libecalc.presentation.yaml.yaml_keywords import EcalcYamlKeywords


def test_control_margin_and_pressure_drop_not_allowed(configuration_service_factory):
def test_control_margin_and_pressure_drop_not_allowed(
configuration_service_factory, yaml_simplified_train_with_control_margin_and_pressure_drop
):
configuration_service = configuration_service_factory(
ResourceStream(name="", stream=StringIO(yaml_simplified_train_with_control_margin_and_pressure_drop))
)
Expand Down Expand Up @@ -55,7 +53,7 @@ def test_control_margin_and_pressure_drop_not_allowed(configuration_service_fact
)


def test_single_speed_chart_not_allowed(configuration_service_factory):
def test_single_speed_chart_not_allowed(configuration_service_factory, yaml_simplified_train_wrong_chart):
configuration_service = configuration_service_factory(
ResourceStream(name="", stream=StringIO(yaml_simplified_train_wrong_chart))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

import pytest

from conftest import (
yaml_single_speed_train_without_control_margin,
)
from ecalc_cli.infrastructure.file_resource_service import FileResourceService
from libecalc.common.time_utils import Frequency
from libecalc.presentation.yaml.model import YamlModel
Expand All @@ -14,7 +11,7 @@
from libecalc.presentation.yaml.yaml_keywords import EcalcYamlKeywords


def test_control_margin_required(configuration_service_factory):
def test_control_margin_required(configuration_service_factory, yaml_single_speed_train_without_control_margin):
configuration_service = configuration_service_factory(
ResourceStream(name="", stream=StringIO(yaml_single_speed_train_without_control_margin))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

import pytest

from conftest import (
yaml_variable_speed_train_without_control_margin,
)
from ecalc_cli.infrastructure.file_resource_service import FileResourceService
from libecalc.common.time_utils import Frequency
from libecalc.presentation.yaml.model import YamlModel
Expand All @@ -14,7 +11,7 @@
from libecalc.presentation.yaml.yaml_keywords import EcalcYamlKeywords


def test_control_margin_required(configuration_service_factory):
def test_control_margin_required(configuration_service_factory, yaml_variable_speed_train_without_control_margin):
configuration_service = configuration_service_factory(
ResourceStream(name="", stream=StringIO(yaml_variable_speed_train_without_control_margin))
)
Expand Down

0 comments on commit 1fca841

Please sign in to comment.