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"

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.4.0.tar.gz (20.4 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.4.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asyncpg_recorder-0.4.0.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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.4.0.tar.gz
Algorithm Hash digest
SHA256 13ac2d036cb99c4683e2820f8a4c7c495940fee0c8604cd66f9b427fff18ed9a
MD5 d59d6753e7c63153ec3af5019bcf2d36
BLAKE2b-256 e7568ac8ebfa60cfb764ce3cd821e01c0a103bc81ffcd0bae351e85d1b405662

See more details on using hashes here.

File details

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

File metadata

  • Download URL: asyncpg_recorder-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b2ac8b107548735af988643283606f4526678c7152bf231ebfd93378c076ee3
MD5 e058232f826fbf4dac2a900ef09fac71
BLAKE2b-256 f0547d6199b75ac66c1f473c71ba205bde3f1ec3a1a89a934b8453a4379ec0f7

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