Refactor test fixtures to include session parameter for improved database handling

This commit is contained in:
Matthieu B
2025-10-26 16:06:05 +01:00
parent 159a25dae3
commit 1fb5881495
2 changed files with 13 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ def admin_user(app):
"""Create an admin account for authenticated requests."""
with app.app_context():
created = False
previous_hash = None
admin = AdminAccount.query.filter_by(username="testadmin").first()
if not admin:
admin = AdminAccount(username="testadmin")
@@ -25,10 +26,20 @@ def admin_user(app):
db.session.add(admin)
db.session.commit()
created = True
else:
previous_hash = admin.password_hash
admin.set_password("TestPass123")
db.session.commit()
yield admin
if created:
db.session.delete(admin)
db.session.commit()
elif previous_hash is not None:
# Restore the original password hash so other tests keep their expectations
admin = AdminAccount.query.filter_by(username="testadmin").first()
if admin:
admin.password_hash = previous_hash
db.session.commit()
@pytest.fixture

View File

@@ -8,7 +8,7 @@ from app.models import AdminAccount, ApiKey, Library, MediaServer, Settings
@pytest.fixture
def api_key(app):
def api_key(app, session):
"""Create a test API key."""
with app.app_context():
# Create admin account if it doesn't exist
@@ -45,7 +45,7 @@ def api_key(app):
@pytest.fixture
def test_server(app):
def test_server(app, session):
"""Create a test media server."""
with app.app_context():
# Create admin account if it doesn't exist