FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add support async postgres driver by fourteekey · Pull Request #320 · testcontainers/testcontainers-python · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .in  (1) .py  (4) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 3 additions & 1 deletion core/testcontainers/core/generic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL