Skip to content

Automatically mock your Postgres interactions to simplify and speed up testing

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING
Notifications You must be signed in to change notification settings

GIScience/asyncpg-recorder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asyncpg Recorder

Installation

uv add git+https://github.com/GIScience/asyncpg-recorder.git

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 pytest

Release

This project uses CalVer.

Format is: YYYY.MM.DD (E.g 2025.9.2).

In case of releasing twice on one day add a micro number starting with 1: YYYY.MM.DD_micro (E.g. 2025.9.2_1).

To make a new release run ./scripts/release.sh YYYY.MM.DD

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.

Inbox

See inbox.md

About

Automatically mock your Postgres interactions to simplify and speed up testing

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •