-
Notifications
You must be signed in to change notification settings - Fork 270
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
Creating a Postgres contrainer with an async driver fails #263
Comments
Hey, do you have a code example to illustrate the issue? |
test_main.py
import pytest
import sqlalchemy
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from testcontainers.postgres import PostgresContainer
from sqlalchemy.orm import Session
def test_simple():
with PostgresContainer("postgres:9.5") as postgres:
print('Connection URL:', postgres.get_connection_url())
engine = sqlalchemy.create_engine(postgres.get_connection_url())
with Session(bind=engine) as session:
result = session.execute("select version()")
(version,) = result.fetchone()
print("Version:", version)
@pytest.mark.asyncio
async def test_async():
with PostgresContainer("postgres:9.5", driver="asyncpg") as postgres:
print('Connection URL:', postgres.get_connection_url())
engine = create_async_engine(postgres.get_connection_url())
async with AsyncSession(bind=engine, expire_on_commit=True) as session:
result = await session.execute("select version()")
(version,) = result.fetchone()
print("Version:", version) requirements.txt
For runing use: Traceback
You are have discussion in #175 . |
Why did you create method |
MR with fixes #320 |
+1 same problem, need this change |
Actually, it's not such a big problem, from testcontainers.postgres import PostgresContainer
with PostgresContainer("postgres:14") as postgres_container:
postgres_container.driver = "asyncpg"
url = postgres_container.get_connection_url() # -> postgresql+asyncpg://user:user@localhost:56268/demo PS: In my case the url contains My only concern is, does this session close? Seems to me there's no reason to leave it open. testcontainers-python/core/testcontainers/core/generic.py Lines 31 to 35 in fce23a4
|
Also, a simple solution for #320 would be to just use testcontainers-python/postgres/testcontainers/postgres/__init__.py Lines 63 to 69 in fce23a4
|
This functionality could also be extended to mysql as well since mysql currently only supports pymysql as the driver. |
Hello,
When creating a new database container, testcontainers automatically tries to connect to it here, but if I specify an async driver (e.g.
PostgresContainer("postgres:14.4", driver="asyncpg")
) sqlalchemy tries to use that and fails because it's not in an async context.Is there any way to skip trying to connect to the database or fix it so it can also handle async context?
The text was updated successfully, but these errors were encountered: