Skip to content

Commit 77c2f93

Browse files
authored
Add more tests from the SDK v1.0.0-rc5 (#12)
- **Add more tests imported from the SDK v1.0.0-rc5** - **Fix new tests imports** - **Fix new tests patching**
2 parents 4f9a5a1 + 0f1962e commit 77c2f93

File tree

6 files changed

+823
-0
lines changed

6 files changed

+823
-0
lines changed

tests/test_component.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# License: MIT
2+
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the microgrid component wrapper."""
5+
6+
import pytest
7+
8+
# pylint: disable=no-name-in-module
9+
from frequenz.api.common.components_pb2 import ComponentCategory
10+
11+
# pylint: enable=no-name-in-module
12+
import frequenz.client.microgrid._component as cp
13+
14+
15+
def test_component_category_from_protobuf() -> None:
16+
"""Test the creating component category from protobuf."""
17+
assert (
18+
cp.component_category_from_protobuf(
19+
ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
20+
)
21+
== cp.ComponentCategory.NONE
22+
)
23+
24+
assert (
25+
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_GRID)
26+
== cp.ComponentCategory.GRID
27+
)
28+
29+
assert (
30+
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_METER)
31+
== cp.ComponentCategory.METER
32+
)
33+
34+
assert (
35+
cp.component_category_from_protobuf(
36+
ComponentCategory.COMPONENT_CATEGORY_INVERTER
37+
)
38+
== cp.ComponentCategory.INVERTER
39+
)
40+
41+
assert (
42+
cp.component_category_from_protobuf(
43+
ComponentCategory.COMPONENT_CATEGORY_BATTERY
44+
)
45+
== cp.ComponentCategory.BATTERY
46+
)
47+
48+
assert (
49+
cp.component_category_from_protobuf(
50+
ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
51+
)
52+
== cp.ComponentCategory.EV_CHARGER
53+
)
54+
55+
assert cp.component_category_from_protobuf(666) == cp.ComponentCategory.NONE # type: ignore
56+
57+
with pytest.raises(ValueError):
58+
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_SENSOR)
59+
60+
61+
# pylint: disable=invalid-name
62+
def test_Component() -> None:
63+
"""Test the component category."""
64+
c0 = cp.Component(0, cp.ComponentCategory.GRID)
65+
assert c0.is_valid()
66+
67+
c1 = cp.Component(1, cp.ComponentCategory.GRID)
68+
assert c1.is_valid()
69+
70+
c4 = cp.Component(4, cp.ComponentCategory.METER)
71+
assert c4.is_valid()
72+
73+
c5 = cp.Component(5, cp.ComponentCategory.INVERTER)
74+
assert c5.is_valid()
75+
76+
c6 = cp.Component(6, cp.ComponentCategory.BATTERY)
77+
assert c6.is_valid()
78+
79+
c7 = cp.Component(7, cp.ComponentCategory.EV_CHARGER)
80+
assert c7.is_valid()
81+
82+
invalid_grid_id = cp.Component(-1, cp.ComponentCategory.GRID)
83+
assert not invalid_grid_id.is_valid()
84+
85+
invalid_type = cp.Component(666, -1) # type: ignore
86+
assert not invalid_type.is_valid()
87+
88+
another_invalid_type = cp.Component(666, 666) # type: ignore
89+
assert not another_invalid_type.is_valid()

tests/test_component_data.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# License: MIT
2+
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the microgrid component data."""
5+
6+
from datetime import datetime, timezone
7+
8+
import pytest
9+
10+
# pylint: disable=no-name-in-module
11+
from frequenz.api.common.metrics.electrical_pb2 import AC
12+
from frequenz.api.common.metrics_pb2 import Bounds, Metric
13+
from frequenz.api.microgrid.inverter_pb2 import (
14+
COMPONENT_STATE_DISCHARGING,
15+
Data,
16+
Error,
17+
Inverter,
18+
State,
19+
)
20+
from frequenz.api.microgrid.microgrid_pb2 import ComponentData as PbComponentData
21+
from google.protobuf.timestamp_pb2 import Timestamp
22+
23+
# pylint: enable=no-name-in-module
24+
from frequenz.client.microgrid import ComponentData, InverterData
25+
26+
27+
def test_component_data_abstract_class() -> None:
28+
"""Verify the base class ComponentData may not be instantiated."""
29+
with pytest.raises(TypeError):
30+
# pylint: disable=abstract-class-instantiated
31+
ComponentData(0, datetime.now(timezone.utc)) # type: ignore
32+
33+
34+
def test_inverter_data() -> None:
35+
"""Verify the constructor for the InverterData class."""
36+
seconds = 1234567890
37+
38+
raw = PbComponentData(
39+
id=5,
40+
ts=Timestamp(seconds=seconds),
41+
inverter=Inverter(
42+
state=State(component_state=COMPONENT_STATE_DISCHARGING),
43+
errors=[Error(msg="error message")],
44+
data=Data(
45+
dc_battery=None,
46+
dc_solar=None,
47+
temperature=None,
48+
ac=AC(
49+
frequency=Metric(value=50.1),
50+
power_active=Metric(
51+
value=100.2,
52+
system_exclusion_bounds=Bounds(lower=-501.0, upper=501.0),
53+
system_inclusion_bounds=Bounds(lower=-51_000.0, upper=51_000.0),
54+
),
55+
phase_1=AC.ACPhase(
56+
current=Metric(value=12.3),
57+
voltage=Metric(value=229.8),
58+
power_active=Metric(value=33.1),
59+
),
60+
phase_2=AC.ACPhase(
61+
current=Metric(value=23.4),
62+
voltage=Metric(value=230.0),
63+
power_active=Metric(value=33.3),
64+
),
65+
phase_3=AC.ACPhase(
66+
current=Metric(value=34.5),
67+
voltage=Metric(value=230.2),
68+
power_active=Metric(value=33.8),
69+
),
70+
),
71+
),
72+
),
73+
)
74+
75+
inv_data = InverterData.from_proto(raw)
76+
assert inv_data.component_id == 5
77+
assert inv_data.timestamp == datetime.fromtimestamp(seconds, timezone.utc)
78+
assert ( # pylint: disable=protected-access
79+
inv_data._component_state == COMPONENT_STATE_DISCHARGING
80+
)
81+
assert inv_data._errors == [ # pylint: disable=protected-access
82+
Error(msg="error message")
83+
]
84+
assert inv_data.frequency == pytest.approx(50.1)
85+
assert inv_data.active_power == pytest.approx(100.2)
86+
assert inv_data.active_power_per_phase == pytest.approx((33.1, 33.3, 33.8))
87+
assert inv_data.current_per_phase == pytest.approx((12.3, 23.4, 34.5))
88+
assert inv_data.voltage_per_phase == pytest.approx((229.8, 230.0, 230.2))
89+
assert inv_data.active_power_inclusion_lower_bound == pytest.approx(-51_000.0)
90+
assert inv_data.active_power_inclusion_upper_bound == pytest.approx(51_000.0)
91+
assert inv_data.active_power_exclusion_lower_bound == pytest.approx(-501.0)
92+
assert inv_data.active_power_exclusion_upper_bound == pytest.approx(501.0)

tests/test_connection.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# License: MIT
2+
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the microgrid Connection type."""
5+
6+
from frequenz.client.microgrid import Connection
7+
8+
9+
# pylint: disable=invalid-name
10+
def test_Connection() -> None:
11+
"""Test the microgrid Connection type."""
12+
c00 = Connection(0, 0)
13+
assert not c00.is_valid()
14+
15+
c01 = Connection(0, 1)
16+
assert c01.is_valid()
17+
18+
c10 = Connection(1, 0)
19+
assert not c10.is_valid()
20+
21+
c11 = Connection(1, 1)
22+
assert not c11.is_valid()
23+
24+
c12 = Connection(1, 2)
25+
assert c12.is_valid()
26+
27+
c21 = Connection(2, 1)
28+
assert c21.is_valid()

0 commit comments

Comments
 (0)