test(bdd): add direct test for _fast_init_or_upgrade early-return behavior #733

Closed
opened 2026-03-12 13:19:18 +00:00 by CoreRasurae · 2 comments
Member

Metadata

  • Commit Message: test(bdd): add direct test for _fast_init_or_upgrade early-return behavior
  • Branch: test/m3-fast-init-early-return

Summary

The behavioral change introduced in PR #727 (skip Alembic migration for existing non-empty test databases) has no direct test verifying the early-return logic. The 15 scenarios in core_cli_commands.feature pass because of the change (no more hangs), but none explicitly verify that _original_init_or_upgrade is NOT invoked when a non-empty DB exists.

Background and Context

Identified during the code review of PR #727 (Finding #2 in the APPROVED review by @hurui200320). The reviewer recommended a follow-up ticket to add explicit test coverage for this behavior.

Current Behavior

A search for _fast_init_or_upgrade across all .feature files returns zero matches. The early-return logic is implicitly tested (scenarios pass because they no longer hang), but there is no explicit assertion that the original migration runner is skipped.

Expected Behavior

A dedicated test should:

  1. Create a non-empty test database file (matching _SCENARIO_DB_PREFIXES)
  2. Call _fast_init_or_upgrade on it
  3. Assert that _original_init_or_upgrade is NOT invoked (e.g., via mock/patch verification)
  4. Verify the function returns immediately

Acceptance Criteria

  • A test exists that directly exercises _fast_init_or_upgrade with a non-empty DB
  • The test asserts _original_init_or_upgrade is not called
  • The test also verifies the template-copy path works for non-existent DBs
  • Test passes under both sequential and parallel execution

Supporting Information

  • Identified in: PR #727 Code Review, Finding #2 (APPROVED review)
  • Review URL: #727
  • Reviewer: @hurui200320
  • Related PR: #727 (the change being tested)
  • Related Issue: #726

