Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4610434
Rename test_base.py to continue naming convention of base modules
mikejturner Sep 22, 2026
2e116ab
Adds validation test for TellegenNode
mikejturner Sep 22, 2026
134af33
Sort imports
mikejturner Sep 22, 2026
39ecf1b
Add extra tests for partition validation
mikejturner Sep 22, 2026
3d1b445
Add unit tests for EVWithProfile
mikejturner Sep 22, 2026
c84f349
Adds unit tests for inverter
mikejturner Sep 22, 2026
37665df
Sort imports
mikejturner Sep 22, 2026
9ec10c9
Add test for InputOutputNode
mikejturner Sep 22, 2026
49d2e85
Add tests for AggregationNode
mikejturner Sep 23, 2026
b0f59e6
Add tests for ParameterisedHeatPump
mikejturner Sep 23, 2026
c6d846b
linting
mikejturner Sep 23, 2026
6e3d43f
Add tests for Transform and TransformNode
mikejturner Sep 23, 2026
b4dc43e
Fix incorrect naming of test
mikejturner Sep 23, 2026
9494d80
Add tests for ThermalStorage
mikejturner Sep 23, 2026
7c2992a
Add tests for SimpleChiller
mikejturner Sep 23, 2026
73162e2
Add tests for simple heatpump
mikejturner Sep 23, 2026
d3809fd
Add tests for ThermalBase
mikejturner Sep 23, 2026
485f9f4
Adds validation test to SimpleHeatPumpDualOutput
mikejturner Sep 24, 2026
de50288
Add test for EVBase
mikejturner Sep 24, 2026
94eee52
Add tests for EVs
mikejturner Sep 24, 2026
533985d
Add tests for DieselGenerator
mikejturner Sep 24, 2026
2d12ebf
Add tests for GasBoilerFixedCOP
mikejturner Sep 24, 2026
65c9068
Add tests for TempControlledBoiler
mikejturner Sep 24, 2026
5d331be
Add tests for TimeVaryingPiecewiseIONode
mikejturner Sep 24, 2026
c5bc294
Add tests for ParameterisedChiller
mikejturner Sep 24, 2026
174b84d
Add tests for Edge
mikejturner Sep 25, 2026
5f3e739
Adds tests for Path
mikejturner Sep 25, 2026
1b85326
Add tests for DemandCharge
mikejturner Sep 25, 2026
12eac62
Adds test for ImportDemandCharge
mikejturner Sep 25, 2026
21febdb
Add test for ExportDemandCharge
mikejturner Sep 25, 2026
536e7f5
Fix bug in validator
mikejturner Sep 25, 2026
bf75b9f
Add test for Window
mikejturner Sep 25, 2026
7786db6
Switch from uuid4 to shortuuid
mikejturner Sep 25, 2026
0b6b998
Add test for Objective
mikejturner Sep 25, 2026
071206c
Add tests for NotFullyChargedPenalty and FinalChargeObjective
mikejturner Sep 28, 2026
1a8afe4
Add more validation tests for Tariff classes
mikejturner Sep 28, 2026
37ee238
Add validation test for contingencies
mikejturner Sep 28, 2026
c8dba15
Fix formatting
mikejturner Sep 28, 2026
49022f9
Update comments
mikejturner Sep 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/echo/models/thermal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ThermalNode(Node):
initial_internal_temp: float = 0 # initial internal temperature

# Pyomo vars/params
internal_temp: str
is_gain: str
losses: str
gains: str
internal_temp: str = ""
is_gain: str = ""
losses: str = ""
gains: str = ""

def __init__(self, **data) -> None:
super().__init__(**data)
Expand Down
7 changes: 3 additions & 4 deletions src/echo/objectives/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import uuid

import pandas as pd
import pyomo.environ as en
import shortuuid
from pydantic import Field

from echo.models.base import BaseModel as EchoBaseModel
Expand All @@ -11,14 +10,14 @@

class Objective(EchoBaseModel):
component: Port | Path | None
uid: uuid.UUID = Field(default_factory=uuid.uuid4)
uid: str = Field(default_factory=shortuuid.uuid)
name: str = ""
weight: float = 1

