|
1 | 1 | # License: MIT |
2 | 2 | # Copyright © 2022 Frequenz Energy-as-a-Service GmbH |
3 | 3 |
|
4 | | -"""Tests for the microgrid client thin wrapper.""" |
| 4 | +"""Tests for the MicrogridApiClient class.""" |
| 5 | + |
| 6 | + |
| 7 | +import pytest |
| 8 | +from frequenz.api.microgrid.v1 import microgrid_pb2_grpc |
| 9 | +from frequenz.client.base.channel import ChannelOptions, KeepAliveOptions, SslOptions |
| 10 | +from frequenz.client.base.retry import LinearBackoff |
| 11 | + |
| 12 | +from frequenz.client.microgrid import ( |
| 13 | + DEFAULT_CHANNEL_OPTIONS, |
| 14 | + ClientNotConnected, |
| 15 | + MicrogridApiClient, |
| 16 | +) |
| 17 | + |
| 18 | +from .util import patch_client_class |
| 19 | + |
| 20 | +# pylint: disable=protected-access |
| 21 | + |
| 22 | + |
| 23 | +@patch_client_class(MicrogridApiClient, microgrid_pb2_grpc.MicrogridStub) |
| 24 | +def test_init_defaults() -> None: |
| 25 | + """Test that MicrogridApiClient initializes correctly with defaults (connected).""" |
| 26 | + client = MicrogridApiClient("grpc://localhost:1234") |
| 27 | + assert client.server_url == "grpc://localhost:1234" |
| 28 | + assert client.is_connected is True |
| 29 | + assert client.stub is not None |
| 30 | + assert client.channel_defaults == DEFAULT_CHANNEL_OPTIONS |
| 31 | + assert client._retry_strategy is None # pylint: disable=protected-access |
| 32 | + |
| 33 | + |
| 34 | +@patch_client_class(MicrogridApiClient, microgrid_pb2_grpc.MicrogridStub) |
| 35 | +def test_init_not_connected() -> None: |
| 36 | + """Test that MicrogridApiClient initializes correctly when not connected.""" |
| 37 | + client = MicrogridApiClient("grpc://localhost:1234", connect=False) |
| 38 | + assert client.server_url == "grpc://localhost:1234" |
| 39 | + assert client.is_connected is False |
| 40 | + with pytest.raises(ClientNotConnected) as excinfo: |
| 41 | + _ = client.stub |
| 42 | + assert "client is not connected" in str(excinfo.value).lower() |
| 43 | + assert "grpc://localhost:1234" in str(excinfo.value) |
| 44 | + |
| 45 | + |
| 46 | +@patch_client_class(MicrogridApiClient, microgrid_pb2_grpc.MicrogridStub) |
| 47 | +def test_init_with_defaults() -> None: |
| 48 | + """Test that MicrogridApiClient initializes correctly with custom defaults.""" |
| 49 | + options = ChannelOptions( |
| 50 | + ssl=SslOptions(enabled=False), |
| 51 | + port=1234, |
| 52 | + keep_alive=KeepAliveOptions(enabled=False), |
| 53 | + ) |
| 54 | + assert options != DEFAULT_CHANNEL_OPTIONS |
| 55 | + client = MicrogridApiClient("grpc://localhost:1234", channel_defaults=options) |
| 56 | + assert client.channel_defaults == options |
| 57 | + |
| 58 | + |
| 59 | +@patch_client_class(MicrogridApiClient, microgrid_pb2_grpc.MicrogridStub) |
| 60 | +def test_init_with_custom_retry_strategy() -> None: |
| 61 | + """Test that MicrogridApiClient initializes correctly with custom retry strategy.""" |
| 62 | + retry_strategy = LinearBackoff(interval=0.1, jitter=0.1, limit=5) |
| 63 | + client = MicrogridApiClient( |
| 64 | + "grpc://localhost:1234", retry_strategy=retry_strategy, connect=False |
| 65 | + ) |
| 66 | + client._retry_strategy = retry_strategy # pylint: disable=protected-access |
0 commit comments