Skip to main content

Automatically mock your Postgres interactions to simplify and speed up testing

Project description

Asyncpg Recorder

Build Status Sonarcloud Status PyPI - Version LICENSE status: active

Installation

uv add asyncpg-recorder

Usage

import asyncpg
from asyncpg_recorder import use_cassette


async def query():
    con = await asyncpg.connect(DSN)
    res = await con.fetch("SELECT NOW();")
    await con.close()
    return res


@use_cassette
def test_select_now_replay():
    query()

When using pytest parametrized fixtures put the @use_cassette decorator on the test function not the fixture:

import asyncpg
from asyncpg_recorder import use_cassette
import pytest
import pytest_asyncio


@pytest_asyncio.fixture(params=[False, True])
async def param(request):
    return request.param


@pytest.mark.asyncio
@pytest.mark.usefixtures("param")
@use_cassette
async def test_parametrized_fixtures(path):
    async def query():
      con = await asyncpg.connect(DSN)
      res = await con.fetch("SELECT NOW();")
      await con.close()
      return res

    await select_version()

If you want to save the cassettes in a specific directory, set the variable cassettes-dir in your pyproject.toml. The path should be relative to pyproject.toml.

[tool.asyncpg-recorder]
"cassettes-dir"="tests/cassettes"

Caveats

The same database request on Python >= 3.14 yields a different hash for cassette entry than on Python <= 3.13. If it is important to run tests against both Python version ranges you need to record twice.

Development

uv run prek install  # pre-commit
uv run pytest

Release

This project uses SemVer.

To make a new release run ./scripts/release.sh <version number>.

Limitation

  • Works only with pytest
  • Depends on testcontainers
    • Testcontainers is used to boot up a temporary Postgres instance to which asyncpg will be connected.
    • This slows test suite down.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncpg_recorder-0.10.3.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncpg_recorder-0.10.3-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file asyncpg_recorder-0.10.3.tar.gz.

File metadata

  • Download URL: asyncpg_recorder-0.10.3.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for asyncpg_recorder-0.10.3.tar.gz
Algorithm Hash digest
SHA256 22e0abf23f6567fbd5b79e391bf092c33ba0807c227af43d5785ca27b3dd5d0b
MD5 a7fdd684ae3485440337560555a44da2
BLAKE2b-256 262cc8e8e38b5201932cb8912a1eea09d8040450a0ae2c8a564b940898e73d39

See more details on using hashes here.

File details

Details for the file asyncpg_recorder-0.10.3-py3-none-any.whl.

File metadata

  • Download URL: asyncpg_recorder-0.10.3-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for asyncpg_recorder-0.10.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6bc8e8976aea595d30560b5f48dd3212d04a573158596ff532872dd97cd78acb
MD5 d7dc3a9f6a9d4700bad8d637ef343c1a
BLAKE2b-256 c0f7de65d2828d14bd577627109c7aa34195d50c0917e63b629a3ffc6a7b59ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page