Skip to content

Commit

Permalink
feat: Add the ability to specify custom engine kwargs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Feb 8, 2022
1 parent 7950e5f commit a87b234
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 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.11"
version = "2.1.12"
description = "A pytest plugin for easily instantiating reproducible mock resources."
authors = [
"Omar Khan <oakhan3@gmail.com>",
Expand Down
16 changes: 4 additions & 12 deletions src/pytest_mock_resources/container/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ def root_database(self):
raise NotImplementedError()


def get_sqlalchemy_engine(config, database_name, isolation_level=None):
URI_TEMPLATE = (
"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}?sslmode=disable"
)
def get_sqlalchemy_engine(config, database_name, **engine_kwargs):
URI_TEMPLATE = "postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}?sslmode=disable"
DB_URI = URI_TEMPLATE.format(
host=config.host,
port=config.port,
Expand All @@ -61,16 +59,12 @@ def get_sqlalchemy_engine(config, database_name, isolation_level=None):
database=database_name,
)

options = {}
if isolation_level:
options["isolation_level"] = isolation_level

# Trigger any psycopg2-based import failures
from pytest_mock_resources.compat import psycopg2

psycopg2.connect

engine = sqlalchemy.create_engine(DB_URI, **options)
engine = sqlalchemy.create_engine(DB_URI, **engine_kwargs)

# Verify engine is connected
engine.connect()
Expand All @@ -83,9 +77,7 @@ def check_postgres_fn(config):
get_sqlalchemy_engine(config, config.root_database)
except sqlalchemy.exc.OperationalError:
raise ContainerCheckFailed(
"Unable to connect to a presumed Postgres test container via given config: {}".format(
config
)
"Unable to connect to a presumed Postgres test container via given config: {}".format(config)
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def pmr_postgres_config():


def create_engine_manager(
pmr_postgres_config, ordered_actions, tables, createdb_template="template1"
pmr_postgres_config, ordered_actions, tables, createdb_template="template1", engine_kwargs=None
):
database_name = produce_clean_database(pmr_postgres_config, createdb_template=createdb_template)

engine = get_sqlalchemy_engine(pmr_postgres_config, database_name)
engine = get_sqlalchemy_engine(pmr_postgres_config, database_name, **(engine_kwargs or {}))
assign_fixture_credentials(
engine,
drivername="postgresql+psycopg2",
Expand All @@ -42,7 +42,8 @@ def create_postgres_fixture(
tables=None,
session=None,
async_=False,
createdb_template="template1"
createdb_template="template1",
engine_kwargs=None,
):
"""Produce a Postgres fixture.
Expand All @@ -59,11 +60,13 @@ def create_postgres_fixture(
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_kwargs: Optional set of kwargs to send into the engine on creation.
"""
engine_manager_kwargs = dict(
ordered_actions=ordered_actions,
tables=tables,
createdb_template=createdb_template,
engine_kwargs=engine_kwargs,
)

@pytest.fixture(scope=scope)
Expand Down

0 comments on commit a87b234

Please sign in to comment.