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,43 +4,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
const (
|
||||
apes = "0a11ce00-0000-4000-8000-000000000001"
|
||||
ghostNights = "0a11ce00-0000-4000-8000-000000000002"
|
||||
@@ -95,7 +68,7 @@ func (f *fakeMB) client() *musicbrainz.Client {
|
||||
func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mb := newFakeMB(t)
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
s := New(pool, mb.client(), nil)
|
||||
q := dbq.New(pool)
|
||||
|
||||
@@ -139,7 +112,7 @@ func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T
|
||||
func TestRefreshReleaseGroup_UnknownAndUnreachable_Integration(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mb := newFakeMB(t)
|
||||
s := New(newPool(t), mb.client(), nil)
|
||||
s := New(dbtest.Pool(t), mb.client(), nil)
|
||||
|
||||
if err := s.RefreshReleaseGroup(ctx, "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) {
|
||||
t.Errorf("unknown group: err = %v, want ErrNotFound", err)
|
||||
|
||||
Reference in New Issue
Block a user