@@ -110,11 +110,14 @@ def _constructor_test_helper(
110110 @mock .patch ("warnings.warn" )
111111 def test_constructor_emulator_host_warning (self , mock_warn , mock_em ):
112112 from google .cloud .spanner_v1 import client as MUT
113+ from google .auth .credentials import AnonymousCredentials
113114
114- expected_scopes = ( MUT . SPANNER_ADMIN_SCOPE ,)
115+ expected_scopes = None
115116 creds = _make_credentials ()
116117 mock_em .return_value = "http://emulator.host.com"
117- self ._constructor_test_helper (expected_scopes , creds )
118+ with mock .patch ("google.cloud.spanner_v1.client.AnonymousCredentials" ) as patch :
119+ expected_creds = patch .return_value = AnonymousCredentials ()
120+ self ._constructor_test_helper (expected_scopes , creds , expected_creds )
118121 mock_warn .assert_called_once_with (MUT ._EMULATOR_HOST_HTTP_SCHEME )
119122
120123 def test_constructor_default_scopes (self ):
@@ -219,6 +222,8 @@ def test_constructor_custom_query_options_env_config(self, mock_ver):
219222 def test_instance_admin_api (self , mock_em ):
220223 from google .cloud .spanner_v1 .client import SPANNER_ADMIN_SCOPE
221224
225+ mock_em .return_value = None
226+
222227 credentials = _make_credentials ()
223228 client_info = mock .Mock ()
224229 client_options = mock .Mock ()
@@ -230,7 +235,6 @@ def test_instance_admin_api(self, mock_em):
230235 )
231236 expected_scopes = (SPANNER_ADMIN_SCOPE ,)
232237
233- mock_em .return_value = None
234238 inst_module = "google.cloud.spanner_v1.client.InstanceAdminClient"
235239 with mock .patch (inst_module ) as instance_admin_client :
236240 api = client .instance_admin_api
@@ -250,7 +254,8 @@ def test_instance_admin_api(self, mock_em):
250254 credentials .with_scopes .assert_called_once_with (expected_scopes )
251255
252256 @mock .patch ("google.cloud.spanner_v1.client._get_spanner_emulator_host" )
253- def test_instance_admin_api_emulator (self , mock_em ):
257+ def test_instance_admin_api_emulator_env (self , mock_em ):
258+ mock_em .return_value = "emulator.host"
254259 credentials = _make_credentials ()
255260 client_info = mock .Mock ()
256261 client_options = mock .Mock ()
@@ -261,7 +266,38 @@ def test_instance_admin_api_emulator(self, mock_em):
261266 client_options = client_options ,
262267 )
263268
264- mock_em .return_value = "true"
269+ inst_module = "google.cloud.spanner_v1.client.InstanceAdminClient"
270+ with mock .patch (inst_module ) as instance_admin_client :
271+ api = client .instance_admin_api
272+
273+ self .assertIs (api , instance_admin_client .return_value )
274+
275+ # API instance is cached
276+ again = client .instance_admin_api
277+ self .assertIs (again , api )
278+
279+ self .assertEqual (len (instance_admin_client .call_args_list ), 1 )
280+ called_args , called_kw = instance_admin_client .call_args
281+ self .assertEqual (called_args , ())
282+ self .assertEqual (called_kw ["client_info" ], client_info )
283+ self .assertEqual (called_kw ["client_options" ], client_options )
284+ self .assertIn ("transport" , called_kw )
285+ self .assertNotIn ("credentials" , called_kw )
286+
287+ def test_instance_admin_api_emulator_code (self ):
288+ from google .auth .credentials import AnonymousCredentials
289+ from google .api_core .client_options import ClientOptions
290+
291+ credentials = AnonymousCredentials ()
292+ client_info = mock .Mock ()
293+ client_options = ClientOptions (api_endpoint = "emulator.host" )
294+ client = self ._make_one (
295+ project = self .PROJECT ,
296+ credentials = credentials ,
297+ client_info = client_info ,
298+ client_options = client_options ,
299+ )
300+
265301 inst_module = "google.cloud.spanner_v1.client.InstanceAdminClient"
266302 with mock .patch (inst_module ) as instance_admin_client :
267303 api = client .instance_admin_api
@@ -284,6 +320,7 @@ def test_instance_admin_api_emulator(self, mock_em):
284320 def test_database_admin_api (self , mock_em ):
285321 from google .cloud .spanner_v1 .client import SPANNER_ADMIN_SCOPE
286322
323+ mock_em .return_value = None
287324 credentials = _make_credentials ()
288325 client_info = mock .Mock ()
289326 client_options = mock .Mock ()
@@ -295,7 +332,6 @@ def test_database_admin_api(self, mock_em):
295332 )
296333 expected_scopes = (SPANNER_ADMIN_SCOPE ,)
297334
298- mock_em .return_value = None
299335 db_module = "google.cloud.spanner_v1.client.DatabaseAdminClient"
300336 with mock .patch (db_module ) as database_admin_client :
301337 api = client .database_admin_api
@@ -315,7 +351,8 @@ def test_database_admin_api(self, mock_em):
315351 credentials .with_scopes .assert_called_once_with (expected_scopes )
316352
317353 @mock .patch ("google.cloud.spanner_v1.client._get_spanner_emulator_host" )
318- def test_database_admin_api_emulator (self , mock_em ):
354+ def test_database_admin_api_emulator_env (self , mock_em ):
355+ mock_em .return_value = "host:port"
319356 credentials = _make_credentials ()
320357 client_info = mock .Mock ()
321358 client_options = mock .Mock ()
@@ -326,7 +363,38 @@ def test_database_admin_api_emulator(self, mock_em):
326363 client_options = client_options ,
327364 )
328365
329- mock_em .return_value = "host:port"
366+ db_module = "google.cloud.spanner_v1.client.DatabaseAdminClient"
367+ with mock .patch (db_module ) as database_admin_client :
368+ api = client .database_admin_api
369+
370+ self .assertIs (api , database_admin_client .return_value )
371+
372+ # API instance is cached
373+ again = client .database_admin_api
374+ self .assertIs (again , api )
375+
376+ self .assertEqual (len (database_admin_client .call_args_list ), 1 )
377+ called_args , called_kw = database_admin_client .call_args
378+ self .assertEqual (called_args , ())
379+ self .assertEqual (called_kw ["client_info" ], client_info )
380+ self .assertEqual (called_kw ["client_options" ], client_options )
381+ self .assertIn ("transport" , called_kw )
382+ self .assertNotIn ("credentials" , called_kw )
383+
384+ def test_database_admin_api_emulator_code (self ):
385+ from google .auth .credentials import AnonymousCredentials
386+ from google .api_core .client_options import ClientOptions
387+
388+ credentials = AnonymousCredentials ()
389+ client_info = mock .Mock ()
390+ client_options = ClientOptions (api_endpoint = "emulator.host" )
391+ client = self ._make_one (
392+ project = self .PROJECT ,
393+ credentials = credentials ,
394+ client_info = client_info ,
395+ client_options = client_options ,
396+ )
397+
330398 db_module = "google.cloud.spanner_v1.client.DatabaseAdminClient"
331399 with mock .patch (db_module ) as database_admin_client :
332400 api = client .database_admin_api
0 commit comments