Skip to content

Commit

Permalink
Fixes #357 add option to alter isolation level
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpunk committed Jan 11, 2021
1 parent 59b95bf commit 3bc6b55
Show file tree
Hide file tree
Showing 3 changed files with 14 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
7 changes: 5 additions & 2 deletions src/pytest_postgresql/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,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 = 0,
) -> 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 +255,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
8 changes: 6 additions & 2 deletions src/pytest_postgresql/janitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(
port: str,
db_name: str,
version: Union[str, float, Version],
password: str = None
password: str = None,
isolation_level: int = 0,
) -> None:
"""
Initialize janitor.
Expand All @@ -34,12 +35,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 +83,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 3bc6b55

Please sign in to comment.