improve unit tests

This commit is contained in:
Evan Jarrett
2026-02-09 23:19:01 -06:00
parent aad9ebfc8b
commit 53de92e5d3
4 changed files with 18 additions and 11 deletions
+5 -2
View File
@@ -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)
}
+4 -3
View File
@@ -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)
}
+5 -4
View File
@@ -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
}
+4 -2
View File
@@ -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)
}