-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_client.py
More file actions
1086 lines (949 loc) · 39.6 KB
/
test_client.py
File metadata and controls
1086 lines (949 loc) · 39.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# License: MIT
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
"""Tests for the microgrid client thin wrapper."""
# We are going to split these tests in the future, but for now...
# pylint: disable=too-many-lines
import logging
from collections.abc import AsyncIterator
from datetime import datetime, timezone
from typing import Any
from unittest import mock
import grpc.aio
import pytest
from frequenz.api.common import components_pb2, metrics_pb2
from frequenz.api.microgrid import grid_pb2, inverter_pb2, microgrid_pb2, sensor_pb2
from frequenz.client.base import conversion, retry
from frequenz.client.common.microgrid import MicrogridId
from frequenz.client.common.microgrid.components import ComponentCategory, ComponentId
from frequenz.client.common.microgrid.sensors import SensorId
from google.protobuf.empty_pb2 import Empty
from frequenz.client.microgrid import (
ApiClientError,
BatteryData,
Component,
ComponentData,
Connection,
EVChargerData,
Fuse,
GridMetadata,
InverterData,
InverterType,
MeterData,
MicrogridApiClient,
)
from frequenz.client.microgrid.sensor import (
Sensor,
SensorDataSamples,
SensorMetric,
SensorMetricSample,
SensorStateCode,
SensorStateSample,
)
class _TestClient(MicrogridApiClient):
def __init__(self, *, retry_strategy: retry.Strategy | None = None) -> None:
# Here we sadly can't use spec=MicrogridStub because the generated stub typing
# is a mess, and for some reason inspection of gRPC methods doesn't work.
# This is also why we need to explicitly create the AsyncMock objects for every
# call.
mock_stub = mock.MagicMock(name="stub")
mock_stub.ListComponents = mock.AsyncMock("ListComponents")
mock_stub.ListConnections = mock.AsyncMock("ListConnections")
mock_stub.SetPowerActive = mock.AsyncMock("SetPowerActive")
mock_stub.SetPowerReactive = mock.AsyncMock("SetPowerReactive")
mock_stub.AddInclusionBounds = mock.AsyncMock("AddInclusionBounds")
mock_stub.StreamComponentData = mock.Mock("StreamComponentData")
mock_stub.GetMicrogridMetadata = mock.AsyncMock("GetMicrogridMetadata")
super().__init__("grpc://mock_host:1234", retry_strategy=retry_strategy)
self.mock_stub = mock_stub
self._stub = mock_stub # pylint: disable=protected-access
@pytest.fixture
async def client() -> AsyncIterator[_TestClient]:
"""Return a test client."""
async with _TestClient(
retry_strategy=retry.LinearBackoff(interval=0.0, jitter=0.0, limit=6)
) as client_instance:
yield client_instance
async def test_components(client: _TestClient) -> None:
"""Test the components() method."""
server_response = microgrid_pb2.ComponentList()
client.mock_stub.ListComponents.return_value = server_response
assert set(await client.components()) == set()
server_response.components.append(
microgrid_pb2.Component(
id=0, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
)
)
assert set(await client.components()) == {
Component(ComponentId(0), ComponentCategory.METER)
}
server_response.components.append(
microgrid_pb2.Component(
id=0, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY
)
)
assert set(await client.components()) == {
Component(ComponentId(0), ComponentCategory.METER),
Component(ComponentId(0), ComponentCategory.BATTERY),
}
server_response.components.append(
microgrid_pb2.Component(
id=0, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
)
)
assert set(await client.components()) == {
Component(ComponentId(0), ComponentCategory.METER),
Component(ComponentId(0), ComponentCategory.BATTERY),
Component(ComponentId(0), ComponentCategory.METER),
}
# sensors are not counted as components by the API client
server_response.components.append(
microgrid_pb2.Component(
id=1, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR
)
)
assert set(await client.components()) == {
Component(ComponentId(0), ComponentCategory.METER),
Component(ComponentId(0), ComponentCategory.BATTERY),
Component(ComponentId(0), ComponentCategory.METER),
}
_replace_components(
server_response,
[
microgrid_pb2.Component(
id=9, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
),
microgrid_pb2.Component(
id=99,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
),
microgrid_pb2.Component(
id=666,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
),
microgrid_pb2.Component(
id=999,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
),
],
)
assert set(await client.components()) == {
Component(ComponentId(9), ComponentCategory.METER),
Component(ComponentId(99), ComponentCategory.INVERTER, InverterType.NONE),
Component(ComponentId(999), ComponentCategory.BATTERY),
}
_replace_components(
server_response,
[
microgrid_pb2.Component(
id=99,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
),
microgrid_pb2.Component(
id=100,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED,
),
microgrid_pb2.Component(
id=104,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER,
),
microgrid_pb2.Component(
id=105,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
),
microgrid_pb2.Component(
id=106,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
),
microgrid_pb2.Component(
id=107,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER,
),
microgrid_pb2.Component(
id=999,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
),
microgrid_pb2.Component(
id=101,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID,
grid=grid_pb2.Metadata(rated_fuse_current=int(123.0)),
),
],
)
grid_fuse = Fuse(123.0)
assert set(await client.components()) == {
Component(ComponentId(100), ComponentCategory.UNSPECIFIED),
Component(
ComponentId(101),
ComponentCategory.GRID,
None,
GridMetadata(fuse=grid_fuse),
),
Component(ComponentId(104), ComponentCategory.METER),
Component(ComponentId(105), ComponentCategory.INVERTER, InverterType.NONE),
Component(ComponentId(106), ComponentCategory.BATTERY),
Component(ComponentId(107), ComponentCategory.EV_CHARGER),
}
_replace_components(
server_response,
[
microgrid_pb2.Component(
id=9, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
),
microgrid_pb2.Component(
id=666,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
),
microgrid_pb2.Component(
id=999,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
),
microgrid_pb2.Component(
id=99,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
inverter=inverter_pb2.Metadata(
type=components_pb2.InverterType.INVERTER_TYPE_BATTERY
),
),
],
)
assert set(await client.components()) == {
Component(ComponentId(9), ComponentCategory.METER),
Component(ComponentId(99), ComponentCategory.INVERTER, InverterType.BATTERY),
Component(ComponentId(999), ComponentCategory.BATTERY),
}
async def test_components_grpc_error(client: _TestClient) -> None:
"""Test the components() method when the gRPC call fails."""
client.mock_stub.ListComponents.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'ListComponents' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.components()
async def test_connections(client: _TestClient) -> None:
"""Test the connections() method."""
def assert_filter(*, starts: set[int], ends: set[int]) -> None:
client.mock_stub.ListConnections.assert_called_once()
filter_ = client.mock_stub.ListConnections.call_args[0][0]
assert isinstance(filter_, microgrid_pb2.ConnectionFilter)
assert set(filter_.starts) == starts
assert set(filter_.ends) == ends
components_response = microgrid_pb2.ComponentList()
connections_response = microgrid_pb2.ConnectionList()
client.mock_stub.ListComponents.return_value = components_response
client.mock_stub.ListConnections.return_value = connections_response
assert set(await client.connections()) == set()
assert_filter(starts=set(), ends=set())
connections_response.connections.append(microgrid_pb2.Connection(start=0, end=0))
assert set(await client.connections()) == {
Connection(ComponentId(0), ComponentId(0))
}
components_response.components.extend(
[
microgrid_pb2.Component(
id=7,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
),
microgrid_pb2.Component(
id=9,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
),
]
)
connections_response.connections.append(microgrid_pb2.Connection(start=7, end=9))
assert set(await client.connections()) == {
Connection(ComponentId(0), ComponentId(0)),
Connection(ComponentId(7), ComponentId(9)),
}
connections_response.connections.append(microgrid_pb2.Connection(start=0, end=0))
assert set(await client.connections()) == {
Connection(ComponentId(0), ComponentId(0)),
Connection(ComponentId(7), ComponentId(9)),
Connection(ComponentId(0), ComponentId(0)),
}
_replace_connections(
connections_response,
[
microgrid_pb2.Connection(start=999, end=9),
microgrid_pb2.Connection(start=99, end=19),
microgrid_pb2.Connection(start=909, end=101),
microgrid_pb2.Connection(start=99, end=91),
],
)
for component_id in [999, 99, 19, 909, 101, 91]:
components_response.components.append(
microgrid_pb2.Component(
id=component_id,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
)
)
assert set(await client.connections()) == {
Connection(ComponentId(999), ComponentId(9)),
Connection(ComponentId(99), ComponentId(19)),
Connection(ComponentId(909), ComponentId(101)),
Connection(ComponentId(99), ComponentId(91)),
}
for component_id in [1, 2, 3, 4, 5, 6, 7, 8]:
components_response.components.append(
microgrid_pb2.Component(
id=component_id,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
)
)
_replace_connections(
connections_response,
[
microgrid_pb2.Connection(start=1, end=2),
microgrid_pb2.Connection(start=2, end=3),
microgrid_pb2.Connection(start=2, end=4),
microgrid_pb2.Connection(start=2, end=5),
microgrid_pb2.Connection(start=4, end=3),
microgrid_pb2.Connection(start=4, end=5),
microgrid_pb2.Connection(start=4, end=6),
microgrid_pb2.Connection(start=5, end=4),
microgrid_pb2.Connection(start=5, end=7),
microgrid_pb2.Connection(start=5, end=8),
],
)
assert set(await client.connections()) == {
Connection(ComponentId(1), ComponentId(2)),
Connection(ComponentId(2), ComponentId(3)),
Connection(ComponentId(2), ComponentId(4)),
Connection(ComponentId(2), ComponentId(5)),
Connection(ComponentId(4), ComponentId(3)),
Connection(ComponentId(4), ComponentId(5)),
Connection(ComponentId(4), ComponentId(6)),
Connection(ComponentId(5), ComponentId(4)),
Connection(ComponentId(5), ComponentId(7)),
Connection(ComponentId(5), ComponentId(8)),
}
# passing empty sets is the same as passing `None`,
# filter is ignored
client.mock_stub.reset_mock()
await client.connections(starts=set(), ends=set())
assert_filter(starts=set(), ends=set())
# include filter for connection start
client.mock_stub.reset_mock()
await client.connections(starts={ComponentId(1), ComponentId(2)})
assert_filter(starts={1, 2}, ends=set())
client.mock_stub.reset_mock()
await client.connections(starts={ComponentId(2)})
assert_filter(starts={2}, ends=set())
# include filter for connection end
client.mock_stub.reset_mock()
await client.connections(ends={ComponentId(1)})
assert_filter(starts=set(), ends={1})
client.mock_stub.reset_mock()
await client.connections(ends={ComponentId(2), ComponentId(4), ComponentId(5)})
assert_filter(starts=set(), ends={2, 4, 5})
# different filters combine with AND logic
client.mock_stub.reset_mock()
await client.connections(
starts={ComponentId(1), ComponentId(2), ComponentId(4)},
ends={ComponentId(4), ComponentId(5), ComponentId(6)},
)
assert_filter(starts={1, 2, 4}, ends={4, 5, 6})
async def test_connections_grpc_error(client: _TestClient) -> None:
"""Test the components() method when the gRPC call fails."""
client.mock_stub.ListConnections.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'ListConnections' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.connections()
async def test_metadata_success(client: _TestClient) -> None:
"""Test the metadata() method with a successful gRPC call."""
mock_metadata_response = microgrid_pb2.MicrogridMetadata(
microgrid_id=123,
location=microgrid_pb2.Location(latitude=40.7128, longitude=-74.0060),
)
client.mock_stub.GetMicrogridMetadata.return_value = mock_metadata_response
metadata = await client.metadata()
assert metadata.microgrid_id == MicrogridId(123)
assert metadata.location is not None
assert metadata.location.latitude == pytest.approx(40.7128)
assert metadata.location.longitude == pytest.approx(-74.0060)
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
async def test_metadata_no_location(client: _TestClient) -> None:
"""Test the metadata() method when location is not set in the response."""
mock_metadata_response = microgrid_pb2.MicrogridMetadata(microgrid_id=456)
client.mock_stub.GetMicrogridMetadata.return_value = mock_metadata_response
metadata = await client.metadata()
assert metadata.microgrid_id == MicrogridId(456)
assert metadata.location is None
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
async def test_metadata_empty_response(client: _TestClient) -> None:
"""Test the metadata() method when the server returns an empty response."""
client.mock_stub.GetMicrogridMetadata.return_value = None
metadata = await client.metadata()
assert metadata.microgrid_id is None
assert metadata.location is None
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
async def test_metadata_grpc_error(
client: _TestClient, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the metadata() method when the gRPC call fails."""
caplog.set_level(logging.WARNING)
client.mock_stub.GetMicrogridMetadata.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details for metadata",
"fake grpc debug_error_string for metadata",
)
metadata = await client.metadata()
assert metadata.microgrid_id is None
assert metadata.location is None
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
assert "The microgrid metadata is not available." in caplog.records[0].message
assert caplog.records[0].exc_text is not None
assert "fake grpc details for metadata" in caplog.records[0].exc_text
async def test_list_sensors(client: _TestClient) -> None:
"""Test the list_sensors() method."""
server_response = microgrid_pb2.ComponentList()
client.mock_stub.ListComponents.return_value = server_response
assert set(await client.list_sensors()) == set()
# Add a sensor
sensor_component = microgrid_pb2.Component(
id=201,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
sensor=sensor_pb2.Metadata(
type=components_pb2.SensorType.SENSOR_TYPE_ACCELEROMETER,
),
)
server_response.components.append(sensor_component)
assert set(await client.list_sensors()) == {
Sensor(id=SensorId(201)),
}
# Add another sensor
sensor_component_2 = microgrid_pb2.Component(
id=202,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
sensor=sensor_pb2.Metadata(
type=components_pb2.SensorType.SENSOR_TYPE_HYGROMETER
),
)
server_response.components.append(sensor_component_2)
assert set(await client.list_sensors()) == {
Sensor(id=SensorId(201)),
Sensor(id=SensorId(202)),
}
# Add a non-sensor component to the mock response from ListSensors
# The client.list_sensors() method should filter this out if it's robust,
# or the ListSensors RPC itself should only return sensor components.
meter_component = microgrid_pb2.Component(
id=203, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
)
server_response.components.append(meter_component)
# Assert that only SENSOR category components are returned by client.list_sensors()
assert set(await client.list_sensors()) == {
Sensor(id=SensorId(201)),
Sensor(id=SensorId(202)),
Sensor(id=SensorId(203)),
}
# Clean up: remove the meter component from the mock response
server_response.components.pop()
_replace_components(
server_response,
[
microgrid_pb2.Component(
id=204,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
sensor=sensor_pb2.Metadata(
type=components_pb2.SensorType.SENSOR_TYPE_ANEMOMETER
),
),
microgrid_pb2.Component(
id=205,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
sensor=sensor_pb2.Metadata(
type=components_pb2.SensorType.SENSOR_TYPE_PYRANOMETER
),
),
],
)
assert set(await client.list_sensors()) == {
Sensor(id=SensorId(204)),
Sensor(id=SensorId(205)),
}
async def test_list_sensors_grpc_error(client: _TestClient) -> None:
"""Test the list_sensors() method when the gRPC call fails."""
client.mock_stub.GetMicrogridMetadata.return_value = (
microgrid_pb2.MicrogridMetadata(microgrid_id=101)
)
client.mock_stub.ListComponents.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'ListComponents' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.list_sensors()
@pytest.fixture
def meter83() -> microgrid_pb2.Component:
"""Return a test meter component."""
return microgrid_pb2.Component(
id=83, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
)
@pytest.fixture
def battery38() -> microgrid_pb2.Component:
"""Return a test battery component."""
return microgrid_pb2.Component(
id=38, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY
)
@pytest.fixture
def inverter99() -> microgrid_pb2.Component:
"""Return a test inverter component."""
return microgrid_pb2.Component(
id=99, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
)
@pytest.fixture
def ev_charger101() -> microgrid_pb2.Component:
"""Return a test EV charger component."""
return microgrid_pb2.Component(
id=101, category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
)
@pytest.fixture
def sensor201() -> microgrid_pb2.Component:
"""Return a test sensor component."""
return microgrid_pb2.Component(
id=201,
category=components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
sensor=sensor_pb2.Metadata(
type=components_pb2.SensorType.SENSOR_TYPE_THERMOMETER
),
)
@pytest.fixture
def component_list(
meter83: microgrid_pb2.Component,
battery38: microgrid_pb2.Component,
inverter99: microgrid_pb2.Component,
ev_charger101: microgrid_pb2.Component,
sensor201: microgrid_pb2.Component,
) -> list[microgrid_pb2.Component]:
"""Return a list of test components."""
return [meter83, battery38, inverter99, ev_charger101, sensor201]
@pytest.mark.parametrize("method", ["meter_data", "battery_data", "inverter_data"])
async def test_data_component_not_found(method: str, client: _TestClient) -> None:
"""Test the meter_data() method."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList()
# It should raise a ValueError for a missing component_id
with pytest.raises(ValueError, match="Unable to find CID20"):
await getattr(client, method)(ComponentId(20))
@pytest.mark.parametrize(
"method, component_id",
[
("meter_data", ComponentId(38)),
("battery_data", ComponentId(83)),
("inverter_data", ComponentId(83)),
("ev_charger_data", ComponentId(99)),
],
)
async def test_data_bad_category(
method: str,
component_id: ComponentId,
component_list: list[microgrid_pb2.Component],
client: _TestClient,
) -> None:
"""Test the meter_data() method."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=component_list
)
# It should raise a ValueError for a wrong component category
with pytest.raises(
ValueError,
match=rf"{component_id} has category .*, but {method[:-5].upper()} was expected",
):
await getattr(client, method)(component_id)
@pytest.mark.parametrize(
"method, component_id, component_class",
[
("meter_data", ComponentId(83), MeterData),
("battery_data", ComponentId(38), BatteryData),
("inverter_data", ComponentId(99), InverterData),
("ev_charger_data", ComponentId(101), EVChargerData),
],
)
async def test_component_data(
method: str,
component_id: ComponentId,
component_class: type[ComponentData],
component_list: list[microgrid_pb2.Component],
client: _TestClient,
) -> None:
"""Test the meter_data() method."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=component_list
)
async def stream_data(
*args: Any, **kwargs: Any # pylint: disable=unused-argument
) -> AsyncIterator[microgrid_pb2.ComponentData]:
yield microgrid_pb2.ComponentData(id=int(component_id))
client.mock_stub.StreamComponentData.side_effect = stream_data
receiver = await getattr(client, method)(component_id)
latest = await receiver.receive()
assert isinstance(latest, component_class)
assert latest.component_id == component_id
@pytest.mark.parametrize(
"method, component_id, component_class",
[
("meter_data", ComponentId(83), MeterData),
("battery_data", ComponentId(38), BatteryData),
("inverter_data", ComponentId(99), InverterData),
("ev_charger_data", ComponentId(101), EVChargerData),
],
)
# pylint: disable-next=too-many-arguments,too-many-positional-arguments
async def test_component_data_grpc_error(
method: str,
component_id: ComponentId,
component_class: type[ComponentData],
component_list: list[microgrid_pb2.Component],
caplog: pytest.LogCaptureFixture,
client: _TestClient,
) -> None:
"""Test the components() method when the gRPC call fails."""
caplog.set_level(logging.WARNING)
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=component_list
)
num_calls = 0
async def stream_data(
*args: Any, **kwargs: Any # pylint: disable=unused-argument
) -> AsyncIterator[microgrid_pb2.ComponentData]:
nonlocal num_calls
num_calls += 1
if num_calls % 2:
raise grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
f"fake grpc details num_calls={num_calls}",
"fake grpc debug_error_string",
)
yield microgrid_pb2.ComponentData(id=int(component_id))
client.mock_stub.StreamComponentData.side_effect = stream_data
receiver = await getattr(client, method)(component_id)
latest = await receiver.receive()
assert isinstance(latest, component_class)
assert latest.component_id == component_id
latest = await receiver.receive()
assert isinstance(latest, component_class)
assert latest.component_id == component_id
latest = await receiver.receive()
assert isinstance(latest, component_class)
assert latest.component_id == component_id
# This is not super portable, it will change if the GrpcStreamBroadcaster changes,
# but without this there isn't much to check by this test.
assert len(caplog.record_tuples) == 6
for n, log_tuple in enumerate(caplog.record_tuples):
assert log_tuple[0] == "frequenz.client.base.streaming"
assert log_tuple[1] == logging.WARNING
assert (
f"raw-component-data-{component_id}: connection ended, retrying"
in log_tuple[2]
)
if n % 2:
assert "Stream exhausted" in log_tuple[2]
else:
assert f"fake grpc details num_calls={n+1}" in log_tuple[2]
@pytest.mark.parametrize("power_w", [0, 0.0, 12, -75, 0.1, -0.0001, 134.0])
async def test_set_power_ok(
power_w: float, meter83: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test if charge is able to charge component."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=[meter83]
)
component_id = ComponentId(83)
await client.set_power(component_id=component_id, power_w=power_w)
client.mock_stub.SetPowerActive.assert_called_once()
call_args = client.mock_stub.SetPowerActive.call_args[0]
assert call_args[0] == microgrid_pb2.SetPowerActiveParam(
component_id=int(component_id), power=power_w
)
async def test_set_power_grpc_error(client: _TestClient) -> None:
"""Test set_power() raises ApiClientError when the gRPC call fails."""
client.mock_stub.SetPowerActive.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'SetPowerActive' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.set_power(component_id=ComponentId(83), power_w=100.0)
@pytest.mark.parametrize(
"reactive_power_var",
[0, 0.0, 12, -75, 0.1, -0.0001, 134.0],
)
async def test_set_reactive_power_ok(
reactive_power_var: float, meter83: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test if charge is able to charge component."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=[meter83]
)
component_id = ComponentId(83)
await client.set_reactive_power(
component_id=component_id, reactive_power_var=reactive_power_var
)
client.mock_stub.SetPowerReactive.assert_called_once()
call_args = client.mock_stub.SetPowerReactive.call_args[0]
assert call_args[0] == microgrid_pb2.SetPowerReactiveParam(
component_id=int(component_id), power=reactive_power_var
)
async def test_set_reactive_power_grpc_error(client: _TestClient) -> None:
"""Test set_power() raises ApiClientError when the gRPC call fails."""
client.mock_stub.SetPowerReactive.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'SetPowerReactive' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.set_reactive_power(
component_id=ComponentId(83), reactive_power_var=100.0
)
@pytest.mark.parametrize(
"bounds",
[
metrics_pb2.Bounds(lower=0.0, upper=0.0),
metrics_pb2.Bounds(lower=0.0, upper=2.0),
metrics_pb2.Bounds(lower=-10.0, upper=0.0),
metrics_pb2.Bounds(lower=-10.0, upper=2.0),
],
ids=str,
)
async def test_set_bounds_ok(
bounds: metrics_pb2.Bounds, inverter99: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test if charge is able to charge component."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=[inverter99]
)
component_id = ComponentId(99)
await client.set_bounds(component_id, bounds.lower, bounds.upper)
client.mock_stub.AddInclusionBounds.assert_called_once()
call_args = client.mock_stub.AddInclusionBounds.call_args[0]
assert call_args[0] == microgrid_pb2.SetBoundsParam(
component_id=int(component_id),
target_metric=microgrid_pb2.SetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE,
bounds=bounds,
)
@pytest.mark.parametrize(
"bounds",
[
metrics_pb2.Bounds(lower=0.0, upper=-2.0),
metrics_pb2.Bounds(lower=10.0, upper=-2.0),
metrics_pb2.Bounds(lower=10.0, upper=0.0),
],
ids=str,
)
async def test_set_bounds_fail(
bounds: metrics_pb2.Bounds, inverter99: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test if charge is able to charge component."""
client.mock_stub.ListComponents.return_value = microgrid_pb2.ComponentList(
components=[inverter99]
)
with pytest.raises(ValueError):
await client.set_bounds(ComponentId(99), bounds.lower, bounds.upper)
client.mock_stub.AddInclusionBounds.assert_not_called()
async def test_set_bounds_grpc_error(client: _TestClient) -> None:
"""Test set_bounds() raises ApiClientError when the gRPC call fails."""
client.mock_stub.AddInclusionBounds.side_effect = grpc.aio.AioRpcError(
mock.MagicMock(name="mock_status"),
mock.MagicMock(name="mock_initial_metadata"),
mock.MagicMock(name="mock_trailing_metadata"),
"fake grpc details",
"fake grpc debug_error_string",
)
with pytest.raises(
ApiClientError,
match=r"Failed calling 'AddInclusionBounds' on 'grpc://mock_host:1234': .* "
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc details "
r"\(fake grpc debug_error_string\)",
):
await client.set_bounds(ComponentId(99), 0.0, 100.0)
async def test_stream_sensor_data_one_metric(
sensor201: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test successful streaming of sensor data."""
now = datetime.now(timezone.utc)
async def stream_data_impl(
*_: Any, **__: Any
) -> AsyncIterator[microgrid_pb2.ComponentData]:
yield microgrid_pb2.ComponentData(
id=int(sensor201.id),
ts=conversion.to_timestamp(now),
sensor=sensor_pb2.Sensor(
state=sensor_pb2.State(
component_state=sensor_pb2.ComponentState.COMPONENT_STATE_OK
),
data=sensor_pb2.Data(
sensor_data=[
sensor_pb2.SensorData(
value=1.0,
sensor_metric=sensor_pb2.SensorMetric.SENSOR_METRIC_TEMPERATURE,
),
sensor_pb2.SensorData(
value=2.0,
sensor_metric=sensor_pb2.SensorMetric.SENSOR_METRIC_PRESSURE,
),
],
),
),
)
client.mock_stub.StreamComponentData.side_effect = stream_data_impl
receiver = client.stream_sensor_data(
SensorId(sensor201.id), [SensorMetric.TEMPERATURE]
)
sample = await receiver.receive()
assert isinstance(sample, SensorDataSamples)
assert int(sample.sensor_id) == sensor201.id
assert sample.states == [
SensorStateSample(
sampled_at=now,
states=frozenset({SensorStateCode.ON}),
warnings=frozenset(),
errors=frozenset(),
)
]
assert sample.metrics == [
SensorMetricSample(sampled_at=now, metric=SensorMetric.TEMPERATURE, value=1.0)
]
async def test_stream_sensor_data_all_metrics(
sensor201: microgrid_pb2.Component, client: _TestClient
) -> None:
"""Test successful streaming of sensor data."""
now = datetime.now(timezone.utc)
async def stream_data_impl(
*_: Any, **__: Any
) -> AsyncIterator[microgrid_pb2.ComponentData]:
yield microgrid_pb2.ComponentData(
id=int(sensor201.id),
ts=conversion.to_timestamp(now),
sensor=sensor_pb2.Sensor(
state=sensor_pb2.State(
component_state=sensor_pb2.ComponentState.COMPONENT_STATE_OK
),
data=sensor_pb2.Data(
sensor_data=[
sensor_pb2.SensorData(
value=1.0,
sensor_metric=sensor_pb2.SensorMetric.SENSOR_METRIC_TEMPERATURE,
),
sensor_pb2.SensorData(
value=2.0,
sensor_metric=sensor_pb2.SensorMetric.SENSOR_METRIC_PRESSURE,
),
],
),
),
)
client.mock_stub.StreamComponentData.side_effect = stream_data_impl