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

add postgres sqlalchemy asyncio support #359

Closed
wants to merge 4 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
3 changes: 2 additions & 1 deletion postgres/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
url="https://github.com/testcontainers/testcontainers-python",
install_requires=[
"testcontainers-core",
"sqlalchemy",
"sqlalchemy[asyncio]",
"psycopg2-binary",
"asyncpg"
],
python_requires=">=3.7",
)
13 changes: 12 additions & 1 deletion postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# under the License.
import os
from typing import Optional
from testcontainers.core.generic import DbContainer
from testcontainers.core.generic import DbContainer, ADDITIONAL_TRANSIENT_ERRORS
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready


class PostgresContainer(DbContainer):
Expand Down Expand Up @@ -63,3 +64,13 @@ def get_connection_url(self, host=None) -> str:
password=self.password, dbname=self.dbname, host=host,
port=self.port,
)

@wait_container_is_ready(*ADDITIONAL_TRANSIENT_ERRORS)
def _connect(self) -> None:
if self.driver == "asyncpg":
from sqlalchemy.ext.asyncio import create_async_engine as create_engine
else:
from sqlalchemy import create_engine
engine = create_engine(self.get_connection_url())
conn = engine.connect()
conn.close()
16 changes: 16 additions & 0 deletions postgres/tests/test_postgres.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from asyncio import sleep

import pytest
import sqlalchemy
from sqlalchemy.ext.asyncio import create_async_engine
from testcontainers.postgres import PostgresContainer


Expand All @@ -18,3 +22,15 @@ 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_postgres_with_driver_asyncio():
postgres_container = PostgresContainer("postgres:9.5", driver="asyncpg")
with postgres_container as postgres:
# in local test need to wait while pg container is ready
# else raised `ConnectionError: unexpected connection_lost() call`
await sleep(5)
engine = create_async_engine(postgres.get_connection_url())
async with engine.begin() as connection:
await connection.execute(sqlalchemy.text("select 1=1"))
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