Skip to content

fix: resolve integration test transaction lifecycle issues#125

Merged
JacobCoffee merged 5 commits intomainfrom
feature/fix-integration-tests
Nov 23, 2025
Merged

fix: resolve integration test transaction lifecycle issues#125
JacobCoffee merged 5 commits intomainfrom
feature/fix-integration-tests

Conversation

@JacobCoffee
Copy link
Owner

Summary

Fixes 5 failing integration tests in test_api_endpoints.py by correcting transaction handling patterns.

Root Cause

The db_session fixture uses a transaction context manager that auto-rolls back:

async with async_session() as session:
    async with session.begin():
        yield session

When tests called await db_session.commit(), it closed the transaction prematurely while still inside the context manager, causing subsequent queries to fail with:

sqlalchemy.exc.InvalidRequestError: Can't operate on closed transaction inside context manager.

Changes

Fixed 5 tests by removing unnecessary commit() calls and relying on flush() for within-transaction visibility:

  1. test_full_guild_with_all_configs_lifecycle - Removed commit() after delete operation
  2. test_guild_with_multiple_sotags_and_users - Removed commit() before verification queries
  3. test_duplicate_guild_id_rejected - Removed commit() and rollback() (fixture handles cleanup)
  4. test_cascade_delete_all_related_configs - Removed two commit() calls
  5. test_pagination_offset_and_limit - Changed pageSize to limit parameter + removed commit()

Testing

  • ✅ All 1036 tests passing (previously 1031/1036)
  • ✅ All prek hooks passing
  • ✅ Full make ci passing

Technical Details

  • flush() makes objects visible within the same transaction for subsequent queries
  • commit() is not needed in tests using the db_session fixture (auto-rollback ensures isolation)
  • Database integrity constraints (unique, foreign key) are enforced during flush(), not just commit()
  • Cascade delete behavior works correctly with flush() within transactions

Commits

  • 0e22872 - fix: remove transaction commit from full guild lifecycle test
  • c1fe78a - fix: remove transaction commit from multiple SO tags test
  • 4e74d54 - fix: remove transaction commit from duplicate guild ID test
  • eb9cd45 - fix: remove transaction commits from cascade delete test
  • 05a848a - fix: use correct pagination parameter in list guilds test

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

The db_session fixture uses a transaction context manager that
auto-rolls back. Calling commit() within the transaction context
closes the transaction and breaks subsequent queries. Fixed by
relying on flush() for within-transaction visibility.

- Remove commit() call after delete operation
- Database queries see flushed changes within transaction
- Test verifies cascade delete behavior correctly

Fixes test: TestFullGuildLifecycleWithAllConfigs::test_full_guild_with_all_configs_lifecycle
Remove commit() call that closes transaction prematurely.
Flush() provides sufficient visibility for verification queries
within the same transaction context.

Fixes test: TestFullGuildLifecycleWithAllConfigs::test_guild_with_multiple_sotags_and_users
Database integrity constraints are enforced during flush(),
not just on commit(). Remove unnecessary commit() and rollback()
calls - the fixture handles transaction cleanup.

Fixes test: TestDatabaseIntegrity::test_duplicate_guild_id_rejected
Cascade delete behavior works within transactions after flush().
Remove commit() calls that close transaction prematurely.

Fixes test: TestDatabaseIntegrity::test_cascade_delete_all_related_configs
The guilds list endpoint uses 'limit' and 'offset' parameters,
not 'pageSize' and 'currentPage'. Update test to match API contract.

This test was sending pageSize=5 but the endpoint ignored it and
used the default limit of 20, causing the assertion to fail.
Also remove unnecessary commit() call from test setup.

Fixes test: TestAPIPaginationConsistency::test_pagination_offset_and_limit
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @JacobCoffee, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@railway-app
Copy link

railway-app bot commented Nov 23, 2025

🚅 Deployed to the byte-pr-125 environment in byte

Service Status Web Updated (UTC)
byte ◻️ Removed (View Logs) Nov 23, 2025 at 9:43 pm

@railway-app railway-app bot temporarily deployed to byte (byte / byte-pr-125) November 23, 2025 21:40 Destroyed
@JacobCoffee JacobCoffee merged commit a1fa62f into main Nov 23, 2025
4 checks passed
@JacobCoffee JacobCoffee deleted the feature/fix-integration-tests branch November 23, 2025 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant