|
16 | 16 | from unittest import mock |
17 | 17 | from google.auth import credentials as auth_credentials |
18 | 18 | from google.auth.credentials import AnonymousCredentials |
| 19 | +from google.api_core import client_info as client_info_lib |
| 20 | +from google.cloud.storage._experimental.asyncio import async_grpc_client |
| 21 | +from google.cloud.storage._experimental.asyncio.async_grpc_client import ( |
| 22 | + DEFAULT_CLIENT_INFO, |
| 23 | +) |
19 | 24 |
|
20 | 25 |
|
21 | 26 | def _make_credentials(spec=None): |
@@ -27,32 +32,60 @@ def _make_credentials(spec=None): |
27 | 32 | class TestAsyncGrpcClient(unittest.TestCase): |
28 | 33 | @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
29 | 34 | def test_constructor_default_options(self, mock_async_storage_client): |
30 | | - from google.cloud.storage._experimental.asyncio import async_grpc_client |
31 | | - |
| 35 | + # Arrange |
32 | 36 | mock_transport_cls = mock.MagicMock() |
33 | 37 | mock_async_storage_client.get_transport_class.return_value = mock_transport_cls |
34 | 38 | mock_creds = _make_credentials() |
35 | 39 |
|
| 40 | + primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent() |
| 41 | + expected_options = (("grpc.primary_user_agent", primary_user_agent),) |
| 42 | + |
| 43 | + # Act |
36 | 44 | async_grpc_client.AsyncGrpcClient(credentials=mock_creds) |
37 | 45 |
|
| 46 | + # Assert |
38 | 47 | mock_async_storage_client.get_transport_class.assert_called_once_with( |
39 | 48 | "grpc_asyncio" |
40 | 49 | ) |
41 | 50 | mock_transport_cls.create_channel.assert_called_once_with( |
42 | | - attempt_direct_path=True, credentials=mock_creds |
| 51 | + attempt_direct_path=True, |
| 52 | + credentials=mock_creds, |
| 53 | + options=expected_options, |
43 | 54 | ) |
44 | 55 | mock_channel = mock_transport_cls.create_channel.return_value |
45 | 56 | mock_transport_cls.assert_called_once_with(channel=mock_channel) |
46 | 57 | mock_transport = mock_transport_cls.return_value |
47 | 58 | mock_async_storage_client.assert_called_once_with( |
48 | 59 | transport=mock_transport, |
49 | 60 | client_options=None, |
50 | | - client_info=None, |
| 61 | + client_info=DEFAULT_CLIENT_INFO, |
| 62 | + ) |
| 63 | + |
| 64 | + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
| 65 | + def test_constructor_with_client_info(self, mock_async_storage_client): |
| 66 | + |
| 67 | + mock_transport_cls = mock.MagicMock() |
| 68 | + mock_async_storage_client.get_transport_class.return_value = mock_transport_cls |
| 69 | + mock_creds = _make_credentials() |
| 70 | + client_info = client_info_lib.ClientInfo( |
| 71 | + client_library_version="1.2.3", |
| 72 | + ) |
| 73 | + |
| 74 | + async_grpc_client.AsyncGrpcClient( |
| 75 | + credentials=mock_creds, client_info=client_info |
| 76 | + ) |
| 77 | + |
| 78 | + primary_user_agent = client_info.to_user_agent() |
| 79 | + expected_options = (("grpc.primary_user_agent", primary_user_agent),) |
| 80 | + |
| 81 | + mock_transport_cls.create_channel.assert_called_once_with( |
| 82 | + attempt_direct_path=True, |
| 83 | + credentials=mock_creds, |
| 84 | + options=expected_options, |
51 | 85 | ) |
52 | 86 |
|
53 | 87 | @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
54 | 88 | def test_constructor_disables_directpath(self, mock_async_storage_client): |
55 | | - from google.cloud.storage._experimental.asyncio import async_grpc_client |
56 | 89 |
|
57 | 90 | mock_transport_cls = mock.MagicMock() |
58 | 91 | mock_async_storage_client.get_transport_class.return_value = mock_transport_cls |
@@ -62,15 +95,19 @@ def test_constructor_disables_directpath(self, mock_async_storage_client): |
62 | 95 | credentials=mock_creds, attempt_direct_path=False |
63 | 96 | ) |
64 | 97 |
|
| 98 | + primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent() |
| 99 | + expected_options = (("grpc.primary_user_agent", primary_user_agent),) |
| 100 | + |
65 | 101 | mock_transport_cls.create_channel.assert_called_once_with( |
66 | | - attempt_direct_path=False, credentials=mock_creds |
| 102 | + attempt_direct_path=False, |
| 103 | + credentials=mock_creds, |
| 104 | + options=expected_options, |
67 | 105 | ) |
68 | 106 | mock_channel = mock_transport_cls.create_channel.return_value |
69 | 107 | mock_transport_cls.assert_called_once_with(channel=mock_channel) |
70 | 108 |
|
71 | 109 | @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
72 | 110 | def test_grpc_client_property(self, mock_grpc_gapic_client): |
73 | | - from google.cloud.storage._experimental.asyncio import async_grpc_client |
74 | 111 |
|
75 | 112 | # Arrange |
76 | 113 | mock_transport_cls = mock.MagicMock() |
@@ -81,7 +118,8 @@ def test_grpc_client_property(self, mock_grpc_gapic_client): |
81 | 118 | mock_transport_cls.return_value = mock.sentinel.transport |
82 | 119 |
|
83 | 120 | mock_creds = _make_credentials() |
84 | | - mock_client_info = mock.sentinel.client_info |
| 121 | + mock_client_info = mock.MagicMock(spec=client_info_lib.ClientInfo) |
| 122 | + mock_client_info.to_user_agent.return_value = "test-user-agent" |
85 | 123 | mock_client_options = mock.sentinel.client_options |
86 | 124 | mock_attempt_direct_path = mock.sentinel.attempt_direct_path |
87 | 125 |
|
@@ -102,8 +140,11 @@ def test_grpc_client_property(self, mock_grpc_gapic_client): |
102 | 140 | retrieved_client = client.grpc_client |
103 | 141 |
|
104 | 142 | # Assert |
| 143 | + expected_options = (("grpc.primary_user_agent", "test-user-agent"),) |
105 | 144 | mock_transport_cls.create_channel.assert_called_once_with( |
106 | | - attempt_direct_path=mock_attempt_direct_path, credentials=mock_creds |
| 145 | + attempt_direct_path=mock_attempt_direct_path, |
| 146 | + credentials=mock_creds, |
| 147 | + options=expected_options, |
107 | 148 | ) |
108 | 149 | mock_transport_cls.assere_with(channel=channel_sentinel) |
109 | 150 | mock_grpc_gapic_client.assert_called_once_with( |
@@ -115,7 +156,6 @@ def test_grpc_client_property(self, mock_grpc_gapic_client): |
115 | 156 |
|
116 | 157 | @mock.patch("google.cloud._storage_v2.StorageAsyncClient") |
117 | 158 | def test_grpc_client_with_anon_creds(self, mock_grpc_gapic_client): |
118 | | - from google.cloud.storage._experimental.asyncio import async_grpc_client |
119 | 159 |
|
120 | 160 | # Arrange |
121 | 161 | mock_transport_cls = mock.MagicMock() |
@@ -133,7 +173,12 @@ def test_grpc_client_with_anon_creds(self, mock_grpc_gapic_client): |
133 | 173 | # Assert |
134 | 174 | self.assertIs(retrieved_client, mock_grpc_gapic_client.return_value) |
135 | 175 |
|
| 176 | + primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent() |
| 177 | + expected_options = (("grpc.primary_user_agent", primary_user_agent),) |
| 178 | + |
136 | 179 | mock_transport_cls.create_channel.assert_called_once_with( |
137 | | - attempt_direct_path=True, credentials=anonymous_creds |
| 180 | + attempt_direct_path=True, |
| 181 | + credentials=anonymous_creds, |
| 182 | + options=expected_options, |
138 | 183 | ) |
139 | 184 | mock_transport_cls.assert_called_once_with(channel=channel_sentinel) |