31
31
from google .cloud .sql .connector .instance import RefreshAheadCache
32
32
33
33
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
36
37
) -> None :
37
38
"""Test that calling connect() with different enable_iam_auth
38
- argument values throws error ."""
39
+ argument values creates two cache entries ."""
39
40
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
54
74
55
75
56
76
async def test_connect_incompatible_driver_error (
@@ -305,15 +325,15 @@ async def test_Connector_remove_cached_bad_instance(
305
325
conn_name = "bad-project:bad-region:bad-inst"
306
326
# populate cache
307
327
cache = RefreshAheadCache (conn_name , fake_client , connector ._keys )
308
- connector ._cache [conn_name ] = cache
328
+ connector ._cache [( conn_name , False ) ] = cache
309
329
# aiohttp client should throw a 404 ClientResponseError
310
330
with pytest .raises (ClientResponseError ):
311
331
await connector .connect_async (
312
332
conn_name ,
313
333
"pg8000" ,
314
334
)
315
335
# 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
317
337
318
338
319
339
async def test_Connector_remove_cached_no_ip_type (
@@ -331,7 +351,7 @@ async def test_Connector_remove_cached_no_ip_type(
331
351
conn_name = "test-project:test-region:test-instance"
332
352
# populate cache
333
353
cache = RefreshAheadCache (conn_name , fake_client , connector ._keys )
334
- connector ._cache [conn_name ] = cache
354
+ connector ._cache [( conn_name , False ) ] = cache
335
355
# test instance does not have Private IP, thus should invalidate cache
336
356
with pytest .raises (CloudSQLIPTypeError ):
337
357
await connector .connect_async (
@@ -342,7 +362,7 @@ async def test_Connector_remove_cached_no_ip_type(
342
362
ip_type = "private" ,
343
363
)
344
364
# 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
346
366
347
367
348
368
def test_default_universe_domain (fake_credentials : Credentials ) -> None :
0 commit comments