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

Tests

uv run pre-commit install
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.3.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.3.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asyncpg_recorder-0.3.0.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for asyncpg_recorder-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8e4f0225e6a0e4c7367bfddc51782fe27a6db4d0fe0934d7e05b5f60cd98cbd3
MD5 2e77db6b47a08c6106fe7ad28fbf52ab
BLAKE2b-256 7c2e96db8baf5e6781ddc124ea96a55668ecdd519891149b5f15b314d09f96ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for asyncpg_recorder-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9bccfd6591b8ec476800f105feb5b44c3876b94caf8482874475d68a711c5e21
MD5 30ae89f4febca6e67baa6068fd617d4b
BLAKE2b-256 de4d2b7983ae40681b45d2f7bebb3d5901d3fad526de9b1c84254872965caa08

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