|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest import mock |
| 17 | +from google.auth import credentials as auth_credentials |
| 18 | + |
| 19 | + |
| 20 | +def _make_credentials(spec=None): |
| 21 | + if spec is None: |
| 22 | + return mock.Mock(spec=auth_credentials.Credentials) |
| 23 | + return mock.Mock(spec=spec) |
| 24 | + |
| 25 | + |
| 26 | +class TestAsyncGrpcClient(unittest.TestCase): |
| 27 | + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
| 28 | + def test_constructor_default_options(self, mock_async_storage_client): |
| 29 | + from google.cloud.storage._experimental import async_grpc_client |
| 30 | + |
| 31 | + mock_transport_cls = mock.MagicMock() |
| 32 | + mock_async_storage_client.get_transport_class.return_value = mock_transport_cls |
| 33 | + mock_creds = _make_credentials() |
| 34 | + |
| 35 | + async_grpc_client.AsyncGrpcClient(credentials=mock_creds) |
| 36 | + |
| 37 | + mock_async_storage_client.get_transport_class.assert_called_once_with( |
| 38 | + "grpc_asyncio" |
| 39 | + ) |
| 40 | + mock_transport_cls.create_channel.assert_called_once_with( |
| 41 | + attempt_direct_path=True |
| 42 | + ) |
| 43 | + mock_channel = mock_transport_cls.create_channel.return_value |
| 44 | + mock_transport_cls.assert_called_once_with( |
| 45 | + credentials=mock_creds, channel=mock_channel |
| 46 | + ) |
| 47 | + mock_transport = mock_transport_cls.return_value |
| 48 | + mock_async_storage_client.assert_called_once_with( |
| 49 | + credentials=mock_creds, |
| 50 | + transport=mock_transport, |
| 51 | + client_options=None, |
| 52 | + client_info=None, |
| 53 | + ) |
| 54 | + |
| 55 | + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
| 56 | + def test_constructor_disables_directpath(self, mock_async_storage_client): |
| 57 | + from google.cloud.storage._experimental import async_grpc_client |
| 58 | + |
| 59 | + mock_transport_cls = mock.MagicMock() |
| 60 | + mock_async_storage_client.get_transport_class.return_value = mock_transport_cls |
| 61 | + mock_creds = _make_credentials() |
| 62 | + |
| 63 | + async_grpc_client.AsyncGrpcClient( |
| 64 | + credentials=mock_creds, attempt_direct_path=False |
| 65 | + ) |
| 66 | + |
| 67 | + mock_transport_cls.create_channel.assert_called_once_with( |
| 68 | + attempt_direct_path=False |
| 69 | + ) |
| 70 | + mock_channel = mock_transport_cls.create_channel.return_value |
| 71 | + mock_transport_cls.assert_called_once_with( |
| 72 | + credentials=mock_creds, channel=mock_channel |
| 73 | + ) |
| 74 | + |
| 75 | + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
| 76 | + def test_grpc_client_property(self, mock_async_storage_client): |
| 77 | + from google.cloud.storage._experimental import async_grpc_client |
| 78 | + |
| 79 | + mock_creds = _make_credentials() |
| 80 | + |
| 81 | + client = async_grpc_client.AsyncGrpcClient(credentials=mock_creds) |
| 82 | + |
| 83 | + retrieved_client = client.grpc_client |
| 84 | + |
| 85 | + self.assertIs(retrieved_client, mock_async_storage_client.return_value) |
0 commit comments