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:
@@ -5,9 +5,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
@@ -15,31 +12,9 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
|
||||
"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")
|
||||
}
|
||||
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 index(name string) int {
|
||||
return slices.IndexFunc(backup.Tables, func(t backup.Table) bool { return t.Name == name })
|
||||
}
|
||||
@@ -47,7 +22,7 @@ func index(name string) int {
|
||||
// Every table in the schema is backed up or excluded with a reason, so a new
|
||||
// table fails here until someone decides which.
|
||||
func TestEveryTableIsAccountedFor_Integration(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
rows, err := pool.Query(context.Background(), "SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -64,7 +39,7 @@ func TestEveryTableIsAccountedFor_Integration(t *testing.T) {
|
||||
// A restore loads each table after the ones it references, and never needs a
|
||||
// table the backup leaves out.
|
||||
func TestTablesComeAfterWhatTheyReference_Integration(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
rows, err := pool.Query(context.Background(), `
|
||||
SELECT cl.relname, pc.relname
|
||||
FROM pg_constraint co
|
||||
@@ -104,7 +79,7 @@ func TestTablesComeAfterWhatTheyReference_Integration(t *testing.T) {
|
||||
// Every omitted column exists, so a renamed column fails here rather than a
|
||||
// sealed credential slipping into the file under its new name.
|
||||
func TestOmittedColumnsExist_Integration(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
for _, tbl := range backup.Tables {
|
||||
for _, col := range tbl.Omit {
|
||||
var n int
|
||||
@@ -137,7 +112,7 @@ func mustID(t *testing.T, pool *pgxpool.Pool, sql string, args ...any) string {
|
||||
// A backup restored into an emptied instance gives back the same rows, byte
|
||||
// for byte, without the credentials and markers it leaves out.
|
||||
func TestARestoredInstanceHoldsWhatTheBackupHeld_Integration(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
|
||||
user := mustID(t, pool, `INSERT INTO users (username, password_hash, api_token_hash, listenbrainz_token)
|
||||
|
||||
Reference in New Issue
Block a user