Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add asyncpg sample usage #225

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 67 additions & 15 deletions tests/system/test_asyncpg_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,86 @@

import os

# [START alloydb_sqlalchemy_connect_async_connector]
import asyncpg
import pytest
import sqlalchemy
from sqlalchemy.ext.asyncio import create_async_engine

from google.cloud.alloydb.connector import AsyncConnector


@pytest.mark.asyncio
async def test_connection_with_asyncpg() -> None:
async with AsyncConnector() as connector:

async def getconn() -> asyncpg.Connection:
conn: asyncpg.Connection = await connector.connect(
os.environ["ALLOYDB_INSTANCE_URI"],
"asyncpg",
user=os.environ["ALLOYDB_USER"],
password=os.environ["ALLOYDB_PASS"],
db=os.environ["ALLOYDB_DB"],
)
return conn
async def create_sqlalchemy_engine(
inst_uri: str,
user: str,
password: str,
db: str,
) -> (sqlalchemy.engine.AsyncEngine, AsyncConnector):
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved
"""Creates a connection pool for an AlloyDB instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.

A sample invocation looks like:

engine, connector = create_sqlalchemy_engine(
inst_uri,
user,
password,
db,
)
with engine.connect() as conn:
time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone()
conn.commit()
curr_time = time[0]
# do something with query result
connector.close()
jackwotherspoon marked this conversation as resolved.
Show resolved Hide resolved

Args:
instance_uri (str):
The instance URI specifies the instance relative to the project,
region, and cluster. For example:
"projects/my-project/locations/us-central1/clusters/my-cluster/instances/my-instance"
user (str):
The database user name, e.g., postgres
password (str):
The database user's password, e.g., secret-password
db_name (str):
The name of the database, e.g., mydb
"""
connector = AsyncConnector()

async def getconn() -> asyncpg.Connection:
conn: asyncpg.Connection = await connector.connect(
inst_uri,
"asyncpg",
user=user,
password=password,
db=db,
)
return conn

# create SQLAlchemy connection pool
pool = create_async_engine(
engine = await sqlalchemy.create_async_engine(
"postgresql+asyncpg://",
async_creator=getconn,
execution_options={"isolation_level": "AUTOCOMMIT"},
)
engine.dialect.description_encoding = None
return engine, connector


# [END alloydb_sqlalchemy_connect_async_connector]


@pytest.mark.asyncio
async def test_connection_with_asyncpg() -> None:
"""Basic test to get time from database."""
inst_uri = os.environ["ALLOYDB_INSTANCE_URI"]
user = os.environ["ALLOYDB_USER"]
password = os.environ["ALLOYDB_PASS"]
db = os.environ["ALLOYDB_DB"]

pool = await create_sqlalchemy_engine(inst_uri, user, password, db)

async with pool.connect() as conn:
res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone()
assert res[0] == 1
Loading