Actually fix the tests?

This commit is contained in:
Dries Peeters
2025-11-13 13:52:03 +01:00
parent 4ddd4fba41
commit 7c0128bda5
3 changed files with 40 additions and 32 deletions
+19
View File
@@ -24,6 +24,7 @@ from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
import posthog
from sqlalchemy.pool import StaticPool
# Load environment variables
load_dotenv()
@@ -205,6 +206,24 @@ def create_app(config=None):
if config:
app.config.update(config)
# Special handling for SQLite in-memory DB during tests:
# ensure a single shared connection so objects don't disappear after commit.
try:
db_uri = str(app.config.get("SQLALCHEMY_DATABASE_URI", "") or "")
if app.config.get("TESTING") and db_uri.startswith("sqlite") and ":memory:" in db_uri:
engine_opts = dict(app.config.get("SQLALCHEMY_ENGINE_OPTIONS") or {})
engine_opts.setdefault("poolclass", StaticPool)
engine_opts.setdefault("connect_args", {"check_same_thread": False})
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = engine_opts
# Avoid attribute expiration on commit during tests to keep objects usable
if app.config.get("TESTING"):
session_opts = dict(app.config.get("SQLALCHEMY_SESSION_OPTIONS") or {})
session_opts.setdefault("expire_on_commit", False)
app.config["SQLALCHEMY_SESSION_OPTIONS"] = session_opts
except Exception:
# Do not fail app creation for engine option tweaks
pass
# Add top-level templates directory in addition to app/templates
extra_templates_path = os.path.abspath(
os.path.join(app.root_path, "..", "templates")
+11 -24
View File
@@ -242,22 +242,13 @@ def test_client(app, user):
)
client.status = 'active' # Set after creation
db.session.add(client)
db.session.commit()
# Store ID before attempting refresh
# Flush to assign primary key before commit to avoid expired attribute reloads
db.session.flush()
client_id = client.id
# Only refresh if client is still in session
try:
db.session.refresh(client)
except Exception:
# If refresh fails, re-query the client using stored ID
client = Client.query.get(client_id)
if client is None:
# If still None, try filter_by as fallback
client = Client.query.filter_by(id=client_id).first()
return client
db.session.commit()
# Re-query to ensure we return a persistent instance without relying on refresh
persisted_client = Client.query.get(client_id) or Client.query.filter_by(id=client_id).first()
return persisted_client
@pytest.fixture
@@ -297,16 +288,12 @@ def project(app, test_client):
)
project.status = 'active' # Set after creation
db.session.add(project)
# Flush to assign ID before commit, then re-query after commit
db.session.flush()
project_id = project.id
db.session.commit()
# Only refresh if project is still in session
try:
db.session.refresh(project)
except Exception:
# If refresh fails, re-query the project
project = Project.query.filter_by(id=project.id).first()
return project
persisted_project = Project.query.get(project_id) or Project.query.filter_by(id=project_id).first()
return persisted_project
@pytest.fixture
+10 -8
View File
@@ -63,10 +63,10 @@ def test_calendar_event_creation(app, user, project):
event_type="meeting"
)
db.session.add(event)
db.session.commit()
# Store ID immediately after commit (before any potential cleanup)
# Flush first to assign PK, then store ID before commit to avoid reloads
db.session.flush()
event_id = event.id
db.session.commit()
assert event_id is not None, "Event should have an ID after commit"
# Verify properties on the event object immediately after commit
@@ -390,12 +390,14 @@ def test_calendar_event_user_relationship(app, user):
event_type="event"
)
db.session.add(event)
db.session.flush()
ev_id = event.id
db.session.commit()
db.session.refresh(event)
assert event.user is not None
assert event.user.id == user.id
assert event.user.username == user.username
persisted = CalendarEvent.query.get(ev_id)
assert persisted is not None
assert persisted.user is not None
assert persisted.user.id == user.id
assert persisted.user.username == user.username
@pytest.mark.unit