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

Commit 98061a5

Browse files
committed
feat: Multiplexed sessions - Add helpers for mock scoped credentials for testing.
Signed-off-by: Taylor Curran <taylor.curran@improving.com>
1 parent 03ec916 commit 98061a5

File tree

4 files changed

+29
-67
lines changed

4 files changed

+29
-67
lines changed

tests/unit/spanner_dbapi/test_connect.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@
1717
import unittest
1818
from unittest import mock
1919

20-
import google
2120
from google.auth.credentials import AnonymousCredentials
2221

22+
from tests._builders import build_scoped_credentials
23+
2324
INSTANCE = "test-instance"
2425
DATABASE = "test-database"
2526
PROJECT = "test-project"
2627
USER_AGENT = "user-agent"
2728

2829

29-
def _make_credentials():
30-
class _CredentialsWithScopes(
31-
google.auth.credentials.Credentials, google.auth.credentials.Scoped
32-
):
33-
pass
34-
35-
return mock.Mock(spec=_CredentialsWithScopes)
36-
37-
3830
@mock.patch("google.cloud.spanner_v1.Client")
3931
class Test_connect(unittest.TestCase):
4032
def test_w_implicit(self, mock_client):
@@ -79,7 +71,7 @@ def test_w_explicit(self, mock_client):
7971
from google.cloud.spanner_dbapi import Connection
8072
from google.cloud.spanner_dbapi.version import PY_VERSION
8173

82-
credentials = _make_credentials()
74+
credentials = build_scoped_credentials()
8375
pool = mock.create_autospec(AbstractSessionPool)
8476
client = mock_client.return_value
8577
instance = client.instance.return_value

tests/unit/spanner_dbapi/test_connection.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@
4646
USER_AGENT = "user-agent"
4747

4848

49-
def _make_credentials():
50-
from google.auth import credentials
51-
52-
class _CredentialsWithScopes(credentials.Credentials, credentials.Scoped):
53-
pass
54-
55-
return mock.Mock(spec=_CredentialsWithScopes)
56-
57-
5849
class TestConnection(unittest.TestCase):
5950
def setUp(self):
6051
self._under_test = self._make_connection()

tests/unit/test_client.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,7 @@
1919
from google.auth.credentials import AnonymousCredentials
2020

2121
from google.cloud.spanner_v1 import DirectedReadOptions, DefaultTransactionOptions
22-
23-
24-
def _make_credentials():
25-
import google.auth.credentials
26-
27-
class _CredentialsWithScopes(
28-
google.auth.credentials.Credentials, google.auth.credentials.Scoped
29-
):
30-
pass
31-
32-
return mock.Mock(spec=_CredentialsWithScopes)
22+
from tests._builders import build_scoped_credentials
3323

3424

3525
class TestClient(unittest.TestCase):
@@ -148,7 +138,7 @@ def test_constructor_emulator_host_warning(self, mock_warn, mock_em):
148138
from google.auth.credentials import AnonymousCredentials
149139

150140
expected_scopes = None
151-
creds = _make_credentials()
141+
creds = build_scoped_credentials()
152142
mock_em.return_value = "http://emulator.host.com"
153143
with mock.patch("google.cloud.spanner_v1.client.AnonymousCredentials") as patch:
154144
expected_creds = patch.return_value = AnonymousCredentials()
@@ -159,23 +149,23 @@ def test_constructor_default_scopes(self):
159149
from google.cloud.spanner_v1 import client as MUT
160150

161151
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
162-
creds = _make_credentials()
152+
creds = build_scoped_credentials()
163153
self._constructor_test_helper(expected_scopes, creds)
164154

165155
def test_constructor_custom_client_info(self):
166156
from google.cloud.spanner_v1 import client as MUT
167157

168158
client_info = mock.Mock()
169159
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
170-
creds = _make_credentials()
160+
creds = build_scoped_credentials()
171161
self._constructor_test_helper(expected_scopes, creds, client_info=client_info)
172162

173163
# Disable metrics to avoid google.auth.default calls from Metric Exporter
174164
@mock.patch.dict(os.environ, {"SPANNER_ENABLE_BUILTIN_METRICS": ""})
175165
def test_constructor_implicit_credentials(self):
176166
from google.cloud.spanner_v1 import client as MUT
177167

178-
creds = _make_credentials()
168+
creds = build_scoped_credentials()
179169

180170
patch = mock.patch("google.auth.default", return_value=(creds, None))
181171
with patch as default:
@@ -186,7 +176,7 @@ def test_constructor_implicit_credentials(self):
186176
default.assert_called_once_with(scopes=(MUT.SPANNER_ADMIN_SCOPE,))
187177

