3131from google .cloud .sql .connector .instance import RefreshAheadCache
3232
3333
34- def test_connect_enable_iam_auth_error (
35- fake_credentials : Credentials , cache : RefreshAheadCache
34+ @pytest .mark .asyncio
35+ async def test_connect_enable_iam_auth_error (
36+ fake_credentials : Credentials , fake_client : CloudSQLClient
3637) -> None :
3738 """Test that calling connect() with different enable_iam_auth
38- argument values throws error ."""
39+ argument values creates two cache entries ."""
3940 connect_string = "test-project:test-region:test-instance"
40- connector = Connector (credentials = fake_credentials )
41- # set cache
42- connector ._cache [connect_string ] = cache
43- # try to connect using enable_iam_auth=True, should raise error
44- with pytest .raises (ValueError ) as exc_info :
45- connector .connect (connect_string , "pg8000" , enable_iam_auth = True )
46- assert (
47- exc_info .value .args [0 ] == "connect() called with 'enable_iam_auth=True', "
48- "but previously used 'enable_iam_auth=False'. "
49- "If you require both for your use case, please use a new "
50- "connector.Connector object."
51- )
52- # remove cache entry to avoid destructor warnings
53- connector ._cache = {}
41+ async with Connector (
42+ credentials = fake_credentials , loop = asyncio .get_running_loop ()
43+ ) as connector :
44+ connector ._client = fake_client
45+ # patch db connection creation
46+ with patch ("google.cloud.sql.connector.asyncpg.connect" ) as mock_connect :
47+ mock_connect .return_value = True
48+ # connect with enable_iam_auth False
49+ connection = await connector .connect_async (
50+ connect_string ,
51+ "asyncpg" ,
52+ user = "my-user" ,
53+ password = "my-pass" ,
54+ db = "my-db" ,
55+ enable_iam_auth = False ,
56+ )
57+ # verify connector made connection call
58+ assert connection is True
59+ # connect with enable_iam_auth True
60+ connection = await connector .connect_async (
61+ connect_string ,
62+ "asyncpg" ,
63+ user = "my-user" ,
64+ password = "my-pass" ,
65+ db = "my-db" ,
66+ enable_iam_auth = True ,
67+ )
68+ # verify connector made connection call
69+ assert connection is True
70+ # verify both cache entries for same instance exist
71+ assert len (connector ._cache ) == 2
72+ assert (connect_string , True ) in connector ._cache
73+ assert (connect_string , False ) in connector ._cache
5474
5575
5676async def test_connect_incompatible_driver_error (
@@ -305,15 +325,15 @@ async def test_Connector_remove_cached_bad_instance(
305325 conn_name = "bad-project:bad-region:bad-inst"
306326 # populate cache
307327 cache = RefreshAheadCache (conn_name , fake_client , connector ._keys )
308- connector ._cache [conn_name ] = cache
328+ connector ._cache [( conn_name , False ) ] = cache
309329 # aiohttp client should throw a 404 ClientResponseError
310330 with pytest .raises (ClientResponseError ):
311331 await connector .connect_async (
312332 conn_name ,
313333 "pg8000" ,
314334 )
315335 # check that cache has been removed from dict
316- assert conn_name not in connector ._cache
336+ assert ( conn_name , False ) not in connector ._cache
317337
318338
319339async def test_Connector_remove_cached_no_ip_type (
@@ -331,7 +351,7 @@ async def test_Connector_remove_cached_no_ip_type(
331351 conn_name = "test-project:test-region:test-instance"
332352 # populate cache
333353 cache = RefreshAheadCache (conn_name , fake_client , connector ._keys )
334- connector ._cache [conn_name ] = cache
354+ connector ._cache [( conn_name , False ) ] = cache
335355 # test instance does not have Private IP, thus should invalidate cache
336356 with pytest .raises (CloudSQLIPTypeError ):
337357 await connector .connect_async (
@@ -342,7 +362,7 @@ async def test_Connector_remove_cached_no_ip_type(
342362 ip_type = "private" ,
343363 )
344364 # check that cache has been removed from dict
345- assert conn_name not in connector ._cache
365+ assert ( conn_name , False ) not in connector ._cache
346366
347367
348368def test_default_universe_domain (fake_credentials : Credentials ) -> None :
0 commit comments