test: one test-database opener, dbtest.Pool, in place of 39 copies (#8002)

Every package carried its own testPool/newPool/newTestPool: skip under
-short or without MINSTREL_TEST_DATABASE_URL, migrate, connect,
register the close, and usually ResetDB. dbtest.Open does the first
four (migrating once per test binary) and dbtest.Pool adds the reset.
The per-package wrappers that did nothing else are gone; fixture
helpers that seed rows keep their own signatures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:49:02 -05:00
co-authored by Claude Opus 5.5
parent 47e749225e
commit 39fa740bff
69 changed files with 329 additions and 975 deletions
+9 -33
View File
@@ -6,37 +6,13 @@ import (
"errors"
"io"
"log/slog"
"os"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
func newPool(t *testing.T) *pgxpool.Pool {
t.Helper()
if testing.Short() {
t.Skip("skipping integration test in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
dbtest.ResetDB(t, pool)
return pool
}
func newService(t *testing.T, pool *pgxpool.Pool) *Service {
t.Helper()
s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
@@ -80,7 +56,7 @@ func auditRows(t *testing.T, pool *pgxpool.Pool) []struct {
}
func TestNew_SeedsShippedDefaults(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
t.Errorf("radio weights = %+v, want shipped defaults", got)
@@ -98,7 +74,7 @@ func TestNew_SeedsShippedDefaults(t *testing.T) {
}
func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
if err := s.UpdateProfile(context.Background(), ScopeRadio,
map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
@@ -132,7 +108,7 @@ func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
}
func TestUpdateProfile_Validation(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
cases := []struct {
name string
@@ -159,7 +135,7 @@ func TestUpdateProfile_Validation(t *testing.T) {
}
func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
// Moving neutral above full must reject on the PATCHED result.
err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
@@ -183,7 +159,7 @@ func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
}
func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
// In-range update persists into cache + the assembled taste config.
if err := s.UpdateTaste(context.Background(),
@@ -204,7 +180,7 @@ func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
}
func TestUpdateTaste_EraScale(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
// In-range update persists into cache + the assembled taste config.
if err := s.UpdateTaste(context.Background(),
@@ -225,7 +201,7 @@ func TestUpdateTaste_EraScale(t *testing.T) {
}
func TestUpdateTaste_MoodScale(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
if err := s.UpdateTaste(context.Background(),
map[string]float64{"mood_scale": 0.8}); err != nil {
@@ -244,7 +220,7 @@ func TestUpdateTaste_MoodScale(t *testing.T) {
}
func TestReset_RestoresShippedAndAudits(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
// Reset with nothing changed is a no-op (no audit row).
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
@@ -270,7 +246,7 @@ func TestReset_RestoresShippedAndAudits(t *testing.T) {
}
func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s := newService(t, pool)
shipped := ShippedRadioWeights()
if err := s.UpdateProfile(context.Background(), ScopeRadio,