chore: consolidate platform split checks
This commit is contained in:
26
tests/db_isolation.py
Normal file
26
tests/db_isolation.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
|
||||
from govoplan_core.db.session import DatabaseHandle, configure_database, get_database, reset_database, set_database
|
||||
|
||||
|
||||
@contextmanager
|
||||
def temporary_database(database_url: str) -> Iterator[DatabaseHandle]:
|
||||
try:
|
||||
previous_database = get_database()
|
||||
except RuntimeError:
|
||||
previous_database = None
|
||||
|
||||
database = configure_database(database_url)
|
||||
should_dispose = database is not previous_database
|
||||
try:
|
||||
yield database
|
||||
finally:
|
||||
if should_dispose:
|
||||
database.engine.dispose()
|
||||
if previous_database is None:
|
||||
reset_database()
|
||||
else:
|
||||
set_database(previous_database)
|
||||
Reference in New Issue
Block a user