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 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
4 changes: 3 additions & 1 deletion core/testcontainers/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,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
2 changes: 2 additions & 0 deletions postgres/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"testcontainers-core",
"sqlalchemy",
"psycopg2-binary",
"asyncpg",
"pytest-asyncio",
],
python_requires=">=3.7",
)
28 changes: 25 additions & 3 deletions postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
import os
from typing import Optional
from testcontainers.core.generic import DbContainer
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 Down Expand Up @@ -41,10 +49,14 @@ class PostgresContainer(DbContainer):
POSTGRES_USER = os.environ.get("POSTGRES_USER", "test")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test")
POSTGRES_DB = os.environ.get("POSTGRES_DB", "test")
DEFAULT_DRIVER = "psycopg2"

def __init__(self, image: str = "postgres:latest", port: int = 5432, user: Optional[str] = None,
password: Optional[str] = None, dbname: Optional[str] = None,
driver: str = "psycopg2", **kwargs) -> None:
driver: Optional[str] = None, **kwargs) -> None:
if driver is None:
driver = self.DEFAULT_DRIVER

super(PostgresContainer, self).__init__(image=image, **kwargs)
self.POSTGRES_USER = user or self.POSTGRES_USER
self.POSTGRES_PASSWORD = password or self.POSTGRES_PASSWORD
Expand All @@ -54,14 +66,24 @@ def __init__(self, image: str = "postgres:latest", port: int = 5432, user: Optio

self.with_exposed_ports(self.port_to_expose)

@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.POSTGRES_USER)
self.with_env("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD)
self.with_env("POSTGRES_DB", self.POSTGRES_DB)

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="postgresql+{}".format(self.driver), username=self.POSTGRES_USER,
dialect="postgresql+{}".format(driver), username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD, db_name=self.POSTGRES_DB, host=host,
port=self.port_to_expose,
)
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")