|
6 | 6 | from datetime import datetime, timezone |
7 | 7 |
|
8 | 8 | import pytest |
| 9 | +from frequenz.api.common import metrics_pb2 |
| 10 | +from frequenz.api.common.metrics import electrical_pb2 |
| 11 | +from frequenz.api.microgrid import inverter_pb2, microgrid_pb2 |
| 12 | +from google.protobuf import timestamp_pb2 |
9 | 13 |
|
10 | | -from frequenz.sdk.microgrid.component import ComponentData |
| 14 | +from frequenz.sdk.microgrid.component import ComponentData, InverterData |
| 15 | + |
| 16 | +# pylint: disable=no-member |
11 | 17 |
|
12 | 18 |
|
13 | 19 | def test_component_data_abstract_class() -> None: |
14 | 20 | """Verify the base class ComponentData may not be instantiated.""" |
15 | 21 | with pytest.raises(TypeError): |
16 | 22 | # pylint: disable=abstract-class-instantiated |
17 | 23 | ComponentData(0, datetime.now(timezone.utc)) # type: ignore |
| 24 | + |
| 25 | + |
| 26 | +def test_inverter_data() -> None: |
| 27 | + """Verify the constructor for the InverterData class.""" |
| 28 | + seconds = 1234567890 |
| 29 | + |
| 30 | + raw = microgrid_pb2.ComponentData( |
| 31 | + id=5, |
| 32 | + ts=timestamp_pb2.Timestamp(seconds=seconds), |
| 33 | + inverter=inverter_pb2.Inverter( |
| 34 | + state=inverter_pb2.State( |
| 35 | + component_state=inverter_pb2.COMPONENT_STATE_DISCHARGING |
| 36 | + ), |
| 37 | + errors=[inverter_pb2.Error(msg="error message")], |
| 38 | + data=inverter_pb2.Data( |
| 39 | + dc_battery=None, |
| 40 | + dc_solar=None, |
| 41 | + temperature=None, |
| 42 | + ac=electrical_pb2.AC( |
| 43 | + frequency=metrics_pb2.Metric(value=50.1), |
| 44 | + power_active=metrics_pb2.Metric( |
| 45 | + value=100.2, |
| 46 | + system_exclusion_bounds=metrics_pb2.Bounds( |
| 47 | + lower=-501.0, upper=501.0 |
| 48 | + ), |
| 49 | + system_inclusion_bounds=metrics_pb2.Bounds( |
| 50 | + lower=-51_000.0, upper=51_000.0 |
| 51 | + ), |
| 52 | + ), |
| 53 | + phase_1=electrical_pb2.AC.ACPhase( |
| 54 | + current=metrics_pb2.Metric(value=12.3), |
| 55 | + ), |
| 56 | + phase_2=electrical_pb2.AC.ACPhase( |
| 57 | + current=metrics_pb2.Metric(value=23.4), |
| 58 | + ), |
| 59 | + phase_3=electrical_pb2.AC.ACPhase( |
| 60 | + current=metrics_pb2.Metric(value=34.5), |
| 61 | + ), |
| 62 | + ), |
| 63 | + ), |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + inv_data = InverterData.from_proto(raw) |
| 68 | + assert inv_data.component_id == 5 |
| 69 | + assert inv_data.timestamp == datetime.fromtimestamp(seconds, timezone.utc) |
| 70 | + assert ( # pylint: disable=protected-access |
| 71 | + inv_data._component_state == inverter_pb2.COMPONENT_STATE_DISCHARGING |
| 72 | + ) |
| 73 | + assert inv_data._errors == [ # pylint: disable=protected-access |
| 74 | + inverter_pb2.Error(msg="error message") |
| 75 | + ] |
| 76 | + assert inv_data.frequency == pytest.approx(50.1) |
| 77 | + assert inv_data.active_power == pytest.approx(100.2) |
| 78 | + assert inv_data.current_per_phase == pytest.approx((12.3, 23.4, 34.5)) |
| 79 | + assert inv_data.active_power_inclusion_lower_bound == pytest.approx(-51_000.0) |
| 80 | + assert inv_data.active_power_inclusion_upper_bound == pytest.approx(51_000.0) |
| 81 | + assert inv_data.active_power_exclusion_lower_bound == pytest.approx(-501.0) |
| 82 | + assert inv_data.active_power_exclusion_upper_bound == pytest.approx(501.0) |
0 commit comments