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:
@@ -10,11 +10,12 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
dir := t.TempDir()
|
||||
svc := playlists.NewService(pool, nil, dir)
|
||||
@@ -34,7 +35,7 @@ func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerateCollage_OneTrack(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
tk := seedTrack(t, pool, "Prismatic", "FoO")
|
||||
dir := t.TempDir()
|
||||
|
||||
@@ -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"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
// order they first appear, at most six: a shelf of tiles picks from them so
|
||||
// no two tiles show the same cover.
|
||||
func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
q := dbq.New(pool)
|
||||
user := seedUser(t, pool, "covers")
|
||||
@@ -91,7 +92,7 @@ func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
|
||||
// A playlist with no covered albums lists none, rather than failing the
|
||||
// List call that carries every other playlist.
|
||||
func TestCoverAlbums_EmptyPlaylistHasNone(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
user := seedUser(t, pool, "nocovers")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
func setGenre(t *testing.T, pool *pgxpool.Pool, trackID pgtype.UUID, genre string) {
|
||||
@@ -35,7 +36,7 @@ func seedTasteTag(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, tag stri
|
||||
// and the bucket shrink to its 3. Capped before, the artist keeps 3 and the
|
||||
// single-tag artists below it fill the rest.
|
||||
func TestListTasteUnheardTracksForDiscover_CapsBeforeLimit(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "tastecap")
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func seedGenre(t *testing.T, pool *pgxpool.Pool, genre string, artists, perArtis
|
||||
// tier 2, an untouched one is tier 3, and a genre too small for a mix is not
|
||||
// offered at all.
|
||||
func TestListGenreMixCandidates_TiersAndSizeFloor(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixcand")
|
||||
now := time.Now().UTC()
|
||||
@@ -85,7 +86,7 @@ func TestListGenreMixCandidates_TiersAndSizeFloor(t *testing.T) {
|
||||
// also carries an excluded genre (matched case-insensitively) belongs to that
|
||||
// genre's mix and is left out of this one.
|
||||
func TestListGenreMixTracks_LeavesOutSongsOfGenresAlreadyPicked(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixexcl")
|
||||
|
||||
@@ -120,7 +121,7 @@ func TestListGenreMixTracks_LeavesOutSongsOfGenresAlreadyPicked(t *testing.T) {
|
||||
// often than played through is left out, while a favourite with stray skips
|
||||
// stays; tracks of other genres never appear.
|
||||
func TestListGenreMixTracks_SplitsFamiliarAndDropsRejectedTracks(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixtracks")
|
||||
now := time.Now().UTC()
|
||||
@@ -178,7 +179,7 @@ func TestListGenreMixTracks_SplitsFamiliarAndDropsRejectedTracks(t *testing.T) {
|
||||
// the genre they listen to, holding only that genre's tracks, with both
|
||||
// halves of the blend stamped.
|
||||
func TestBuildSystemPlaylists_GenreMixes(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixbuild")
|
||||
now := time.Now().UTC()
|
||||
@@ -270,7 +271,7 @@ func meanPosition(sum map[pgtype.UUID]int, tracks []dbq.Track) float64 {
|
||||
// user knows, ones played often and liked come up earlier than ones played
|
||||
// once.
|
||||
func TestListGenreMixTracks_FamiliarLeansTowardFavourites(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixfav")
|
||||
now := time.Now().UTC()
|
||||
@@ -301,7 +302,7 @@ func TestListGenreMixTracks_FamiliarLeansTowardFavourites(t *testing.T) {
|
||||
// have never met, and so does an artist similar to theirs. The stranger
|
||||
// still appears: the lean tilts the draw, it does not exclude.
|
||||
func TestListGenreMixTracks_UnheardLeansTowardTheUsersArtists(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "gmixartist")
|
||||
now := time.Now().UTC()
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
@@ -40,7 +41,7 @@ func TestOrderByTracks_ArrangesNamedEntriesInTheirOwnPlaces(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -71,7 +72,7 @@ func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
a := seedTrack(t, pool, "A", "Artist")
|
||||
b := seedTrack(t, pool, "B", "Artist")
|
||||
@@ -95,7 +96,7 @@ func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReorderTracks_ASecondSendChangesNothing(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
a := seedTrack(t, pool, "A", "Artist")
|
||||
b := seedTrack(t, pool, "B", "Artist")
|
||||
@@ -118,7 +119,7 @@ func TestReorderTracks_ASecondSendChangesNothing(t *testing.T) {
|
||||
|
||||
// Every change tells the owner's clients, whichever client made it.
|
||||
func TestChanges_TellTheOwnersClients(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
a := seedTrack(t, pool, "A", "Artist")
|
||||
bus := eventbus.New()
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
func likeTrack(t *testing.T, pool *pgxpool.Pool, userID, trackID pgtype.UUID) {
|
||||
@@ -30,7 +31,7 @@ func markMissing(t *testing.T, pool *pgxpool.Pool, trackID pgtype.UUID) {
|
||||
// TestPickTopPlayedTracksForUser_LikedTierSkipsMissing: with no plays, For
|
||||
// You seeds from likes. A liked track whose file is gone is not a seed.
|
||||
func TestPickTopPlayedTracksForUser_LikedTierSkipsMissing(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
u := seedUser(t, pool, "seedmissing")
|
||||
present := seedTrack(t, pool, "Present", "Seed Artist A")
|
||||
gone := seedTrack(t, pool, "Gone", "Seed Artist B")
|
||||
@@ -51,7 +52,7 @@ func TestPickTopPlayedTracksForUser_LikedTierSkipsMissing(t *testing.T) {
|
||||
// Songs-like seeds from the artist's newest album. When that album's track is
|
||||
// missing, the seed comes from the next album rather than the missing file.
|
||||
func TestPickTopPlayedTrackForArtistByUser_FallbackSkipsMissing(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "songslikemissing")
|
||||
older := seedTrack(t, pool, "Older", "Songs Like Artist")
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
func TestCreate_HappyPath(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
dir := t.TempDir()
|
||||
|
||||
@@ -37,7 +38,7 @@ func TestCreate_HappyPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreate_EmptyNameRejected(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -48,7 +49,7 @@ func TestCreate_EmptyNameRejected(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_NotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -59,7 +60,7 @@ func TestGet_NotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_Forbidden_PrivatePlaylistFromOtherUser(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -75,7 +76,7 @@ func TestGet_Forbidden_PrivatePlaylistFromOtherUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_Public_VisibleToOtherUser(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -101,7 +102,7 @@ func TestGet_Public_VisibleToOtherUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_Owner_SeesOwnPrivate(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -119,7 +120,7 @@ func TestGet_Owner_SeesOwnPrivate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList_OwnAndPublic(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -152,7 +153,7 @@ func TestList_OwnAndPublic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate_OwnerOnly(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -177,7 +178,7 @@ func TestUpdate_OwnerOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate_PartialFieldsLeaveOthersAlone(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -204,7 +205,7 @@ func TestUpdate_PartialFieldsLeaveOthersAlone(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate_EmptyNameRejected(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -220,7 +221,7 @@ func TestUpdate_EmptyNameRejected(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate_NotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -231,7 +232,7 @@ func TestUpdate_NotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete_OwnerOnly(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -254,7 +255,7 @@ func TestDelete_OwnerOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete_NotFound(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -264,7 +265,7 @@ func TestDelete_NotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete_RemovesCoverFile(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
dir := t.TempDir()
|
||||
svc := playlists.NewService(pool, nil, dir)
|
||||
@@ -299,7 +300,7 @@ func TestDelete_RemovesCoverFile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete_MissingCoverFileTolerated(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
dir := t.TempDir()
|
||||
svc := playlists.NewService(pool, nil, dir)
|
||||
@@ -322,7 +323,7 @@ func TestDelete_MissingCoverFileTolerated(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppendTracks_Snapshot(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
track := seedTrack(t, pool, "Prismatic", "Fields of Ontario")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -348,7 +349,7 @@ func TestAppendTracks_Snapshot(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppendTracks_OwnerOnly(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
alice := seedUser(t, pool, "alice")
|
||||
bob := seedUser(t, pool, "bob")
|
||||
track := seedTrack(t, pool, "T", "A")
|
||||
@@ -362,7 +363,7 @@ func TestAppendTracks_OwnerOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppendTracks_NonExistentTrack(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -374,7 +375,7 @@ func TestAppendTracks_NonExistentTrack(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRemoveTrack_RenumbersPositions(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
t1 := seedTrack(t, pool, "Track 1", "Artist")
|
||||
t2 := seedTrack(t, pool, "Track 2", "Artist")
|
||||
@@ -402,7 +403,7 @@ func TestRemoveTrack_RenumbersPositions(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReorder_Permutation(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
t1 := seedTrack(t, pool, "A", "Artist")
|
||||
t2 := seedTrack(t, pool, "B", "Artist")
|
||||
@@ -427,7 +428,7 @@ func TestReorder_Permutation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReorder_RejectsNonPermutation(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
tk := seedTrack(t, pool, "T", "A")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
@@ -451,7 +452,7 @@ func TestReorder_RejectsNonPermutation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSnapshotPersistsAfterUpstreamTrackDelete(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "alice")
|
||||
tk := seedTrack(t, pool, "Doomed track", "Artist")
|
||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||
|
||||
@@ -4,9 +4,6 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -14,7 +11,6 @@ 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"
|
||||
)
|
||||
@@ -25,31 +21,6 @@ import (
|
||||
// tests seed multiple tracks within one test function.
|
||||
var seedTrackCounter uint64
|
||||
|
||||
// newPool migrates a fresh schema against MINSTREL_TEST_DATABASE_URL
|
||||
// and returns a connection pool with all data tables wiped (test
|
||||
// users only — see internal/dbtest/reset.go for the why). Skips when
|
||||
// the env var isn't set or -short is passed.
|
||||
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
|
||||
}
|
||||
|
||||
// seedUser inserts a `test-`-prefixed user (so dbtest.ResetDB can
|
||||
// clean it without touching the developer's admin row) and returns
|
||||
// the persisted user record.
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
// album cut, the better-ranked copy stays; a rip is left out and the next copy
|
||||
// of its song takes the place.
|
||||
func TestOneCopyPerSong_Integration(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
q := dbq.New(pool)
|
||||
single := seedTrack(t, pool, "copies-single", "copies-artist")
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
// seedTrackForArtist creates another track under an existing artist +
|
||||
@@ -108,7 +109,7 @@ func likeArtist(t *testing.T, pool *pgxpool.Pool, userID, artistID pgtype.UUID)
|
||||
func TestListDeepCutsTracks_SkippedTracksExcluded(t *testing.T) {
|
||||
// Eligibility counts skipped plays too, so a track skipped repeatedly
|
||||
// (and never finished) does not read as "barely heard".
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u := seedUser(t, pool, "dc1")
|
||||
now := time.Now().UTC()
|
||||
@@ -153,7 +154,7 @@ func TestListOnThisDayTracks_WrapsYearBoundary(t *testing.T) {
|
||||
// The day-of-year window wraps the year: plain ABS would make Dec 28 vs Jan
|
||||
// 3 read as 359 days apart. The build date parameter anchors "today" so the
|
||||
// wrap is testable at any run date.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u := seedUser(t, pool, "otd1")
|
||||
|
||||
@@ -186,7 +187,7 @@ func TestListNewForYouTracks_TiersAndConsumption(t *testing.T) {
|
||||
// affinity albums; tier 2 = affinity albums from the wider 90-day
|
||||
// window; tier 3 = fresh additions without affinity; any attempted
|
||||
// (>=30s) track consumes the album out of the mix entirely.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u := seedUser(t, pool, "nfy1")
|
||||
now := time.Now().UTC()
|
||||
@@ -243,7 +244,7 @@ func TestListFirstListensTracks_AttemptThreshold(t *testing.T) {
|
||||
// "Attempted" is track-level at >=30s. A 2-second brush does not
|
||||
// disqualify an album; barely-attempted albums step back
|
||||
// to tier 2 with the attempted tracks themselves excluded.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u := seedUser(t, pool, "fl1")
|
||||
now := time.Now().UTC()
|
||||
@@ -301,7 +302,7 @@ func TestListRediscoverTracks_SkippedResurfacingCoolsDown(t *testing.T) {
|
||||
// invitation — the track sits out ~90 days instead of re-qualifying
|
||||
// the next day. Skips from other surfaces don't cool down, and the
|
||||
// cooldown expires.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u := seedUser(t, pool, "rd1")
|
||||
now := time.Now().UTC()
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
@@ -79,7 +80,7 @@ func seedActiveLibrary(t *testing.T, pool *pgxpool.Pool, name string, numArtists
|
||||
}
|
||||
|
||||
func TestBuildSystemPlaylists_SufficientActivity(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "act1", 4, 3)
|
||||
if err := playlists.BuildSystemPlaylists(context.Background(), pool, logger, u.ID, time.Now().UTC(), t.TempDir()); err != nil {
|
||||
@@ -146,7 +147,7 @@ func TestBuildSystemPlaylists_StaleActivity_SongsLikeSurvives(t *testing.T) {
|
||||
// inside 30d) must still seed the mixes —
|
||||
// and the built tracks carry the tier2 stamp so metrics can compare
|
||||
// stale-seeded mixes against fresh ones.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u := seedUser(t, pool, "stale1")
|
||||
old := time.Now().UTC().Add(-20 * 24 * time.Hour)
|
||||
@@ -193,7 +194,7 @@ func TestBuildSystemPlaylists_StaleActivity_SongsLikeSurvives(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildSystemPlaylists_QuarantineExcluded(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, tracks := seedActiveLibrary(t, pool, "qact", 3, 3)
|
||||
|
||||
@@ -228,7 +229,7 @@ func TestBuildSystemPlaylists_QuarantineExcluded(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "atomic", 3, 3)
|
||||
ctx := context.Background()
|
||||
@@ -265,7 +266,7 @@ func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildSystemPlaylists_Concurrency(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "conc", 3, 3)
|
||||
ctx := context.Background()
|
||||
@@ -361,7 +362,7 @@ func seedSharedArtistLibrary(
|
||||
// to survive a filter coming back in a different shape — and because an
|
||||
// absence check would match the comment explaining why there is no filter.
|
||||
func TestBuildSystemPlaylists_SongsLikeIncludesItsSeedArtist(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedSharedArtistLibrary(t, pool, "seedartist", 4, 6)
|
||||
ctx := context.Background()
|
||||
@@ -423,7 +424,7 @@ func TestBuildSystemPlaylists_SongsLikeIncludesItsSeedArtist(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildSystemPlaylists_DailyNonceDeterminism(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "nonce", 4, 5)
|
||||
ctx := context.Background()
|
||||
@@ -538,7 +539,7 @@ func equalSnapshots(a, b map[string][]string) bool {
|
||||
}
|
||||
|
||||
func TestListActiveUsersForSystemPlaylists(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
activeU, tracks := seedActiveLibrary(t, pool, "active", 1, 1)
|
||||
inactiveU := seedUser(t, pool, "inactive")
|
||||
@@ -570,7 +571,7 @@ func TestListActiveUsersForSystemPlaylists(t *testing.T) {
|
||||
func TestBuildSystemPlaylists_DiscoverColdStart(t *testing.T) {
|
||||
// User has no plays / likes → Discover's dormant + cross-user
|
||||
// buckets are empty (or minimal), all slots come from random unheard.
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "discover_cold", 4, 5)
|
||||
// seedActiveLibrary seeds 3 plays per track; delete them so dormant
|
||||
@@ -619,7 +620,7 @@ func systemVariantsOf(rows []dbq.ListPlaylistsByUserAndKindRow) []string {
|
||||
}
|
||||
|
||||
func TestStartupRecoveryClearsStaleInFlight(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "stale")
|
||||
// Seed a stale in_flight=true row.
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,7 @@ func countYouMightLike(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID) (al
|
||||
// You-might-like rows — the build ships nothing rather than near-random
|
||||
// tiles.
|
||||
func TestYouMightLike_ColdStartGate(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "ymlcold", 3, 3)
|
||||
|
||||
@@ -54,7 +55,7 @@ func TestYouMightLike_ColdStartGate(t *testing.T) {
|
||||
// × 4 tracks = 32 distinct tracks, clearing both gate bars) populates the
|
||||
// You-might-like tables in the daily build.
|
||||
func TestYouMightLike_SufficientActivityPopulates(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "ymlrich", 8, 4)
|
||||
|
||||
@@ -76,7 +77,7 @@ func TestYouMightLike_SufficientActivityPopulates(t *testing.T) {
|
||||
// TestYouMightLike_AtomicReplace: a second build replaces rather than
|
||||
// appends, so counts stay bounded by the persisted depth.
|
||||
func TestYouMightLike_AtomicReplace(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
logger := discardLogger()
|
||||
u, _ := seedActiveLibrary(t, pool, "ymlreplace", 8, 4)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user