Skip to content

add support async postgres driver #320

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion core/testcontainers/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class DbContainer(DockerContainer):
"""
Generic database container.
"""

@wait_container_is_ready(*ADDITIONAL_TRANSIENT_ERRORS)
def _connect(self) -> None:
import sqlalchemy
engine = sqlalchemy.create_engine(self.get_connection_url())
engine.connect()
conn = engine.connect()
conn.close()

def get_connection_url(self) -> str:
raise NotImplementedError
Expand Down
1 change: 1 addition & 0 deletions postgres/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"testcontainers-core",
"sqlalchemy",
"psycopg2-binary",
"asyncpg",
],
python_requires=">=3.7",
)
29 changes: 26 additions & 3 deletions postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
from typing import Optional
from testcontainers.core.generic import DbContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready

ADDITIONAL_TRANSIENT_ERRORS = []
try:
from sqlalchemy.exc import DBAPIError
ADDITIONAL_TRANSIENT_ERRORS.append(DBAPIError)
except ImportError:
pass


class PostgresContainer(DbContainer):
Expand All @@ -39,10 +47,15 @@ class PostgresContainer(DbContainer):
>>> version
'PostgreSQL 9.5...'
"""

DEFAULT_DRIVER = "psycopg2"

def __init__(self, image: str = "postgres:latest", port: int = 5432,
username: Optional[str] = None, password: Optional[str] = None,
dbname: Optional[str] = None, driver: str = "psycopg2", **kwargs) -> None:
dbname: Optional[str] = None, driver: Optional[str] = None, **kwargs) -> None:
raise_for_deprecated_parameter(kwargs, "user", "username")
if driver is None:
driver = self.DEFAULT_DRIVER
super(PostgresContainer, self).__init__(image=image, **kwargs)
self.username = username or os.environ.get("POSTGRES_USER", "test")
self.password = password or os.environ.get("POSTGRES_PASSWORD", "test")
Expand All @@ -52,14 +65,24 @@ def __init__(self, image: str = "postgres:latest", port: int = 5432,

self.with_exposed_ports(self.port)

@wait_container_is_ready(*ADDITIONAL_TRANSIENT_ERRORS)
def _connect(self) -> None:
import sqlalchemy
engine = sqlalchemy.create_engine(self.get_connection_url(driver=self.DEFAULT_DRIVER))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use the super call here? It looks like the method body is the same as in the parent class (except for the driver argument which is already handled in the updated get_connection_url).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fourteekey, could you please address this question?

conn = engine.connect()
conn.close()

def _configure(self) -> None:
self.with_env("POSTGRES_USER", self.username)
self.with_env("POSTGRES_PASSWORD", self.password)
self.with_env("POSTGRES_DB", self.dbname)

def get_connection_url(self, host=None) -> str:
def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] = None) -> str:
if driver is None:
driver = self.driver

return super()._create_connection_url(
dialect=f"postgresql+{self.driver}", username=self.username,
dialect=f"postgresql+{driver}", username=self.username,
password=self.password, dbname=self.dbname, host=host,
port=self.port,
)
12 changes: 12 additions & 0 deletions postgres/tests/test_postgres.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import sqlalchemy
from sqlalchemy.ext.asyncio import create_async_engine
from testcontainers.postgres import PostgresContainer


Expand All @@ -18,3 +20,13 @@ def test_docker_run_postgres_with_driver_pg8000():
engine = sqlalchemy.create_engine(postgres.get_connection_url())
with engine.begin() as connection:
connection.execute(sqlalchemy.text("select 1=1"))


@pytest.mark.asyncio
async def test_docker_run_async_postgres():
with PostgresContainer("postgres:9.5", driver="asyncpg") as postgres:
engine = create_async_engine(postgres.get_connection_url())
async with engine.begin() as connection:
result = await connection.execute(sqlalchemy.text("select version()"))
for row in result:
assert row[0].lower().startswith("postgresql 9.5")
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ flake8<3.8.0 # 3.8.0 adds a dependency on importlib-metadata which conflicts wi
pg8000
pytest
pytest-cov
pytest-asyncio
sphinx
twine
wheel