GithubHelp home page GithubHelp logo

inerv / asyncpg-engine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sivakov512/asyncpg-engine

0.0 0.0 0.0 25 KB

Little wrapper around asyncpg for specific experience.

License: MIT License

Nix 9.00% Python 91.00%

asyncpg-engine's Introduction

asyncpg-engine

Small wrapper around asyncpg for specific experience and transactional testing.

Build Status Code style: black Python versions PyPi

Basic usage

from asyncpg_engine import Engine


engine = await Engine.create("postgres://guest:guest@localhost:5432/guest?sslmode=disable")

async with engine.acquire() as con:
    # https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection
    assert await con.fetchval("SELECT 1") == 1

Custom type conversions

You can specify custom encoder/decoder by subclassing Engine:

from asyncpg_engine import Engine
import orjson


class MyEngine(Engine):

    @staticmethod
    async def _set_codecs(con: Connection) -> None:
        # https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection.set_type_codec
        await con.set_type_codec(
            "json", encoder=orjson.dumps, decoder=orjson.loads, schema="pg_catalog"
        )

Pytest plugin

Library includes pytest plugin with support for transactional testing.

To start using it install pytest, enable plugin in your root conftest.py and define postgres_url fixture that returns database connection string:

pytest_plugins = ["asyncpg_engine"]


@pytest.fixture()
def postgres_url() -> str:
    return "postgres://guest:guest@localhost:5432/guest?sslmode=disable"

Now you can use two fixtures:

  • db that returns Engine instance:
async def test_returns_true(db):
    async with db.acquire() as con:
        assert await con.fetchval("SELECT true")
  • con that returns already acquired connection:
async def test_returns_true(con):
    assert await con.fetchval("SELECT true")

By default Engine is configured for transactional testing, so every call to db.acquire or con usage will return the same connection with already started transaction. Transaction is rolled back at the end of test, so all your changes in db are rolled back too.

You can override this behaviour with asyncpg_engine mark:

@pytest.mark.asyncpg_engine(transactional=False)
async def test_returns_true(con):
    assert await con.fetchval("SELECT true")


@pytest.mark.asyncpg_engine(transactional=False)
async def test_returns_true_too(db):
    async with db.acquire() as con:
        assert await con.fetchval("SELECT true")

If you want to use your own custom Engine subclass in tests you can define asyncpg_engine_cls fixture that returns it:

from asyncpg_engine import Engine


class MyPrettyEngine(Engine):
    pass


@pytest.fixture()
def asyncpg_engine_cls() -> typing.Type[MyPrettyEngine]:
    return MyPrettyEngine


async def test_returns_my_pretty_engine(db: MyPrettyEngine) -> None:
    assert isinstance(db, MyPrettyEngine)

Development and contribution

First of all you should install Poetry using official instructions or solutions provided by your distro. Then install dependencies:

poetry install

Run PostgreSQL using provided docker-compose configuration:

docker-compose up  # run it in another terminal or add `-d` to daemonize

Project uses a combination of flake8, black, isort and mypy for linting and pytest for testing.

poetry run flake8
poetry run mypy ./
poetry run pytest

asyncpg-engine's People

Contributors

konstantin-popov avatar sivakov512 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.