Skip to content

Commit

Permalink
Add unit test to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lumbric committed May 14, 2024
1 parent 169adee commit 022f914
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
19 changes: 15 additions & 4 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,27 @@ def test_inconsistent_time_coords(input_output, wrong_length):
Network([wind, electricity, demand])


@pytest.mark.parametrize("missing_size_commodity", [False, True])
def test_multiple_commodities_chp_power_plant(missing_size_commodity):
@pytest.mark.parametrize("error", [None, "missing_size_commodity", "storage_not_allowed"])
def test_multiple_commodities_chp_power_plant(error):
time_coords_num = 10
storage = Storage(
costs=10 * ureg.EUR / ureg.MWh,
max_charging_speed=0.1,
storage_loss=0.1,
charging_loss=0.0,
)
chp_power_plant = Node(
name="chp_power_plant",
inputs=[],
input_commodities=["gas"],
costs=3 * ureg.EUR / ureg.MW,
input_flow_costs=10 * ureg.EUR / ureg.t,
size_commodity=None if missing_size_commodity else "electricity",
size_commodity=None if error == "missing_size_commodity" else "electricity",
convert_factors={
"electricity": ("gas", 2 / (ureg.t / ureg.h) * ureg.MW),
"heat": ("gas", 4 / (ureg.t / ureg.h) * ureg.MW),
},
storage=storage if error == "storage_not_allowed" else None,
)
electricity_demand = NodeFixOutput(
name="electricity_demand",
Expand All @@ -426,14 +433,18 @@ def test_multiple_commodities_chp_power_plant(missing_size_commodity):
time_coords_num=time_coords_num,
)

if missing_size_commodity:
if error == "missing_size_commodity":
# if a node has multiple ouptut commodities, size_commodity should be set
msg = (
"node 'chp_power_plant': missing size_commodity parameter, but required for "
"multiple different output commodities"
)
with pytest.raises(ValueError, match=msg):
_ = Network(**network_parmas)
elif error == "storage_not_allowed":
msg = "storage not supported for multiple output commodities"
with pytest.raises(ValueError, match=msg):
_ = Network(**network_parmas)
else:
network = Network(**network_parmas)

Expand Down
34 changes: 33 additions & 1 deletion tests/test_node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

from syfop.node import Node, NodeScalableInput
from syfop.node import (
Node,
NodeFixOutput,
NodeScalableInput,
NodeScalableOutput,
Storage,
)
from syfop.units import ureg
from syfop.util import const_time_series

Expand Down Expand Up @@ -95,3 +101,29 @@ def test_wrong_node_input():
input_commodities="electricity",
costs=0,
)


def test_scalable_output_not_implemented():
# this unit test is a bit useless, but it increases coverage
error_msg = "NodeScalableOutput is not implemented yet"
with pytest.raises(NotImplementedError, match=error_msg):
NodeScalableOutput()


def test_storage_forbidden_for_output_nodes():
storage = Storage(
costs=10 * ureg.EUR / ureg.MWh,
max_charging_speed=1.0,
storage_loss=0.0,
charging_loss=0.0,
)

error_msg = "storage is not supported for output nodes"
with pytest.raises(NotImplementedError, match=error_msg):
_ = NodeFixOutput(
name="demand",
inputs=[],
input_commodities="electricity",
output_flow=const_time_series(5.0) * ureg.kW,
storage=storage,
)

0 comments on commit 022f914

Please sign in to comment.