feat(order): Add finalize order functionality

This commit is contained in:
2025-12-13 11:53:41 +01:00
parent 00246819cc
commit fd544fcebc
9 changed files with 161 additions and 35 deletions

View File

@@ -28,14 +28,14 @@ Base.metadata.create_all(bind=connection)
# Bind sessions to the single connection so all sessions share the same DB
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=connection)
test_session = None
def get_test_session():
"""Dependency override for get_session"""
db = TestSessionLocal()
try:
yield db
finally:
db.close()
assert test_session is not None, "test_session is not set"
yield test_session
test_session.flush()
app.dependency_overrides[get_session] = get_test_session
@@ -57,21 +57,11 @@ def unauthorized_client():
def test_db():
"""Provides a database session for direct test usage"""
db = TestSessionLocal()
# Start a SAVEPOINT so test changes can be rolled back without
# closing the shared connection. Also restart the nested transaction
# when the session issues commits internally.
db.begin_nested()
@event.listens_for(db, "after_transaction_end")
def restart_savepoint(session, transaction):
# If the nested transaction ended, re-open it for continued isolation
if transaction.nested and not session.is_active:
session.begin_nested()
global test_session
test_session = db
try:
yield db
finally:
# Rollback to the SAVEPOINT and close the session to clean up
test_session = None
db.rollback()
db.close()
db.close()