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,16 +3,12 @@ package tracks
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
@@ -20,27 +16,6 @@ import (
|
||||
|
||||
// --- test seed helpers (inlined; mirrors quarantine patterns) -----------
|
||||
|
||||
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 seedAdmin(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||
t.Helper()
|
||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
@@ -108,7 +83,7 @@ func markMissing(t *testing.T, pool *pgxpool.Pool, ids ...pgtype.UUID) {
|
||||
}
|
||||
|
||||
func TestForgetTracks_NotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
admin := seedAdmin(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}
|
||||
@@ -123,7 +98,7 @@ func TestForgetTracks_NotFound(t *testing.T) {
|
||||
// A track that has its file plays, and is not forgotten; nor is the waiting
|
||||
// track asked about with it.
|
||||
func TestForgetTracks_RefusesATrackThatHasItsFile(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
admin := seedAdmin(t, pool, "alice")
|
||||
dir := t.TempDir()
|
||||
_, _, present := seedArtistAlbumTrack(t, pool, "Present", "", filepath.Join(dir, "p.mp3"))
|
||||
@@ -145,7 +120,7 @@ func TestForgetTracks_RefusesATrackThatHasItsFile(t *testing.T) {
|
||||
// Forget deletes the waiting tracks, tidies away the album and artist they
|
||||
// leave empty, and names each in the audit log.
|
||||
func TestForgetTracks_DeletesWaitingTracksAndAudits(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
admin := seedAdmin(t, pool, "alice")
|
||||
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
||||
t.Fatalf("reset audit_log: %v", err)
|
||||
@@ -185,7 +160,7 @@ func TestForgetTracks_DeletesWaitingTracksAndAudits(t *testing.T) {
|
||||
|
||||
// An album that keeps another track is left alone.
|
||||
func TestForgetTracks_KeepsAnAlbumThatStillHasTracks(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
admin := seedAdmin(t, pool, "alice")
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user