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:
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -12,19 +11,12 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
func newTestPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), url)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
pool := dbtest.Open(t)
|
||||
// Reset audit_log so each test runs against a known state.
|
||||
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
||||
t.Fatalf("reset audit_log: %v", err)
|
||||
@@ -102,14 +94,7 @@ func TestWriteOrLog_Success(t *testing.T) {
|
||||
// closed pool, and asserts the logger captured a Warn record with
|
||||
// action + err keys.
|
||||
func TestWriteOrLog_DBFailure_Logs(t *testing.T) {
|
||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), url)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
pool := dbtest.Open(t)
|
||||
pool.Close() // force subsequent ops to fail
|
||||
|
||||
var buf bytes.Buffer
|
||||
@@ -135,14 +120,7 @@ func TestWriteOrLog_DBFailure_Logs(t *testing.T) {
|
||||
// TestWriteOrLog_NilLogger ensures a nil logger + failing pool does
|
||||
// not panic. Uses a closed pool to force the failure branch.
|
||||
func TestWriteOrLog_NilLogger(t *testing.T) {
|
||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), url)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
pool := dbtest.Open(t)
|
||||
pool.Close()
|
||||
|
||||
var nilUUID pgtype.UUID
|
||||
|
||||
Reference in New Issue
Block a user