def __init__(self, **data) -> None:
super().__init__(**data)
if not self.name:
self.name = "obj_" + str(self.uid)
self.name = "obj_" + self.uid

def verify_objective(self, model: EchoConcreteModel, df: pd.DataFrame | None) -> None:
pass
Expand Down
11 changes: 6 additions & 5 deletions src/echo/objectives/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,11 @@ def overlaps(self, other: "TimePeriod") -> bool:
]
else:
other_ranges = [(other.start_time, end_time)]
for self_range in self_ranges:
for other_range in other_ranges:
if self_range[0] < other_range[1] and other_range[0] < self_range[1]:
return True

for self_range in self_ranges:
for other_range in other_ranges:
if self_range[0] < other_range[1] and other_range[0] < self_range[1]:
return True
return False


Expand Down Expand Up @@ -497,7 +498,7 @@ def check_import_or_export(cls, values: dict) -> dict:
return values

@root_validator(pre=True)
def check_name(cls, values: dict) -> dict:
def set_uid_and_name(cls, values: dict) -> dict:
if not values.get("uid"):
values["uid"] = shortuuid.uuid()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from echo.configuration import Units
from echo.exceptions import ConfigurationError
from echo.models.agnostic.flex import FlexPort
from echo.models.agnostic.tellegen.partitioned_multi_commodity import PartitionedMultiCommodityTellegenNode

Expand Down Expand Up @@ -40,13 +41,39 @@ def test_partitioned_node_add_port():
assert len(node.ports) == start_number_ports + 1


def test_partitioned_node_error():
def test_partitioned_node_validation_ports_or_partitions_only():
"""Test only ports or partitions validation error"""
with pytest.raises(Exception):
with pytest.raises(ValueError):
PartitionedMultiCommodityTellegenNode(
partitions={
"partition_1": [FlexPort(units=Units.KW), FlexPort(units=Units.KW)],
"partition_2": [FlexPort(units=Units.KW), FlexPort(units=Units.KW)],
},
ports={"port_1": FlexPort(units=Units.KW)},
)


def test_partitioned_node_validation_partition_ports_same_name():
partition_1_ports = [FlexPort(port_name="same_name", units=Units.KW)]
partition_2_ports = [FlexPort(port_name="same_name", units=Units.KW)]

with pytest.raises(ConfigurationError):
PartitionedMultiCommodityTellegenNode(
partitions={
"partition_1": partition_1_ports,
"partition_2": partition_2_ports,
},
)


def test_partitioned_node_validation_partition_ports_same_id():
# Same ports, therefore ports in different partitions have the same uid
partition_ports = [FlexPort(port_name="same_name", units=Units.KW)]

with pytest.raises(ConfigurationError):
PartitionedMultiCommodityTellegenNode(
partitions={
"partition_1": partition_ports,
"partition_2": partition_ports,
},
)
33 changes: 33 additions & 0 deletions tests/unit/models/agnostic/tellegen/test_tellegen_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from echo.configuration import FlowConstraint, Flows, OptimisationType, Units
from echo.exceptions import ConfigurationError
from echo.models.agnostic.tellegen.base import TellegenNode
from echo.models.base.port import Port


def test_tellegennode_validation():
# Arrange
common_port_params = {
"flows": Flows.Import,
"import_constraint": FlowConstraint.NoConstraint,
"flow_type": OptimisationType.Variable,
}
electrical_ports = {f"elec_port_{i}": Port(units=Units.KW, **common_port_params) for i in range(3)}
gas_ports = {f"gas_port_{i}": Port(units=Units.JPS, **common_port_params) for i in range(3)}
ports = {**electrical_ports, **gas_ports}

# Act
with pytest.raises(ConfigurationError):
TellegenNode(ports=ports)

with pytest.raises(ConfigurationError):
node = TellegenNode()
node.ports = ports

# This should raise an error due to mixing of different units on ports
# There is a corresponding [github issue](https://github.com/bsgip/echo/issues/126)
# with pytest.raises(ConfigurationError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this commented out code block be removed? Or is it a placeholder for something to be tested in the future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placeholder for the future

