diff --git a/quetz/database.py b/quetz/database.py index 0ba97cf8..b4809cb7 100644 --- a/quetz/database.py +++ b/quetz/database.py @@ -65,9 +65,7 @@ def on_close(dbapi_conn, conn_record): def get_session_maker( bind: sqlalchemy.engine.Engine | sqlalchemy.engine.Connection, ) -> Callable[[], sessionmaker]: - return sessionmaker( - autocommit=False, autoflush=True, bind=bind, expire_on_commit=True - ) + return sessionmaker(autocommit=False, bind=bind) def get_session(config: Config | None) -> Session: diff --git a/quetz/testing/fixtures.py b/quetz/testing/fixtures.py index d7d27a19..0e0a4a78 100644 --- a/quetz/testing/fixtures.py +++ b/quetz/testing/fixtures.py @@ -19,7 +19,9 @@ def pytest_configure(config): pytest.quetz_variables = { - var: value for var, value in os.environ.items() if var.startswith("QUETZ_") + var: value + for var, value in os.environ.items() + if var.startswith("QUETZ_") and not var.startswith("QUETZ_TEST") } for var in pytest.quetz_variables: del os.environ[var] @@ -158,10 +160,10 @@ def wrapper(*args, **kwargs): @pytest.fixture def db( session_maker: sqlalchemy.orm.sessionmaker, -) -> Iterator[sqlalchemy.orm.Session]: +) -> sqlalchemy.orm.Session: with mock.patch("quetz.database.get_session", session_maker): with session_maker() as db: - yield db + return db @pytest.fixture diff --git a/quetz/tests/api/test_users.py b/quetz/tests/api/test_users.py index 4f3473bd..9a643039 100644 --- a/quetz/tests/api/test_users.py +++ b/quetz/tests/api/test_users.py @@ -176,7 +176,6 @@ def test_delete_user_permission( other_user, auth_client, db, user_role, target_user, expected_status, user, api_keys ): response = auth_client.delete(f"/api/users/{target_user}") - deleted_user = db.query(User).filter(User.username == target_user).one_or_none() if expected_status == 200: @@ -196,11 +195,12 @@ def test_delete_user_permission( assert deleted_user.api_keys_user # check if other users were not accidently removed - existing_user = db.query(User).filter(User.username != target_user).one_or_none() - assert existing_user - assert existing_user.profile - assert existing_user.api_keys_owner - assert existing_user.api_keys_user + existing_users = db.query(User).filter(User.username != target_user).all() + assert existing_users + for existing_user in existing_users: + assert existing_user.profile + assert existing_user.api_keys_owner + assert existing_user.api_keys_user @pytest.mark.parametrize("user_role", ["owner"]) diff --git a/quetz/tests/test_workers.py b/quetz/tests/test_workers.py index b8a1353f..c5436eea 100644 --- a/quetz/tests/test_workers.py +++ b/quetz/tests/test_workers.py @@ -8,6 +8,7 @@ from quetz.authorization import Rules from quetz.dao import Dao +from quetz.database import get_session_maker from quetz.db_models import User from quetz.tasks.workers import RQManager, SubprocessWorker, ThreadingWorker @@ -124,19 +125,23 @@ def function_with_dao(dao: Dao): @pytest.fixture -def db_cleanup(config): +def db_cleanup(engine): # we can't use the db fixture for cleaning up because # it automatically rollsback all operations yield - from quetz.database import get_session - - with get_session(config) as db: - user = db.query(User).one_or_none() - if user: - db.delete(user) - db.commit() + with engine.connect() as con: + session_maker = get_session_maker(con) + with session_maker() as db: + user = db.query(User).one_or_none() + if user: + db.delete(user) + db.commit() + + with session_maker() as db: + user = db.query(User).one_or_none() + assert user is None @pytest.mark.asyncio