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,43 +5,19 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"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/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
||||
)
|
||||
|
||||
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 mkUser(t *testing.T, q *dbq.Queries, name string, admin bool) pgtype.UUID {
|
||||
t.Helper()
|
||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
@@ -71,7 +47,7 @@ func count(t *testing.T, payload []byte) int64 {
|
||||
}
|
||||
|
||||
func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
alice := mkUser(t, q, "alice", false)
|
||||
@@ -103,7 +79,7 @@ func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
flagger := mkUser(t, q, "admin-flagger", true)
|
||||
@@ -122,7 +98,7 @@ func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotify_CoalescedCountAddsUpWhileUnread_AndStartsAfreshOnceRead(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
admin := mkUser(t, q, "admin", true)
|
||||
@@ -150,7 +126,7 @@ func TestNotify_CoalescedCountAddsUpWhileUnread_AndStartsAfreshOnceRead(t *testi
|
||||
// created_at would announce it again on the phone, and a cleared emailed_at
|
||||
// would email it again.
|
||||
func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
admin := mkUser(t, q, "admin", true)
|
||||
@@ -186,7 +162,7 @@ func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
|
||||
// Each scan's new music is its own notice: one already emailed in the daily
|
||||
// summary must not quietly take in albums that arrive after it.
|
||||
func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
alice := mkUser(t, q, "alice", false)
|
||||
@@ -198,7 +174,7 @@ func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
admin := mkUser(t, q, "admin", true)
|
||||
@@ -215,7 +191,7 @@ func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
muted := mkUser(t, q, "muted", false)
|
||||
@@ -233,7 +209,7 @@ func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T)
|
||||
}
|
||||
|
||||
func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
alice := mkUser(t, q, "alice", false)
|
||||
@@ -261,7 +237,7 @@ func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
|
||||
// added in Go without its migration fails here, not in production at INSERT
|
||||
// time.
|
||||
func TestEveryKindPassesTheSchemaChecks(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
user := mkUser(t, q, "kinds", true)
|
||||
@@ -278,7 +254,7 @@ func TestEveryKindPassesTheSchemaChecks(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRetention_TrimsOldReadRowsAndAncientUnreadOnes(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
ctx := context.Background()
|
||||
user := mkUser(t, q, "retention", false)
|
||||
|
||||
Reference in New Issue
Block a user