# node = TellegenNode()
# node.add_ports_from_list(["electrical1", "electical2", "electrical3"], Port, units=Units.KW)
# node.add_ports_from_list(["gas1", "gas2", "gas3"], Port, units=Units.JPS)
49 changes: 49 additions & 0 deletions tests/unit/models/agnostic/test_aggregation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest

from echo.configuration import Units
from echo.exceptions import ConfigurationError
from echo.models.agnostic.aggregation import AggregationNode
from echo.models.agnostic.flex import FlexSink
from echo.models.base.port import Port


@pytest.mark.parametrize(
"raw_ports, expected_error",
[
([FlexSink(units=Units.KW), FlexSink(units=Units.KW)], None), # ports compatible
(
[FlexSink(units=Units.KW), FlexSink(units=Units.JPS)],
ConfigurationError,
), # Units.JPS mismatch with port_units of Units.KW
(
[Port(units=Units.KW), Port(units=Units.KW)],
None,
), # ports compatible but AggregationNode should accept *only* FlexSink ports TODO: Fix
],
)
def test_aggregation_validation(raw_ports, expected_error):
ports = {f"port_{i}": port for i, port in enumerate(raw_ports)}
if expected_error:
with pytest.raises(expected_error):
AggregationNode(port_units=Units.KW, ports=ports)
else:
AggregationNode(port_units=Units.KW, ports=ports)


def test_aggregation_add_port():
port_name = "new-port"
node = AggregationNode(port_units=Units.KW)
node.add_port(name=port_name)
assert port_name in node.ports
assert all([p.units == node.port_units for p in node.ports.values()])


def test_aggregation_add_port_supplied():
units = Units.JPS
port_name = "new-port"
port = FlexSink(port_name=port_name, units=units)
node = AggregationNode(port_units=units)
node.add_port(name=port.port_name, port=port)
assert port_name in node.ports
assert port in node.ports.values()
assert all([p.units == node.port_units for p in node.ports.values()])
39 changes: 39 additions & 0 deletions tests/unit/models/agnostic/test_input_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from echo.configuration import Units
from echo.models.agnostic import InputOutputNode
from echo.models.agnostic.flex import FlexPort


@pytest.mark.parametrize(
"params",
[
{
"input_port_ref": "in",
"output_port_ref": "out",
"input_port_unit": Units.KW,
"output_port_unit": Units.JPS,
}, # both input and output port refs defined
{
"output_port_ref": "out",
"input_port_unit": Units.KW,
"output_port_unit": Units.JPS,
}, # no input_port_ref defined
{
"input_port_ref": "in",
"input_port_unit": Units.KW,
"output_port_unit": Units.JPS,
}, # no output_port_ref defined
],
)
def test_inputoutputnode_initialisation(params):
node = InputOutputNode(**params)
input_name = params["input_port_ref"] if "input_port_ref" in params else "input"
assert input_name in node.ports
assert isinstance(node.ports[input_name], FlexPort)
assert node.ports[input_name].units == params["input_port_unit"]

