Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 4a858a0

Browse files
committed
add more tests
1 parent 2e3a245 commit 4a858a0

1 file changed

Lines changed: 71 additions & 1 deletion

File tree

tests/unit/test_grpc_client.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
from unittest import mock
1717
from google.auth import credentials as auth_credentials
18+
from google.api_core import client_options as client_options_lib
1819

1920

2021
class TestGrpcClient(unittest.TestCase):
@@ -89,7 +90,7 @@ def test_constructor_disables_direct_path(
8990

9091
@mock.patch("google.cloud.storage._experimental.grpc_client.ClientWithProject")
9192
@mock.patch("google.cloud._storage_v2.StorageClient")
92-
def test_constructor_handles_api_key(self, mock_storage_client, mock_base_client):
93+
def test_constructor_initialize_with_api_key(self, mock_storage_client, mock_base_client):
9394
from google.cloud.storage._experimental import grpc_client
9495

9596
mock_transport_cls = mock.MagicMock()
@@ -128,3 +129,72 @@ def test_grpc_client_property(self, mock_storage_client, mock_base_client):
128129
retrieved_client = client.grpc_client
129130

130131
self.assertIs(retrieved_client, mock_storage_client.return_value)
132+
133+
@mock.patch("google.cloud.storage._experimental.grpc_client.ClientWithProject")
134+
@mock.patch("google.cloud._storage_v2.StorageClient")
135+
def test_constructor_with_api_key_and_client_options(
136+
self, mock_storage_client, mock_base_client
137+
):
138+
from google.cloud.storage._experimental import grpc_client
139+
140+
mock_transport_cls = mock.MagicMock()
141+
mock_storage_client.get_transport_class.return_value = mock_transport_cls
142+
mock_transport = mock_transport_cls.return_value
143+
144+
mock_creds = mock.Mock(spec=auth_credentials.Credentials)
145+
mock_base_instance = mock_base_client.return_value
146+
mock_base_instance._credentials = mock_creds
147+
148+
client_options_obj = client_options_lib.ClientOptions(
149+
api_endpoint="test.endpoint"
150+
)
151+
self.assertIsNone(client_options_obj.api_key)
152+
153+
grpc_client.GrpcClient(
154+
project="test-project",
155+
credentials=mock_creds,
156+
client_options=client_options_obj,
157+
api_key="new-test-key",
158+
)
159+
160+
mock_storage_client.assert_called_once_with(
161+
credentials=mock_creds,
162+
transport=mock_transport,
163+
client_info=None,
164+
client_options=client_options_obj,
165+
)
166+
self.assertEqual(client_options_obj.api_key, "new-test-key")
167+
168+
@mock.patch("google.cloud.storage._experimental.grpc_client.ClientWithProject")
169+
@mock.patch("google.cloud._storage_v2.StorageClient")
170+
def test_constructor_with_api_key_and_dict_options(
171+
self, mock_storage_client, mock_base_client
172+
):
173+
from google.cloud.storage._experimental import grpc_client
174+
175+
mock_creds = mock.Mock(spec=auth_credentials.Credentials)
176+
mock_base_instance = mock_base_client.return_value
177+
mock_base_instance._credentials = mock_creds
178+
mock_transport_cls = mock.MagicMock()
179+
mock_storage_client.get_transport_class.return_value = mock_transport_cls
180+
mock_transport = mock_transport_cls.return_value
181+
182+
client_options_dict = {"api_endpoint": "test.endpoint"}
183+
184+
grpc_client.GrpcClient(
185+
project="test-project",
186+
credentials=mock_creds,
187+
client_options=client_options_dict,
188+
api_key="new-test-key",
189+
)
190+
191+
expected_options = {
192+
"api_endpoint": "test.endpoint",
193+
"api_key": "new-test-key",
194+
}
195+
mock_storage_client.assert_called_once_with(
196+
credentials=mock_creds,
197+
transport=mock_transport,
198+
client_info=None,
199+
client_options=expected_options,
200+
)

0 commit comments

Comments
 (0)