1111import pytest
1212import pytest_mock
1313
14+ from frequenz .client .base .channel import ChannelOptions , SslOptions
1415from frequenz .client .base .client import BaseApiClient , StubT , call_stub_method
1516from frequenz .client .base .exception import ClientNotConnected , UnknownError
1617
@@ -52,6 +53,7 @@ def create_client_with_mocks(
5253 * ,
5354 auto_connect : bool = True ,
5455 server_url : str = _DEFAULT_SERVER_URL ,
56+ channel_defaults : ChannelOptions | None = None ,
5557) -> tuple [BaseApiClient [mock .MagicMock ], _ClientMocks ]:
5658 """Create a BaseApiClient instance with mocks."""
5759 mock_stub = mock .MagicMock (name = "stub" )
@@ -60,10 +62,14 @@ def create_client_with_mocks(
6062 mock_parse_grpc_uri = mocker .patch (
6163 "frequenz.client.base.client.parse_grpc_uri" , return_value = mock_channel
6264 )
65+ kwargs = {}
66+ if channel_defaults is not None :
67+ kwargs ["channel_defaults" ] = channel_defaults
6368 client = BaseApiClient (
6469 server_url = server_url ,
6570 create_stub = mock_create_stub ,
6671 connect = auto_connect ,
72+ ** kwargs ,
6773 )
6874 return client , _ClientMocks (
6975 stub = mock_stub ,
@@ -82,7 +88,9 @@ def test_base_api_client_init(
8288 client , mocks = create_client_with_mocks (mocker , auto_connect = auto_connect )
8389 assert client .server_url == _DEFAULT_SERVER_URL
8490 if auto_connect :
85- mocks .parse_grpc_uri .assert_called_once_with (client .server_url )
91+ mocks .parse_grpc_uri .assert_called_once_with (
92+ client .server_url , ChannelOptions ()
93+ )
8694 assert client .channel is mocks .channel
8795 assert client .stub is mocks .stub
8896 assert client .is_connected
@@ -93,6 +101,20 @@ def test_base_api_client_init(
93101 mocks .create_stub .assert_not_called ()
94102
95103
104+ def test_base_api_client_init_with_channel_defaults (
105+ mocker : pytest_mock .MockFixture ,
106+ ) -> None :
107+ """Test initializing the BaseApiClient with channel defaults."""
108+ channel_defaults = ChannelOptions (port = 1234 , ssl = SslOptions (enabled = False ))
109+ client , mocks = create_client_with_mocks (mocker , channel_defaults = channel_defaults )
110+ assert client .server_url == _DEFAULT_SERVER_URL
111+ mocks .parse_grpc_uri .assert_called_once_with (client .server_url , channel_defaults )
112+ assert client .channel is mocks .channel
113+ assert client .stub is mocks .stub
114+ assert client .is_connected
115+ mocks .create_stub .assert_called_once_with (mocks .channel )
116+
117+
96118@pytest .mark .parametrize (
97119 "new_server_url" , [None , _DEFAULT_SERVER_URL , "grpc://localhost:50051" ]
98120)
@@ -128,7 +150,9 @@ def test_base_api_client_connect(
128150 mocks .parse_grpc_uri .assert_not_called ()
129151 mocks .create_stub .assert_not_called ()
130152 else :
131- mocks .parse_grpc_uri .assert_called_once_with (client .server_url )
153+ mocks .parse_grpc_uri .assert_called_once_with (
154+ client .server_url , ChannelOptions ()
155+ )
132156 mocks .create_stub .assert_called_once_with (mocks .channel )
133157
134158
@@ -166,7 +190,9 @@ async def test_base_api_client_async_context_manager(
166190 mocks .parse_grpc_uri .assert_not_called ()
167191 mocks .create_stub .assert_not_called ()
168192 else :
169- mocks .parse_grpc_uri .assert_called_once_with (client .server_url )
193+ mocks .parse_grpc_uri .assert_called_once_with (
194+ client .server_url , ChannelOptions ()
195+ )
170196 mocks .create_stub .assert_called_once_with (mocks .channel )
171197
172198 mocks .channel .__aexit__ .assert_called_once_with (None , None , None )
0 commit comments