output_name = params["output_port_ref"] if "output_port_ref" in params else "output"
assert output_name in node.ports
assert isinstance(node.ports[output_name], FlexPort)
assert node.ports[output_name].units == params["output_port_unit"]
6 changes: 3 additions & 3 deletions tests/unit/models/agnostic/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_storage_validation_storage_capactity_cost(
):
storage_params = {"max_capacity": 0, "charging_power_limit": 0, "discharging_power_limit": 0}
if expected_error:
with pytest.raises(ValidationError):
with pytest.raises(expected_error):
Storage(storage_capacity_cost=storage_capacity_cost, **storage_params)
else:
Storage(storage_capacity_cost=storage_capacity_cost, **storage_params)
Expand All @@ -48,7 +48,7 @@ def test_storage_validation_storage_capactity_cost(
def test_storage_validation_depth_of_discharge_check(storage_params, expected_error):
common_storage_params = {"max_capacity": 0, "charging_power_limit": 0, "discharging_power_limit": 0}
if expected_error:
with pytest.raises(ValidationError):
with pytest.raises(expected_error):
Storage(**common_storage_params, **storage_params)
else:
Storage(**common_storage_params, **storage_params)
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_storage_validation_depth_of_discharge_check(storage_params, expected_er
# # (see function dod_checks)
# }
# if expected_error:
# with pytest.raises(ValidationError):
# with pytest.raises(expected_error):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this commented out code or keep it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a placeholder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should work. There is an Issue in github and the comment above the test links to that issue to be as clear as possible.

# Storage(initial_state_of_charge=initial_state_of_charge, **storage_params)
# else:
# Storage(initial_state_of_charge=initial_state_of_charge, **storage_params)
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/models/agnostic/test_time_varying.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pytest

from echo.configuration import Units
from echo.exceptions import ConfigurationError
from echo.models.agnostic.time_varying import TimeVaryingPiecewiseIONode


def test_timevarying_node():
# Synthesise `input_points` and `output_points` dicts
MAX_VALUE = 10
MIN_VALUE = 5
TIME_PERIODS = 6
points = {(0, i): list(range(MIN_VALUE, MAX_VALUE + 1)) for i in range(TIME_PERIODS)}

required_params = {
"input_port_unit": Units.KW,
"output_port_unit": Units.KW,
}

node = TimeVaryingPiecewiseIONode(
input_points=points,
output_points=points,
**required_params,
)
# Verify bounds set correctly
assert node.min_input == MIN_VALUE
assert node.max_input == MAX_VALUE
assert node.min_output == MIN_VALUE
assert node.max_output == MAX_VALUE


@pytest.mark.parametrize(
"min_input, max_input, input_periods, min_output, max_output, output_periods, expected_error",
[
(5, 10, 6, 5, 10, 6, None),
(5, 10, 6, 5, 10, 7, ConfigurationError), # time periods not equal (6 != 7)
(5, 8, 6, 5, 10, 6, ConfigurationError), # input value range (5 to 8) < output value range (5 to 10)
],
)
def test_timevarying_node_validation_piecewise_arrays(
min_input,
max_input,
input_periods,
min_output,
max_output,
output_periods,
expected_error,
):
required_params = {
"input_port_unit": Units.KW,
"output_port_unit": Units.KW,
}

input_points = {(0, i): list(range(min_input, max_input + 1)) for i in range(input_periods)}
output_points = {(0, i): list(range(min_output, max_output + 1)) for i in range(output_periods)}

if expected_error:
with pytest.raises(expected_error):
TimeVaryingPiecewiseIONode(
input_points=input_points,
output_points=output_points,
**required_params,
)
else:
TimeVaryingPiecewiseIONode(
input_points=input_points,
output_points=output_points,
**required_params,
)
29 changes: 29 additions & 0 deletions tests/unit/models/base/test_edge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from pydantic import ValidationError

from echo.models.base.edge import Edge
from echo.models.base.port import Port

SHORTUUID_LENGTH = 22


def test_edge_initialisation_missing_required_params():
# required parameters: vertices
with pytest.raises(ValidationError):
Edge()


@pytest.mark.parametrize("edge_name", [None, "edge-name"])
def test_edge_initialisation(edge_name):
port_1 = Port()
required_params = {"vertices": (port_1, port_1)}

edge = Edge(edge_name=edge_name, **required_params)

assert len(edge.uid) == SHORTUUID_LENGTH

assert edge.edge_name is not None
if edge_name:
assert edge.edge_name == edge_name
else:
assert edge.edge_name.endswith(edge.uid)
27 changes: 27 additions & 0 deletions tests/unit/models/base/test_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from pydantic import ValidationError

from echo.models.base.path import Path

SHORTUUID_LENGTH = 22


def test_path_initialisation_missing_required_params():
# required parameters: vertices
with pytest.raises(ValidationError):
Path()


@pytest.mark.parametrize("path_name", [None, "path-name"])
def test_path_initialisation(path_name):
required_params = {"vertices": []}

path = Path(path_name=path_name, **required_params)

assert len(path.uid) == SHORTUUID_LENGTH

assert path.path_name is not None
if path_name:
assert path.path_name == path_name
else:
assert path.path_name.endswith(path.uid)
Loading
Loading