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>
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package recommendation
|
|
|
|
// Shared fixtures for this package's integration tests.
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
|
)
|
|
|
|
func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
|
t.Helper()
|
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
|
Username: dbtest.TestUserPrefix + name, PasswordHash: "x",
|
|
ApiTokenHash: name + "-token", IsAdmin: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed user: %v", err)
|
|
}
|
|
return u
|
|
}
|
|
|
|
func seedArtist(t *testing.T, pool *pgxpool.Pool, name, mbid string) dbq.Artist {
|
|
t.Helper()
|
|
var mbidPtr *string
|
|
if mbid != "" {
|
|
mbidPtr = &mbid
|
|
}
|
|
a, err := dbq.New(pool).UpsertArtist(context.Background(), dbq.UpsertArtistParams{
|
|
Name: name, SortName: name, Mbid: mbidPtr,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed artist: %v", err)
|
|
}
|
|
return a
|
|
}
|
|
|
|
func seedAlbumForArtist(t *testing.T, pool *pgxpool.Pool, artistID pgtype.UUID, title string) dbq.Album {
|
|
t.Helper()
|
|
a, err := dbq.New(pool).UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{
|
|
Title: title, SortTitle: title, ArtistID: artistID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed album: %v", err)
|
|
}
|
|
return a
|
|
}
|
|
|
|
func seedTrackOnAlbum(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string) dbq.Track {
|
|
t.Helper()
|
|
tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{
|
|
Title: title, AlbumID: albumID, ArtistID: artistID,
|
|
DurationMs: 1000, FilePath: "/tmp/m5c-" + title + ".mp3",
|
|
FileSize: 1, FileFormat: "mp3",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed track: %v", err)
|
|
}
|
|
return tr
|
|
}
|
|
|
|
func insertPlayEvent(t *testing.T, pool *pgxpool.Pool, userID, trackID pgtype.UUID, startedAt time.Time) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
var sessionID pgtype.UUID
|
|
if err := pool.QueryRow(ctx,
|
|
`INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id)
|
|
VALUES ($1, $2, $2, 'm5c-test') RETURNING id`,
|
|
userID, startedAt,
|
|
).Scan(&sessionID); err != nil {
|
|
t.Fatalf("insert play_session: %v", err)
|
|
}
|
|
if _, err := pool.Exec(ctx,
|
|
`INSERT INTO play_events (user_id, track_id, session_id, started_at, was_skipped)
|
|
VALUES ($1, $2, $3, $4, false)`,
|
|
userID, trackID, sessionID, startedAt,
|
|
); err != nil {
|
|
t.Fatalf("insert play_event: %v", err)
|
|
}
|
|
}
|
|
|
|
func likeArtist(t *testing.T, pool *pgxpool.Pool, userID, artistID pgtype.UUID) {
|
|
t.Helper()
|
|
if _, err := pool.Exec(context.Background(),
|
|
`INSERT INTO general_likes_artists (user_id, artist_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
|
userID, artistID,
|
|
); err != nil {
|
|
t.Fatalf("like artist: %v", err)
|
|
}
|
|
}
|