Skip to content

Commit b5b52d5

Browse files
committed
Fix component tests to use betterproto
These tests need just minor changes to the imports. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 45cef00 commit b5b52d5

File tree

2 files changed

+84
-80
lines changed

2 files changed

+84
-80
lines changed

tests/test_component.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,93 @@
44
"""Tests for the microgrid component wrapper."""
55

66
import pytest
7+
from frequenz.microgrid.betterproto.frequenz.api.common import components
78

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
9+
from frequenz.client.microgrid._component import (
10+
Component,
11+
ComponentCategory,
12+
component_category_from_protobuf,
13+
)
1314

1415

1516
def test_component_category_from_protobuf() -> None:
1617
"""Test the creating component category from protobuf."""
1718
assert (
18-
cp.component_category_from_protobuf(
19-
ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
19+
component_category_from_protobuf(
20+
components.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
2021
)
21-
== cp.ComponentCategory.NONE
22+
== ComponentCategory.NONE
2223
)
2324

2425
assert (
25-
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_GRID)
26-
== cp.ComponentCategory.GRID
26+
component_category_from_protobuf(
27+
components.ComponentCategory.COMPONENT_CATEGORY_GRID
28+
)
29+
== ComponentCategory.GRID
2730
)
2831

2932
assert (
30-
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_METER)
31-
== cp.ComponentCategory.METER
33+
component_category_from_protobuf(
34+
components.ComponentCategory.COMPONENT_CATEGORY_METER
35+
)
36+
== ComponentCategory.METER
3237
)
3338

