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:
@@ -3,41 +3,16 @@ package recommendation
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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 testPool(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
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
pool *pgxpool.Pool
|
||||
q *dbq.Queries
|
||||
@@ -47,7 +22,7 @@ type fixture struct {
|
||||
|
||||
func newFixture(t *testing.T, n int) fixture {
|
||||
t.Helper()
|
||||
pool := testPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
q := dbq.New(pool)
|
||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
||||
|
||||
@@ -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 insertPlayEventDevice(
|
||||
@@ -37,7 +38,7 @@ func insertPlayEventDevice(
|
||||
// the mobile artist out-scores the web artist and vice-versa, while the
|
||||
// device-agnostic (”) pass treats them equally.
|
||||
func TestLoadContextAffinity_DeviceNarrowing(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
q := dbq.New(pool)
|
||||
|
||||
|
||||
@@ -4,41 +4,16 @@ package recommendation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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 seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||
t.Helper()
|
||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
// TestHomeData_YouMightLike_FallbackFillsFromLikedArtists: when the taste
|
||||
@@ -17,7 +18,7 @@ import (
|
||||
// You-might-like fallback fills the rest disjointly so no artist is shown
|
||||
// twice. 20 liked artists, none played → Rediscover 10 + You-might-like 10.
|
||||
func TestHomeData_YouMightLike_FallbackFillsFromLikedArtists(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-yml-fallback")
|
||||
liked := make(map[pgtype.UUID]bool, 20)
|
||||
for i := 0; i < 20; i++ {
|
||||
@@ -53,7 +54,7 @@ func TestHomeData_YouMightLike_FallbackFillsFromLikedArtists(t *testing.T) {
|
||||
// (most users like tracks/albums, rarely artists). 12 liked tracks by distinct
|
||||
// artists, no artist-likes, no plays → the artist row fills from them.
|
||||
func TestHomeData_YouMightLike_FallbackFromLikedTrackArtists(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-yml-trackartists")
|
||||
fromLiked := make(map[pgtype.UUID]bool, 12)
|
||||
for i := 0; i < 12; i++ {
|
||||
@@ -83,7 +84,7 @@ func TestHomeData_YouMightLike_FallbackFromLikedTrackArtists(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHomeData_NewUser_AllSectionsEmpty(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-newuser")
|
||||
|
||||
got, err := HomeData(context.Background(), pool, user.ID)
|
||||
@@ -116,7 +117,7 @@ func TestHomeData_NewUser_AllSectionsEmpty(t *testing.T) {
|
||||
var _ map[pgtype.UUID]struct{}
|
||||
|
||||
func TestHomeData_RecentlyAdded_OrderedByCreatedAt(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-recents")
|
||||
a1 := seedArtist(t, pool, "ArtistA", "")
|
||||
// Three albums, oldest first — created_at increases naturally with each insert.
|
||||
@@ -142,7 +143,7 @@ func TestHomeData_RecentlyAdded_OrderedByCreatedAt(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHomeData_MostPlayed_RanksByCount_HonorsQuarantine(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-mostplayed")
|
||||
artist := seedArtist(t, pool, "ArtistB", "")
|
||||
album := seedAlbumForArtist(t, pool, artist.ID, "AlbumB")
|
||||
@@ -179,7 +180,7 @@ func TestHomeData_MostPlayed_RanksByCount_HonorsQuarantine(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHomeData_RediscoverAlbums_FallbackWhenSparse(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-rediscover")
|
||||
artist := seedArtist(t, pool, "ArtistC", "")
|
||||
// Like one album NOW (won't satisfy >30d eligibility) — should still
|
||||
@@ -208,7 +209,7 @@ func TestHomeData_RediscoverAlbums_FallbackWhenSparse(t *testing.T) {
|
||||
// ago) is enough for the album to appear in Rediscover, even with no
|
||||
// explicit general_likes_albums entry.
|
||||
func TestHomeData_Rediscover_AlbumFromTrackLikes(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-redis-tracklike")
|
||||
artist := seedArtist(t, pool, "ArtistTL", "")
|
||||
al := seedAlbumForArtist(t, pool, artist.ID, "TrackLiked")
|
||||
@@ -240,7 +241,7 @@ func TestHomeData_Rediscover_AlbumFromTrackLikes(t *testing.T) {
|
||||
// track-derived path. The fallback should also miss this since no
|
||||
// album-like exists.
|
||||
func TestHomeData_Rediscover_AlbumTrackLikeThreshold(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-redis-threshold")
|
||||
artist := seedArtist(t, pool, "ArtistTh", "")
|
||||
al := seedAlbumForArtist(t, pool, artist.ID, "OneTrackLiked")
|
||||
@@ -265,7 +266,7 @@ func TestHomeData_Rediscover_AlbumTrackLikeThreshold(t *testing.T) {
|
||||
// albums from the same artist appear in Rediscover even when more are
|
||||
// eligible.
|
||||
func TestHomeData_Rediscover_DiversityCap(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-redis-cap")
|
||||
artist := seedArtist(t, pool, "ArtistDom", "")
|
||||
albums := []dbq.Album{
|
||||
@@ -299,7 +300,7 @@ func TestHomeData_Rediscover_DiversityCap(t *testing.T) {
|
||||
// dropped from Rediscover (the "you forgot about this" contract is
|
||||
// broken if the user is actively spinning it).
|
||||
func TestHomeData_Rediscover_DedupVsMostPlayed(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
user := seedUser(t, pool, "home-redis-dedup")
|
||||
artist := seedArtist(t, pool, "ArtistD", "")
|
||||
// "Forgotten" album: liked >30d ago, no recent plays — would be
|
||||
|
||||
@@ -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 uuidN(n byte) pgtype.UUID {
|
||||
@@ -132,7 +133,7 @@ func almostEq(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
||||
// TestLoadTasteProfile_RoundTrip seeds taste_profile rows and verifies the
|
||||
// reader hydrates them into a profile that scores a matching track positively.
|
||||
func TestLoadTasteProfile_RoundTrip(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
pool := dbtest.Pool(t)
|
||||
ctx := context.Background()
|
||||
u := seedUser(t, pool, "taste-rt")
|
||||
art := seedArtist(t, pool, "Loved Artist", "")
|
||||
|
||||
Reference in New Issue
Block a user