From eba2228cf419827ca62162ab9be4d5c89f2dd0c2 Mon Sep 17 00:00:00 2001 From: kgala2 Date: Fri, 28 Mar 2025 10:50:17 -0700 Subject: [PATCH 1/7] feat: update ip type for connectors --- tests/system/test_asyncpg_connection.py | 8 ++++++-- tests/system/test_asyncpg_iam_auth.py | 4 +++- tests/system/test_pg8000_connection.py | 4 +++- tests/system/test_pg8000_iam_auth.py | 4 +++- tests/system/test_pymysql_connection.py | 4 +++- tests/system/test_pymysql_iam_auth.py | 4 +++- tests/system/test_pytds_connection.py | 4 +++- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index e64bbc90..48eb40be 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -87,7 +87,9 @@ async def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" ), execution_options={"isolation_level": "AUTOCOMMIT"}, ) @@ -145,7 +147,9 @@ async def getconn( user=user, password=password, db=db, - ip_type="public", # can also be "private" or "psc", + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc", **kwargs, ) return conn diff --git a/tests/system/test_asyncpg_iam_auth.py b/tests/system/test_asyncpg_iam_auth.py index ddf6b5e6..803796e9 100644 --- a/tests/system/test_asyncpg_iam_auth.py +++ b/tests/system/test_asyncpg_iam_auth.py @@ -71,7 +71,9 @@ async def create_sqlalchemy_engine( "asyncpg", user=user, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" enable_iam_auth=True, ), execution_options={"isolation_level": "AUTOCOMMIT"}, diff --git a/tests/system/test_pg8000_connection.py b/tests/system/test_pg8000_connection.py index f5d161cc..d6c72d71 100644 --- a/tests/system/test_pg8000_connection.py +++ b/tests/system/test_pg8000_connection.py @@ -85,7 +85,9 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" ), ) return engine, connector diff --git a/tests/system/test_pg8000_iam_auth.py b/tests/system/test_pg8000_iam_auth.py index 902d9eb9..9734ed57 100644 --- a/tests/system/test_pg8000_iam_auth.py +++ b/tests/system/test_pg8000_iam_auth.py @@ -70,7 +70,9 @@ def create_sqlalchemy_engine( "pg8000", user=user, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" enable_iam_auth=True, ), ) diff --git a/tests/system/test_pymysql_connection.py b/tests/system/test_pymysql_connection.py index 7d7edadc..5dcade0d 100644 --- a/tests/system/test_pymysql_connection.py +++ b/tests/system/test_pymysql_connection.py @@ -75,7 +75,9 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" ), ) return engine, connector diff --git a/tests/system/test_pymysql_iam_auth.py b/tests/system/test_pymysql_iam_auth.py index 56e26d2b..3b289241 100644 --- a/tests/system/test_pymysql_iam_auth.py +++ b/tests/system/test_pymysql_iam_auth.py @@ -70,7 +70,9 @@ def create_sqlalchemy_engine( "pymysql", user=user, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" enable_iam_auth=True, ), ) diff --git a/tests/system/test_pytds_connection.py b/tests/system/test_pytds_connection.py index a75b3da4..ba93d7b9 100644 --- a/tests/system/test_pytds_connection.py +++ b/tests/system/test_pytds_connection.py @@ -73,7 +73,9 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type="public", # can also be "private" or "psc" + ip_type=os.environ.get( + "IP_TYPE", "public" + ), # can also be "private" or "psc" ), ) return engine, connector From 6f06ebf0de774c1ac9bbf969ed6c409026dd9a7e Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 11:39:13 -0700 Subject: [PATCH 2/7] chore: update ip_type parameter in system tests --- tests/system/test_asyncpg_connection.py | 29 +++++++++++++++---------- tests/system/test_asyncpg_iam_auth.py | 13 ++++++----- tests/system/test_connector_object.py | 3 +++ tests/system/test_pg8000_connection.py | 22 ++++++++++++------- tests/system/test_pg8000_iam_auth.py | 13 ++++++----- tests/system/test_pymysql_connection.py | 13 ++++++----- tests/system/test_pymysql_iam_auth.py | 13 ++++++----- tests/system/test_pytds_connection.py | 13 ++++++----- 8 files changed, 75 insertions(+), 44 deletions(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index 48eb40be..eba8f87d 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -32,6 +32,7 @@ async def create_sqlalchemy_engine( user: str, password: str, db: str, + ip_type: str, refresh_strategy: str = "background", resolver: Union[type[DefaultResolver], type[DnsResolver]] = DefaultResolver, ) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]: @@ -63,6 +64,8 @@ async def create_sqlalchemy_engine( The database user's password, e.g., secret-password db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -87,9 +90,7 @@ async def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, ), execution_options={"isolation_level": "AUTOCOMMIT"}, ) @@ -101,6 +102,7 @@ async def create_asyncpg_pool( user: str, password: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[asyncpg.Pool, Connector]: """Creates a native asyncpg connection pool for a Cloud SQL instance and @@ -130,6 +132,8 @@ async def create_asyncpg_pool( The database user's password, e.g., secret-password db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -147,9 +151,7 @@ async def getconn( user=user, password=password, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc", + ip_type=ip_type **kwargs, ) return conn @@ -165,8 +167,9 @@ async def test_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, password, db) + pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) async with pool.connect() as conn: res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() @@ -181,9 +184,10 @@ async def test_lazy_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_sqlalchemy_engine( - inst_conn_name, user, password, db, "lazy" + inst_conn_name, user, password, db, ip_type, "lazy" ) async with pool.connect() as conn: @@ -199,9 +203,10 @@ async def test_custom_SAN_with_dns_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_sqlalchemy_engine( - inst_conn_name, user, password, db, resolver=DnsResolver + inst_conn_name, user, password, db, ip_type,resolver=DnsResolver ) async with pool.connect() as conn: @@ -217,8 +222,9 @@ async def test_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_asyncpg_pool(inst_conn_name, user, password, db) + pool, connector = await create_asyncpg_pool(inst_conn_name, user, password, db, ip_type) async with pool.acquire() as conn: res = await conn.fetch("SELECT 1") @@ -233,9 +239,10 @@ async def test_lazy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_asyncpg_pool( - inst_conn_name, user, password, db, "lazy" + inst_conn_name, user, password, db, ip_type, "lazy" ) async with pool.acquire() as conn: diff --git a/tests/system/test_asyncpg_iam_auth.py b/tests/system/test_asyncpg_iam_auth.py index 803796e9..5687dda3 100644 --- a/tests/system/test_asyncpg_iam_auth.py +++ b/tests/system/test_asyncpg_iam_auth.py @@ -27,6 +27,7 @@ async def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -55,6 +56,8 @@ async def create_sqlalchemy_engine( e.g., my-email@test.com, service-account@project-id.iam db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -71,9 +74,7 @@ async def create_sqlalchemy_engine( "asyncpg", user=user, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, enable_iam_auth=True, ), execution_options={"isolation_level": "AUTOCOMMIT"}, @@ -86,8 +87,9 @@ async def test_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db) + pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) async with pool.connect() as conn: res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() @@ -101,8 +103,9 @@ async def test_lazy_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, "lazy") + pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") async with pool.connect() as conn: res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() diff --git a/tests/system/test_connector_object.py b/tests/system/test_connector_object.py index 258b80aa..9a8c44b0 100644 --- a/tests/system/test_connector_object.py +++ b/tests/system/test_connector_object.py @@ -39,6 +39,9 @@ def getconn() -> pymysql.connections.Connection: user=os.environ["MYSQL_USER"], password=os.environ["MYSQL_PASS"], db=os.environ["MYSQL_DB"], + ip_type=os.environ.get( + "IP_TYPE", "public" + ), ) return conn diff --git a/tests/system/test_pg8000_connection.py b/tests/system/test_pg8000_connection.py index d6c72d71..906b21e7 100644 --- a/tests/system/test_pg8000_connection.py +++ b/tests/system/test_pg8000_connection.py @@ -32,6 +32,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, + ip_type: str, refresh_strategy: str = "background", resolver: Union[type[DefaultResolver], type[DnsResolver]] = DefaultResolver, ) -> tuple[sqlalchemy.engine.Engine, Connector]: @@ -64,6 +65,8 @@ def create_sqlalchemy_engine( The database user's password, e.g., secret-password db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -85,9 +88,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, ), ) return engine, connector @@ -102,8 +103,9 @@ def test_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -118,9 +120,10 @@ def test_lazy_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( - inst_conn_name, user, password, db, "lazy" + inst_conn_name, user, password, db, ip_type, "lazy" ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() @@ -136,8 +139,9 @@ def test_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CAS_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -152,8 +156,9 @@ def test_customer_managed_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -168,9 +173,10 @@ def test_custom_SAN_with_dns_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( - inst_conn_name, user, password, db, resolver=DnsResolver + inst_conn_name, user, password, db, ip_type, resolver=DnsResolver ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() diff --git a/tests/system/test_pg8000_iam_auth.py b/tests/system/test_pg8000_iam_auth.py index 9734ed57..e276f63c 100644 --- a/tests/system/test_pg8000_iam_auth.py +++ b/tests/system/test_pg8000_iam_auth.py @@ -26,6 +26,7 @@ def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -55,6 +56,8 @@ def create_sqlalchemy_engine( e.g., my-email@test.com, service-account@project-id.iam db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -70,9 +73,7 @@ def create_sqlalchemy_engine( "pg8000", user=user, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, enable_iam_auth=True, ), ) @@ -84,8 +85,9 @@ def test_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -99,8 +101,9 @@ def test_lazy_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, "lazy") + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() diff --git a/tests/system/test_pymysql_connection.py b/tests/system/test_pymysql_connection.py index 5dcade0d..a6a8ea1f 100644 --- a/tests/system/test_pymysql_connection.py +++ b/tests/system/test_pymysql_connection.py @@ -28,6 +28,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -59,6 +60,8 @@ def create_sqlalchemy_engine( The database user's password, e.g., secret-password db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -75,9 +78,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, ), ) return engine, connector @@ -92,8 +93,9 @@ def test_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -108,9 +110,10 @@ def test_lazy_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( - inst_conn_name, user, password, db, "lazy" + inst_conn_name, user, password, db, ip_type, "lazy" ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() diff --git a/tests/system/test_pymysql_iam_auth.py b/tests/system/test_pymysql_iam_auth.py index 3b289241..8cc9a0cc 100644 --- a/tests/system/test_pymysql_iam_auth.py +++ b/tests/system/test_pymysql_iam_auth.py @@ -26,6 +26,7 @@ def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -55,6 +56,8 @@ def create_sqlalchemy_engine( e.g., my-email@test.com -> my-email db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -70,9 +73,7 @@ def create_sqlalchemy_engine( "pymysql", user=user, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, enable_iam_auth=True, ), ) @@ -84,8 +85,9 @@ def test_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -99,8 +101,9 @@ def test_lazy_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, "lazy") + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() diff --git a/tests/system/test_pytds_connection.py b/tests/system/test_pytds_connection.py index ba93d7b9..f43888d9 100644 --- a/tests/system/test_pytds_connection.py +++ b/tests/system/test_pytds_connection.py @@ -27,6 +27,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, + ip_type: str, refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -57,6 +58,8 @@ def create_sqlalchemy_engine( The database user's password, e.g., secret-password db (str): The name of the database, e.g., mydb + ip_type (str): + The IP type of the Cloud SQL instance. refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -73,9 +76,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=os.environ.get( - "IP_TYPE", "public" - ), # can also be "private" or "psc" + ip_type=ip_type, # can also be "private" or "psc" ), ) return engine, connector @@ -90,8 +91,9 @@ def test_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db) + engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) with engine.connect() as conn: res = conn.execute(sqlalchemy.text("SELECT 1")).fetchone() conn.commit() @@ -105,9 +107,10 @@ def test_lazy_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( - inst_conn_name, user, password, db, "lazy" + inst_conn_name, user, password, db, ip_type, "lazy" ) with engine.connect() as conn: res = conn.execute(sqlalchemy.text("SELECT 1")).fetchone() From 6d60f5adddc454d9c4b750d1fe232ba641a69327 Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 11:50:03 -0700 Subject: [PATCH 3/7] chore: update test_async_conn ip_type param --- tests/system/test_asyncpg_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index eba8f87d..8d4a9bf9 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -151,7 +151,7 @@ async def getconn( user=user, password=password, db=db, - ip_type=ip_type + ip_type=ip_type, **kwargs, ) return conn From 8d614b962e0fada284963c9df88831ebe5acd99e Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 11:56:05 -0700 Subject: [PATCH 4/7] chore: update lint for all files --- tests/system/test_asyncpg_connection.py | 20 ++++++++++++-------- tests/system/test_asyncpg_iam_auth.py | 8 +++++--- tests/system/test_connector_object.py | 4 +--- tests/system/test_pg8000_connection.py | 22 ++++++++++++++-------- tests/system/test_pg8000_iam_auth.py | 8 +++++--- tests/system/test_pymysql_connection.py | 8 +++++--- tests/system/test_pymysql_iam_auth.py | 8 +++++--- tests/system/test_pytds_connection.py | 8 +++++--- 8 files changed, 52 insertions(+), 34 deletions(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index 8d4a9bf9..4a2d5f09 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -167,9 +167,11 @@ async def test_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + pool, connector = await create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) async with pool.connect() as conn: res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() @@ -184,7 +186,7 @@ async def test_lazy_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" @@ -203,10 +205,10 @@ async def test_custom_SAN_with_dns_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_sqlalchemy_engine( - inst_conn_name, user, password, db, ip_type,resolver=DnsResolver + inst_conn_name, user, password, db, ip_type, resolver=DnsResolver ) async with pool.connect() as conn: @@ -222,9 +224,11 @@ async def test_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_asyncpg_pool(inst_conn_name, user, password, db, ip_type) + pool, connector = await create_asyncpg_pool( + inst_conn_name, user, password, db, ip_type + ) async with pool.acquire() as conn: res = await conn.fetch("SELECT 1") @@ -239,7 +243,7 @@ async def test_lazy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_asyncpg_pool( inst_conn_name, user, password, db, ip_type, "lazy" diff --git a/tests/system/test_asyncpg_iam_auth.py b/tests/system/test_asyncpg_iam_auth.py index 5687dda3..876f6b03 100644 --- a/tests/system/test_asyncpg_iam_auth.py +++ b/tests/system/test_asyncpg_iam_auth.py @@ -87,7 +87,7 @@ async def test_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) @@ -103,9 +103,11 @@ async def test_lazy_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") + pool, connector = await create_sqlalchemy_engine( + inst_conn_name, user, db, ip_type, "lazy" + ) async with pool.connect() as conn: res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() diff --git a/tests/system/test_connector_object.py b/tests/system/test_connector_object.py index 9a8c44b0..66fe9c25 100644 --- a/tests/system/test_connector_object.py +++ b/tests/system/test_connector_object.py @@ -39,9 +39,7 @@ def getconn() -> pymysql.connections.Connection: user=os.environ["MYSQL_USER"], password=os.environ["MYSQL_PASS"], db=os.environ["MYSQL_DB"], - ip_type=os.environ.get( - "IP_TYPE", "public" - ), + ip_type=os.environ.get("IP_TYPE", "public"), ) return conn diff --git a/tests/system/test_pg8000_connection.py b/tests/system/test_pg8000_connection.py index 906b21e7..aec1256c 100644 --- a/tests/system/test_pg8000_connection.py +++ b/tests/system/test_pg8000_connection.py @@ -103,9 +103,11 @@ def test_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -120,7 +122,7 @@ def test_lazy_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" @@ -139,9 +141,11 @@ def test_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -156,9 +160,11 @@ def test_customer_managed_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -173,7 +179,7 @@ def test_custom_SAN_with_dns_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, resolver=DnsResolver diff --git a/tests/system/test_pg8000_iam_auth.py b/tests/system/test_pg8000_iam_auth.py index e276f63c..d65df9b0 100644 --- a/tests/system/test_pg8000_iam_auth.py +++ b/tests/system/test_pg8000_iam_auth.py @@ -85,7 +85,7 @@ def test_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: @@ -101,9 +101,11 @@ def test_lazy_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, db, ip_type, "lazy" + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() diff --git a/tests/system/test_pymysql_connection.py b/tests/system/test_pymysql_connection.py index a6a8ea1f..f9060e9d 100644 --- a/tests/system/test_pymysql_connection.py +++ b/tests/system/test_pymysql_connection.py @@ -93,9 +93,11 @@ def test_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() @@ -110,7 +112,7 @@ def test_lazy_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" diff --git a/tests/system/test_pymysql_iam_auth.py b/tests/system/test_pymysql_iam_auth.py index 8cc9a0cc..58930c9a 100644 --- a/tests/system/test_pymysql_iam_auth.py +++ b/tests/system/test_pymysql_iam_auth.py @@ -85,7 +85,7 @@ def test_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: @@ -101,9 +101,11 @@ def test_lazy_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type, "lazy") + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, db, ip_type, "lazy" + ) with engine.connect() as conn: time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() conn.commit() diff --git a/tests/system/test_pytds_connection.py b/tests/system/test_pytds_connection.py index f43888d9..03b8ff6f 100644 --- a/tests/system/test_pytds_connection.py +++ b/tests/system/test_pytds_connection.py @@ -91,9 +91,11 @@ def test_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" - engine, connector = create_sqlalchemy_engine(inst_conn_name, user, password, db, ip_type) + engine, connector = create_sqlalchemy_engine( + inst_conn_name, user, password, db, ip_type + ) with engine.connect() as conn: res = conn.execute(sqlalchemy.text("SELECT 1")).fetchone() conn.commit() @@ -107,7 +109,7 @@ def test_lazy_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" From f4973490cf1bf9e23f895f7ea4ea0ad33f3e59e3 Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 12:42:34 -0700 Subject: [PATCH 5/7] chore: update default ip_type for connector engine --- tests/system/test_asyncpg_connection.py | 22 +++++++++++----------- tests/system/test_asyncpg_iam_auth.py | 10 +++++----- tests/system/test_ip_types.py | 2 +- tests/system/test_pg8000_connection.py | 16 ++++++++-------- tests/system/test_pg8000_iam_auth.py | 10 +++++----- tests/system/test_pymysql_connection.py | 10 +++++----- tests/system/test_pymysql_iam_auth.py | 10 +++++----- tests/system/test_pytds_connection.py | 10 +++++----- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index 4a2d5f09..fb743133 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -32,7 +32,7 @@ async def create_sqlalchemy_engine( user: str, password: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", resolver: Union[type[DefaultResolver], type[DnsResolver]] = DefaultResolver, ) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]: @@ -65,7 +65,7 @@ async def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -90,7 +90,7 @@ async def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" ), execution_options={"isolation_level": "AUTOCOMMIT"}, ) @@ -102,7 +102,7 @@ async def create_asyncpg_pool( user: str, password: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[asyncpg.Pool, Connector]: """Creates a native asyncpg connection pool for a Cloud SQL instance and @@ -133,7 +133,7 @@ async def create_asyncpg_pool( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -151,7 +151,7 @@ async def getconn( user=user, password=password, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" **kwargs, ) return conn @@ -167,7 +167,7 @@ async def test_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -186,7 +186,7 @@ async def test_lazy_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" @@ -205,7 +205,7 @@ async def test_custom_SAN_with_dns_sqlalchemy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, resolver=DnsResolver @@ -224,7 +224,7 @@ async def test_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_asyncpg_pool( inst_conn_name, user, password, db, ip_type @@ -243,7 +243,7 @@ async def test_lazy_connection_with_asyncpg() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_asyncpg_pool( inst_conn_name, user, password, db, ip_type, "lazy" diff --git a/tests/system/test_asyncpg_iam_auth.py b/tests/system/test_asyncpg_iam_auth.py index 876f6b03..be52a3ee 100644 --- a/tests/system/test_asyncpg_iam_auth.py +++ b/tests/system/test_asyncpg_iam_auth.py @@ -27,7 +27,7 @@ async def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -57,7 +57,7 @@ async def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -74,7 +74,7 @@ async def create_sqlalchemy_engine( "asyncpg", user=user, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" enable_iam_auth=True, ), execution_options={"isolation_level": "AUTOCOMMIT"}, @@ -87,7 +87,7 @@ async def test_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) @@ -103,7 +103,7 @@ async def test_lazy_iam_authn_connection_with_asyncpg() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") pool, connector = await create_sqlalchemy_engine( inst_conn_name, user, db, ip_type, "lazy" diff --git a/tests/system/test_ip_types.py b/tests/system/test_ip_types.py index 3af49c54..397ad483 100644 --- a/tests/system/test_ip_types.py +++ b/tests/system/test_ip_types.py @@ -34,7 +34,7 @@ def getconn() -> pymysql.connections.Connection: conn: pymysql.connections.Connection = connector.connect( os.environ["MYSQL_CONNECTION_NAME"], "pymysql", - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" user=os.environ["MYSQL_USER"], password=os.environ["MYSQL_PASS"], db=os.environ["MYSQL_DB"], diff --git a/tests/system/test_pg8000_connection.py b/tests/system/test_pg8000_connection.py index aec1256c..8cd78e8c 100644 --- a/tests/system/test_pg8000_connection.py +++ b/tests/system/test_pg8000_connection.py @@ -32,7 +32,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", resolver: Union[type[DefaultResolver], type[DnsResolver]] = DefaultResolver, ) -> tuple[sqlalchemy.engine.Engine, Connector]: @@ -66,7 +66,7 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -88,7 +88,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" ), ) return engine, connector @@ -103,7 +103,7 @@ def test_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -122,7 +122,7 @@ def test_lazy_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" @@ -141,7 +141,7 @@ def test_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -160,7 +160,7 @@ def test_customer_managed_CAS_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -179,7 +179,7 @@ def test_custom_SAN_with_dns_pg8000_connection() -> None: user = os.environ["POSTGRES_USER"] password = os.environ["POSTGRES_CUSTOMER_CAS_PASS"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, resolver=DnsResolver diff --git a/tests/system/test_pg8000_iam_auth.py b/tests/system/test_pg8000_iam_auth.py index d65df9b0..1abf1322 100644 --- a/tests/system/test_pg8000_iam_auth.py +++ b/tests/system/test_pg8000_iam_auth.py @@ -26,7 +26,7 @@ def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -57,7 +57,7 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -73,7 +73,7 @@ def create_sqlalchemy_engine( "pg8000", user=user, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" enable_iam_auth=True, ), ) @@ -85,7 +85,7 @@ def test_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: @@ -101,7 +101,7 @@ def test_lazy_pg8000_iam_authn_connection() -> None: inst_conn_name = os.environ["POSTGRES_CONNECTION_NAME"] user = os.environ["POSTGRES_IAM_USER"] db = os.environ["POSTGRES_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, db, ip_type, "lazy" diff --git a/tests/system/test_pymysql_connection.py b/tests/system/test_pymysql_connection.py index f9060e9d..6986adb4 100644 --- a/tests/system/test_pymysql_connection.py +++ b/tests/system/test_pymysql_connection.py @@ -28,7 +28,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -61,7 +61,7 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -78,7 +78,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" ), ) return engine, connector @@ -93,7 +93,7 @@ def test_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -112,7 +112,7 @@ def test_lazy_pymysql_connection() -> None: user = os.environ["MYSQL_USER"] password = os.environ["MYSQL_PASS"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" diff --git a/tests/system/test_pymysql_iam_auth.py b/tests/system/test_pymysql_iam_auth.py index 58930c9a..2f73d1a8 100644 --- a/tests/system/test_pymysql_iam_auth.py +++ b/tests/system/test_pymysql_iam_auth.py @@ -26,7 +26,7 @@ def create_sqlalchemy_engine( instance_connection_name: str, user: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -57,7 +57,7 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -73,7 +73,7 @@ def create_sqlalchemy_engine( "pymysql", user=user, db=db, - ip_type=ip_type, + ip_type=ip_type, # can be "public", "private" or "psc" enable_iam_auth=True, ), ) @@ -85,7 +85,7 @@ def test_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine(inst_conn_name, user, db, ip_type) with engine.connect() as conn: @@ -101,7 +101,7 @@ def test_lazy_pymysql_iam_authn_connection() -> None: inst_conn_name = os.environ["MYSQL_CONNECTION_NAME"] user = os.environ["MYSQL_IAM_USER"] db = os.environ["MYSQL_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, db, ip_type, "lazy" diff --git a/tests/system/test_pytds_connection.py b/tests/system/test_pytds_connection.py index 03b8ff6f..743666cf 100644 --- a/tests/system/test_pytds_connection.py +++ b/tests/system/test_pytds_connection.py @@ -27,7 +27,7 @@ def create_sqlalchemy_engine( user: str, password: str, db: str, - ip_type: str, + ip_type: str = "public", refresh_strategy: str = "background", ) -> tuple[sqlalchemy.engine.Engine, Connector]: """Creates a connection pool for a Cloud SQL instance and returns the pool @@ -59,7 +59,7 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. + The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -76,7 +76,7 @@ def create_sqlalchemy_engine( user=user, password=password, db=db, - ip_type=ip_type, # can also be "private" or "psc" + ip_type=ip_type, # can be "public", "private" or "psc" ), ) return engine, connector @@ -91,7 +91,7 @@ def test_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type @@ -109,7 +109,7 @@ def test_lazy_pytds_connection() -> None: user = os.environ["SQLSERVER_USER"] password = os.environ["SQLSERVER_PASS"] db = os.environ["SQLSERVER_DB"] - ip_type = os.environ.get("IP_TYPE", "public") # can be "public", "private" or "psc" + ip_type = os.environ.get("IP_TYPE", "public") engine, connector = create_sqlalchemy_engine( inst_conn_name, user, password, db, ip_type, "lazy" From 4a4d43d5ba81c576c6e83c681fe96f159ca2a490 Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 12:47:59 -0700 Subject: [PATCH 6/7] chore: edit test_ip_types comment --- tests/system/test_ip_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/test_ip_types.py b/tests/system/test_ip_types.py index 397ad483..3af49c54 100644 --- a/tests/system/test_ip_types.py +++ b/tests/system/test_ip_types.py @@ -34,7 +34,7 @@ def getconn() -> pymysql.connections.Connection: conn: pymysql.connections.Connection = connector.connect( os.environ["MYSQL_CONNECTION_NAME"], "pymysql", - ip_type=ip_type, # can be "public", "private" or "psc" + ip_type=ip_type, user=os.environ["MYSQL_USER"], password=os.environ["MYSQL_PASS"], db=os.environ["MYSQL_DB"], From 15a16e510d40943622244883bed9ef20a11c7a50 Mon Sep 17 00:00:00 2001 From: kgala2 Date: Mon, 31 Mar 2025 12:54:12 -0700 Subject: [PATCH 7/7] chore: update docstrings comment --- tests/system/test_asyncpg_connection.py | 6 ++++-- tests/system/test_asyncpg_iam_auth.py | 3 ++- tests/system/test_pg8000_connection.py | 3 ++- tests/system/test_pg8000_iam_auth.py | 3 ++- tests/system/test_pymysql_connection.py | 3 ++- tests/system/test_pymysql_iam_auth.py | 3 ++- tests/system/test_pytds_connection.py | 3 ++- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/system/test_asyncpg_connection.py b/tests/system/test_asyncpg_connection.py index fb743133..d7602e53 100644 --- a/tests/system/test_asyncpg_connection.py +++ b/tests/system/test_asyncpg_connection.py @@ -65,7 +65,8 @@ async def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid @@ -133,7 +134,8 @@ async def create_asyncpg_pool( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_asyncpg_iam_auth.py b/tests/system/test_asyncpg_iam_auth.py index be52a3ee..0e0c01e8 100644 --- a/tests/system/test_asyncpg_iam_auth.py +++ b/tests/system/test_asyncpg_iam_auth.py @@ -57,7 +57,8 @@ async def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_pg8000_connection.py b/tests/system/test_pg8000_connection.py index 8cd78e8c..42a8d989 100644 --- a/tests/system/test_pg8000_connection.py +++ b/tests/system/test_pg8000_connection.py @@ -66,7 +66,8 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_pg8000_iam_auth.py b/tests/system/test_pg8000_iam_auth.py index 1abf1322..38ee7661 100644 --- a/tests/system/test_pg8000_iam_auth.py +++ b/tests/system/test_pg8000_iam_auth.py @@ -57,7 +57,8 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_pymysql_connection.py b/tests/system/test_pymysql_connection.py index 6986adb4..2748a8d7 100644 --- a/tests/system/test_pymysql_connection.py +++ b/tests/system/test_pymysql_connection.py @@ -61,7 +61,8 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_pymysql_iam_auth.py b/tests/system/test_pymysql_iam_auth.py index 2f73d1a8..39af5c71 100644 --- a/tests/system/test_pymysql_iam_auth.py +++ b/tests/system/test_pymysql_iam_auth.py @@ -57,7 +57,8 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid diff --git a/tests/system/test_pytds_connection.py b/tests/system/test_pytds_connection.py index 743666cf..198c3307 100644 --- a/tests/system/test_pytds_connection.py +++ b/tests/system/test_pytds_connection.py @@ -59,7 +59,8 @@ def create_sqlalchemy_engine( db (str): The name of the database, e.g., mydb ip_type (str): - The IP type of the Cloud SQL instance. Can be one of "public", "private", or "psc". + The IP type of the Cloud SQL instance to connect to. Can be one + of "public", "private", or "psc". refresh_strategy (Optional[str]): Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" or "background". For serverless environments use "lazy" to avoid