Skip to content

Commit

Permalink
Merge 9c4bb29 into f5317d9
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpunk committed Jan 25, 2021
2 parents f5317d9 + 9c4bb29 commit 12f8b0e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ or use it as a context manager:
DatabaseJanitor manages the state of the database, but you'll have to create
connection to use in test code yourself.

You can optionally pass in a recognized postgresql ISOLATION_LEVEL for
additional control.

Package resources
-----------------

Expand Down
8 changes: 6 additions & 2 deletions src/pytest_postgresql/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from pytest_postgresql.janitor import DatabaseJanitor
from pytest_postgresql.executor import PostgreSQLExecutor
from pytest_postgresql.port import get_port
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def get_config(request: FixtureRequest) -> dict:
Expand Down Expand Up @@ -213,14 +214,17 @@ def postgresql_noproc_fixture(request: FixtureRequest) -> NoopExecutor:


def postgresql(
process_fixture_name: str, db_name: str = None, load: List[str] = None
process_fixture_name: str, db_name: str = None, load: List[str] = None,
isolation_level: int = ISOLATION_LEVEL_AUTOCOMMIT,
) -> Callable[[FixtureRequest], connection]:
"""
Return connection fixture factory for PostgreSQL.
:param process_fixture_name: name of the process fixture
:param db_name: database name
:param load: SQL to automatically load into our test database
:param isolation_level: optional postgresql isolation level
defaults to ISOLATION_LEVEL_AUTOCOMMIT
:returns: function which makes a connection to postgresql
"""

Expand Down Expand Up @@ -252,7 +256,7 @@ def postgresql_factory(request: FixtureRequest) -> connection:

with DatabaseJanitor(
pg_user, pg_host, pg_port, pg_db, proc_fixture.version,
pg_password
pg_password, isolation_level
):
db_connection: connection = psycopg2.connect(
dbname=pg_db,
Expand Down
9 changes: 7 additions & 2 deletions src/pytest_postgresql/janitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pkg_resources import parse_version

from pytest_postgresql.compat import psycopg2, cursor
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

Version = type(parse_version('1')) # pylint:disable=invalid-name

Expand All @@ -23,7 +24,8 @@ def __init__(
port: str,
db_name: str,
version: Union[str, float, Version],
password: str = None
password: str = None,
isolation_level: int = ISOLATION_LEVEL_AUTOCOMMIT,
) -> None:
"""
Initialize janitor.
Expand All @@ -34,12 +36,15 @@ def __init__(
:param db_name: database name
:param version: postgresql version number
:param password: optional postgresql password
:param isolation_level: optional postgresql isolation level
defaults to ISOLATION_LEVEL_AUTOCOMMIT
"""
self.user = user
self.password = password
self.host = host
self.port = port
self.db_name = db_name
self.isolation_level = isolation_level
if not isinstance(version, Version):
self.version = parse_version(str(version))
else:
Expand Down Expand Up @@ -79,7 +84,7 @@ def cursor(self) -> cursor:
host=self.host,
port=self.port,
)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
conn.set_isolation_level(self.isolation_level)
cur = conn.cursor()
try:
yield cur
Expand Down

0 comments on commit 12f8b0e

Please sign in to comment.