Skip to content

Commit

Permalink
feat: Add configurable template option for postgres database creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Jan 7, 2022
1 parent f83a457 commit 6305307
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-mock-resources"
version = "2.1.9"
version = "2.1.10"
description = "A pytest plugin for easily instantiating reproducible mock resources."
authors = [
"Omar Khan <oakhan3@gmail.com>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def pmr_postgres_config():
return PostgresConfig()


def create_engine_manager(pmr_postgres_config, ordered_actions, tables):
database_name = _create_clean_database(pmr_postgres_config)
def create_engine_manager(
pmr_postgres_config, ordered_actions, tables, createdb_template="template1"
):
database_name = _create_clean_database(pmr_postgres_config, createdb_template=createdb_template)
engine = get_sqlalchemy_engine(pmr_postgres_config, database_name)
assign_fixture_credentials(
engine,
Expand All @@ -34,7 +36,12 @@ def create_engine_manager(pmr_postgres_config, ordered_actions, tables):


def create_postgres_fixture(
*ordered_actions, scope="function", tables=None, session=None, async_=False
*ordered_actions,
scope="function",
tables=None,
session=None,
async_=False,
createdb_template="template1"
):
"""Produce a Postgres fixture.
Expand All @@ -49,16 +56,23 @@ def create_postgres_fixture(
session: Whether to return a session instead of an engine directly. This can
either be a bool or a callable capable of producing a session.
async_: Whether to return an async fixture/client.
createdb_template: The template database used to create sub-databases. "template1" is the
default chosen when no template is specified.
"""
engine_manager_kwargs = dict(
ordered_actions=ordered_actions,
tables=tables,
createdb_template=createdb_template,
)

@pytest.fixture(scope=scope)
def _sync(_postgres_container, pmr_postgres_config):
engine_manager = create_engine_manager(pmr_postgres_config, ordered_actions, tables)
engine_manager = create_engine_manager(pmr_postgres_config, **engine_manager_kwargs)
yield from engine_manager.manage_sync(session=session)

@pytest.fixture(scope=scope)
async def _async(_postgres_container, pmr_postgres_config):
engine_manager = create_engine_manager(pmr_postgres_config, ordered_actions, tables)
engine_manager = create_engine_manager(pmr_postgres_config, **engine_manager_kwargs)
async for engine in engine_manager.manage_async(session=session):
yield engine

Expand All @@ -68,7 +82,7 @@ async def _async(_postgres_container, pmr_postgres_config):
return _sync


def _create_clean_database(config):
def _create_clean_database(config, createdb_template="template1"):
root_engine = get_sqlalchemy_engine(config, config.root_database, isolation_level="AUTOCOMMIT")

try:
Expand All @@ -93,7 +107,7 @@ def _create_clean_database(config):
id_ = tuple(result)[0][0]
database_name = "pytest_mock_resource_db_{}".format(id_)

root_engine.execute('CREATE DATABASE "{}"'.format(database_name))
root_engine.execute('CREATE DATABASE "{}" template={}'.format(database_name, createdb_template))
root_engine.execute(
'GRANT ALL PRIVILEGES ON DATABASE "{}" TO CURRENT_USER'.format(database_name)
)
Expand Down
22 changes: 22 additions & 0 deletions tests/fixture/database/test_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

from pytest_mock_resources import create_postgres_fixture

Base = declarative_base()


class Thing(Base):
__tablename__ = "thing"

id = Column(Integer, autoincrement=True, primary_key=True)


createdb_template_pg = create_postgres_fixture(Base, createdb_template="template0", session=True)


def test_createdb_template(createdb_template_pg):
"""Assert successful usage of a fixture which sets the `createdb_template` argument."""
thing = Thing(id=1)
createdb_template_pg.add(thing)
createdb_template_pg.commit()

0 comments on commit 6305307

Please sign in to comment.