A pytest automatic engine fixture managing a postgres cluster.
Added tag 0.3.2 for changeset be341509a1ea
pkg: follow weird psycopg2 packaging policy change
Added tag 0.3.1 for changeset b745326898cd

clone

read-only
https://hg.sr.ht/~pythonian/pytest_sa_pg
read/write
ssh://hg@hg.sr.ht/~pythonian/pytest_sa_pg

#PYTEST SQLALCHEMY/POSTGRES FIXTURE

#Automatic engine fixture with postgres cluster

From a conftest.py it is sufficient to write this:

from pytest_sa_pg import fixture
    from myapp import schema

    engine = fixture.engine_fixture(schema.meta, 'data', 5433)

This creates a session-scoped fixture.

The postgres database files for the local test cluster will be then created under data/pgdb.

From this, in test modules, one can use the fixture as such:

def test_foo(engine):
        name = engine.execute('select name from person where person.id = 42').scalar()
        assert name == 'Babar'

If there is a schema initialization function, it can also be given to engine_fixture:

from myapp import schema
    engine = fixture.engine_fixture(schema.meta, 'data', 5433,
                                    # must accept `engine` and `meta` objects
                                    schema.init)

#Directly using the postgres cluster handler

Sometimes the default fixture is not sufficient (e.g. because one uses namespace schemas and these are not automatically handled by sqlalchemy).

We can use the library as such:

import pytest
    from pytest_sa_pg import db

    @pytest.fixture(scope='session')
    def engine(request):
        db.setup_local_pg_cluster(request, 'data', 5433)
        ...

Sometimes it is convenient to adjust a configuration parameter. The setup_local_pg_cluster function takes a parameter to handle this, as such:

db.setup_local_pg_cluster(request, 'data', 5433,
                              postgresql_conf_options={
                                  'max_locks_per_transaction': 42
                              })