|
| 1 | +""" |
| 2 | +Copyright 2022 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | +import os |
| 17 | +import uuid |
| 18 | +from typing import Generator |
| 19 | + |
| 20 | +import pytest |
| 21 | +import pymysql |
| 22 | +import sqlalchemy |
| 23 | +from google.cloud.sql.connector import Connector |
| 24 | + |
| 25 | +table_name = f"books_{uuid.uuid4().hex}" |
| 26 | + |
| 27 | + |
| 28 | +# [START cloud_sql_connector_mysql_pymysql_iam_auth] |
| 29 | +# The Cloud SQL Python Connector can be used along with SQLAlchemy using the |
| 30 | +# 'creator' argument to 'create_engine' |
| 31 | +def init_connection_engine() -> sqlalchemy.engine.Engine: |
| 32 | + def getconn() -> pymysql.connections.Connection: |
| 33 | + # initialize Connector object for connections to Cloud SQL |
| 34 | + with Connector() as connector: |
| 35 | + conn: pymysql.connections.Connection = connector.connect( |
| 36 | + os.environ["MYSQL_IAM_CONNECTION_NAME"], |
| 37 | + "pymysql", |
| 38 | + user=os.environ["MYSQL_IAM_USER"], |
| 39 | + db=os.environ["MYSQL_DB"], |
| 40 | + enable_iam_auth=True, |
| 41 | + ) |
| 42 | + return conn |
| 43 | + |
| 44 | + # create SQLAlchemy connection pool |
| 45 | + pool = sqlalchemy.create_engine( |
| 46 | + "mysql+pymysql://", |
| 47 | + creator=getconn, |
| 48 | + ) |
| 49 | + return pool |
| 50 | + |
| 51 | + |
| 52 | +# [END cloud_sql_connector_mysql_pymysql_iam_auth] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(name="pool") |
| 56 | +def setup() -> Generator: |
| 57 | + pool = init_connection_engine() |
| 58 | + |
| 59 | + with pool.connect() as conn: |
| 60 | + conn.execute( |
| 61 | + f"CREATE TABLE IF NOT EXISTS {table_name}" |
| 62 | + " ( id CHAR(20) NOT NULL, title TEXT NOT NULL );" |
| 63 | + ) |
| 64 | + |
| 65 | + yield pool |
| 66 | + |
| 67 | + with pool.connect() as conn: |
| 68 | + conn.execute(f"DROP TABLE IF EXISTS {table_name}") |
| 69 | + |
| 70 | + |
| 71 | +def test_pooled_connection_with_pymysql_iam_auth( |
| 72 | + pool: sqlalchemy.engine.Engine, |
| 73 | +) -> None: |
| 74 | + insert_stmt = sqlalchemy.text( |
| 75 | + f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)", |
| 76 | + ) |
| 77 | + with pool.connect() as conn: |
| 78 | + conn.execute(insert_stmt, id="book1", title="Book One") |
| 79 | + conn.execute(insert_stmt, id="book2", title="Book Two") |
| 80 | + |
| 81 | + select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;") |
| 82 | + with pool.connect() as conn: |
| 83 | + rows = conn.execute(select_stmt).fetchall() |
| 84 | + titles = [row[0] for row in rows] |
| 85 | + |
| 86 | + assert titles == ["Book One", "Book Two"] |
0 commit comments