Hardcharger#
Integration with Hardcharger, an analytics server built on DuckDB. Clients talk to it over Chargewire, an HTTP protocol that carries results as Apache Arrow.
Note
The container serves plain HTTP (no TLS) and keeps its database in memory, so every new container starts empty.
HardchargerService.uri already carries ?tls=false.
Installation#
pip install pytest-databases
The fixture needs no database client: it validates container readiness by running SELECT 4242 through the hardcharger command-line client inside the container and then checking the published port. To connect from your own tests, install the client you normally use, for example the ADBC driver:
pip install adbc-driver-hardcharger
Usage Example#
Connect using the uri and connection_kwargs properties on HardchargerService:
from adbc_driver_hardcharger import dbapi as hardcharger
from pytest_databases.docker.hardcharger import HardchargerService
pytest_plugins = ["pytest_databases.docker.hardcharger"]
def test(hardcharger_service: HardchargerService) -> None:
with hardcharger.connect(
uri=hardcharger_service.uri,
db_kwargs=hardcharger_service.connection_kwargs,
autocommit=True,
) as conn:
with conn.cursor() as cur:
cur.execute("CREATE TABLE test_table (id INTEGER, name VARCHAR)")
cur.execute("INSERT INTO test_table VALUES (1, 'test')")
with conn.cursor() as cur:
cur.execute("SELECT * FROM test_table")
result = cur.fetchone()
assert result is not None
assert result[0] == 1
exec_hardcharger runs SQL with the client bundled in the container, which is useful for seeding data without installing a driver. It returns the exit code and the output as CSV with a header row:
from pytest_databases.docker.hardcharger import HardchargerService, exec_hardcharger
pytest_plugins = ["pytest_databases.docker.hardcharger"]
def test(hardcharger_service: HardchargerService) -> None:
exit_code, output = exec_hardcharger(
container=hardcharger_service.container,
sql="SELECT 42 AS answer",
user=hardcharger_service.username,
password=hardcharger_service.password,
)
assert exit_code == 0
assert output.decode().splitlines() == ["answer", "42"]
Available Fixtures#
hardcharger_image: The container image to use for Hardcharger (default:ghcr.io/gizmodata/hardcharger:latest).hardcharger_username: The administrator’s user name (default:hardcharger_username).hardcharger_password: The administrator’s password (default:hardcharger_password).hardcharger_service: A running Hardcharger service.xdist_hardcharger_isolation_level:database(default) keeps worker containers until the session’s containers are cleaned up;serverremoves each one when its fixture is torn down.
Override hardcharger_image to pin a version:
@pytest.fixture(scope="session")
def hardcharger_image():
return "ghcr.io/gizmodata/hardcharger:2.10.0"
Parallel Testing (xdist)#
Hardcharger assigns each pytest-xdist worker its own container (hardcharger_<worker>) because the server’s
in-memory DuckDB database cannot be split into independent databases per worker. This holds for both isolation levels;
xdist_hardcharger_isolation_level only controls when those containers are removed.
@pytest.fixture(scope="session")
def xdist_hardcharger_isolation_level():
return "server"
Service API#
Hardcharger (DuckDB analytics server speaking Chargewire) container service fixtures.
- pytest_databases.docker.hardcharger.HARDCHARGER_PORT = 31337#
The port the Hardcharger server listens on inside the container.
- class pytest_databases.docker.hardcharger.HardchargerService(container: docker.models.containers.Container, host: str, port: int, username: str, password: str)[source]#
Bases:
ServiceContainerService container for Hardcharger.
Hardcharger is an analytics server built on DuckDB. Clients talk to it over Chargewire, an HTTP protocol that carries results as Apache Arrow. The container serves plain HTTP (no TLS) and keeps its database in memory, so every new container starts empty.
- pytest_databases.docker.hardcharger.exec_hardcharger(container: docker.models.containers.Container, sql: str, *, user: str, password: str) tuple[int, bytes][source]#
Execute SQL inside a Hardcharger container using the bundled
hardchargercommand-line client.The client prints results as CSV with a header row and exits non-zero when a statement fails.
- Parameters:
container – Running Hardcharger container.
sql – SQL to execute; several statements may be separated by semicolons.
user – User name to sign in with.
password – Password to sign in with.
- Returns:
Tuple of
(exit_code, raw_output_bytes). The exit code is-1when the command could not be started.
- pytest_databases.docker.hardcharger.hardcharger_image() str[source]#
Return the container image tag for Hardcharger.
- pytest_databases.docker.hardcharger.hardcharger_password() str[source]#
Return the default administrator password for Hardcharger.
- pytest_databases.docker.hardcharger.hardcharger_service(docker_service: DockerService, xdist_hardcharger_isolation_level: XdistIsolationLevel, hardcharger_image: str, hardcharger_username: str, hardcharger_password: str) Generator[HardchargerService, None, None][source]#
Provide a running Hardcharger service.
- Yields:
A
HardchargerServiceinstance.
- pytest_databases.docker.hardcharger.hardcharger_username() str[source]#
Return the default administrator user name for Hardcharger.
- pytest_databases.docker.hardcharger.provide_hardcharger_service(docker_service: DockerService, isolation_level: XdistIsolationLevel, image: str, username: str, password: str) Generator[HardchargerService, None, None][source]#
Start and yield a Hardcharger container.
The server’s in-memory DuckDB database cannot be split into independent databases per worker, so under
pytest-xdistevery worker gets its own container regardless ofisolation_level.isolation_levelonly decides whether the container is removed when the fixture is torn down (server) or kept until the session’s containers are cleaned up (database).- Parameters:
docker_service – Container lifecycle manager.
isolation_level –
pytest-xdistisolation level.image – Container image tag.
username – The administrator’s user name.
password – The administrator’s password.
- Yields:
A running
HardchargerServiceinstance.