Skip to main content

allure-db-client

A tiny helper around psycopg for running PostgreSQL queries in tests and utilities, with optional Allure reporting of executed SQL and results.

The package exposes two clients:

  • DBClient — sync client (psycopg Connection)
  • AsyncDBClient — async client (psycopg AsyncConnection)

Both clients support a small, convenient API for common cases and can be used as context managers to automatically open/close connections.

Requirements

  • Python 3.11
  • PostgreSQL accessible via a connection string

Installation

Install from PyPI:

pip install allure-db-client

Or with Poetry:

poetry add allure-db-client

Connection string

Use a standard psycopg connection URI, for example:

postgresql://USER:PASSWORD@HOST:PORT/DBNAME

Example for local Dockerized Postgres:

postgresql://postgres:postgres@localhost:5432/postgres

Quick start (sync)

from allure_db_client import DBClient

conn_str = "postgresql://postgres:postgres@localhost:5432/postgres"

# with_allure=True will attach query and result to Allure report (if Allure is used in your tests)
with DBClient(connection_string=conn_str, with_allure=True) as db:
    # read examples
    rows = db.select_all("SELECT 1 AS one, 2 AS two")
    first_row = db.get_first_row("SELECT 42")         # -> (42,)
    first_value = db.get_first_value("SELECT 'hi'")   # -> 'hi'
    ids = db.get_list("SELECT generate_series(1, 3)") # -> [1, 2, 3]

    # get_dict expects first two columns to be key/value
    pairs = db.get_dict("SELECT 1, 'a' UNION ALL SELECT 2, 'b'") # -> {1: 'a', 2: 'b'}

    # write example
    db.execute("CREATE TEMP TABLE t(id int)")
    db.execute("INSERT INTO t(id) VALUES (%(id)s)", params={"id": 7})

Quick start (async)

import asyncio
from allure_db_client import AsyncDBClient

conn_str = "postgresql://postgres:postgres@localhost:5432/postgres"

async def main():
    async with AsyncDBClient(connection_string=conn_str, with_allure=True) as db:
        rows = await db.select_all("SELECT 1")
        value = await db.get_first_value("SELECT 'ok'")
        await db.execute("CREATE TEMP TABLE t(id int)")

asyncio.run(main())

API overview

  • select_all(query, params=None) -> list[tuple]
  • get_first_row(query, params=None) -> tuple | None
  • get_first_value(query, params=None) -> Any | None
  • get_list(query, params=None) -> list[Any]
  • get_dict(query, params=None) -> dict[Any, Any] | None
  • execute(query, params=None) -> None

Notes:

  • params is a dict passed to psycopg; use named placeholders like %(name)s in SQL.
  • If with_allure=True, each call attaches the SQL and (for read queries) the result to the Allure report.

Using with Allure

The clients can attach SQL and results to Allure steps when with_allure=True. Add allure-pytest to your test environment and run tests with Allure:

pip install allure-pytest
pytest --alluredir=./allure-results

Then open the report with your Allure CLI.

Running tests locally

This repository includes a minimal test setup using Docker. It will start Postgres and run tests against it.

Prerequisites:

  • Docker and docker-compose

Commands (from project root):

cd tests
docker compose up --build --abort-on-container-exit

The tests container will set PG_CONNECTION_STRING for the clients automatically, as defined in tests/docker-compose.yml.

License

MIT — see LICENSE.

Release files for allure-db-client 25.298

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for allure-db-client 25.298
File Size Uploaded
allure_db_client-25.298.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for allure-db-client 25.298
File Interpreter ABI Platform
allure_db_client-25.298-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.3 kB

Release files / allure_db_client-25.298.tar.gz

Download URL allure_db_client-25.298.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
090345f3e07cbbe34c96358c256120e56953f722130ec77fee861063d99c6116
BLAKE2b-256 checksum
How to use checksums
81110db6a2fcfddebc34f74ddca40e07d3a25ff229cd09f47a7f8a98e45d161a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

Release files / allure_db_client-25.298-py2.py3-none-any.whl

Download URL allure_db_client-25.298-py2.py3-none-any.whl
Size 8.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
042163ba18beaed6f92849aacfc6036f70ca4ad387a9095170e0b1011f14e895
BLAKE2b-256 checksum
How to use checksums
d3cb4d6c6567adfb05488562b9a659df6125b24094cc7becca35af702614e8df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

25.298 This release

2 release files

25.297

2 release files

1.4.1

2 release files

1.4

2 release files

1.3.8

2 release files

1.3.7

2 release files

1.3.6

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page