|
| 1 | +import pytest |
| 2 | +import sqlalchemy as sa |
| 3 | +from sqlalchemy.dialects import registry as dialect_registry |
| 4 | + |
| 5 | +from sqlalchemy_cratedb.sa_version import SA_2_0, SA_VERSION |
| 6 | + |
| 7 | +if SA_VERSION < SA_2_0: |
| 8 | + raise pytest.skip("Only supported on SQLAlchemy 2.0 and higher", allow_module_level=True) |
| 9 | + |
| 10 | +from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine |
| 11 | + |
| 12 | +# Registering the additional dialects manually seems to be needed when running |
| 13 | +# under tests. Apparently, manual registration is not needed under regular |
| 14 | +# circumstances, as this is wired through the `sqlalchemy.dialects` entrypoint |
| 15 | +# registrations in `pyproject.toml`. It is definitively weird, but c'est la vie. |
| 16 | +dialect_registry.register("crate.urllib3", "sqlalchemy_cratedb.dialect_more", "dialect_urllib3") |
| 17 | +dialect_registry.register("crate.asyncpg", "sqlalchemy_cratedb.dialect_more", "dialect_asyncpg") |
| 18 | +dialect_registry.register("crate.psycopg", "sqlalchemy_cratedb.dialect_more", "dialect_psycopg") |
| 19 | + |
| 20 | + |
| 21 | +QUERY = sa.text("SELECT mountain, coordinates FROM sys.summits ORDER BY mountain LIMIT 3;") |
| 22 | + |
| 23 | + |
| 24 | +def test_engine_sync_vanilla(cratedb_service): |
| 25 | + """ |
| 26 | + crate:// -- Verify connectivity and data transport with vanilla HTTP-based driver. |
| 27 | + """ |
| 28 | + port4200 = cratedb_service.cratedb.get_exposed_port(4200) |
| 29 | + engine = sa.create_engine(f"crate://crate@localhost:{port4200}/", echo=True) |
| 30 | + assert isinstance(engine, sa.engine.Engine) |
| 31 | + with engine.connect() as connection: |
| 32 | + result = connection.execute(QUERY) |
| 33 | + assert result.mappings().fetchone() == { |
| 34 | + "mountain": "Acherkogel", |
| 35 | + "coordinates": [10.95667, 47.18917], |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def test_engine_sync_urllib3(cratedb_service): |
| 40 | + """ |
| 41 | + crate+urllib3:// -- Verify connectivity and data transport *explicitly* selecting the HTTP driver. |
| 42 | + """ # noqa: E501 |
| 43 | + port4200 = cratedb_service.cratedb.get_exposed_port(4200) |
| 44 | + engine = sa.create_engine( |
| 45 | + f"crate+urllib3://crate@localhost:{port4200}/", isolation_level="AUTOCOMMIT", echo=True |
| 46 | + ) |
| 47 | + assert isinstance(engine, sa.engine.Engine) |
| 48 | + with engine.connect() as connection: |
| 49 | + result = connection.execute(QUERY) |
| 50 | + assert result.mappings().fetchone() == { |
| 51 | + "mountain": "Acherkogel", |
| 52 | + "coordinates": [10.95667, 47.18917], |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_engine_sync_psycopg(cratedb_service): |
| 57 | + """ |
| 58 | + crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3). |
| 59 | + """ |
| 60 | + port5432 = cratedb_service.cratedb.get_exposed_port(5432) |
| 61 | + engine = sa.create_engine( |
| 62 | + f"crate+psycopg://crate@localhost:{port5432}/", isolation_level="AUTOCOMMIT", echo=True |
| 63 | + ) |
| 64 | + assert isinstance(engine, sa.engine.Engine) |
| 65 | + with engine.connect() as connection: |
| 66 | + result = connection.execute(QUERY) |
| 67 | + assert result.mappings().fetchone() == { |
| 68 | + "mountain": "Acherkogel", |
| 69 | + "coordinates": "(10.95667,47.18917)", |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_engine_async_psycopg(cratedb_service): |
| 75 | + """ |
| 76 | + crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3). |
| 77 | + This time, in asynchronous mode. |
| 78 | + """ |
| 79 | + port5432 = cratedb_service.cratedb.get_exposed_port(5432) |
| 80 | + engine = create_async_engine( |
| 81 | + f"crate+psycopg://crate@localhost:{port5432}/", isolation_level="AUTOCOMMIT", echo=True |
| 82 | + ) |
| 83 | + assert isinstance(engine, AsyncEngine) |
| 84 | + async with engine.begin() as conn: |
| 85 | + result = await conn.execute(QUERY) |
| 86 | + assert result.mappings().fetchone() == { |
| 87 | + "mountain": "Acherkogel", |
| 88 | + "coordinates": "(10.95667,47.18917)", |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_engine_async_asyncpg(cratedb_service): |
| 94 | + """ |
| 95 | + crate+asyncpg:// -- Verify connectivity and data transport using the asyncpg driver. |
| 96 | + This exclusively uses asynchronous mode. |
| 97 | + """ |
| 98 | + port5432 = cratedb_service.cratedb.get_exposed_port(5432) |
| 99 | + from asyncpg.pgproto.types import Point |
| 100 | + |
| 101 | + engine = create_async_engine( |
| 102 | + f"crate+asyncpg://crate@localhost:{port5432}/", isolation_level="AUTOCOMMIT", echo=True |
| 103 | + ) |
| 104 | + assert isinstance(engine, AsyncEngine) |
| 105 | + async with engine.begin() as conn: |
| 106 | + result = await conn.execute(QUERY) |
| 107 | + assert result.mappings().fetchone() == { |
| 108 | + "mountain": "Acherkogel", |
| 109 | + "coordinates": Point(10.95667, 47.18917), |
| 110 | + } |
0 commit comments