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:
2026-11-26 12:49:02 -05:00
co-authored by Claude Opus 5.5
parent 47e749225e
commit 39fa740bff
69 changed files with 329 additions and 975 deletions
+10 -9
View File
@@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
"git.fabledsword.com/bvandeusen/minstrel/internal/mailer"
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
)
@@ -30,7 +31,7 @@ func digestWith(pool *pgxpool.Pool) (*notifications.Digest, *mailer.FakeSender)
}
func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", true)
n := notifications.New(pool, nil, nil)
@@ -57,7 +58,7 @@ func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
}
func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
alice := emailUser(t, pool, "alice", true)
@@ -74,7 +75,7 @@ func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
}
func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
alice := emailUser(t, pool, "alice", true)
@@ -99,7 +100,7 @@ func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
}
func TestDigest_NoAddressNoEmail(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
bob := mkUser(t, dbq.New(pool), "bob", true)
n := notifications.New(pool, nil, nil)
@@ -111,7 +112,7 @@ func TestDigest_NoAddressNoEmail(t *testing.T) {
}
func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
root := emailUser(t, pool, "root", true)
@@ -134,7 +135,7 @@ func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
}
func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", true)
n := notifications.New(pool, nil, nil)
@@ -158,7 +159,7 @@ func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
}
func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", true) // timezone defaults to UTC
n := notifications.New(pool, nil, nil)
@@ -192,7 +193,7 @@ func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
}
func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
@@ -209,7 +210,7 @@ func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
}
func TestDigest_UsesTheConfiguredBatchWindow(t *testing.T) {
pool := testPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", true)
_, err := notifications.SaveEmailSettings(ctx, dbq.New(pool), notifications.EmailSettings{SummaryHour: 9, BatchWindowMinutes: 15})
+10 -34
View File
@@ -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)