From 53de92e5d353113a62dcddc9944c48391bb60a97 Mon Sep 17 00:00:00 2001 From: Evan Jarrett Date: Mon, 9 Feb 2026 23:19:01 -0600 Subject: [PATCH] improve unit tests --- pkg/appview/db/annotations_test.go | 7 +++++-- pkg/appview/db/device_store_test.go | 7 ++++--- pkg/appview/db/hold_store_test.go | 9 +++++---- pkg/appview/db/session_store_test.go | 6 ++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/pkg/appview/db/annotations_test.go b/pkg/appview/db/annotations_test.go index 463901f..5f91b44 100644 --- a/pkg/appview/db/annotations_test.go +++ b/pkg/appview/db/annotations_test.go @@ -2,6 +2,8 @@ package db import ( "database/sql" + "fmt" + "strings" "testing" ) @@ -20,8 +22,9 @@ func TestAnnotations_Placeholder(t *testing.T) { func setupAnnotationsTestDB(t *testing.T) *sql.DB { t.Helper() - // Use file::memory: with cache=shared to ensure all connections share the same in-memory DB - db, err := InitDB("file::memory:?cache=shared", LibsqlConfig{}) + // Use a named in-memory DB unique to this test to ensure isolation between tests + safeName := strings.ReplaceAll(t.Name(), "/", "_") + db, err := InitDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", safeName), LibsqlConfig{}) if err != nil { t.Fatalf("Failed to initialize test database: %v", err) } diff --git a/pkg/appview/db/device_store_test.go b/pkg/appview/db/device_store_test.go index 5917a90..f0b5695 100644 --- a/pkg/appview/db/device_store_test.go +++ b/pkg/appview/db/device_store_test.go @@ -2,6 +2,7 @@ package db import ( "context" + "fmt" "strings" "testing" "time" @@ -12,9 +13,9 @@ import ( // setupTestDB creates an in-memory SQLite database for testing func setupTestDB(t *testing.T) *DeviceStore { t.Helper() - // Use file::memory: with cache=shared to ensure all connections share the same in-memory DB - // This prevents race conditions where different connections see different databases - db, err := InitDB("file::memory:?cache=shared", LibsqlConfig{}) + // Use a named in-memory DB unique to this test to ensure isolation between tests + safeName := strings.ReplaceAll(t.Name(), "/", "_") + db, err := InitDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", safeName), LibsqlConfig{}) if err != nil { t.Fatalf("Failed to initialize test database: %v", err) } diff --git a/pkg/appview/db/hold_store_test.go b/pkg/appview/db/hold_store_test.go index fa24162..3df0a98 100644 --- a/pkg/appview/db/hold_store_test.go +++ b/pkg/appview/db/hold_store_test.go @@ -2,6 +2,8 @@ package db import ( "database/sql" + "fmt" + "strings" "testing" "time" ) @@ -80,15 +82,14 @@ func TestNullString(t *testing.T) { func setupHoldTestDB(t *testing.T) *sql.DB { t.Helper() - // Use file::memory: with cache=shared to ensure all connections share the same in-memory DB - db, err := InitDB("file::memory:?cache=shared", LibsqlConfig{}) + // Use a named in-memory DB unique to this test to ensure isolation between tests + safeName := strings.ReplaceAll(t.Name(), "/", "_") + db, err := InitDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", safeName), LibsqlConfig{}) if err != nil { t.Fatalf("Failed to initialize test database: %v", err) } // Limit to single connection to avoid race conditions in tests db.SetMaxOpenConns(1) - // Clean slate: shared-cache in-memory DB may retain data from prior subtests - db.Exec("DELETE FROM hold_captain_records") t.Cleanup(func() { db.Close() }) return db } diff --git a/pkg/appview/db/session_store_test.go b/pkg/appview/db/session_store_test.go index 61b9440..2a4ebc7 100644 --- a/pkg/appview/db/session_store_test.go +++ b/pkg/appview/db/session_store_test.go @@ -2,6 +2,7 @@ package db import ( "context" + "fmt" "net/http" "net/http/httptest" "strings" @@ -12,8 +13,9 @@ import ( // setupSessionTestDB creates an in-memory SQLite database for testing func setupSessionTestDB(t *testing.T) *SessionStore { t.Helper() - // Use file::memory: with cache=shared to ensure all connections share the same in-memory DB - db, err := InitDB("file::memory:?cache=shared", LibsqlConfig{}) + // Use a named in-memory DB unique to this test to ensure isolation between tests + safeName := strings.ReplaceAll(t.Name(), "/", "_") + db, err := InitDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", safeName), LibsqlConfig{}) if err != nil { t.Fatalf("Failed to initialize test database: %v", err) }