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:
@@ -3,8 +3,6 @@ package quarantine
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -12,32 +10,10 @@ 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"
|
||||
)
|
||||
|
||||
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, slog.New(slog.NewTextHandler(io.Discard, nil))); 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{
|
||||
@@ -79,7 +55,7 @@ func seedTrack(t *testing.T, pool *pgxpool.Pool, title, mbid string) (dbq.Track,
|
||||
}
|
||||
|
||||
func TestFlag_HappyPath(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track, _, _ := seedTrack(t, pool, "Bad Track", "abc")
|
||||
|
||||
@@ -97,7 +73,7 @@ func TestFlag_HappyPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFlag_UpsertOnSecondFlag(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||
|
||||
@@ -118,7 +94,7 @@ func TestFlag_UpsertOnSecondFlag(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
|
||||
var bogus pgtype.UUID
|
||||
@@ -133,7 +109,7 @@ func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFlag_BadReasonRejected(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||
|
||||
@@ -145,7 +121,7 @@ func TestFlag_BadReasonRejected(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnflag_DeletesRow(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||
|
||||
@@ -162,7 +138,7 @@ func TestUnflag_DeletesRow(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestListMine_OrderedNewestFirst(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
t1, _, _ := seedTrack(t, pool, "T1", "x")
|
||||
t2, _, _ := seedTrack(t, pool, "T2", "y")
|
||||
@@ -189,7 +165,7 @@ func TestListMine_OrderedNewestFirst(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestListAdminQueue_AggregatesByTrackWithReasonCounts(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
carol := seedUser(t, pool, "carol")
|
||||
@@ -226,7 +202,7 @@ func TestListAdminQueue_AggregatesByTrackWithReasonCounts(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolve_ClearsRowsAndWritesAudit(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||
@@ -256,7 +232,7 @@ func TestResolve_ClearsRowsAndWritesAudit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||
|
||||
@@ -275,7 +251,7 @@ func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolve_TrackNotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
var bogus pgtype.UUID
|
||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||
@@ -289,7 +265,7 @@ func TestResolve_TrackNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
q := dbq.New(pool)
|
||||
|
||||
@@ -333,7 +309,7 @@ func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
|
||||
// the listeners' rows go, open playback errors read deleted, and the history
|
||||
// says the file was removed. A "wrong file" report drops the file's MBID.
|
||||
func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
alice := seedUser(t, pool, "alice")
|
||||
admin := seedUser(t, pool, "admin")
|
||||
@@ -401,7 +377,7 @@ func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteFile_TrackNotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
var bogus pgtype.UUID
|
||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||
|
||||
Reference in New Issue
Block a user