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:
@@ -2,41 +2,16 @@ package playsessions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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 testPool(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 seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
||||
t.Helper()
|
||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
@@ -52,7 +27,7 @@ func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
||||
}
|
||||
|
||||
func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedTestUser(t, pool)
|
||||
q := dbq.New(pool)
|
||||
|
||||
@@ -77,7 +52,7 @@ func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedTestUser(t, pool)
|
||||
q := dbq.New(pool)
|
||||
|
||||
@@ -106,7 +81,7 @@ func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedTestUser(t, pool)
|
||||
q := dbq.New(pool)
|
||||
|
||||
@@ -125,7 +100,7 @@ func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindOrCreate_AtExactBoundaryExtendsExisting(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedTestUser(t, pool)
|
||||
q := dbq.New(pool)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user