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:
@@ -13,32 +13,10 @@ import (
|
||||
"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 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
|
||||
}
|
||||
|
||||
func discardLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
@@ -97,7 +75,7 @@ func seedAlbumWithTrack(t *testing.T, pool *pgxpool.Pool, title, artistName, mbi
|
||||
}
|
||||
|
||||
func TestEnrichAlbum_SidecarFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "Sid", "Artist", "")
|
||||
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("img"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -122,7 +100,7 @@ func TestEnrichAlbum_SidecarFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichAlbum_NoSidecarNoMBID_SettlesNone(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, _ := seedAlbumWithTrack(t, pool, "NoMbid", "Artist", "")
|
||||
resetRegistryForTests() // deterministic empty registry (no provider can supply)
|
||||
t.Cleanup(resetRegistryForTests)
|
||||
@@ -143,7 +121,7 @@ func TestEnrichAlbum_NoSidecarNoMBID_SettlesNone(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichAlbum_ProviderFetchSuccess(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "Prov", "Artist", "test-mbid-123")
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -194,7 +172,7 @@ func TestEnrichAlbum_ProviderFetchSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichAlbum_All404_RecordsNone(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, _ := seedAlbumWithTrack(t, pool, "Missing", "Artist", "missing-mbid")
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -231,7 +209,7 @@ func TestEnrichAlbum_All404_RecordsNone(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichAlbum_AlreadySidecar_NoOp(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "Already", "Artist", "")
|
||||
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("first"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -258,7 +236,7 @@ func TestEnrichAlbum_AlreadySidecar_NoOp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichBatch_DrainsNullSourceOnly(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id1, dir1 := seedAlbumWithTrack(t, pool, "Drain1", "A", "")
|
||||
if err := os.WriteFile(filepath.Join(dir1, "cover.jpg"), []byte("c"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -305,7 +283,7 @@ func TestEnrichBatch_DrainsNullSourceOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRetryAlbum_DeletesWrittenFileAndRefetches(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "Retry", "Artist", "retry-mbid")
|
||||
|
||||
hits := 0
|
||||
@@ -351,7 +329,7 @@ func TestRetryAlbum_DeletesWrittenFileAndRefetches(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnrichRetryMissing_DrainsNullAndNone(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
// Three albums:
|
||||
// id1: NULL source, has sidecar → drains, becomes 'sidecar'.
|
||||
// id2: 'none' source, has sidecar → cleared then drained, becomes 'sidecar'.
|
||||
@@ -412,7 +390,7 @@ func TestEnrichRetryMissing_DrainsNullAndNone(t *testing.T) {
|
||||
// TestEnrichAlbum_MultiProviderChain verifies that when provider A
|
||||
// returns ErrNotFound, the enricher continues to provider B.
|
||||
func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "Multi", "Artist", "multi-mbid")
|
||||
|
||||
// Provider A: always 404.
|
||||
@@ -473,7 +451,7 @@ func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
|
||||
// TestEnrichAlbum_TransientLeavesNull verifies that a transient error
|
||||
// from the provider leaves the row NULL for next-pass retry.
|
||||
func TestEnrichAlbum_TransientLeavesNull(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, _ := seedAlbumWithTrack(t, pool, "Transient", "Artist", "transient-mbid")
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -514,7 +492,7 @@ func TestEnrichAlbum_TransientLeavesNull(t *testing.T) {
|
||||
// cover_art_source='none' and a stale version gets cleared and
|
||||
// re-enriched (sidecar wins because the album dir now has a cover file).
|
||||
func TestEnrichAlbum_StaleNoneFlipsAndRetries(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
id, dir := seedAlbumWithTrack(t, pool, "StaleNone", "Artist", "")
|
||||
|
||||
// Mark the album as 'none' (simulating a previous pass).
|
||||
|
||||
Reference in New Issue
Block a user