|
15 | 15 | import unittest |
16 | 16 | from unittest import mock |
17 | 17 | from google.auth import credentials as auth_credentials |
| 18 | +from google.api_core import client_options as client_options_lib |
18 | 19 |
|
19 | 20 |
|
20 | 21 | class TestGrpcClient(unittest.TestCase): |
@@ -89,7 +90,7 @@ def test_constructor_disables_direct_path( |
89 | 90 |
|
90 | 91 | @mock.patch("google.cloud.storage._experimental.grpc_client.ClientWithProject") |
91 | 92 | @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): |
93 | 94 | from google.cloud.storage._experimental import grpc_client |
94 | 95 |
|
95 | 96 | mock_transport_cls = mock.MagicMock() |
@@ -128,3 +129,72 @@ def test_grpc_client_property(self, mock_storage_client, mock_base_client): |
128 | 129 | retrieved_client = client.grpc_client |
129 | 130 |
|
130 | 131 | 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