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
+4 -4
View File
@@ -158,7 +158,7 @@ func addTrack(t *testing.T, q *dbq.Queries, album dbq.Album, artist dbq.Artist,
// restoring `mbid = EXCLUDED.mbid` in UpsertTrack: the first re-read below
// then erases the looked-up id.
func TestUpsertTrack_KeepsALookedUpMbidUntilATagCarriesOne_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
@@ -198,7 +198,7 @@ func TestUpsertTrack_KeepsALookedUpMbidUntilATagCarriesOne_Integration(t *testin
}
func TestAcoustIDLookup_FillsOnlyWhatItCanSettle_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
@@ -269,7 +269,7 @@ func TestAcoustIDLookup_FillsOnlyWhatItCanSettle_Integration(t *testing.T) {
}
func TestAcoustIDLookup_StopsWhenTheServiceCannotAnswer_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
@@ -306,7 +306,7 @@ func TestAcoustIDLookup_StopsWhenTheServiceCannotAnswer_Integration(t *testing.T
// similarity worker picks the track as a seed and can map ListenBrainz's
// answers back to it.
func TestAcoustIDLookup_FilledTrackReachesSimilarity_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+2 -1
View File
@@ -9,6 +9,7 @@ import (
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
@@ -115,7 +116,7 @@ func TestComputeAlbumLoudness(t *testing.T) {
// stored histograms, does nothing when nothing changed, and recomputes on
// each kind of membership change.
func TestAlbumLoudness_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+5 -29
View File
@@ -3,9 +3,7 @@ package library
import (
"context"
"errors"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"syscall"
@@ -14,32 +12,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 seedTrack(t *testing.T, pool *pgxpool.Pool, filePath string) (dbq.Track, dbq.Album, dbq.Artist) {
t.Helper()
q := dbq.New(pool)
@@ -76,7 +52,7 @@ func stubRemoveFile(t *testing.T, fn func(string) error) {
}
func TestDeleteTrackFile_HappyPath(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
q := dbq.New(pool)
dir := t.TempDir()
@@ -114,7 +90,7 @@ func TestDeleteTrackFile_HappyPath(t *testing.T) {
}
func TestDeleteTrackFile_EmptiedAlbumAndArtistGoToo(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
q := dbq.New(pool)
path := filepath.Join(t.TempDir(), "lone.mp3")
@@ -157,7 +133,7 @@ func TestDeleteTrackFile_UnremovableFileDeletesNothing(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
q := dbq.New(pool)
dir := t.TempDir()
@@ -202,7 +178,7 @@ func TestDeleteTrackFile_UnremovableFileDeletesNothing(t *testing.T) {
}
func TestDeleteTrackFile_FileAlreadyGoneSucceeds(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
q := dbq.New(pool)
track, _, _ := seedTrack(t, pool, "/no/such/file/anywhere.mp3")
@@ -216,7 +192,7 @@ func TestDeleteTrackFile_FileAlreadyGoneSucceeds(t *testing.T) {
}
func TestDeleteTrackFile_NotFoundReturnsErr(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
var bogus pgtype.UUID
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
+1 -1
View File
@@ -32,7 +32,7 @@ type mergeFixture struct {
func newMergeFixture(t *testing.T) mergeFixture {
t.Helper()
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+4 -3
View File
@@ -12,6 +12,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
@@ -26,7 +27,7 @@ type resolveFixture struct {
func newResolveFixture(t *testing.T) resolveFixture {
t.Helper()
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := filepath.Join(t.TempDir(), "Plastic Apes", "Mammalz (2017)")
@@ -145,7 +146,7 @@ type crossReleaseRipFixture struct {
func newCrossReleaseRipFixture(t *testing.T) crossReleaseRipFixture {
t.Helper()
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
root := t.TempDir()
@@ -279,7 +280,7 @@ type oneAlbumFixture struct {
func newOneAlbumFixture(t *testing.T, tier, titleA, titleB string) oneAlbumFixture {
t.Helper()
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := filepath.Join(t.TempDir(), "Savanta", "Void")
+2 -1
View File
@@ -14,6 +14,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
@@ -41,7 +42,7 @@ func TestCoveredByDismissal(t *testing.T) {
// out, and how re-sweeping treats a dismissal and a proposal that no longer
// holds.
func TestDuplicateSweep_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
@@ -13,12 +13,13 @@ import (
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// TestFingerprintBackfill_Integration pins which tracks a pass touches, that a
// pass ends, and that the coverage gauge counts what the pass wrote.
func TestFingerprintBackfill_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+2 -17
View File
@@ -14,9 +14,8 @@ import (
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// TestScanner_FingerprintsOnlyNewOrChangedBytes_Integration pins WHEN the scan
@@ -27,24 +26,10 @@ import (
// The fingerprinter is stubbed. CI has no real audio, and the tools' output is
// covered by the parser tests; this covers the scan's decisions.
func TestScanner_FingerprintsOnlyNewOrChangedBytes_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
@@ -6,6 +6,8 @@ import (
"math"
"testing"
"time"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
func TestValidateFingerprintSettings(t *testing.T) {
@@ -64,7 +66,7 @@ func TestFingerprintSettingsService_NilServesDefaults(t *testing.T) {
}
func TestFingerprintSettingsService_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
reload := func(step string) FingerprintSettings {
t.Helper()
+3 -2
View File
@@ -14,6 +14,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
func mkdir(t *testing.T, parts ...string) string {
@@ -84,7 +85,7 @@ func clearSavedRoots(t *testing.T, pool *pgxpool.Pool) {
}
func TestSetRoots_TracksMoveWithTheirFolder_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
clearSavedRoots(t, pool)
ctx := context.Background()
base := t.TempDir()
@@ -126,7 +127,7 @@ func TestSetRoots_TracksMoveWithTheirFolder_Integration(t *testing.T) {
}
func TestSetRoots_RefusesAMoveToAFolderWithoutTheFiles_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
clearSavedRoots(t, pool)
ctx := context.Background()
base := t.TempDir()
+3 -2
View File
@@ -11,6 +11,7 @@ import (
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
@@ -18,7 +19,7 @@ import (
// stores for each kind of result, that a pass ends, and that the gauge counts
// what the passes wrote.
func TestLoudnessBackfill_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
@@ -230,7 +231,7 @@ func TestLoudnessBackfill_Integration(t *testing.T) {
// The Go defaults must match the migration's, or a database that cannot be
// read would analyze differently from a fresh install.
func TestLoudnessSettings_DefaultsMatchMigration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
s, err := NewLoudnessSettingsService(context.Background(), pool)
if err != nil {
t.Fatalf("load: %v", err)
+2 -1
View File
@@ -10,13 +10,14 @@ import (
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// The MBID backfills read tags from files, so a track whose file is gone has
// nothing to give them: they skip it rather than log an "open failed" warning
// per missing track on every scan.
func TestMBIDBackfill_SkipsMissingTracks_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+2 -2
View File
@@ -114,7 +114,7 @@ func (f pairFixture) count(sql string, args ...any) int {
// On rows: each rule folds the pair it should, history moves onto
// the replacement, and every shape the pass must leave alone is left alone.
func TestPairMissingTracks_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Pair Artist", SortName: "Pair Artist"})
@@ -236,7 +236,7 @@ func TestPairMissingTracks_Integration(t *testing.T) {
// arrives at a new path with no recording MBID, so move adoption has nothing to
// match. The scan must still end with one row, carrying the old row's history.
func TestScanner_FoldsUpgradedFile_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
+1 -1
View File
@@ -38,7 +38,7 @@ func newMusicNotices(t *testing.T, q *dbq.Queries, user dbq.User) []notification
// into an empty library is the library being set up, and an album that only
// formed because tracks already held were retagged into it is not new music.
func TestAnnounceNewAlbums_TellsEveryoneOfEachNewAlbumOnce(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
admin := notifyingAdmin(t, pool)
ctx := context.Background()
q := dbq.New(pool)
+1 -1
View File
@@ -30,7 +30,7 @@ func TestNormalizationPrefs_Validate(t *testing.T) {
// migration's defaults for any column the insert left out; the two must
// agree, or a user would hear a different level after their first save.
func TestNormalizationPrefs_DefaultsAndRoundTrip_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
user, err := q.CreateUser(ctx, dbq.CreateUserParams{
+4 -4
View File
@@ -62,7 +62,7 @@ func unreadNotices(t *testing.T, pool *pgxpool.Pool, user dbq.User) []notice {
}
func TestNotifyScanFinished_FailuresCoalesceAndShutdownIsSilent(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
admin := notifyingAdmin(t, pool)
ctx := context.Background()
@@ -91,7 +91,7 @@ func TestNotifyScanFinished_FailuresCoalesceAndShutdownIsSilent(t *testing.T) {
// it has adopted moves and folded replacements, so a file it marked and then
// healed is never reported as lost.
func TestReconcileMissing_RecordsTheMarksWithoutNotifying(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
admin := notifyingAdmin(t, pool)
s := testScanner(t, populatedRoot(t))
@@ -117,7 +117,7 @@ func TestReconcileMissing_RecordsTheMarksWithoutNotifying(t *testing.T) {
}
func TestNotifyStillMissing_NamesOnlyTheTracksStillGone(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
admin := notifyingAdmin(t, pool)
ctx := context.Background()
q := dbq.New(pool)
@@ -158,7 +158,7 @@ func TestNotifyStillMissing_NamesOnlyTheTracksStillGone(t *testing.T) {
// it for them. A group the resolver settles or links is never news, and a
// group already seen by an earlier pass is not news again.
func TestResolveDuplicates_TellsAdminsOnlyOfNewGroupsLeftForThem(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
admin := notifyingAdmin(t, pool)
ctx := context.Background()
q := dbq.New(pool)
+4 -4
View File
@@ -19,7 +19,7 @@ import (
// A removed file's track stays, missing, with its likes and plays and the
// reason it waits, and the good copy that lands later takes its history over.
func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
admin, err := q.CreateUser(ctx, dbq.CreateUserParams{
@@ -86,7 +86,7 @@ func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) {
// ForgetMBID drops the recording MBID that came from a file that was not the
// track's song.
func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "rip.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
@@ -112,7 +112,7 @@ func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) {
// A file that cannot be removed leaves the track as it was.
func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "stuck.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
@@ -139,7 +139,7 @@ func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) {
// The reason describes one missing spell: a file back at its path clears it
// with the missing mark.
func TestRemoveFile_ReasonGoesWhenTheFileIsBack(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "back.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
+2 -1
View File
@@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
func TestGainDBAndLinearPeak(t *testing.T) {
@@ -45,7 +46,7 @@ func TestGainDBAndLinearPeak(t *testing.T) {
}
func TestReplayGainForTracks_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+7 -78
View File
@@ -11,10 +11,9 @@ import (
"testing"
"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 TestReleaseDateFromYear(t *testing.T) {
@@ -45,24 +44,10 @@ func TestReleaseDateFromYear(t *testing.T) {
// TestScanner_Integration exercises walk → tag-parse → upsert → incremental
// skip against a real Postgres. Gated on MINSTREL_TEST_DATABASE_URL.
func TestScanner_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
@@ -221,24 +206,10 @@ func writeTestMP3(t *testing.T, path string, frames map[string]string) {
// reconcile would refuse to mark, adoption could not fire, and the file would
// fork. See the "reconcile skipped" warning in Scan.
func TestScanner_AdoptsMovedFile_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
@@ -315,24 +286,10 @@ func TestScanner_AdoptsMovedFile_Integration(t *testing.T) {
// read existed gets it on the next scan, through the tagReadVersion bump,
// without its file changing.
func TestScanner_ReleaseGroupMBID_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
@@ -392,23 +349,9 @@ func TestScanner_ReleaseGroupMBID_Integration(t *testing.T) {
// track of the same group joins its album, and a track filed under the other
// album's row by the old lookup moves to its own on the tagReadVersion re-read.
func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
@@ -493,7 +436,7 @@ func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing.
// with its old mtime, which takes the unchanged-file skip. The track keeps its
// id, and clients are told it plays again.
func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
@@ -554,23 +497,9 @@ func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing
// one album, and every track keeps its own artist. The row the old filing
// left behind is removed by the scan's tidy-up once a re-read empties it.
func TestScanner_AlbumArtistKeepsAnAlbumTogether_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
pool := dbtest.Open(t)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
+4 -4
View File
@@ -75,7 +75,7 @@ func newSongLinkFixture(t *testing.T, pool *pgxpool.Pool) songLinkFixture {
// list shows the song once, a like or unlike on either copy reaches both, and
// "not the same song" undoes the link.
func TestCrossReleaseCopiesCountAsOneSong_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
f := newSongLinkFixture(t, pool)
@@ -153,7 +153,7 @@ func TestCrossReleaseCopiesCountAsOneSong_Integration(t *testing.T) {
// With auto-resolve off the resolver links nothing.
func TestCrossReleaseLinkNeedsAutoResolve_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
f := newSongLinkFixture(t, pool)
@@ -172,7 +172,7 @@ func TestCrossReleaseLinkNeedsAutoResolve_Integration(t *testing.T) {
// they make it the key.
func TestMergeInheritsSongLink_Integration(t *testing.T) {
for _, removeAlbumCut := range []bool{true, false} {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
f := newSongLinkFixture(t, pool)
@@ -226,7 +226,7 @@ func songKeys(t *testing.T, q *dbq.Queries, ids ...pgtype.UUID) []pgtype.UUID {
// extra copy is merged into its FLAC, the single stays, and the two copies
// left are linked as one song.
func TestResolveDuplicates_FoldsAnAlbumsExtraCopyInACrossReleaseGroup_Integration(t *testing.T) {
pool := newPool(t)
pool := dbtest.Pool(t)
ctx := context.Background()
q := dbq.New(pool)
dir := t.TempDir()
+4 -18
View File
@@ -5,27 +5,20 @@ import (
"errors"
"io"
"log/slog"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// triggerTestPool returns a pool against MINSTREL_TEST_DATABASE_URL or skips.
func triggerTestPool(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 scan_runs so each test starts clean.
if _, err := pool.Exec(context.Background(), `DELETE FROM scan_runs`); err != nil {
t.Fatalf("reset: %v", err)
@@ -135,14 +128,7 @@ func TestTryStartScan_StaleInFlight_Reaps(t *testing.T) {
}
func TestTryStartScan_DBError_Propagates(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() // close immediately to force errors on subsequent queries
started, existing, err := TryStartScan(