Repository navigation
Adds tests exercising validators #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4610434
2e116ab
134af33
39ecf1b
3d1b445
c84f349
37665df
9ec10c9
49d2e85
b0f59e6
c6d846b
6e3d43f
b4dc43e
9494d80
7c2992a
73162e2
d3809fd
485f9f4
de50288
94eee52
533985d
2d12ebf
65c9068
5d331be
c5bc294
174b84d
5f3e739
1b85326
12eac62
21febdb
536e7f5
bf75b9f
7786db6
0b6b998
071206c
1a8afe4
37ee238
c8dba15
49022f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
| # 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) | ||
| 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()]) |
| 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"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -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) | ||
|
|
@@ -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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove this commented out code or keep it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a placeholder
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
| 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, | ||
| ) |
| 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) |
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Placeholder for the future