188178
def test_constructor_credentials_wo_create_scoped(self):
189-
creds = _make_credentials()
179+
creds = build_scoped_credentials()
190180
expected_scopes = None
191181
self._constructor_test_helper(expected_scopes, creds)
192182

@@ -195,7 +185,7 @@ def test_constructor_custom_client_options_obj(self):
195185
from google.cloud.spanner_v1 import client as MUT
196186

197187
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
198-
creds = _make_credentials()
188+
creds = build_scoped_credentials()
199189
self._constructor_test_helper(
200190
expected_scopes,
201191
creds,
@@ -206,7 +196,7 @@ def test_constructor_custom_client_options_dict(self):
206196
from google.cloud.spanner_v1 import client as MUT
207197

208198
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
209-
creds = _make_credentials()
199+
creds = build_scoped_credentials()
210200
self._constructor_test_helper(
211201
expected_scopes, creds, client_options={"api_endpoint": "endpoint"}
212202
)
@@ -216,7 +206,7 @@ def test_constructor_custom_query_options_client_config(self):
216206
from google.cloud.spanner_v1 import client as MUT
217207

218208
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
219-
creds = _make_credentials()
209+
creds = build_scoped_credentials()
220210
query_options = expected_query_options = ExecuteSqlRequest.QueryOptions(
221211
optimizer_version="1",
222212
optimizer_statistics_package="auto_20191128_14_47_22UTC",
@@ -237,7 +227,7 @@ def test_constructor_custom_query_options_env_config(self, mock_ver, mock_stats)
237227
from google.cloud.spanner_v1 import client as MUT
238228

239229
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
240-
creds = _make_credentials()
230+
creds = build_scoped_credentials()
241231
mock_ver.return_value = "2"
242232
mock_stats.return_value = "auto_20191128_14_47_22UTC"
243233
query_options = ExecuteSqlRequest.QueryOptions(
@@ -259,7 +249,7 @@ def test_constructor_w_directed_read_options(self):
259249
from google.cloud.spanner_v1 import client as MUT
260250

261251
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
262-
creds = _make_credentials()
252+
creds = build_scoped_credentials()
263253
self._constructor_test_helper(
264254
expected_scopes, creds, directed_read_options=self.DIRECTED_READ_OPTIONS
265255
)
@@ -268,7 +258,7 @@ def test_constructor_route_to_leader_disbled(self):
268258
from google.cloud.spanner_v1 import client as MUT
269259

270260
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
271-
creds = _make_credentials()
261+
creds = build_scoped_credentials()
272262
self._constructor_test_helper(
273263
expected_scopes, creds, route_to_leader_enabled=False
274264
)
@@ -277,7 +267,7 @@ def test_constructor_w_default_transaction_options(self):
277267
from google.cloud.spanner_v1 import client as MUT
278268

279269
expected_scopes = (MUT.SPANNER_ADMIN_SCOPE,)
280-
creds = _make_credentials()
270+
creds = build_scoped_credentials()
281271
self._constructor_test_helper(
282272
expected_scopes,
283273
creds,
@@ -291,7 +281,7 @@ def test_instance_admin_api(self, mock_em):
291281

292282
mock_em.return_value = None
293283

294-
credentials = _make_credentials()
284+
credentials = build_scoped_credentials()
295285
client_info = mock.Mock()
296286
client_options = ClientOptions(quota_project_id="QUOTA-PROJECT")
297287
client = self._make_one(
@@ -325,7 +315,7 @@ def test_instance_admin_api_emulator_env(self, mock_em):
325315
from google.api_core.client_options import ClientOptions
326316

327317
mock_em.return_value = "emulator.host"
328-
credentials = _make_credentials()
318+
credentials = build_scoped_credentials()
329319
client_info = mock.Mock()
330320
client_options = ClientOptions(api_endpoint="endpoint")
331321
client = self._make_one(
@@ -391,7 +381,7 @@ def test_database_admin_api(self, mock_em):
391381
from google.api_core.client_options import ClientOptions
392382

393383
mock_em.return_value = None
394-
credentials = _make_credentials()
384+
credentials = build_scoped_credentials()
395385
client_info = mock.Mock()
396386
client_options = ClientOptions(quota_project_id="QUOTA-PROJECT")
397387
client = self._make_one(
@@ -425,7 +415,7 @@ def test_database_admin_api_emulator_env(self, mock_em):
425415
from google.api_core.client_options import ClientOptions
426416

427417
mock_em.return_value = "host:port"
428-
credentials = _make_credentials()
418+
credentials = build_scoped_credentials()
429419
client_info = mock.Mock()
430420
client_options = ClientOptions(api_endpoint="endpoint")
431421
client = self._make_one(
@@ -486,7 +476,7 @@ def test_database_admin_api_emulator_code(self):
486476
self.assertNotIn("credentials", called_kw)
487477

488478
def test_copy(self):
489-
credentials = _make_credentials()
479+
credentials = build_scoped_credentials()
490480
# Make sure it "already" is scoped.
491481
credentials.requires_scopes = False
492482

@@ -497,12 +487,12 @@ def test_copy(self):
497487
self.assertEqual(new_client.project, client.project)
498488

499489
def test_credentials_property(self):
500-
credentials = _make_credentials()
490+
credentials = build_scoped_credentials()
501491
client = self._make_one(project=self.PROJECT, credentials=credentials)
502492
self.assertIs(client.credentials, credentials.with_scopes.return_value)
503493

504494
def test_project_name_property(self):
505-
credentials = _make_credentials()
495+
credentials = build_scoped_credentials()
506496
client = self._make_one(project=self.PROJECT, credentials=credentials)
507497
project_name = "projects/" + self.PROJECT
508498
self.assertEqual(client.project_name, project_name)
@@ -516,7 +506,7 @@ def test_list_instance_configs(self):
516506
from google.cloud.spanner_admin_instance_v1 import ListInstanceConfigsResponse
517507

518508
api = InstanceAdminClient(credentials=AnonymousCredentials())
519-
credentials = _make_credentials()
509+
credentials = build_scoped_credentials()
520510
client = self._make_one(project=self.PROJECT, credentials=credentials)
521511
client._instance_admin_api = api
522512

@@ -562,7 +552,7 @@ def test_list_instance_configs_w_options(self):
562552
from google.cloud.spanner_admin_instance_v1 import ListInstanceConfigsRequest
563553
from google.cloud.spanner_admin_instance_v1 import ListInstanceConfigsResponse
564554

565-
credentials = _make_credentials()
555+
credentials = build_scoped_credentials()
566556
api = InstanceAdminClient(credentials=credentials)
567557
client = self._make_one(project=self.PROJECT, credentials=credentials)
568558
client._instance_admin_api = api
@@ -597,7 +587,7 @@ def test_instance_factory_defaults(self):
597587
from google.cloud.spanner_v1.instance import DEFAULT_NODE_COUNT
598588
from google.cloud.spanner_v1.instance import Instance
599589

600-
credentials = _make_credentials()
590+
credentials = build_scoped_credentials()
601591
client = self._make_one(project=self.PROJECT, credentials=credentials)
602592

603593
instance = client.instance(self.INSTANCE_ID)
@@ -613,7 +603,7 @@ def test_instance_factory_defaults(self):
613603
def test_instance_factory_explicit(self):
614604
from google.cloud.spanner_v1.instance import Instance
615605

616-
credentials = _make_credentials()
606+
credentials = build_scoped_credentials()
617607
client = self._make_one(project=self.PROJECT, credentials=credentials)
618608

619609
instance = client.instance(
@@ -638,7 +628,7 @@ def test_list_instances(self):
638628
from google.cloud.spanner_admin_instance_v1 import ListInstancesRequest
639629
from google.cloud.spanner_admin_instance_v1 import ListInstancesResponse
640630

641-
credentials = _make_credentials()
631+
credentials = build_scoped_credentials()
642632
api = InstanceAdminClient(credentials=credentials)
643633
client = self._make_one(project=self.PROJECT, credentials=credentials)
644634
client._instance_admin_api = api
@@ -686,7 +676,7 @@ def test_list_instances_w_options(self):
686676
from google.cloud.spanner_admin_instance_v1 import ListInstancesRequest
687677
from google.cloud.spanner_admin_instance_v1 import ListInstancesResponse
688678

689-
credentials = _make_credentials()
679+
credentials = build_scoped_credentials()
690680
api = InstanceAdminClient(credentials=credentials)
691681
client = self._make_one(project=self.PROJECT, credentials=credentials)
692682
client._instance_admin_api = api

tests/unit/test_database.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@
6363
}
6464

6565

66-
def _make_credentials(): # pragma: NO COVER
67-
import google.auth.credentials
68-
69-
class _CredentialsWithScopes(
70-
google.auth.credentials.Credentials, google.auth.credentials.Scoped
71-
):
72-
pass
73-
74-
return mock.Mock(spec=_CredentialsWithScopes)
75-
76-
7766
class _BaseTest(unittest.TestCase):
7867
PROJECT_ID = "project-id"
7968
PARENT = "projects/" + PROJECT_ID

0 commit comments

Comments
 (0)