Subtasks

  • Create a unit test or BDD scenario that sets up a non-empty DB file with a matching prefix
  • Mock/patch _original_init_or_upgrade and assert it is not called
  • Add a complementary test for the template-copy path (DB doesn't exist yet)
  • Add a test for the fallback path (non-matching prefix still delegates to original)
  • Run nox -s unit_tests to verify all tests pass

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
## Metadata - **Commit Message**: `test(bdd): add direct test for _fast_init_or_upgrade early-return behavior` - **Branch**: `test/m3-fast-init-early-return` ## Summary The behavioral change introduced in PR #727 (skip Alembic migration for existing non-empty test databases) has no direct test verifying the early-return logic. The 15 scenarios in `core_cli_commands.feature` pass because of the change (no more hangs), but none explicitly verify that `_original_init_or_upgrade` is NOT invoked when a non-empty DB exists. ## Background and Context Identified during the code review of PR #727 (Finding #2 in the APPROVED review by @hurui200320). The reviewer recommended a follow-up ticket to add explicit test coverage for this behavior. ## Current Behavior A search for `_fast_init_or_upgrade` across all `.feature` files returns zero matches. The early-return logic is implicitly tested (scenarios pass because they no longer hang), but there is no explicit assertion that the original migration runner is skipped. ## Expected Behavior A dedicated test should: 1. Create a non-empty test database file (matching `_SCENARIO_DB_PREFIXES`) 2. Call `_fast_init_or_upgrade` on it 3. Assert that `_original_init_or_upgrade` is NOT invoked (e.g., via mock/patch verification) 4. Verify the function returns immediately ## Acceptance Criteria - [ ] A test exists that directly exercises `_fast_init_or_upgrade` with a non-empty DB - [ ] The test asserts `_original_init_or_upgrade` is not called - [ ] The test also verifies the template-copy path works for non-existent DBs - [ ] Test passes under both sequential and parallel execution ## Supporting Information - Identified in: PR #727 Code Review, Finding #2 (APPROVED review) - Review URL: https://git.cleverthis.com/cleveragents/cleveragents-core/pulls/727 - Reviewer: @hurui200320 - Related PR: #727 (the change being tested) - Related Issue: #726 ## Subtasks - [ ] Create a unit test or BDD scenario that sets up a non-empty DB file with a matching prefix - [ ] Mock/patch `_original_init_or_upgrade` and assert it is not called - [ ] Add a complementary test for the template-copy path (DB doesn't exist yet) - [ ] Add a test for the fallback path (non-matching prefix still delegates to original) - [ ] Run `nox -s unit_tests` to verify all tests pass ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done.
freemo added this to the v3.2.0 milestone 2026-03-12 20:22:20 +00:00
Author
Member

Implementation Notes — @CoreRasurae

Design Decisions

  1. Testing technique: Used closure cell manipulation to swap the _original_init_or_upgrade variable captured in _fast_init_or_upgrade's closure with a MagicMock. This tests the actual production function rather than a recreation, providing high confidence in the test's validity.

  2. Mock placement: Created features/mocks/fast_init_test_helpers.py with a patch_original_init_or_upgrade() context manager, following the project rule that all mocking code belongs in features/mocks/.

  3. Scenario coverage: 5 scenarios covering all code paths:

    • Non-empty DB with matching prefix (early return, original NOT called)
    • New DB with template available (template copy path)
    • Non-matching prefix (delegates to original)
    • In-memory SQLite URL (delegates to original)
    • Non-SQLite URL (delegates to original)
  4. Cleanup: All scenarios use _cleanup_handlers to ensure temp files are removed, supporting both sequential and parallel execution.

Key Code Locations

  • features.fast_init_upgrade feature — BDD scenarios (commit 1b1dc3a1)
  • features.steps.fast_init_upgrade_steps — Step definitions (commit 1b1dc3a1)
  • features.mocks.fast_init_test_helpers.patch_original_init_or_upgrade — Mock helper (commit 1b1dc3a1)

Quality Gate Results

Gate Result
nox -s lint PASS
nox -s typecheck PASS (0 errors)
nox -s unit_tests PASS (12,426 scenarios)
nox -s integration_tests PASS (1,727 tests)
nox -s coverage_report PASS (98% >= 97%)

PR

PR #1156: #1156
Commit: 1b1dc3a1e52dbae56d76f812a75e811a0281918f

## Implementation Notes — @CoreRasurae ### Design Decisions 1. **Testing technique**: Used closure cell manipulation to swap the `_original_init_or_upgrade` variable captured in `_fast_init_or_upgrade`'s closure with a `MagicMock`. This tests the actual production function rather than a recreation, providing high confidence in the test's validity. 2. **Mock placement**: Created `features/mocks/fast_init_test_helpers.py` with a `patch_original_init_or_upgrade()` context manager, following the project rule that all mocking code belongs in `features/mocks/`. 3. **Scenario coverage**: 5 scenarios covering all code paths: - Non-empty DB with matching prefix (early return, original NOT called) - New DB with template available (template copy path) - Non-matching prefix (delegates to original) - In-memory SQLite URL (delegates to original) - Non-SQLite URL (delegates to original) 4. **Cleanup**: All scenarios use `_cleanup_handlers` to ensure temp files are removed, supporting both sequential and parallel execution. ### Key Code Locations - `features.fast_init_upgrade` feature — BDD scenarios (commit `1b1dc3a1`) - `features.steps.fast_init_upgrade_steps` — Step definitions (commit `1b1dc3a1`) - `features.mocks.fast_init_test_helpers.patch_original_init_or_upgrade` — Mock helper (commit `1b1dc3a1`) ### Quality Gate Results | Gate | Result | |------|--------| | `nox -s lint` | PASS | | `nox -s typecheck` | PASS (0 errors) | | `nox -s unit_tests` | PASS (12,426 scenarios) | | `nox -s integration_tests` | PASS (1,727 tests) | | `nox -s coverage_report` | PASS (98% >= 97%) | ### PR PR #1156: https://git.cleverthis.com/cleveragents/cleveragents-core/pulls/1156 Commit: `1b1dc3a1e52dbae56d76f812a75e811a0281918f`
Author
Member

Validation/update note for issue #733:

I revalidated the review findings against this issue’s acceptance criteria and the scoped implementation branch (test/m3-fast-init-early-return), then applied all valid fixes that did not conflict with docs/specification.md.

Applied updates:

  • Replaced insecure temp-path creation in fast-init test steps with race-safe temp directory allocation and explicit cleanup.
  • Added an explicit scenario for the existing-empty-DB branch to improve direct behavioral coverage of _fast_init_or_upgrade.
  • Removed inline type suppression in the fast-init closure helper.

Validation checks (with TEST_PROCESSES=9):

  • nox -s unit_tests -- features/fast_init_upgrade.feature --processes 1
  • nox -s unit_tests -- features/fast_init_upgrade.feature --processes 9
  • nox -s lint
  • nox -s typecheck

No remaining scoped findings were rejected as out-of-spec for #733.

Validation/update note for issue #733: I revalidated the review findings against this issue’s acceptance criteria and the scoped implementation branch (`test/m3-fast-init-early-return`), then applied all valid fixes that did not conflict with `docs/specification.md`. Applied updates: - Replaced insecure temp-path creation in fast-init test steps with race-safe temp directory allocation and explicit cleanup. - Added an explicit scenario for the existing-empty-DB branch to improve direct behavioral coverage of `_fast_init_or_upgrade`. - Removed inline type suppression in the fast-init closure helper. Validation checks (with `TEST_PROCESSES=9`): - `nox -s unit_tests -- features/fast_init_upgrade.feature --processes 1` ✅ - `nox -s unit_tests -- features/fast_init_upgrade.feature --processes 9` ✅ - `nox -s lint` ✅ - `nox -s typecheck` ✅ No remaining scoped findings were rejected as out-of-spec for #733.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#733
No description provided.