Skip to content

Use with connection pools

This guide shows how to test code that uses a connection pool with pytest-adbc-replay. The examples use adbc-poolhouse, but any pool library that calls adbc_clone() internally works the same way.

Configure auto-patching

List the driver module in adbc_auto_patch so the plugin intercepts connect() calls automatically:

[tool.pytest.ini_options]
adbc_auto_patch = [
    "adbc_driver_duckdb.dbapi",
]
[pytest]
adbc_auto_patch =
    adbc_driver_duckdb.dbapi

With auto-patching enabled, the connection returned by the driver's connect() is already a ReplayConnection. When the pool library calls adbc_clone() on it, each clone shares the same cassette path and configuration.

Create a pool fixture

Create the pool in a function-scoped fixture, so each test's pool connects inside that test. The plugin fixes a connection's cassette, and its record/replay state, when the source connection is created:

# conftest.py
import pytest
from adbc_poolhouse import DuckDBConfig, create_pool, close_pool


@pytest.fixture
def pool():
    p = create_pool(DuckDBConfig(database="/tmp/test.db"))
    yield p
    close_pool(p)


@pytest.fixture
def db_conn(pool):
    conn = pool.connect()
    yield conn
    conn.close()

pool.connect() calls adbc_clone() internally, so each checkout connection is a ReplayConnection sharing the same cassette.

Note

A session- or module-scoped pool connects once, during the first test that uses it. Every later test then records into, and replays from, that first test's cassette, with one shared once decision and replay queue. Use a function-scoped pool unless your tests are meant to share one cassette.

Write a test

Use the db_conn fixture with @pytest.mark.adbc_cassette as usual:

import pytest


@pytest.mark.adbc_cassette("pool_query")
def test_pool_query(db_conn):
    with db_conn.cursor() as cur:
        cur.execute("SELECT 42 AS answer")
        result = cur.fetchall()
    assert result

Explicit wrap() approach

If you prefer not to use auto-patching, open the source connection with the adbc_connect fixture and build the pool on its adbc_clone(). This example uses SQLAlchemy's QueuePool directly:

# conftest.py
import pytest
from sqlalchemy.pool import QueuePool


@pytest.fixture
def pool(adbc_connect):
    source = adbc_connect("adbc_driver_duckdb.dbapi", path="/tmp/test.db")
    p = QueuePool(source.adbc_clone, pool_size=5)
    yield p
    p.dispose()

The pool calls adbc_clone() on the ReplayConnection for each new connection, and all checkouts share the test's cassette. The adbc_connect fixture closes the source connection when the test ends.

Record the cassette with --adbc-record=once as usual. Every query the test runs is recorded, whichever pool connection runs it, and replay hands each connection the results in the order they were recorded.

Warning

If several threads run the same query at the same time and its recorded results differ, which thread receives which result depends on thread scheduling. Different queries are always matched correctly. See the explanation article for details.