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:
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -16,7 +15,6 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
@@ -25,27 +23,6 @@ var seedCounter uint64
|
||||
|
||||
func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||
|
||||
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")
|
||||
}
|
||||
if err := db.Migrate(dsn, discardLogger()); 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 seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||
t.Helper()
|
||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
@@ -142,7 +119,7 @@ func tagWeight(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, tag string)
|
||||
// positive weight; an early-skipped artist earns a negative one. Genre tags
|
||||
// follow the same sign.
|
||||
func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
now := time.Now().UTC()
|
||||
u := seedUser(t, pool, "tp1")
|
||||
|
||||
@@ -176,7 +153,7 @@ func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) {
|
||||
// completed plays must NOT make a loved artist net-negative (the user's
|
||||
// concern that a track-dislike shouldn't tank the artist).
|
||||
func TestBuildTasteProfile_AggregationProtectsArtist(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
now := time.Now().UTC()
|
||||
u := seedUser(t, pool, "tp2")
|
||||
fav := seedArtist(t, pool, "Fav")
|
||||
@@ -202,7 +179,7 @@ func TestBuildTasteProfile_AggregationProtectsArtist(t *testing.T) {
|
||||
// TestBuildTasteProfile_ArtistLikeBonus: an explicitly-liked artist with no
|
||||
// plays still earns a positive weight from the like bonus.
|
||||
func TestBuildTasteProfile_ArtistLikeBonus(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
u := seedUser(t, pool, "tp3")
|
||||
liked := seedArtist(t, pool, "LikedOnly")
|
||||
if _, err := pool.Exec(context.Background(),
|
||||
@@ -222,7 +199,7 @@ func TestBuildTasteProfile_ArtistLikeBonus(t *testing.T) {
|
||||
|
||||
// TestBuildTasteProfile_AtomicReplace: a second build replaces, not appends.
|
||||
func TestBuildTasteProfile_AtomicReplace(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
u := seedUser(t, pool, "tp4")
|
||||
|
||||
Reference in New Issue
Block a user