Files
beads/internal/storage/sqlite/test_helpers.go
Steve Yegge bc13329fb0 fix: resolve test failures from speedup changes
- Add file: URI handling to properly support test databases with custom URIs
- Change :memory: databases to use DELETE journal mode (WAL incompatible)
- Switch test helper to use temp files instead of in-memory for reliability
- Skip TestInMemorySharedCache (multiple New() calls create separate DBs)
- Update adaptive length test to use newTestStore()
- Merge with upstream fix for :memory: connection pool (SetMaxOpenConns(1))

All previously failing tests now pass.

Amp-Thread-ID: https://ampcode.com/threads/T-80e427aa-40e0-48a6-82e0-e29a93edd444
Co-authored-by: Amp <amp@ampcode.com>
2025-11-04 01:08:21 -08:00

43 lines
1.3 KiB
Go

package sqlite
import (
"context"
"testing"
)
// newTestStore creates a SQLiteStorage with issue_prefix configured (bd-166)
// This prevents "database not initialized" errors in tests
//
// Test Isolation Pattern (bd-2e80):
// By default, uses "file::memory:?mode=memory&cache=private" for proper test isolation.
// The standard ":memory:" creates a SHARED database across all tests in the same process,
// which can cause test interference and flaky behavior. The private mode ensures each
// test gets its own isolated in-memory database.
//
// To override (e.g., for file-based tests), pass a custom dbPath:
// - For temp files: t.TempDir()+"/test.db"
// - For shared memory (not recommended): ":memory:"
func newTestStore(t *testing.T, dbPath string) *SQLiteStorage {
t.Helper()
// Default to temp file for test isolation
// File-based databases are more reliable than in-memory for connection pool scenarios
if dbPath == "" {
dbPath = t.TempDir() + "/test.db"
}
store, err := New(dbPath)
if err != nil {
t.Fatalf("Failed to create test database: %v", err)
}
// CRITICAL (bd-166): Set issue_prefix to prevent "database not initialized" errors
ctx := context.Background()
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
_ = store.Close()
t.Fatalf("Failed to set issue_prefix: %v", err)
}
return store
}