1919from google .auth .credentials import AnonymousCredentials
2020
2121from 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
3525class 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
0 commit comments