Skip to content

Commit 40425f9

Browse files
chore: update system tests to use Connector object (#392)
1 parent c25cecd commit 40425f9

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

tests/system/test_ip_types.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
import logging
1716
import os
1817
import uuid
1918

2019
import pymysql
2120
import pytest
2221
import sqlalchemy
23-
from google.cloud.sql.connector import connector, IPTypes
22+
from google.cloud.sql.connector import Connector, IPTypes
2423

2524
table_name = f"books_{uuid.uuid4().hex}"
2625

2726

28-
def init_connection_engine(ip_type: IPTypes) -> sqlalchemy.engine.Engine:
27+
def init_connection_engine(
28+
connector: Connector, ip_type: IPTypes
29+
) -> sqlalchemy.engine.Engine:
2930
def getconn() -> pymysql.connections.Connection:
3031
conn: pymysql.connections.Connection = connector.connect(
3132
os.environ["MYSQL_CONNECTION_NAME"],
@@ -45,19 +46,15 @@ def getconn() -> pymysql.connections.Connection:
4546

4647

4748
def test_public_ip() -> None:
48-
try:
49-
pool = init_connection_engine(IPTypes.PUBLIC)
50-
except Exception as e:
51-
logging.exception("Failed to initialize pool with public IP", e)
52-
with pool.connect() as conn:
53-
conn.execute("SELECT 1")
49+
with Connector() as connector:
50+
pool = init_connection_engine(connector, IPTypes.PUBLIC)
51+
with pool.connect() as conn:
52+
conn.execute("SELECT 1")
5453

5554

5655
@pytest.mark.private_ip
5756
def test_private_ip() -> None:
58-
try:
59-
pool = init_connection_engine(IPTypes.PRIVATE)
60-
except Exception as e:
61-
logging.exception("Failed to initialize pool with private IP", e)
62-
with pool.connect() as conn:
63-
conn.execute("SELECT 1")
57+
with Connector() as connector:
58+
pool = init_connection_engine(connector, IPTypes.PRIVATE)
59+
with pool.connect() as conn:
60+
conn.execute("SELECT 1")

tests/system/test_pg8000_connection.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pg8000
2121
import pytest
2222
import sqlalchemy
23-
from google.cloud.sql.connector import connector
23+
from google.cloud.sql.connector import Connector
2424

2525
table_name = f"books_{uuid.uuid4().hex}"
2626

@@ -30,14 +30,15 @@
3030
# 'creator' argument to 'create_engine'
3131
def init_connection_engine() -> sqlalchemy.engine.Engine:
3232
def getconn() -> pg8000.dbapi.Connection:
33-
conn: pg8000.dbapi.Connection = connector.connect(
34-
os.environ["POSTGRES_CONNECTION_NAME"],
35-
"pg8000",
36-
user=os.environ["POSTGRES_USER"],
37-
password=os.environ["POSTGRES_PASS"],
38-
db=os.environ["POSTGRES_DB"],
39-
)
40-
return conn
33+
with Connector() as connector:
34+
conn: pg8000.dbapi.Connection = connector.connect(
35+
os.environ["POSTGRES_CONNECTION_NAME"],
36+
"pg8000",
37+
user=os.environ["POSTGRES_USER"],
38+
password=os.environ["POSTGRES_PASS"],
39+
db=os.environ["POSTGRES_DB"],
40+
)
41+
return conn
4142

4243
engine = sqlalchemy.create_engine(
4344
"postgresql+pg8000://",

tests/system/test_pg8000_iam_auth.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pg8000
2121
import pytest
2222
import sqlalchemy
23-
from google.cloud.sql.connector import connector
23+
from google.cloud.sql.connector import Connector
2424

2525
table_name = f"books_{uuid.uuid4().hex}"
2626

@@ -30,14 +30,15 @@
3030
# 'creator' argument to 'create_engine'
3131
def init_connection_engine() -> sqlalchemy.engine.Engine:
3232
def getconn() -> pg8000.dbapi.Connection:
33-
conn: pg8000.dbapi.Connection = connector.connect(
34-
os.environ["POSTGRES_IAM_CONNECTION_NAME"],
35-
"pg8000",
36-
user=os.environ["POSTGRES_IAM_USER"],
37-
db=os.environ["POSTGRES_DB"],
38-
enable_iam_auth=True,
39-
)
40-
return conn
33+
with Connector() as connector:
34+
conn: pg8000.dbapi.Connection = connector.connect(
35+
os.environ["POSTGRES_IAM_CONNECTION_NAME"],
36+
"pg8000",
37+
user=os.environ["POSTGRES_IAM_USER"],
38+
db=os.environ["POSTGRES_DB"],
39+
enable_iam_auth=True,
40+
)
41+
return conn
4142

4243
engine = sqlalchemy.create_engine(
4344
"postgresql+pg8000://",

tests/system/test_pymysql_connection.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pymysql
2121
import pytest
2222
import sqlalchemy
23-
from google.cloud.sql.connector import connector
23+
from google.cloud.sql.connector import Connector
2424

2525
table_name = f"books_{uuid.uuid4().hex}"
2626

@@ -30,14 +30,15 @@
3030
# 'creator' argument to 'create_engine'
3131
def init_connection_engine() -> sqlalchemy.engine.Engine:
3232
def getconn() -> pymysql.connections.Connection:
33-
conn: pymysql.connections.Connection = connector.connect(
34-
os.environ["MYSQL_CONNECTION_NAME"],
35-
"pymysql",
36-
user=os.environ["MYSQL_USER"],
37-
password=os.environ["MYSQL_PASS"],
38-
db=os.environ["MYSQL_DB"],
39-
)
40-
return conn
33+
with Connector() as connector:
34+
conn: pymysql.connections.Connection = connector.connect(
35+
os.environ["MYSQL_CONNECTION_NAME"],
36+
"pymysql",
37+
user=os.environ["MYSQL_USER"],
38+
password=os.environ["MYSQL_PASS"],
39+
db=os.environ["MYSQL_DB"],
40+
)
41+
return conn
4142

4243
engine = sqlalchemy.create_engine(
4344
"mysql+pymysql://",

tests/system/test_pytds_connection.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pytds
2121
import pytest
2222
import sqlalchemy
23-
from google.cloud.sql.connector import connector
23+
from google.cloud.sql.connector import Connector
2424

2525
table_name = f"books_{uuid.uuid4().hex}"
2626

@@ -31,14 +31,15 @@
3131
# 'sqlalchemy-pytds` in your dependencies
3232
def init_connection_engine() -> sqlalchemy.engine.Engine:
3333
def getconn() -> pytds.Connection:
34-
conn = connector.connect(
35-
os.environ["SQLSERVER_CONNECTION_NAME"],
36-
"pytds",
37-
user=os.environ["SQLSERVER_USER"],
38-
password=os.environ["SQLSERVER_PASS"],
39-
db=os.environ["SQLSERVER_DB"],
40-
)
41-
return conn
34+
with Connector() as connector:
35+
conn = connector.connect(
36+
os.environ["SQLSERVER_CONNECTION_NAME"],
37+
"pytds",
38+
user=os.environ["SQLSERVER_USER"],
39+
password=os.environ["SQLSERVER_PASS"],
40+
db=os.environ["SQLSERVER_DB"],
41+
)
42+
return conn
4243

4344
engine = sqlalchemy.create_engine(
4445
"mssql+pytds://localhost",

0 commit comments

Comments
 (0)