Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasAlbertQC committed Jul 22, 2024
1 parent 020c2bf commit a04dc69
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
4 changes: 1 addition & 3 deletions quetz/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions quetz/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions quetz/tests/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"])
Expand Down
21 changes: 13 additions & 8 deletions quetz/tests/test_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a04dc69

Please sign in to comment.