3439
assert (
35-
cp.component_category_from_protobuf(
36-
ComponentCategory.COMPONENT_CATEGORY_INVERTER
40+
component_category_from_protobuf(
41+
components.ComponentCategory.COMPONENT_CATEGORY_INVERTER
3742
)
38-
== cp.ComponentCategory.INVERTER
43+
== ComponentCategory.INVERTER
3944
)
4045

4146
assert (
42-
cp.component_category_from_protobuf(
43-
ComponentCategory.COMPONENT_CATEGORY_BATTERY
47+
component_category_from_protobuf(
48+
components.ComponentCategory.COMPONENT_CATEGORY_BATTERY
4449
)
45-
== cp.ComponentCategory.BATTERY
50+
== ComponentCategory.BATTERY
4651
)
4752

4853
assert (
49-
cp.component_category_from_protobuf(
50-
ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
54+
component_category_from_protobuf(
55+
components.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
5156
)
52-
== cp.ComponentCategory.EV_CHARGER
57+
== ComponentCategory.EV_CHARGER
5358
)
5459

55-
assert cp.component_category_from_protobuf(666) == cp.ComponentCategory.NONE # type: ignore
60+
assert component_category_from_protobuf(666) == ComponentCategory.NONE # type: ignore
5661

5762
with pytest.raises(ValueError):
58-
cp.component_category_from_protobuf(ComponentCategory.COMPONENT_CATEGORY_SENSOR)
63+
component_category_from_protobuf(
64+
components.ComponentCategory.COMPONENT_CATEGORY_SENSOR
65+
)
5966

6067

6168
# pylint: disable=invalid-name
6269
def test_Component() -> None:
6370
"""Test the component category."""
64-
c0 = cp.Component(0, cp.ComponentCategory.GRID)
71+
c0 = Component(0, ComponentCategory.GRID)
6572
assert c0.is_valid()
6673

67-
c1 = cp.Component(1, cp.ComponentCategory.GRID)
74+
c1 = Component(1, ComponentCategory.GRID)
6875
assert c1.is_valid()
6976

70-
c4 = cp.Component(4, cp.ComponentCategory.METER)
77+
c4 = Component(4, ComponentCategory.METER)
7178
assert c4.is_valid()
7279

73-
c5 = cp.Component(5, cp.ComponentCategory.INVERTER)
80+
c5 = Component(5, ComponentCategory.INVERTER)
7481
assert c5.is_valid()
7582

76-
c6 = cp.Component(6, cp.ComponentCategory.BATTERY)
83+
c6 = Component(6, ComponentCategory.BATTERY)
7784
assert c6.is_valid()
7885

79-
c7 = cp.Component(7, cp.ComponentCategory.EV_CHARGER)
86+
c7 = Component(7, ComponentCategory.EV_CHARGER)
8087
assert c7.is_valid()
8188

82-
invalid_grid_id = cp.Component(-1, cp.ComponentCategory.GRID)
89+
invalid_grid_id = Component(-1, ComponentCategory.GRID)
8390
assert not invalid_grid_id.is_valid()
8491

85-
invalid_type = cp.Component(666, -1) # type: ignore
92+
invalid_type = Component(666, -1) # type: ignore
8693
assert not invalid_type.is_valid()
8794

88-
another_invalid_type = cp.Component(666, 666) # type: ignore
95+
another_invalid_type = Component(666, 666) # type: ignore
8996
assert not another_invalid_type.is_valid()

tests/test_component_data.py

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,11 @@
66
from datetime import datetime, timezone
77

88
import pytest
9+
from frequenz.microgrid.betterproto.frequenz.api import microgrid
10+
from frequenz.microgrid.betterproto.frequenz.api.common import metrics
11+
from frequenz.microgrid.betterproto.frequenz.api.common.metrics import electrical
12+
from frequenz.microgrid.betterproto.frequenz.api.microgrid import inverter
913

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
2414
from frequenz.client.microgrid import ComponentData, InverterData
2515

2616

@@ -35,45 +25,52 @@ def test_inverter_data() -> None:
3525
"""Verify the constructor for the InverterData class."""
3626
seconds = 1234567890
3727

38-
raw = PbComponentData(
28+
raw = microgrid.ComponentData(
3929
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(
30+
ts=datetime.fromtimestamp(seconds, timezone.utc),
31+
inverter=inverter.Inverter(
32+
state=inverter.State(
33+
component_state=inverter.ComponentState.COMPONENT_STATE_DISCHARGING
34+
),
35+
errors=[inverter.Error(msg="error message")],
36+
data=inverter.Data(
37+
ac=electrical.Ac(
38+
frequency=metrics.Metric(value=50.1),
39+
power_active=metrics.Metric(
5140
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),
41+
system_exclusion_bounds=metrics.Bounds(
42+
lower=-501.0, upper=501.0
43+
),
44+
system_inclusion_bounds=metrics.Bounds(
45+
lower=-51_000.0, upper=51_000.0
46+
),
5447
),
55-
power_reactive=Metric(
48+
power_reactive=metrics.Metric(
5649
value=200.3,
57-
system_exclusion_bounds=Bounds(lower=-502.0, upper=502.0),
58-
system_inclusion_bounds=Bounds(lower=-52_000.0, upper=52_000.0),
50+
system_exclusion_bounds=metrics.Bounds(
51+
lower=-502.0, upper=502.0
52+
),
53+
system_inclusion_bounds=metrics.Bounds(
54+
lower=-52_000.0, upper=52_000.0
55+
),
5956
),
60-
phase_1=AC.ACPhase(
61-
current=Metric(value=12.3),
62-
voltage=Metric(value=229.8),
63-
power_active=Metric(value=33.1),
64-
power_reactive=Metric(value=10.1),
57+
phase_1=electrical.AcAcPhase(
58+
current=metrics.Metric(value=12.3),
59+
voltage=metrics.Metric(value=229.8),
60+
power_active=metrics.Metric(value=33.1),
61+
power_reactive=metrics.Metric(value=10.1),
6562
),
66-
phase_2=AC.ACPhase(
67-
current=Metric(value=23.4),
68-
voltage=Metric(value=230.0),
69-
power_active=Metric(value=33.3),
70-
power_reactive=Metric(value=10.2),
63+
phase_2=electrical.AcAcPhase(
64+
current=metrics.Metric(value=23.4),
65+
voltage=metrics.Metric(value=230.0),
66+
power_active=metrics.Metric(value=33.3),
67+
power_reactive=metrics.Metric(value=10.2),
7168
),
72-
phase_3=AC.ACPhase(
73-
current=Metric(value=34.5),
74-
voltage=Metric(value=230.2),
75-
power_active=Metric(value=33.8),
76-
power_reactive=Metric(value=10.3),
69+
phase_3=electrical.AcAcPhase(
70+
current=metrics.Metric(value=34.5),
71+
voltage=metrics.Metric(value=230.2),
72+
power_active=metrics.Metric(value=33.8),
73+
power_reactive=metrics.Metric(value=10.3),
7774
),
7875
),
7976
),
@@ -84,10 +81,10 @@ def test_inverter_data() -> None:
8481
assert inv_data.component_id == 5
8582
assert inv_data.timestamp == datetime.fromtimestamp(seconds, timezone.utc)
8683
assert ( # pylint: disable=protected-access
87-
inv_data._component_state == COMPONENT_STATE_DISCHARGING
84+
inv_data._component_state == inverter.ComponentState.COMPONENT_STATE_DISCHARGING
8885
)
8986
assert inv_data._errors == [ # pylint: disable=protected-access
90-
Error(msg="error message")
87+
inverter.Error(msg="error message")
9188
]
9289
assert inv_data.frequency == pytest.approx(50.1)
9390
assert inv_data.active_power == pytest.approx(100.2)

0 commit comments

Comments
 (0)