Files
cleveragents-core/features/fast_init_upgrade.feature
CoreRasurae b21e0fedea test(bdd): add direct test for _fast_init_or_upgrade early-return behavior
Add 8 BDD scenarios in features/fast_init_upgrade.feature that directly
exercise the _fast_init_or_upgrade closure installed by
_install_template_db_patch in features/environment.py.

Scenarios cover all code paths:
- Non-empty DB with matching prefix → early return (original NOT invoked)
- Non-existent DB with matching prefix → template copied (original NOT invoked)
- Existing empty DB with matching prefix → template copied (original NOT invoked)
- Non-matching prefix → delegates to original init_or_upgrade
- In-memory SQLite → delegates to original init_or_upgrade
- Non-SQLite URL → delegates to original init_or_upgrade
- Bare sqlite:// URL → delegates to original init_or_upgrade
- Delegation forwards runner instance and keyword arguments correctly

Test infrastructure:
- features/mocks/fast_init_test_helpers.py provides a context manager
  (patch_original_init_or_upgrade) that replaces the _original_init_or_upgrade
  reference inside the closure via cell_contents manipulation, enabling
  precise call-tracking without recreating the function under test.
- All scenarios tagged @mock_only to skip unnecessary DB setup.
- Cleanup registered via context._cleanup_handlers for temp file removal.
- Works in both sequential and parallel execution modes.

Review fix round:
- Added bare sqlite:// URL scenario covering the second branch of the
  in-memory disjunction (L1).
- Added scenario verifying runner instance and keyword argument forwarding
  through the delegation path (L2, L3).
- Replaced hardcoded test credentials with clearly synthetic pattern (I4).
- Fixed CHANGELOG wording that incorrectly claimed mktemp replacement (L5).

ISSUES CLOSED: #733
2026-04-01 23:52:20 +01:00

60 lines
2.8 KiB
Gherkin

Feature: Fast init_or_upgrade early-return behavior
As the test harness
I need _fast_init_or_upgrade to skip Alembic migrations for existing databases
So that parallel test execution does not hang on redundant migrations
Background:
Given the fast-init template-DB patch is installed
@mock_only
Scenario: Non-empty database with matching prefix skips original migration
Given a non-empty SQLite database file with a matching scenario prefix
When I call init_or_upgrade on the fast-init database
Then the original init_or_upgrade should not have been invoked
And the database file content should be unchanged
@mock_only
Scenario: Template is copied for a new database with matching prefix
Given a non-existent SQLite database path with a matching scenario prefix
When I call init_or_upgrade on the fast-init database
Then the database file should be a copy of the template
And the original init_or_upgrade should not have been invoked
@mock_only
Scenario: Existing empty database with matching prefix copies template
Given an empty SQLite database file with a matching scenario prefix
When I call init_or_upgrade on the fast-init database
Then the database file should be a copy of the template
And the original init_or_upgrade should not have been invoked
@mock_only
Scenario: Non-matching prefix delegates to original init_or_upgrade
Given a SQLite database URL whose filename has a non-matching prefix
When I call init_or_upgrade on the fast-init database
Then the original init_or_upgrade should have been invoked exactly once
@mock_only
Scenario: In-memory SQLite delegates to original init_or_upgrade
Given an in-memory SQLite database URL for fast-init testing
When I call init_or_upgrade on the fast-init database
Then the original init_or_upgrade should have been invoked exactly once
@mock_only
Scenario: Non-SQLite URL delegates to original init_or_upgrade
Given a non-SQLite database URL for fast-init testing
When I call init_or_upgrade on the fast-init database
Then the original init_or_upgrade should have been invoked exactly once
@mock_only
Scenario: Bare sqlite:// URL delegates to original init_or_upgrade
Given a bare sqlite:// database URL for fast-init testing
When I call init_or_upgrade on the fast-init database
Then the original init_or_upgrade should have been invoked exactly once
@mock_only
Scenario: Delegation forwards runner instance and keyword arguments
Given a non-SQLite database URL for fast-init testing
When I call init_or_upgrade with keyword arguments on the fast-init database
Then the original init_or_upgrade should have been invoked exactly once
And the original should have received the runner instance and keyword arguments