diff --git a/pyproject.toml b/pyproject.toml index aeeed464..c9aa038e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ dependencies = [ dev = [ "factory_boy<4", "pytest-factoryboy<3", - "pytest", + "pytest<8.4", "pytest-cov", "sphinx-autobuild<=2024.5", ] diff --git a/snowexsql/db.py b/snowexsql/db.py index ec012f63..9a2c6a88 100644 --- a/snowexsql/db.py +++ b/snowexsql/db.py @@ -22,9 +22,7 @@ def initialize(engine): """ - Creates the original database from scratch, currently only for - point data - + Creates the original database from scratch. """ meta = Base.metadata meta.drop_all(bind=engine) @@ -117,7 +115,10 @@ def db_session_with_credentials(credentials_path=None): credentials_path (string): Full path to credentials file (Optional) """ - yield get_db(credentials_path) + engine, session = get_db(credentials_path) + yield engine, session + session.close() + engine.dispose() def get_table_attributes(DataCls): diff --git a/tests/test_db.py b/tests/test_db.py index ed03bdb7..ce9d2d3f 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -7,6 +7,7 @@ load_credentials) from sqlalchemy import Engine, MetaData from sqlalchemy.orm import Session +from sqlalchemy import text @pytest.fixture(scope='function') @@ -75,8 +76,13 @@ def test_db_session_with_credentials(self, monkeypatch, test_db_info): engine = test_engine session = test_session - assert isinstance(engine, Engine) - assert isinstance(session, Session) + assert isinstance(engine, Engine) + assert isinstance(session, Session) + # Query to create a transaction + session.query(text('1')).all() + + # On session.close(), all transactions should be gone + assert session._transaction is None @pytest.mark.usefixtures('db_connection_string_patch') @pytest.mark.parametrize("return_metadata, expected_objs", [