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:
@@ -8,7 +8,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ import (
|
|||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||||
@@ -37,23 +35,8 @@ import (
|
|||||||
// used elsewhere (scanner_test.go, etc.).
|
// used elsewhere (scanner_test.go, etc.).
|
||||||
func testHandlers(t *testing.T) (*handlers, *pgxpool.Pool) {
|
func testHandlers(t *testing.T) (*handlers, *pgxpool.Pool) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping api integration in -short mode")
|
|
||||||
}
|
|
||||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
|
||||||
if dsn == "" {
|
|
||||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
|
||||||
}
|
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Pool(t)
|
||||||
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)
|
|
||||||
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
||||||
recCfg := config.RecommendationConfig{
|
recCfg := config.RecommendationConfig{
|
||||||
RecentlyPlayedHours: 1, RadioSize: 50, RadioSizeMax: 200,
|
RecentlyPlayedHours: 1, RadioSize: 50, RadioSizeMax: 200,
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -12,19 +11,12 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTestPool(t *testing.T) *pgxpool.Pool {
|
func newTestPool(t *testing.T) *pgxpool.Pool {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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)
|
|
||||||
// Reset audit_log so each test runs against a known state.
|
// Reset audit_log so each test runs against a known state.
|
||||||
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
||||||
t.Fatalf("reset audit_log: %v", err)
|
t.Fatalf("reset audit_log: %v", err)
|
||||||
@@ -102,14 +94,7 @@ func TestWriteOrLog_Success(t *testing.T) {
|
|||||||
// closed pool, and asserts the logger captured a Warn record with
|
// closed pool, and asserts the logger captured a Warn record with
|
||||||
// action + err keys.
|
// action + err keys.
|
||||||
func TestWriteOrLog_DBFailure_Logs(t *testing.T) {
|
func TestWriteOrLog_DBFailure_Logs(t *testing.T) {
|
||||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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.Close() // force subsequent ops to fail
|
pool.Close() // force subsequent ops to fail
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@@ -135,14 +120,7 @@ func TestWriteOrLog_DBFailure_Logs(t *testing.T) {
|
|||||||
// TestWriteOrLog_NilLogger ensures a nil logger + failing pool does
|
// TestWriteOrLog_NilLogger ensures a nil logger + failing pool does
|
||||||
// not panic. Uses a closed pool to force the failure branch.
|
// not panic. Uses a closed pool to force the failure branch.
|
||||||
func TestWriteOrLog_NilLogger(t *testing.T) {
|
func TestWriteOrLog_NilLogger(t *testing.T) {
|
||||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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.Close()
|
pool.Close()
|
||||||
|
|
||||||
var nilUUID pgtype.UUID
|
var nilUUID pgtype.UUID
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -15,31 +12,9 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 index(name string) int {
|
func index(name string) int {
|
||||||
return slices.IndexFunc(backup.Tables, func(t backup.Table) bool { return t.Name == name })
|
return slices.IndexFunc(backup.Tables, func(t backup.Table) bool { return t.Name == name })
|
||||||
}
|
}
|
||||||
@@ -47,7 +22,7 @@ func index(name string) int {
|
|||||||
// Every table in the schema is backed up or excluded with a reason, so a new
|
// Every table in the schema is backed up or excluded with a reason, so a new
|
||||||
// table fails here until someone decides which.
|
// table fails here until someone decides which.
|
||||||
func TestEveryTableIsAccountedFor_Integration(t *testing.T) {
|
func TestEveryTableIsAccountedFor_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
rows, err := pool.Query(context.Background(), "SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
|
rows, err := pool.Query(context.Background(), "SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -64,7 +39,7 @@ func TestEveryTableIsAccountedFor_Integration(t *testing.T) {
|
|||||||
// A restore loads each table after the ones it references, and never needs a
|
// A restore loads each table after the ones it references, and never needs a
|
||||||
// table the backup leaves out.
|
// table the backup leaves out.
|
||||||
func TestTablesComeAfterWhatTheyReference_Integration(t *testing.T) {
|
func TestTablesComeAfterWhatTheyReference_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
rows, err := pool.Query(context.Background(), `
|
rows, err := pool.Query(context.Background(), `
|
||||||
SELECT cl.relname, pc.relname
|
SELECT cl.relname, pc.relname
|
||||||
FROM pg_constraint co
|
FROM pg_constraint co
|
||||||
@@ -104,7 +79,7 @@ func TestTablesComeAfterWhatTheyReference_Integration(t *testing.T) {
|
|||||||
// Every omitted column exists, so a renamed column fails here rather than a
|
// Every omitted column exists, so a renamed column fails here rather than a
|
||||||
// sealed credential slipping into the file under its new name.
|
// sealed credential slipping into the file under its new name.
|
||||||
func TestOmittedColumnsExist_Integration(t *testing.T) {
|
func TestOmittedColumnsExist_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
for _, tbl := range backup.Tables {
|
for _, tbl := range backup.Tables {
|
||||||
for _, col := range tbl.Omit {
|
for _, col := range tbl.Omit {
|
||||||
var n int
|
var n int
|
||||||
@@ -137,7 +112,7 @@ func mustID(t *testing.T, pool *pgxpool.Pool, sql string, args ...any) string {
|
|||||||
// A backup restored into an emptied instance gives back the same rows, byte
|
// A backup restored into an emptied instance gives back the same rows, byte
|
||||||
// for byte, without the credentials and markers it leaves out.
|
// for byte, without the credentials and markers it leaves out.
|
||||||
func TestARestoredInstanceHoldsWhatTheBackupHeld_Integration(t *testing.T) {
|
func TestARestoredInstanceHoldsWhatTheBackupHeld_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
user := mustID(t, pool, `INSERT INTO users (username, password_hash, api_token_hash, listenbrainz_token)
|
user := mustID(t, pool, `INSERT INTO users (username, password_hash, api_token_hash, listenbrainz_token)
|
||||||
|
|||||||
@@ -19,11 +19,12 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newService(t *testing.T) (*backup.Service, *pgxpool.Pool) {
|
func newService(t *testing.T) (*backup.Service, *pgxpool.Pool) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
svc := backup.NewService(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), "test-build")
|
svc := backup.NewService(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), "test-build")
|
||||||
t.Cleanup(svc.Close)
|
t.Cleanup(svc.Close)
|
||||||
return svc, pool
|
return svc, pool
|
||||||
|
|||||||
@@ -4,43 +4,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
apes = "0a11ce00-0000-4000-8000-000000000001"
|
apes = "0a11ce00-0000-4000-8000-000000000001"
|
||||||
ghostNights = "0a11ce00-0000-4000-8000-000000000002"
|
ghostNights = "0a11ce00-0000-4000-8000-000000000002"
|
||||||
@@ -95,7 +68,7 @@ func (f *fakeMB) client() *musicbrainz.Client {
|
|||||||
func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T) {
|
func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
mb := newFakeMB(t)
|
mb := newFakeMB(t)
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := New(pool, mb.client(), nil)
|
s := New(pool, mb.client(), nil)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -139,7 +112,7 @@ func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T
|
|||||||
func TestRefreshReleaseGroup_UnknownAndUnreachable_Integration(t *testing.T) {
|
func TestRefreshReleaseGroup_UnknownAndUnreachable_Integration(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
mb := newFakeMB(t)
|
mb := newFakeMB(t)
|
||||||
s := New(newPool(t), mb.client(), nil)
|
s := New(dbtest.Pool(t), mb.client(), nil)
|
||||||
|
|
||||||
if err := s.RefreshReleaseGroup(ctx, "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) {
|
if err := s.RefreshReleaseGroup(ctx, "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) {
|
||||||
t.Errorf("unknown group: err = %v, want ErrNotFound", err)
|
t.Errorf("unknown group: err = %v, want ErrNotFound", err)
|
||||||
|
|||||||
@@ -4,39 +4,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testPool(t *testing.T) *pgxpool.Pool {
|
|
||||||
t.Helper()
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping 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)) }
|
func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||||
|
|
||||||
func seedUser(t *testing.T, q *dbq.Queries, name string) pgtype.UUID {
|
func seedUser(t *testing.T, q *dbq.Queries, name string) pgtype.UUID {
|
||||||
@@ -105,7 +82,7 @@ func coplayEdgeCount(t *testing.T, pool *pgxpool.Pool) int {
|
|||||||
// TestCoplayWorker_BuildsEdges: two users who both play two artists produce a
|
// TestCoplayWorker_BuildsEdges: two users who both play two artists produce a
|
||||||
// perfect-overlap co-play edge (Jaccard 1.0) in each direction.
|
// perfect-overlap co-play edge (Jaccard 1.0) in each direction.
|
||||||
func TestCoplayWorker_BuildsEdges(t *testing.T) {
|
func TestCoplayWorker_BuildsEdges(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -143,7 +120,7 @@ func TestCoplayWorker_BuildsEdges(t *testing.T) {
|
|||||||
// TestCoplayWorker_SingleUserNoEdges: one user's own co-listening yields no
|
// TestCoplayWorker_SingleUserNoEdges: one user's own co-listening yields no
|
||||||
// edges — the >= 2 distinct-co-player gate keeps single-user servers empty.
|
// edges — the >= 2 distinct-co-player gate keeps single-user servers empty.
|
||||||
func TestCoplayWorker_SingleUserNoEdges(t *testing.T) {
|
func TestCoplayWorker_SingleUserNoEdges(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// stubArtistProvider is a configurable ArtistArtProvider for artist enricher
|
// stubArtistProvider is a configurable ArtistArtProvider for artist enricher
|
||||||
@@ -46,7 +47,7 @@ func insertArtist(t *testing.T, q *dbq.Queries, ctx context.Context, name, mbid
|
|||||||
// --- Eligibility tests ---
|
// --- Eligibility tests ---
|
||||||
|
|
||||||
func TestEnrichArtist_NullSource_Eligible(t *testing.T) {
|
func TestEnrichArtist_NullSource_Eligible(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ func TestEnrichArtist_NullSource_Eligible(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichArtist_FoundSource_Skip(t *testing.T) {
|
func TestEnrichArtist_FoundSource_Skip(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -124,7 +125,7 @@ func TestEnrichArtist_FoundSource_Skip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichArtist_NoneCurrentVersion_Skip(t *testing.T) {
|
func TestEnrichArtist_NoneCurrentVersion_Skip(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -165,7 +166,7 @@ func TestEnrichArtist_NoneCurrentVersion_Skip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichArtist_NoneStaleVersion_FlipAndRetry(t *testing.T) {
|
func TestEnrichArtist_NoneStaleVersion_FlipAndRetry(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -212,7 +213,7 @@ func TestEnrichArtist_NoneStaleVersion_FlipAndRetry(t *testing.T) {
|
|||||||
// --- Atomicity: thumb-only / fanart-only ---
|
// --- Atomicity: thumb-only / fanart-only ---
|
||||||
|
|
||||||
func TestEnrichArtist_ThumbOnly_ThumbPathSetFanartNull(t *testing.T) {
|
func TestEnrichArtist_ThumbOnly_ThumbPathSetFanartNull(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -259,7 +260,7 @@ func TestEnrichArtist_ThumbOnly_ThumbPathSetFanartNull(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichArtist_FanartOnly_FanartPathSetThumbNull(t *testing.T) {
|
func TestEnrichArtist_FanartOnly_FanartPathSetThumbNull(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -299,7 +300,7 @@ func TestEnrichArtist_FanartOnly_FanartPathSetThumbNull(t *testing.T) {
|
|||||||
// --- All-404 settles to 'none' ---
|
// --- All-404 settles to 'none' ---
|
||||||
|
|
||||||
func TestEnrichArtist_All404_SettlesNone(t *testing.T) {
|
func TestEnrichArtist_All404_SettlesNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -335,7 +336,7 @@ func TestEnrichArtist_All404_SettlesNone(t *testing.T) {
|
|||||||
// --- Empty providers list settles vacuously to 'none' ---
|
// --- Empty providers list settles vacuously to 'none' ---
|
||||||
|
|
||||||
func TestEnrichArtist_EmptyProviders_SettlesNone(t *testing.T) {
|
func TestEnrichArtist_EmptyProviders_SettlesNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -365,7 +366,7 @@ func TestEnrichArtist_EmptyProviders_SettlesNone(t *testing.T) {
|
|||||||
// --- ErrTransient leaves row NULL ---
|
// --- ErrTransient leaves row NULL ---
|
||||||
|
|
||||||
func TestEnrichArtist_ErrTransient_LeavesNull(t *testing.T) {
|
func TestEnrichArtist_ErrTransient_LeavesNull(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -399,7 +400,7 @@ func TestEnrichArtist_ErrTransient_LeavesNull(t *testing.T) {
|
|||||||
// --- Filesystem layout ---
|
// --- Filesystem layout ---
|
||||||
|
|
||||||
func TestEnrichArtist_FilesystemLayout(t *testing.T) {
|
func TestEnrichArtist_FilesystemLayout(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -497,7 +498,7 @@ func TestCleanupArtistArt_IdempotentMissingDir(t *testing.T) {
|
|||||||
// --- MBID guard ---
|
// --- MBID guard ---
|
||||||
|
|
||||||
func TestEnrichArtist_NoMBID_SettlesNone(t *testing.T) {
|
func TestEnrichArtist_NoMBID_SettlesNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
|
|||||||
@@ -13,32 +13,10 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 {
|
func discardLogger() *slog.Logger {
|
||||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
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) {
|
func TestEnrichAlbum_SidecarFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, dir := seedAlbumWithTrack(t, pool, "Sid", "Artist", "")
|
id, dir := seedAlbumWithTrack(t, pool, "Sid", "Artist", "")
|
||||||
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("img"), 0o644); err != nil {
|
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("img"), 0o644); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -122,7 +100,7 @@ func TestEnrichAlbum_SidecarFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichAlbum_NoSidecarNoMBID_SettlesNone(t *testing.T) {
|
func TestEnrichAlbum_NoSidecarNoMBID_SettlesNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, _ := seedAlbumWithTrack(t, pool, "NoMbid", "Artist", "")
|
id, _ := seedAlbumWithTrack(t, pool, "NoMbid", "Artist", "")
|
||||||
resetRegistryForTests() // deterministic empty registry (no provider can supply)
|
resetRegistryForTests() // deterministic empty registry (no provider can supply)
|
||||||
t.Cleanup(resetRegistryForTests)
|
t.Cleanup(resetRegistryForTests)
|
||||||
@@ -143,7 +121,7 @@ func TestEnrichAlbum_NoSidecarNoMBID_SettlesNone(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichAlbum_ProviderFetchSuccess(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")
|
id, dir := seedAlbumWithTrack(t, pool, "Prov", "Artist", "test-mbid-123")
|
||||||
|
|
||||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
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) {
|
func TestEnrichAlbum_All404_RecordsNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, _ := seedAlbumWithTrack(t, pool, "Missing", "Artist", "missing-mbid")
|
id, _ := seedAlbumWithTrack(t, pool, "Missing", "Artist", "missing-mbid")
|
||||||
|
|
||||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
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) {
|
func TestEnrichAlbum_AlreadySidecar_NoOp(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, dir := seedAlbumWithTrack(t, pool, "Already", "Artist", "")
|
id, dir := seedAlbumWithTrack(t, pool, "Already", "Artist", "")
|
||||||
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("first"), 0o644); err != nil {
|
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("first"), 0o644); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -258,7 +236,7 @@ func TestEnrichAlbum_AlreadySidecar_NoOp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichBatch_DrainsNullSourceOnly(t *testing.T) {
|
func TestEnrichBatch_DrainsNullSourceOnly(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id1, dir1 := seedAlbumWithTrack(t, pool, "Drain1", "A", "")
|
id1, dir1 := seedAlbumWithTrack(t, pool, "Drain1", "A", "")
|
||||||
if err := os.WriteFile(filepath.Join(dir1, "cover.jpg"), []byte("c"), 0o644); err != nil {
|
if err := os.WriteFile(filepath.Join(dir1, "cover.jpg"), []byte("c"), 0o644); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -305,7 +283,7 @@ func TestEnrichBatch_DrainsNullSourceOnly(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRetryAlbum_DeletesWrittenFileAndRefetches(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")
|
id, dir := seedAlbumWithTrack(t, pool, "Retry", "Artist", "retry-mbid")
|
||||||
|
|
||||||
hits := 0
|
hits := 0
|
||||||
@@ -351,7 +329,7 @@ func TestRetryAlbum_DeletesWrittenFileAndRefetches(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEnrichRetryMissing_DrainsNullAndNone(t *testing.T) {
|
func TestEnrichRetryMissing_DrainsNullAndNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
// Three albums:
|
// Three albums:
|
||||||
// id1: NULL source, has sidecar → drains, becomes 'sidecar'.
|
// id1: NULL source, has sidecar → drains, becomes 'sidecar'.
|
||||||
// id2: 'none' source, has sidecar → cleared then drained, 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
|
// TestEnrichAlbum_MultiProviderChain verifies that when provider A
|
||||||
// returns ErrNotFound, the enricher continues to provider B.
|
// returns ErrNotFound, the enricher continues to provider B.
|
||||||
func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
|
func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, dir := seedAlbumWithTrack(t, pool, "Multi", "Artist", "multi-mbid")
|
id, dir := seedAlbumWithTrack(t, pool, "Multi", "Artist", "multi-mbid")
|
||||||
|
|
||||||
// Provider A: always 404.
|
// Provider A: always 404.
|
||||||
@@ -473,7 +451,7 @@ func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
|
|||||||
// TestEnrichAlbum_TransientLeavesNull verifies that a transient error
|
// TestEnrichAlbum_TransientLeavesNull verifies that a transient error
|
||||||
// from the provider leaves the row NULL for next-pass retry.
|
// from the provider leaves the row NULL for next-pass retry.
|
||||||
func TestEnrichAlbum_TransientLeavesNull(t *testing.T) {
|
func TestEnrichAlbum_TransientLeavesNull(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, _ := seedAlbumWithTrack(t, pool, "Transient", "Artist", "transient-mbid")
|
id, _ := seedAlbumWithTrack(t, pool, "Transient", "Artist", "transient-mbid")
|
||||||
|
|
||||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
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
|
// cover_art_source='none' and a stale version gets cleared and
|
||||||
// re-enriched (sidecar wins because the album dir now has a cover file).
|
// re-enriched (sidecar wins because the album dir now has a cover file).
|
||||||
func TestEnrichAlbum_StaleNoneFlipsAndRetries(t *testing.T) {
|
func TestEnrichAlbum_StaleNoneFlipsAndRetries(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
id, dir := seedAlbumWithTrack(t, pool, "StaleNone", "Artist", "")
|
id, dir := seedAlbumWithTrack(t, pool, "StaleNone", "Artist", "")
|
||||||
|
|
||||||
// Mark the album as 'none' (simulating a previous pass).
|
// Mark the album as 'none' (simulating a previous pass).
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fakeArtistProvider opts into ArtistArtProvider for capability tests.
|
// fakeArtistProvider opts into ArtistArtProvider for capability tests.
|
||||||
@@ -27,7 +29,7 @@ func (f *fakeTestableAlbumProvider) TestConnection(_ context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_BootInsertsDefaultRows(t *testing.T) {
|
func TestSettingsService_BootInsertsDefaultRows(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -60,7 +62,7 @@ func TestSettingsService_BootInsertsDefaultRows(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_UpdateProvider_FlippingEnabledBumpsVersion(t *testing.T) {
|
func TestSettingsService_UpdateProvider_FlippingEnabledBumpsVersion(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -92,7 +94,7 @@ func TestSettingsService_UpdateProvider_FlippingEnabledBumpsVersion(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T) {
|
func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -131,7 +133,7 @@ func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_UpdateProvider_ClearKey(t *testing.T) {
|
func TestSettingsService_UpdateProvider_ClearKey(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -161,7 +163,7 @@ func TestSettingsService_UpdateProvider_ClearKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_TestProvider_Routing(t *testing.T) {
|
func TestSettingsService_TestProvider_Routing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -195,7 +197,7 @@ func TestSettingsService_TestProvider_Routing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_ListProviderInfo_Capabilities(t *testing.T) {
|
func TestSettingsService_ListProviderInfo_Capabilities(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -235,7 +237,7 @@ func TestSettingsService_ListProviderInfo_Capabilities(t *testing.T) {
|
|||||||
// TestSettingsService_BumpVersionIfProvidersChanged_FirstBoot: stored
|
// TestSettingsService_BumpVersionIfProvidersChanged_FirstBoot: stored
|
||||||
// hash is empty (column default ""), so a real provider set produces a bump.
|
// hash is empty (column default ""), so a real provider set produces a bump.
|
||||||
func TestSettingsService_BumpVersionIfProvidersChanged_FirstBoot(t *testing.T) {
|
func TestSettingsService_BumpVersionIfProvidersChanged_FirstBoot(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -266,7 +268,7 @@ func TestSettingsService_BumpVersionIfProvidersChanged_FirstBoot(t *testing.T) {
|
|||||||
// call back-to-back returns bumped=false because the stored hash now
|
// call back-to-back returns bumped=false because the stored hash now
|
||||||
// matches the registered set.
|
// matches the registered set.
|
||||||
func TestSettingsService_BumpVersionIfProvidersChanged_NoChange(t *testing.T) {
|
func TestSettingsService_BumpVersionIfProvidersChanged_NoChange(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
@@ -299,7 +301,7 @@ func TestSettingsService_BumpVersionIfProvidersChanged_NoChange(t *testing.T) {
|
|||||||
// path. Distinct from BumpVersionIfProvidersChanged, this always writes
|
// path. Distinct from BumpVersionIfProvidersChanged, this always writes
|
||||||
// regardless of the stored provider-set hash.
|
// regardless of the stored provider-set hash.
|
||||||
func TestSettingsService_BumpVersion_Increments(t *testing.T) {
|
func TestSettingsService_BumpVersion_Increments(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package dbtest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EnvDatabaseURL names the variable holding the Postgres DSN integration
|
||||||
|
// tests run against. Without it, every test that asks for a pool skips.
|
||||||
|
const EnvDatabaseURL = "MINSTREL_TEST_DATABASE_URL"
|
||||||
|
|
||||||
|
var (
|
||||||
|
migrateOnce sync.Once
|
||||||
|
errMigrate error
|
||||||
|
)
|
||||||
|
|
||||||
|
// Open returns a pool on the test database with the schema migrated to the
|
||||||
|
// latest version, closed when t ends. It skips t under -short or when
|
||||||
|
// MINSTREL_TEST_DATABASE_URL is unset. It does not clear any data: use Pool
|
||||||
|
// for that, unless the test manages its own rows.
|
||||||
|
func Open(t *testing.T) *pgxpool.Pool {
|
||||||
|
t.Helper()
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("skipping integration test in -short mode")
|
||||||
|
}
|
||||||
|
dsn := os.Getenv(EnvDatabaseURL)
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip(EnvDatabaseURL + " not set")
|
||||||
|
}
|
||||||
|
// Migrating is idempotent but not free; once per test binary is enough.
|
||||||
|
migrateOnce.Do(func() {
|
||||||
|
errMigrate = db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||||
|
})
|
||||||
|
if errMigrate != nil {
|
||||||
|
t.Fatalf("migrate: %v", errMigrate)
|
||||||
|
}
|
||||||
|
pool, err := pgxpool.New(context.Background(), dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("connect to test database: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(pool.Close)
|
||||||
|
return pool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pool is Open followed by ResetDB: a migrated pool with every data table
|
||||||
|
// empty and only non-test users left.
|
||||||
|
func Pool(t *testing.T) *pgxpool.Pool {
|
||||||
|
t.Helper()
|
||||||
|
pool := Open(t)
|
||||||
|
ResetDB(t, pool)
|
||||||
|
return pool
|
||||||
|
}
|
||||||
@@ -4,14 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
@@ -22,23 +20,8 @@ import (
|
|||||||
// to seed.
|
// to seed.
|
||||||
func testWorker(t *testing.T) (*Worker, *pgxpool.Pool) {
|
func testWorker(t *testing.T) (*Worker, *pgxpool.Pool) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping gc integration in -short mode")
|
|
||||||
}
|
|
||||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
|
||||||
if dsn == "" {
|
|
||||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
|
||||||
}
|
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Pool(t)
|
||||||
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 NewWorker(pool, logger), pool
|
return NewWorker(pool, logger), pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// restoring `mbid = EXCLUDED.mbid` in UpsertTrack: the first re-read below
|
||||||
// then erases the looked-up id.
|
// then erases the looked-up id.
|
||||||
func TestUpsertTrack_KeepsALookedUpMbidUntilATagCarriesOne_Integration(t *testing.T) {
|
func TestUpsertTrack_KeepsALookedUpMbidUntilATagCarriesOne_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
@@ -198,7 +198,7 @@ func TestUpsertTrack_KeepsALookedUpMbidUntilATagCarriesOne_Integration(t *testin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAcoustIDLookup_FillsOnlyWhatItCanSettle_Integration(t *testing.T) {
|
func TestAcoustIDLookup_FillsOnlyWhatItCanSettle_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
@@ -269,7 +269,7 @@ func TestAcoustIDLookup_FillsOnlyWhatItCanSettle_Integration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAcoustIDLookup_StopsWhenTheServiceCannotAnswer_Integration(t *testing.T) {
|
func TestAcoustIDLookup_StopsWhenTheServiceCannotAnswer_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
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
|
// similarity worker picks the track as a seed and can map ListenBrainz's
|
||||||
// answers back to it.
|
// answers back to it.
|
||||||
func TestAcoustIDLookup_FilledTrackReachesSimilarity_Integration(t *testing.T) {
|
func TestAcoustIDLookup_FilledTrackReachesSimilarity_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
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
|
// stored histograms, does nothing when nothing changed, and recomputes on
|
||||||
// each kind of membership change.
|
// each kind of membership change.
|
||||||
func TestAlbumLoudness_Integration(t *testing.T) {
|
func TestAlbumLoudness_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package library
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -14,32 +12,10 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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) {
|
func seedTrack(t *testing.T, pool *pgxpool.Pool, filePath string) (dbq.Track, dbq.Album, dbq.Artist) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
@@ -76,7 +52,7 @@ func stubRemoveFile(t *testing.T, fn func(string) error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteTrackFile_HappyPath(t *testing.T) {
|
func TestDeleteTrackFile_HappyPath(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
@@ -114,7 +90,7 @@ func TestDeleteTrackFile_HappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteTrackFile_EmptiedAlbumAndArtistGoToo(t *testing.T) {
|
func TestDeleteTrackFile_EmptiedAlbumAndArtistGoToo(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
path := filepath.Join(t.TempDir(), "lone.mp3")
|
path := filepath.Join(t.TempDir(), "lone.mp3")
|
||||||
@@ -157,7 +133,7 @@ func TestDeleteTrackFile_UnremovableFileDeletesNothing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
@@ -202,7 +178,7 @@ func TestDeleteTrackFile_UnremovableFileDeletesNothing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteTrackFile_FileAlreadyGoneSucceeds(t *testing.T) {
|
func TestDeleteTrackFile_FileAlreadyGoneSucceeds(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
track, _, _ := seedTrack(t, pool, "/no/such/file/anywhere.mp3")
|
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) {
|
func TestDeleteTrackFile_NotFoundReturnsErr(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
|
|
||||||
var bogus pgtype.UUID
|
var bogus pgtype.UUID
|
||||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ type mergeFixture struct {
|
|||||||
|
|
||||||
func newMergeFixture(t *testing.T) mergeFixture {
|
func newMergeFixture(t *testing.T) mergeFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ type resolveFixture struct {
|
|||||||
|
|
||||||
func newResolveFixture(t *testing.T) resolveFixture {
|
func newResolveFixture(t *testing.T) resolveFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := filepath.Join(t.TempDir(), "Plastic Apes", "Mammalz (2017)")
|
dir := filepath.Join(t.TempDir(), "Plastic Apes", "Mammalz (2017)")
|
||||||
@@ -145,7 +146,7 @@ type crossReleaseRipFixture struct {
|
|||||||
|
|
||||||
func newCrossReleaseRipFixture(t *testing.T) crossReleaseRipFixture {
|
func newCrossReleaseRipFixture(t *testing.T) crossReleaseRipFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
@@ -279,7 +280,7 @@ type oneAlbumFixture struct {
|
|||||||
|
|
||||||
func newOneAlbumFixture(t *testing.T, tier, titleA, titleB string) oneAlbumFixture {
|
func newOneAlbumFixture(t *testing.T, tier, titleA, titleB string) oneAlbumFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := filepath.Join(t.TempDir(), "Savanta", "Void")
|
dir := filepath.Join(t.TempDir(), "Savanta", "Void")
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
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
|
// out, and how re-sweeping treats a dismissal and a proposal that no longer
|
||||||
// holds.
|
// holds.
|
||||||
func TestDuplicateSweep_Integration(t *testing.T) {
|
func TestDuplicateSweep_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -13,12 +13,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"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
|
// TestFingerprintBackfill_Integration pins which tracks a pass touches, that a
|
||||||
// pass ends, and that the coverage gauge counts what the pass wrote.
|
// pass ends, and that the coverage gauge counts what the pass wrote.
|
||||||
func TestFingerprintBackfill_Integration(t *testing.T) {
|
func TestFingerprintBackfill_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -14,9 +14,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"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
|
// 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
|
// 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.
|
// covered by the parser tests; this covers the scan's decisions.
|
||||||
func TestScanner_FingerprintsOnlyNewOrChangedBytes_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
t.Fatalf("truncate: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateFingerprintSettings(t *testing.T) {
|
func TestValidateFingerprintSettings(t *testing.T) {
|
||||||
@@ -64,7 +66,7 @@ func TestFingerprintSettingsService_NilServesDefaults(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFingerprintSettingsService_Integration(t *testing.T) {
|
func TestFingerprintSettingsService_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
reload := func(step string) FingerprintSettings {
|
reload := func(step string) FingerprintSettings {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mkdir(t *testing.T, parts ...string) string {
|
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) {
|
func TestSetRoots_TracksMoveWithTheirFolder_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
clearSavedRoots(t, pool)
|
clearSavedRoots(t, pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
@@ -126,7 +127,7 @@ func TestSetRoots_TracksMoveWithTheirFolder_Integration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSetRoots_RefusesAMoveToAFolderWithoutTheFiles_Integration(t *testing.T) {
|
func TestSetRoots_RefusesAMoveToAFolderWithoutTheFiles_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
clearSavedRoots(t, pool)
|
clearSavedRoots(t, pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
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
|
// stores for each kind of result, that a pass ends, and that the gauge counts
|
||||||
// what the passes wrote.
|
// what the passes wrote.
|
||||||
func TestLoudnessBackfill_Integration(t *testing.T) {
|
func TestLoudnessBackfill_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
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
|
// The Go defaults must match the migration's, or a database that cannot be
|
||||||
// read would analyze differently from a fresh install.
|
// read would analyze differently from a fresh install.
|
||||||
func TestLoudnessSettings_DefaultsMatchMigration(t *testing.T) {
|
func TestLoudnessSettings_DefaultsMatchMigration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s, err := NewLoudnessSettingsService(context.Background(), pool)
|
s, err := NewLoudnessSettingsService(context.Background(), pool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load: %v", err)
|
t.Fatalf("load: %v", err)
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"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
|
// 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
|
// nothing to give them: they skip it rather than log an "open failed" warning
|
||||||
// per missing track on every scan.
|
// per missing track on every scan.
|
||||||
func TestMBIDBackfill_SkipsMissingTracks_Integration(t *testing.T) {
|
func TestMBIDBackfill_SkipsMissingTracks_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -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
|
// 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.
|
// the replacement, and every shape the pass must leave alone is left alone.
|
||||||
func TestPairMissingTracks_Integration(t *testing.T) {
|
func TestPairMissingTracks_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Pair Artist", SortName: "Pair Artist"})
|
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
|
// 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.
|
// match. The scan must still end with one row, carrying the old row's history.
|
||||||
func TestScanner_FoldsUpgradedFile_Integration(t *testing.T) {
|
func TestScanner_FoldsUpgradedFile_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// 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.
|
// formed because tracks already held were retagged into it is not new music.
|
||||||
func TestAnnounceNewAlbums_TellsEveryoneOfEachNewAlbumOnce(t *testing.T) {
|
func TestAnnounceNewAlbums_TellsEveryoneOfEachNewAlbumOnce(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := notifyingAdmin(t, pool)
|
admin := notifyingAdmin(t, pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func TestNormalizationPrefs_Validate(t *testing.T) {
|
|||||||
// migration's defaults for any column the insert left out; the two must
|
// 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.
|
// agree, or a user would hear a different level after their first save.
|
||||||
func TestNormalizationPrefs_DefaultsAndRoundTrip_Integration(t *testing.T) {
|
func TestNormalizationPrefs_DefaultsAndRoundTrip_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
user, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
user, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func unreadNotices(t *testing.T, pool *pgxpool.Pool, user dbq.User) []notice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotifyScanFinished_FailuresCoalesceAndShutdownIsSilent(t *testing.T) {
|
func TestNotifyScanFinished_FailuresCoalesceAndShutdownIsSilent(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := notifyingAdmin(t, pool)
|
admin := notifyingAdmin(t, pool)
|
||||||
ctx := context.Background()
|
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
|
// it has adopted moves and folded replacements, so a file it marked and then
|
||||||
// healed is never reported as lost.
|
// healed is never reported as lost.
|
||||||
func TestReconcileMissing_RecordsTheMarksWithoutNotifying(t *testing.T) {
|
func TestReconcileMissing_RecordsTheMarksWithoutNotifying(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := notifyingAdmin(t, pool)
|
admin := notifyingAdmin(t, pool)
|
||||||
s := testScanner(t, populatedRoot(t))
|
s := testScanner(t, populatedRoot(t))
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ func TestReconcileMissing_RecordsTheMarksWithoutNotifying(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotifyStillMissing_NamesOnlyTheTracksStillGone(t *testing.T) {
|
func TestNotifyStillMissing_NamesOnlyTheTracksStillGone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := notifyingAdmin(t, pool)
|
admin := notifyingAdmin(t, pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
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
|
// 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.
|
// group already seen by an earlier pass is not news again.
|
||||||
func TestResolveDuplicates_TellsAdminsOnlyOfNewGroupsLeftForThem(t *testing.T) {
|
func TestResolveDuplicates_TellsAdminsOnlyOfNewGroupsLeftForThem(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := notifyingAdmin(t, pool)
|
admin := notifyingAdmin(t, pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import (
|
|||||||
// A removed file's track stays, missing, with its likes and plays and the
|
// 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.
|
// reason it waits, and the good copy that lands later takes its history over.
|
||||||
func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) {
|
func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
admin, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
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
|
// ForgetMBID drops the recording MBID that came from a file that was not the
|
||||||
// track's song.
|
// track's song.
|
||||||
func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) {
|
func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
path := filepath.Join(t.TempDir(), "rip.mp3")
|
path := filepath.Join(t.TempDir(), "rip.mp3")
|
||||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
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.
|
// A file that cannot be removed leaves the track as it was.
|
||||||
func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) {
|
func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
path := filepath.Join(t.TempDir(), "stuck.mp3")
|
path := filepath.Join(t.TempDir(), "stuck.mp3")
|
||||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
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
|
// The reason describes one missing spell: a file back at its path clears it
|
||||||
// with the missing mark.
|
// with the missing mark.
|
||||||
func TestRemoveFile_ReasonGoesWhenTheFileIsBack(t *testing.T) {
|
func TestRemoveFile_ReasonGoesWhenTheFileIsBack(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
path := filepath.Join(t.TempDir(), "back.mp3")
|
path := filepath.Join(t.TempDir(), "back.mp3")
|
||||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGainDBAndLinearPeak(t *testing.T) {
|
func TestGainDBAndLinearPeak(t *testing.T) {
|
||||||
@@ -45,7 +46,7 @@ func TestGainDBAndLinearPeak(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReplayGainForTracks_Integration(t *testing.T) {
|
func TestReplayGainForTracks_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -11,10 +11,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"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/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReleaseDateFromYear(t *testing.T) {
|
func TestReleaseDateFromYear(t *testing.T) {
|
||||||
@@ -45,24 +44,10 @@ func TestReleaseDateFromYear(t *testing.T) {
|
|||||||
// TestScanner_Integration exercises walk → tag-parse → upsert → incremental
|
// TestScanner_Integration exercises walk → tag-parse → upsert → incremental
|
||||||
// skip against a real Postgres. Gated on MINSTREL_TEST_DATABASE_URL.
|
// skip against a real Postgres. Gated on MINSTREL_TEST_DATABASE_URL.
|
||||||
func TestScanner_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
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
|
// reconcile would refuse to mark, adoption could not fire, and the file would
|
||||||
// fork. See the "reconcile skipped" warning in Scan.
|
// fork. See the "reconcile skipped" warning in Scan.
|
||||||
func TestScanner_AdoptsMovedFile_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
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,
|
// read existed gets it on the next scan, through the tagReadVersion bump,
|
||||||
// without its file changing.
|
// without its file changing.
|
||||||
func TestScanner_ReleaseGroupMBID_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
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
|
// 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.
|
// album's row by the old lookup moves to its own on the tagReadVersion re-read.
|
||||||
func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
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
|
// with its old mtime, which takes the unchanged-file skip. The track keeps its
|
||||||
// id, and clients are told it plays again.
|
// id, and clients are told it plays again.
|
||||||
func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) {
|
func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
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
|
// 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.
|
// left behind is removed by the scan's tidy-up once a re-read empties it.
|
||||||
func TestScanner_AlbumArtistKeepsAnAlbumTogether_Integration(t *testing.T) {
|
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()
|
ctx := context.Background()
|
||||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
if err := db.Migrate(dsn, logger); err != nil {
|
pool := dbtest.Open(t)
|
||||||
t.Fatalf("migrate: %v", err)
|
|
||||||
}
|
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
t.Fatalf("truncate: %v", err)
|
t.Fatalf("truncate: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// list shows the song once, a like or unlike on either copy reaches both, and
|
||||||
// "not the same song" undoes the link.
|
// "not the same song" undoes the link.
|
||||||
func TestCrossReleaseCopiesCountAsOneSong_Integration(t *testing.T) {
|
func TestCrossReleaseCopiesCountAsOneSong_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
f := newSongLinkFixture(t, pool)
|
f := newSongLinkFixture(t, pool)
|
||||||
@@ -153,7 +153,7 @@ func TestCrossReleaseCopiesCountAsOneSong_Integration(t *testing.T) {
|
|||||||
|
|
||||||
// With auto-resolve off the resolver links nothing.
|
// With auto-resolve off the resolver links nothing.
|
||||||
func TestCrossReleaseLinkNeedsAutoResolve_Integration(t *testing.T) {
|
func TestCrossReleaseLinkNeedsAutoResolve_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
f := newSongLinkFixture(t, pool)
|
f := newSongLinkFixture(t, pool)
|
||||||
@@ -172,7 +172,7 @@ func TestCrossReleaseLinkNeedsAutoResolve_Integration(t *testing.T) {
|
|||||||
// they make it the key.
|
// they make it the key.
|
||||||
func TestMergeInheritsSongLink_Integration(t *testing.T) {
|
func TestMergeInheritsSongLink_Integration(t *testing.T) {
|
||||||
for _, removeAlbumCut := range []bool{true, false} {
|
for _, removeAlbumCut := range []bool{true, false} {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
f := newSongLinkFixture(t, 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
|
// extra copy is merged into its FLAC, the single stays, and the two copies
|
||||||
// left are linked as one song.
|
// left are linked as one song.
|
||||||
func TestResolveDuplicates_FoldsAnAlbumsExtraCopyInACrossReleaseGroup_Integration(t *testing.T) {
|
func TestResolveDuplicates_FoldsAnAlbumsExtraCopyInACrossReleaseGroup_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -5,27 +5,20 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// triggerTestPool returns a pool against MINSTREL_TEST_DATABASE_URL or skips.
|
// triggerTestPool returns a pool against MINSTREL_TEST_DATABASE_URL or skips.
|
||||||
func triggerTestPool(t *testing.T) *pgxpool.Pool {
|
func triggerTestPool(t *testing.T) *pgxpool.Pool {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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)
|
|
||||||
// Reset scan_runs so each test starts clean.
|
// Reset scan_runs so each test starts clean.
|
||||||
if _, err := pool.Exec(context.Background(), `DELETE FROM scan_runs`); err != nil {
|
if _, err := pool.Exec(context.Background(), `DELETE FROM scan_runs`); err != nil {
|
||||||
t.Fatalf("reset: %v", err)
|
t.Fatalf("reset: %v", err)
|
||||||
@@ -135,14 +128,7 @@ func TestTryStartScan_StaleInFlight_Reaps(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTryStartScan_DBError_Propagates(t *testing.T) {
|
func TestTryStartScan_DBError_Propagates(t *testing.T) {
|
||||||
url := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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.Close() // close immediately to force errors on subsequent queries
|
pool.Close() // close immediately to force errors on subsequent queries
|
||||||
|
|
||||||
started, existing, err := TryStartScan(
|
started, existing, err := TryStartScan(
|
||||||
|
|||||||
@@ -2,14 +2,8 @@ package lookups
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,23 +18,8 @@ func TestGet_WithoutAServiceBothLookupsAreOn(t *testing.T) {
|
|||||||
// A new install starts with both on; a save is cached for the workers and
|
// A new install starts with both on; a save is cached for the workers and
|
||||||
// read back by a server started afterwards.
|
// read back by a server started afterwards.
|
||||||
func TestSettings_StartOnAndKeepWhatIsSaved_Integration(t *testing.T) {
|
func TestSettings_StartOnAndKeepWhatIsSaved_Integration(t *testing.T) {
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping 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)
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pool, err := pgxpool.New(ctx, dsn)
|
pool := dbtest.Pool(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
dbtest.ResetDB(t, pool)
|
|
||||||
|
|
||||||
s, err := New(ctx, pool)
|
s, err := New(ctx, pool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/mailer"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/mailer"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
||||||
)
|
)
|
||||||
@@ -30,7 +31,7 @@ func digestWith(pool *pgxpool.Pool) (*notifications.Digest, *mailer.FakeSender)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
|
func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := emailUser(t, pool, "alice", true)
|
alice := emailUser(t, pool, "alice", true)
|
||||||
n := notifications.New(pool, nil, nil)
|
n := notifications.New(pool, nil, nil)
|
||||||
@@ -57,7 +58,7 @@ func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
|
func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
alice := emailUser(t, pool, "alice", true)
|
alice := emailUser(t, pool, "alice", true)
|
||||||
@@ -74,7 +75,7 @@ func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
|
func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
alice := emailUser(t, pool, "alice", true)
|
alice := emailUser(t, pool, "alice", true)
|
||||||
@@ -99,7 +100,7 @@ func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_NoAddressNoEmail(t *testing.T) {
|
func TestDigest_NoAddressNoEmail(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
bob := mkUser(t, dbq.New(pool), "bob", true)
|
bob := mkUser(t, dbq.New(pool), "bob", true)
|
||||||
n := notifications.New(pool, nil, nil)
|
n := notifications.New(pool, nil, nil)
|
||||||
@@ -111,7 +112,7 @@ func TestDigest_NoAddressNoEmail(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
|
func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
root := emailUser(t, pool, "root", true)
|
root := emailUser(t, pool, "root", true)
|
||||||
@@ -134,7 +135,7 @@ func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
|
func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := emailUser(t, pool, "alice", true)
|
alice := emailUser(t, pool, "alice", true)
|
||||||
n := notifications.New(pool, nil, nil)
|
n := notifications.New(pool, nil, nil)
|
||||||
@@ -158,7 +159,7 @@ func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
|
func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := emailUser(t, pool, "alice", true) // timezone defaults to UTC
|
alice := emailUser(t, pool, "alice", true) // timezone defaults to UTC
|
||||||
n := notifications.New(pool, nil, nil)
|
n := notifications.New(pool, nil, nil)
|
||||||
@@ -192,7 +193,7 @@ func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
|
func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -209,7 +210,7 @@ func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDigest_UsesTheConfiguredBatchWindow(t *testing.T) {
|
func TestDigest_UsesTheConfiguredBatchWindow(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := emailUser(t, pool, "alice", true)
|
alice := emailUser(t, pool, "alice", true)
|
||||||
_, err := notifications.SaveEmailSettings(ctx, dbq.New(pool), notifications.EmailSettings{SummaryHour: 9, BatchWindowMinutes: 15})
|
_, err := notifications.SaveEmailSettings(ctx, dbq.New(pool), notifications.EmailSettings{SummaryHour: 9, BatchWindowMinutes: 15})
|
||||||
|
|||||||
@@ -5,43 +5,19 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func mkUser(t *testing.T, q *dbq.Queries, name string, admin bool) pgtype.UUID {
|
func mkUser(t *testing.T, q *dbq.Queries, name string, admin bool) pgtype.UUID {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
@@ -71,7 +47,7 @@ func count(t *testing.T, payload []byte) int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
|
func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := mkUser(t, q, "alice", false)
|
alice := mkUser(t, q, "alice", false)
|
||||||
@@ -103,7 +79,7 @@ func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
|
func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
flagger := mkUser(t, q, "admin-flagger", true)
|
flagger := mkUser(t, q, "admin-flagger", true)
|
||||||
@@ -122,7 +98,7 @@ func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify_CoalescedCountAddsUpWhileUnread_AndStartsAfreshOnceRead(t *testing.T) {
|
func TestNotify_CoalescedCountAddsUpWhileUnread_AndStartsAfreshOnceRead(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
admin := mkUser(t, q, "admin", true)
|
admin := mkUser(t, q, "admin", true)
|
||||||
@@ -150,7 +126,7 @@ func TestNotify_CoalescedCountAddsUpWhileUnread_AndStartsAfreshOnceRead(t *testi
|
|||||||
// created_at would announce it again on the phone, and a cleared emailed_at
|
// created_at would announce it again on the phone, and a cleared emailed_at
|
||||||
// would email it again.
|
// would email it again.
|
||||||
func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
|
func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
admin := mkUser(t, q, "admin", true)
|
admin := mkUser(t, q, "admin", true)
|
||||||
@@ -186,7 +162,7 @@ func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
|
|||||||
// Each scan's new music is its own notice: one already emailed in the daily
|
// Each scan's new music is its own notice: one already emailed in the daily
|
||||||
// summary must not quietly take in albums that arrive after it.
|
// summary must not quietly take in albums that arrive after it.
|
||||||
func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
|
func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := mkUser(t, q, "alice", false)
|
alice := mkUser(t, q, "alice", false)
|
||||||
@@ -198,7 +174,7 @@ func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
|
func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
admin := mkUser(t, q, "admin", true)
|
admin := mkUser(t, q, "admin", true)
|
||||||
@@ -215,7 +191,7 @@ func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T) {
|
func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
muted := mkUser(t, q, "muted", false)
|
muted := mkUser(t, q, "muted", false)
|
||||||
@@ -233,7 +209,7 @@ func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
|
func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := mkUser(t, q, "alice", false)
|
alice := mkUser(t, q, "alice", false)
|
||||||
@@ -261,7 +237,7 @@ func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
|
|||||||
// added in Go without its migration fails here, not in production at INSERT
|
// added in Go without its migration fails here, not in production at INSERT
|
||||||
// time.
|
// time.
|
||||||
func TestEveryKindPassesTheSchemaChecks(t *testing.T) {
|
func TestEveryKindPassesTheSchemaChecks(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user := mkUser(t, q, "kinds", true)
|
user := mkUser(t, q, "kinds", true)
|
||||||
@@ -278,7 +254,7 @@ func TestEveryKindPassesTheSchemaChecks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRetention_TrimsOldReadRowsAndAncientUnreadOnes(t *testing.T) {
|
func TestRetention_TrimsOldReadRowsAndAncientUnreadOnes(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user := mkUser(t, q, "retention", false)
|
user := mkUser(t, q, "retention", false)
|
||||||
|
|||||||
@@ -5,39 +5,16 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 {
|
type fixture struct {
|
||||||
pool *pgxpool.Pool
|
pool *pgxpool.Pool
|
||||||
q *dbq.Queries
|
q *dbq.Queries
|
||||||
@@ -48,7 +25,7 @@ type fixture struct {
|
|||||||
|
|
||||||
func newFixture(t *testing.T, durationMs int32) fixture {
|
func newFixture(t *testing.T, durationMs int32) fixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ import (
|
|||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
|
func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
svc := playlists.NewService(pool, nil, dir)
|
svc := playlists.NewService(pool, nil, dir)
|
||||||
@@ -34,7 +35,7 @@ func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateCollage_OneTrack(t *testing.T) {
|
func TestGenerateCollage_OneTrack(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
tk := seedTrack(t, pool, "Prismatic", "FoO")
|
tk := seedTrack(t, pool, "Prismatic", "FoO")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"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
|
// order they first appear, at most six: a shelf of tiles picks from them so
|
||||||
// no two tiles show the same cover.
|
// no two tiles show the same cover.
|
||||||
func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
|
func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
user := seedUser(t, pool, "covers")
|
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
|
// A playlist with no covered albums lists none, rather than failing the
|
||||||
// List call that carries every other playlist.
|
// List call that carries every other playlist.
|
||||||
func TestCoverAlbums_EmptyPlaylistHasNone(t *testing.T) {
|
func TestCoverAlbums_EmptyPlaylistHasNone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user := seedUser(t, pool, "nocovers")
|
user := seedUser(t, pool, "nocovers")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"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) {
|
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
|
// and the bucket shrink to its 3. Capped before, the artist keeps 3 and the
|
||||||
// single-tag artists below it fill the rest.
|
// single-tag artists below it fill the rest.
|
||||||
func TestListTasteUnheardTracksForDiscover_CapsBeforeLimit(t *testing.T) {
|
func TestListTasteUnheardTracksForDiscover_CapsBeforeLimit(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "tastecap")
|
u := seedUser(t, pool, "tastecap")
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"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
|
// tier 2, an untouched one is tier 3, and a genre too small for a mix is not
|
||||||
// offered at all.
|
// offered at all.
|
||||||
func TestListGenreMixCandidates_TiersAndSizeFloor(t *testing.T) {
|
func TestListGenreMixCandidates_TiersAndSizeFloor(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixcand")
|
u := seedUser(t, pool, "gmixcand")
|
||||||
now := time.Now().UTC()
|
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
|
// also carries an excluded genre (matched case-insensitively) belongs to that
|
||||||
// genre's mix and is left out of this one.
|
// genre's mix and is left out of this one.
|
||||||
func TestListGenreMixTracks_LeavesOutSongsOfGenresAlreadyPicked(t *testing.T) {
|
func TestListGenreMixTracks_LeavesOutSongsOfGenresAlreadyPicked(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixexcl")
|
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
|
// often than played through is left out, while a favourite with stray skips
|
||||||
// stays; tracks of other genres never appear.
|
// stays; tracks of other genres never appear.
|
||||||
func TestListGenreMixTracks_SplitsFamiliarAndDropsRejectedTracks(t *testing.T) {
|
func TestListGenreMixTracks_SplitsFamiliarAndDropsRejectedTracks(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixtracks")
|
u := seedUser(t, pool, "gmixtracks")
|
||||||
now := time.Now().UTC()
|
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
|
// the genre they listen to, holding only that genre's tracks, with both
|
||||||
// halves of the blend stamped.
|
// halves of the blend stamped.
|
||||||
func TestBuildSystemPlaylists_GenreMixes(t *testing.T) {
|
func TestBuildSystemPlaylists_GenreMixes(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixbuild")
|
u := seedUser(t, pool, "gmixbuild")
|
||||||
now := time.Now().UTC()
|
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
|
// user knows, ones played often and liked come up earlier than ones played
|
||||||
// once.
|
// once.
|
||||||
func TestListGenreMixTracks_FamiliarLeansTowardFavourites(t *testing.T) {
|
func TestListGenreMixTracks_FamiliarLeansTowardFavourites(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixfav")
|
u := seedUser(t, pool, "gmixfav")
|
||||||
now := time.Now().UTC()
|
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
|
// have never met, and so does an artist similar to theirs. The stranger
|
||||||
// still appears: the lean tilts the draw, it does not exclude.
|
// still appears: the lean tilts the draw, it does not exclude.
|
||||||
func TestListGenreMixTracks_UnheardLeansTowardTheUsersArtists(t *testing.T) {
|
func TestListGenreMixTracks_UnheardLeansTowardTheUsersArtists(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "gmixartist")
|
u := seedUser(t, pool, "gmixartist")
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"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/eventbus"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||||
)
|
)
|
||||||
@@ -40,7 +41,7 @@ func TestOrderByTracks_ArrangesNamedEntriesInTheirOwnPlaces(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
|
func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -71,7 +72,7 @@ func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
|
func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
a := seedTrack(t, pool, "A", "Artist")
|
a := seedTrack(t, pool, "A", "Artist")
|
||||||
b := seedTrack(t, pool, "B", "Artist")
|
b := seedTrack(t, pool, "B", "Artist")
|
||||||
@@ -95,7 +96,7 @@ func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReorderTracks_ASecondSendChangesNothing(t *testing.T) {
|
func TestReorderTracks_ASecondSendChangesNothing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
a := seedTrack(t, pool, "A", "Artist")
|
a := seedTrack(t, pool, "A", "Artist")
|
||||||
b := seedTrack(t, pool, "B", "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.
|
// Every change tells the owner's clients, whichever client made it.
|
||||||
func TestChanges_TellTheOwnersClients(t *testing.T) {
|
func TestChanges_TellTheOwnersClients(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
a := seedTrack(t, pool, "A", "Artist")
|
a := seedTrack(t, pool, "A", "Artist")
|
||||||
bus := eventbus.New()
|
bus := eventbus.New()
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"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) {
|
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
|
// TestPickTopPlayedTracksForUser_LikedTierSkipsMissing: with no plays, For
|
||||||
// You seeds from likes. A liked track whose file is gone is not a seed.
|
// You seeds from likes. A liked track whose file is gone is not a seed.
|
||||||
func TestPickTopPlayedTracksForUser_LikedTierSkipsMissing(t *testing.T) {
|
func TestPickTopPlayedTracksForUser_LikedTierSkipsMissing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
u := seedUser(t, pool, "seedmissing")
|
u := seedUser(t, pool, "seedmissing")
|
||||||
present := seedTrack(t, pool, "Present", "Seed Artist A")
|
present := seedTrack(t, pool, "Present", "Seed Artist A")
|
||||||
gone := seedTrack(t, pool, "Gone", "Seed Artist B")
|
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
|
// 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.
|
// missing, the seed comes from the next album rather than the missing file.
|
||||||
func TestPickTopPlayedTrackForArtistByUser_FallbackSkipsMissing(t *testing.T) {
|
func TestPickTopPlayedTrackForArtistByUser_FallbackSkipsMissing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "songslikemissing")
|
u := seedUser(t, pool, "songslikemissing")
|
||||||
older := seedTrack(t, pool, "Older", "Songs Like Artist")
|
older := seedTrack(t, pool, "Older", "Songs Like Artist")
|
||||||
|
|||||||
@@ -9,11 +9,12 @@ import (
|
|||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreate_HappyPath(t *testing.T) {
|
func TestCreate_HappyPath(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ func TestCreate_HappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreate_EmptyNameRejected(t *testing.T) {
|
func TestCreate_EmptyNameRejected(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ func TestCreate_EmptyNameRejected(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet_NotFound(t *testing.T) {
|
func TestGet_NotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ func TestGet_NotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet_Forbidden_PrivatePlaylistFromOtherUser(t *testing.T) {
|
func TestGet_Forbidden_PrivatePlaylistFromOtherUser(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
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) {
|
func TestGet_Public_VisibleToOtherUser(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
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) {
|
func TestGet_Owner_SeesOwnPrivate(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -119,7 +120,7 @@ func TestGet_Owner_SeesOwnPrivate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestList_OwnAndPublic(t *testing.T) {
|
func TestList_OwnAndPublic(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -152,7 +153,7 @@ func TestList_OwnAndPublic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate_OwnerOnly(t *testing.T) {
|
func TestUpdate_OwnerOnly(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -177,7 +178,7 @@ func TestUpdate_OwnerOnly(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate_PartialFieldsLeaveOthersAlone(t *testing.T) {
|
func TestUpdate_PartialFieldsLeaveOthersAlone(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -204,7 +205,7 @@ func TestUpdate_PartialFieldsLeaveOthersAlone(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate_EmptyNameRejected(t *testing.T) {
|
func TestUpdate_EmptyNameRejected(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -220,7 +221,7 @@ func TestUpdate_EmptyNameRejected(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate_NotFound(t *testing.T) {
|
func TestUpdate_NotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -231,7 +232,7 @@ func TestUpdate_NotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete_OwnerOnly(t *testing.T) {
|
func TestDelete_OwnerOnly(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -254,7 +255,7 @@ func TestDelete_OwnerOnly(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete_NotFound(t *testing.T) {
|
func TestDelete_NotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -264,7 +265,7 @@ func TestDelete_NotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete_RemovesCoverFile(t *testing.T) {
|
func TestDelete_RemovesCoverFile(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
svc := playlists.NewService(pool, nil, dir)
|
svc := playlists.NewService(pool, nil, dir)
|
||||||
@@ -299,7 +300,7 @@ func TestDelete_RemovesCoverFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete_MissingCoverFileTolerated(t *testing.T) {
|
func TestDelete_MissingCoverFileTolerated(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
svc := playlists.NewService(pool, nil, dir)
|
svc := playlists.NewService(pool, nil, dir)
|
||||||
@@ -322,7 +323,7 @@ func TestDelete_MissingCoverFileTolerated(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAppendTracks_Snapshot(t *testing.T) {
|
func TestAppendTracks_Snapshot(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track := seedTrack(t, pool, "Prismatic", "Fields of Ontario")
|
track := seedTrack(t, pool, "Prismatic", "Fields of Ontario")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -348,7 +349,7 @@ func TestAppendTracks_Snapshot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAppendTracks_OwnerOnly(t *testing.T) {
|
func TestAppendTracks_OwnerOnly(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
track := seedTrack(t, pool, "T", "A")
|
track := seedTrack(t, pool, "T", "A")
|
||||||
@@ -362,7 +363,7 @@ func TestAppendTracks_OwnerOnly(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAppendTracks_NonExistentTrack(t *testing.T) {
|
func TestAppendTracks_NonExistentTrack(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|
||||||
@@ -374,7 +375,7 @@ func TestAppendTracks_NonExistentTrack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveTrack_RenumbersPositions(t *testing.T) {
|
func TestRemoveTrack_RenumbersPositions(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
t1 := seedTrack(t, pool, "Track 1", "Artist")
|
t1 := seedTrack(t, pool, "Track 1", "Artist")
|
||||||
t2 := seedTrack(t, pool, "Track 2", "Artist")
|
t2 := seedTrack(t, pool, "Track 2", "Artist")
|
||||||
@@ -402,7 +403,7 @@ func TestRemoveTrack_RenumbersPositions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReorder_Permutation(t *testing.T) {
|
func TestReorder_Permutation(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
t1 := seedTrack(t, pool, "A", "Artist")
|
t1 := seedTrack(t, pool, "A", "Artist")
|
||||||
t2 := seedTrack(t, pool, "B", "Artist")
|
t2 := seedTrack(t, pool, "B", "Artist")
|
||||||
@@ -427,7 +428,7 @@ func TestReorder_Permutation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReorder_RejectsNonPermutation(t *testing.T) {
|
func TestReorder_RejectsNonPermutation(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
tk := seedTrack(t, pool, "T", "A")
|
tk := seedTrack(t, pool, "T", "A")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
@@ -451,7 +452,7 @@ func TestReorder_RejectsNonPermutation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotPersistsAfterUpstreamTrackDelete(t *testing.T) {
|
func TestSnapshotPersistsAfterUpstreamTrackDelete(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
tk := seedTrack(t, pool, "Doomed track", "Artist")
|
tk := seedTrack(t, pool, "Doomed track", "Artist")
|
||||||
svc := playlists.NewService(pool, nil, t.TempDir())
|
svc := playlists.NewService(pool, nil, t.TempDir())
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -14,7 +11,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
@@ -25,31 +21,6 @@ import (
|
|||||||
// tests seed multiple tracks within one test function.
|
// tests seed multiple tracks within one test function.
|
||||||
var seedTrackCounter uint64
|
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
|
// seedUser inserts a `test-`-prefixed user (so dbtest.ResetDB can
|
||||||
// clean it without touching the developer's admin row) and returns
|
// clean it without touching the developer's admin row) and returns
|
||||||
// the persisted user record.
|
// the persisted user record.
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"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
|
// album cut, the better-ranked copy stays; a rip is left out and the next copy
|
||||||
// of its song takes the place.
|
// of its song takes the place.
|
||||||
func TestOneCopyPerSong_Integration(t *testing.T) {
|
func TestOneCopyPerSong_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
single := seedTrack(t, pool, "copies-single", "copies-artist")
|
single := seedTrack(t, pool, "copies-single", "copies-artist")
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// seedTrackForArtist creates another track under an existing artist +
|
// 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) {
|
func TestListDeepCutsTracks_SkippedTracksExcluded(t *testing.T) {
|
||||||
// Eligibility counts skipped plays too, so a track skipped repeatedly
|
// Eligibility counts skipped plays too, so a track skipped repeatedly
|
||||||
// (and never finished) does not read as "barely heard".
|
// (and never finished) does not read as "barely heard".
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u := seedUser(t, pool, "dc1")
|
u := seedUser(t, pool, "dc1")
|
||||||
now := time.Now().UTC()
|
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
|
// 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
|
// 3 read as 359 days apart. The build date parameter anchors "today" so the
|
||||||
// wrap is testable at any run date.
|
// wrap is testable at any run date.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u := seedUser(t, pool, "otd1")
|
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
|
// affinity albums; tier 2 = affinity albums from the wider 90-day
|
||||||
// window; tier 3 = fresh additions without affinity; any attempted
|
// window; tier 3 = fresh additions without affinity; any attempted
|
||||||
// (>=30s) track consumes the album out of the mix entirely.
|
// (>=30s) track consumes the album out of the mix entirely.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u := seedUser(t, pool, "nfy1")
|
u := seedUser(t, pool, "nfy1")
|
||||||
now := time.Now().UTC()
|
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
|
// "Attempted" is track-level at >=30s. A 2-second brush does not
|
||||||
// disqualify an album; barely-attempted albums step back
|
// disqualify an album; barely-attempted albums step back
|
||||||
// to tier 2 with the attempted tracks themselves excluded.
|
// to tier 2 with the attempted tracks themselves excluded.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u := seedUser(t, pool, "fl1")
|
u := seedUser(t, pool, "fl1")
|
||||||
now := time.Now().UTC()
|
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
|
// invitation — the track sits out ~90 days instead of re-qualifying
|
||||||
// the next day. Skips from other surfaces don't cool down, and the
|
// the next day. Skips from other surfaces don't cool down, and the
|
||||||
// cooldown expires.
|
// cooldown expires.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u := seedUser(t, pool, "rd1")
|
u := seedUser(t, pool, "rd1")
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"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) {
|
func TestBuildSystemPlaylists_SufficientActivity(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "act1", 4, 3)
|
u, _ := seedActiveLibrary(t, pool, "act1", 4, 3)
|
||||||
if err := playlists.BuildSystemPlaylists(context.Background(), pool, logger, u.ID, time.Now().UTC(), t.TempDir()); err != nil {
|
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 —
|
// inside 30d) must still seed the mixes —
|
||||||
// and the built tracks carry the tier2 stamp so metrics can compare
|
// and the built tracks carry the tier2 stamp so metrics can compare
|
||||||
// stale-seeded mixes against fresh ones.
|
// stale-seeded mixes against fresh ones.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u := seedUser(t, pool, "stale1")
|
u := seedUser(t, pool, "stale1")
|
||||||
old := time.Now().UTC().Add(-20 * 24 * time.Hour)
|
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) {
|
func TestBuildSystemPlaylists_QuarantineExcluded(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, tracks := seedActiveLibrary(t, pool, "qact", 3, 3)
|
u, tracks := seedActiveLibrary(t, pool, "qact", 3, 3)
|
||||||
|
|
||||||
@@ -228,7 +229,7 @@ func TestBuildSystemPlaylists_QuarantineExcluded(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
|
func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "atomic", 3, 3)
|
u, _ := seedActiveLibrary(t, pool, "atomic", 3, 3)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -265,7 +266,7 @@ func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildSystemPlaylists_Concurrency(t *testing.T) {
|
func TestBuildSystemPlaylists_Concurrency(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "conc", 3, 3)
|
u, _ := seedActiveLibrary(t, pool, "conc", 3, 3)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -361,7 +362,7 @@ func seedSharedArtistLibrary(
|
|||||||
// to survive a filter coming back in a different shape — and because an
|
// 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.
|
// absence check would match the comment explaining why there is no filter.
|
||||||
func TestBuildSystemPlaylists_SongsLikeIncludesItsSeedArtist(t *testing.T) {
|
func TestBuildSystemPlaylists_SongsLikeIncludesItsSeedArtist(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedSharedArtistLibrary(t, pool, "seedartist", 4, 6)
|
u, _ := seedSharedArtistLibrary(t, pool, "seedartist", 4, 6)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -423,7 +424,7 @@ func TestBuildSystemPlaylists_SongsLikeIncludesItsSeedArtist(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildSystemPlaylists_DailyNonceDeterminism(t *testing.T) {
|
func TestBuildSystemPlaylists_DailyNonceDeterminism(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "nonce", 4, 5)
|
u, _ := seedActiveLibrary(t, pool, "nonce", 4, 5)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -538,7 +539,7 @@ func equalSnapshots(a, b map[string][]string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestListActiveUsersForSystemPlaylists(t *testing.T) {
|
func TestListActiveUsersForSystemPlaylists(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
activeU, tracks := seedActiveLibrary(t, pool, "active", 1, 1)
|
activeU, tracks := seedActiveLibrary(t, pool, "active", 1, 1)
|
||||||
inactiveU := seedUser(t, pool, "inactive")
|
inactiveU := seedUser(t, pool, "inactive")
|
||||||
@@ -570,7 +571,7 @@ func TestListActiveUsersForSystemPlaylists(t *testing.T) {
|
|||||||
func TestBuildSystemPlaylists_DiscoverColdStart(t *testing.T) {
|
func TestBuildSystemPlaylists_DiscoverColdStart(t *testing.T) {
|
||||||
// User has no plays / likes → Discover's dormant + cross-user
|
// User has no plays / likes → Discover's dormant + cross-user
|
||||||
// buckets are empty (or minimal), all slots come from random unheard.
|
// buckets are empty (or minimal), all slots come from random unheard.
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "discover_cold", 4, 5)
|
u, _ := seedActiveLibrary(t, pool, "discover_cold", 4, 5)
|
||||||
// seedActiveLibrary seeds 3 plays per track; delete them so dormant
|
// 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) {
|
func TestStartupRecoveryClearsStaleInFlight(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "stale")
|
u := seedUser(t, pool, "stale")
|
||||||
// Seed a stale in_flight=true row.
|
// Seed a stale in_flight=true row.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"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
|
// You-might-like rows — the build ships nothing rather than near-random
|
||||||
// tiles.
|
// tiles.
|
||||||
func TestYouMightLike_ColdStartGate(t *testing.T) {
|
func TestYouMightLike_ColdStartGate(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "ymlcold", 3, 3)
|
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
|
// × 4 tracks = 32 distinct tracks, clearing both gate bars) populates the
|
||||||
// You-might-like tables in the daily build.
|
// You-might-like tables in the daily build.
|
||||||
func TestYouMightLike_SufficientActivityPopulates(t *testing.T) {
|
func TestYouMightLike_SufficientActivityPopulates(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "ymlrich", 8, 4)
|
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
|
// TestYouMightLike_AtomicReplace: a second build replaces rather than
|
||||||
// appends, so counts stay bounded by the persisted depth.
|
// appends, so counts stay bounded by the persisted depth.
|
||||||
func TestYouMightLike_AtomicReplace(t *testing.T) {
|
func TestYouMightLike_AtomicReplace(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
logger := discardLogger()
|
logger := discardLogger()
|
||||||
u, _ := seedActiveLibrary(t, pool, "ymlreplace", 8, 4)
|
u, _ := seedActiveLibrary(t, pool, "ymlreplace", 8, 4)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|||||||
@@ -2,41 +2,16 @@ package playsessions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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
|
|
||||||
}
|
|
||||||
|
|
||||||
func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
@@ -52,7 +27,7 @@ func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
|
func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedTestUser(t, pool)
|
user := seedTestUser(t, pool)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -77,7 +52,7 @@ func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
|
func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedTestUser(t, pool)
|
user := seedTestUser(t, pool)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -106,7 +81,7 @@ func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
|
func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedTestUser(t, pool)
|
user := seedTestUser(t, pool)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -125,7 +100,7 @@ func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindOrCreate_AtExactBoundaryExtendsExisting(t *testing.T) {
|
func TestFindOrCreate_AtExactBoundaryExtendsExisting(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedTestUser(t, pool)
|
user := seedTestUser(t, pool)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package quarantine
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -12,32 +10,10 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 {
|
func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
@@ -79,7 +55,7 @@ func seedTrack(t *testing.T, pool *pgxpool.Pool, title, mbid string) (dbq.Track,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFlag_HappyPath(t *testing.T) {
|
func TestFlag_HappyPath(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track, _, _ := seedTrack(t, pool, "Bad Track", "abc")
|
track, _, _ := seedTrack(t, pool, "Bad Track", "abc")
|
||||||
|
|
||||||
@@ -97,7 +73,7 @@ func TestFlag_HappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFlag_UpsertOnSecondFlag(t *testing.T) {
|
func TestFlag_UpsertOnSecondFlag(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||||
|
|
||||||
@@ -118,7 +94,7 @@ func TestFlag_UpsertOnSecondFlag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
|
func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
|
|
||||||
var bogus pgtype.UUID
|
var bogus pgtype.UUID
|
||||||
@@ -133,7 +109,7 @@ func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFlag_BadReasonRejected(t *testing.T) {
|
func TestFlag_BadReasonRejected(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||||
|
|
||||||
@@ -145,7 +121,7 @@ func TestFlag_BadReasonRejected(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnflag_DeletesRow(t *testing.T) {
|
func TestUnflag_DeletesRow(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||||
|
|
||||||
@@ -162,7 +138,7 @@ func TestUnflag_DeletesRow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestListMine_OrderedNewestFirst(t *testing.T) {
|
func TestListMine_OrderedNewestFirst(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
t1, _, _ := seedTrack(t, pool, "T1", "x")
|
t1, _, _ := seedTrack(t, pool, "T1", "x")
|
||||||
t2, _, _ := seedTrack(t, pool, "T2", "y")
|
t2, _, _ := seedTrack(t, pool, "T2", "y")
|
||||||
@@ -189,7 +165,7 @@ func TestListMine_OrderedNewestFirst(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestListAdminQueue_AggregatesByTrackWithReasonCounts(t *testing.T) {
|
func TestListAdminQueue_AggregatesByTrackWithReasonCounts(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
carol := seedUser(t, pool, "carol")
|
carol := seedUser(t, pool, "carol")
|
||||||
@@ -226,7 +202,7 @@ func TestListAdminQueue_AggregatesByTrackWithReasonCounts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolve_ClearsRowsAndWritesAudit(t *testing.T) {
|
func TestResolve_ClearsRowsAndWritesAudit(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
bob := seedUser(t, pool, "bob")
|
bob := seedUser(t, pool, "bob")
|
||||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||||
@@ -256,7 +232,7 @@ func TestResolve_ClearsRowsAndWritesAudit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
|
func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
track, _, _ := seedTrack(t, pool, "T", "x")
|
track, _, _ := seedTrack(t, pool, "T", "x")
|
||||||
|
|
||||||
@@ -275,7 +251,7 @@ func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolve_TrackNotFound(t *testing.T) {
|
func TestResolve_TrackNotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
var bogus pgtype.UUID
|
var bogus pgtype.UUID
|
||||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||||
@@ -289,7 +265,7 @@ func TestResolve_TrackNotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
|
func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
@@ -333,7 +309,7 @@ func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
|
|||||||
// the listeners' rows go, open playback errors read deleted, and the history
|
// the listeners' rows go, open playback errors read deleted, and the history
|
||||||
// says the file was removed. A "wrong file" report drops the file's MBID.
|
// says the file was removed. A "wrong file" report drops the file's MBID.
|
||||||
func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
|
func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
alice := seedUser(t, pool, "alice")
|
alice := seedUser(t, pool, "alice")
|
||||||
admin := seedUser(t, pool, "admin")
|
admin := seedUser(t, pool, "admin")
|
||||||
@@ -401,7 +377,7 @@ func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteFile_TrackNotFound(t *testing.T) {
|
func TestDeleteFile_TrackNotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "alice")
|
user := seedUser(t, pool, "alice")
|
||||||
var bogus pgtype.UUID
|
var bogus pgtype.UUID
|
||||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||||
|
|||||||
@@ -3,41 +3,16 @@ package recommendation
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 {
|
type fixture struct {
|
||||||
pool *pgxpool.Pool
|
pool *pgxpool.Pool
|
||||||
q *dbq.Queries
|
q *dbq.Queries
|
||||||
@@ -47,7 +22,7 @@ type fixture struct {
|
|||||||
|
|
||||||
func newFixture(t *testing.T, n int) fixture {
|
func newFixture(t *testing.T, n int) fixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func insertPlayEventDevice(
|
func insertPlayEventDevice(
|
||||||
@@ -37,7 +38,7 @@ func insertPlayEventDevice(
|
|||||||
// the mobile artist out-scores the web artist and vice-versa, while the
|
// the mobile artist out-scores the web artist and vice-versa, while the
|
||||||
// device-agnostic (”) pass treats them equally.
|
// device-agnostic (”) pass treats them equally.
|
||||||
func TestLoadContextAffinity_DeviceNarrowing(t *testing.T) {
|
func TestLoadContextAffinity_DeviceNarrowing(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
|
|||||||
@@ -4,41 +4,16 @@ package recommendation
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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 {
|
func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestHomeData_YouMightLike_FallbackFillsFromLikedArtists: when the taste
|
// TestHomeData_YouMightLike_FallbackFillsFromLikedArtists: when the taste
|
||||||
@@ -17,7 +18,7 @@ import (
|
|||||||
// You-might-like fallback fills the rest disjointly so no artist is shown
|
// 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.
|
// twice. 20 liked artists, none played → Rediscover 10 + You-might-like 10.
|
||||||
func TestHomeData_YouMightLike_FallbackFillsFromLikedArtists(t *testing.T) {
|
func TestHomeData_YouMightLike_FallbackFillsFromLikedArtists(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-yml-fallback")
|
user := seedUser(t, pool, "home-yml-fallback")
|
||||||
liked := make(map[pgtype.UUID]bool, 20)
|
liked := make(map[pgtype.UUID]bool, 20)
|
||||||
for i := 0; i < 20; i++ {
|
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
|
// (most users like tracks/albums, rarely artists). 12 liked tracks by distinct
|
||||||
// artists, no artist-likes, no plays → the artist row fills from them.
|
// artists, no artist-likes, no plays → the artist row fills from them.
|
||||||
func TestHomeData_YouMightLike_FallbackFromLikedTrackArtists(t *testing.T) {
|
func TestHomeData_YouMightLike_FallbackFromLikedTrackArtists(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-yml-trackartists")
|
user := seedUser(t, pool, "home-yml-trackartists")
|
||||||
fromLiked := make(map[pgtype.UUID]bool, 12)
|
fromLiked := make(map[pgtype.UUID]bool, 12)
|
||||||
for i := 0; i < 12; i++ {
|
for i := 0; i < 12; i++ {
|
||||||
@@ -83,7 +84,7 @@ func TestHomeData_YouMightLike_FallbackFromLikedTrackArtists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHomeData_NewUser_AllSectionsEmpty(t *testing.T) {
|
func TestHomeData_NewUser_AllSectionsEmpty(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-newuser")
|
user := seedUser(t, pool, "home-newuser")
|
||||||
|
|
||||||
got, err := HomeData(context.Background(), pool, user.ID)
|
got, err := HomeData(context.Background(), pool, user.ID)
|
||||||
@@ -116,7 +117,7 @@ func TestHomeData_NewUser_AllSectionsEmpty(t *testing.T) {
|
|||||||
var _ map[pgtype.UUID]struct{}
|
var _ map[pgtype.UUID]struct{}
|
||||||
|
|
||||||
func TestHomeData_RecentlyAdded_OrderedByCreatedAt(t *testing.T) {
|
func TestHomeData_RecentlyAdded_OrderedByCreatedAt(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-recents")
|
user := seedUser(t, pool, "home-recents")
|
||||||
a1 := seedArtist(t, pool, "ArtistA", "")
|
a1 := seedArtist(t, pool, "ArtistA", "")
|
||||||
// Three albums, oldest first — created_at increases naturally with each insert.
|
// 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) {
|
func TestHomeData_MostPlayed_RanksByCount_HonorsQuarantine(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-mostplayed")
|
user := seedUser(t, pool, "home-mostplayed")
|
||||||
artist := seedArtist(t, pool, "ArtistB", "")
|
artist := seedArtist(t, pool, "ArtistB", "")
|
||||||
album := seedAlbumForArtist(t, pool, artist.ID, "AlbumB")
|
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) {
|
func TestHomeData_RediscoverAlbums_FallbackWhenSparse(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-rediscover")
|
user := seedUser(t, pool, "home-rediscover")
|
||||||
artist := seedArtist(t, pool, "ArtistC", "")
|
artist := seedArtist(t, pool, "ArtistC", "")
|
||||||
// Like one album NOW (won't satisfy >30d eligibility) — should still
|
// 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
|
// ago) is enough for the album to appear in Rediscover, even with no
|
||||||
// explicit general_likes_albums entry.
|
// explicit general_likes_albums entry.
|
||||||
func TestHomeData_Rediscover_AlbumFromTrackLikes(t *testing.T) {
|
func TestHomeData_Rediscover_AlbumFromTrackLikes(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-redis-tracklike")
|
user := seedUser(t, pool, "home-redis-tracklike")
|
||||||
artist := seedArtist(t, pool, "ArtistTL", "")
|
artist := seedArtist(t, pool, "ArtistTL", "")
|
||||||
al := seedAlbumForArtist(t, pool, artist.ID, "TrackLiked")
|
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
|
// track-derived path. The fallback should also miss this since no
|
||||||
// album-like exists.
|
// album-like exists.
|
||||||
func TestHomeData_Rediscover_AlbumTrackLikeThreshold(t *testing.T) {
|
func TestHomeData_Rediscover_AlbumTrackLikeThreshold(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-redis-threshold")
|
user := seedUser(t, pool, "home-redis-threshold")
|
||||||
artist := seedArtist(t, pool, "ArtistTh", "")
|
artist := seedArtist(t, pool, "ArtistTh", "")
|
||||||
al := seedAlbumForArtist(t, pool, artist.ID, "OneTrackLiked")
|
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
|
// albums from the same artist appear in Rediscover even when more are
|
||||||
// eligible.
|
// eligible.
|
||||||
func TestHomeData_Rediscover_DiversityCap(t *testing.T) {
|
func TestHomeData_Rediscover_DiversityCap(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-redis-cap")
|
user := seedUser(t, pool, "home-redis-cap")
|
||||||
artist := seedArtist(t, pool, "ArtistDom", "")
|
artist := seedArtist(t, pool, "ArtistDom", "")
|
||||||
albums := []dbq.Album{
|
albums := []dbq.Album{
|
||||||
@@ -299,7 +300,7 @@ func TestHomeData_Rediscover_DiversityCap(t *testing.T) {
|
|||||||
// dropped from Rediscover (the "you forgot about this" contract is
|
// dropped from Rediscover (the "you forgot about this" contract is
|
||||||
// broken if the user is actively spinning it).
|
// broken if the user is actively spinning it).
|
||||||
func TestHomeData_Rediscover_DedupVsMostPlayed(t *testing.T) {
|
func TestHomeData_Rediscover_DedupVsMostPlayed(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
user := seedUser(t, pool, "home-redis-dedup")
|
user := seedUser(t, pool, "home-redis-dedup")
|
||||||
artist := seedArtist(t, pool, "ArtistD", "")
|
artist := seedArtist(t, pool, "ArtistD", "")
|
||||||
// "Forgotten" album: liked >30d ago, no recent plays — would be
|
// "Forgotten" album: liked >30d ago, no recent plays — would be
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uuidN(n byte) pgtype.UUID {
|
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
|
// TestLoadTasteProfile_RoundTrip seeds taste_profile rows and verifies the
|
||||||
// reader hydrates them into a profile that scores a matching track positively.
|
// reader hydrates them into a profile that scores a matching track positively.
|
||||||
func TestLoadTasteProfile_RoundTrip(t *testing.T) {
|
func TestLoadTasteProfile_RoundTrip(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
u := seedUser(t, pool, "taste-rt")
|
u := seedUser(t, pool, "taste-rt")
|
||||||
art := seedArtist(t, pool, "Loved Artist", "")
|
art := seedArtist(t, pool, "Loved Artist", "")
|
||||||
|
|||||||
@@ -6,37 +6,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"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")
|
|
||||||
}
|
|
||||||
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(context.Background(), dsn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pool: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(pool.Close)
|
|
||||||
dbtest.ResetDB(t, pool)
|
|
||||||
return pool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newService(t *testing.T, pool *pgxpool.Pool) *Service {
|
func newService(t *testing.T, pool *pgxpool.Pool) *Service {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||||
@@ -80,7 +56,7 @@ func auditRows(t *testing.T, pool *pgxpool.Pool) []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNew_SeedsShippedDefaults(t *testing.T) {
|
func TestNew_SeedsShippedDefaults(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
|
if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
|
||||||
t.Errorf("radio weights = %+v, want shipped defaults", got)
|
t.Errorf("radio weights = %+v, want shipped defaults", got)
|
||||||
@@ -98,7 +74,7 @@ func TestNew_SeedsShippedDefaults(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
|
func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
||||||
map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
|
map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
|
||||||
@@ -132,7 +108,7 @@ func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateProfile_Validation(t *testing.T) {
|
func TestUpdateProfile_Validation(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -159,7 +135,7 @@ func TestUpdateProfile_Validation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
|
func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
// Moving neutral above full must reject on the PATCHED result.
|
// Moving neutral above full must reject on the PATCHED result.
|
||||||
err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
|
err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
|
||||||
@@ -183,7 +159,7 @@ func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
|
func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
// In-range update persists into cache + the assembled taste config.
|
// In-range update persists into cache + the assembled taste config.
|
||||||
if err := s.UpdateTaste(context.Background(),
|
if err := s.UpdateTaste(context.Background(),
|
||||||
@@ -204,7 +180,7 @@ func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateTaste_EraScale(t *testing.T) {
|
func TestUpdateTaste_EraScale(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
// In-range update persists into cache + the assembled taste config.
|
// In-range update persists into cache + the assembled taste config.
|
||||||
if err := s.UpdateTaste(context.Background(),
|
if err := s.UpdateTaste(context.Background(),
|
||||||
@@ -225,7 +201,7 @@ func TestUpdateTaste_EraScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateTaste_MoodScale(t *testing.T) {
|
func TestUpdateTaste_MoodScale(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
if err := s.UpdateTaste(context.Background(),
|
if err := s.UpdateTaste(context.Background(),
|
||||||
map[string]float64{"mood_scale": 0.8}); err != nil {
|
map[string]float64{"mood_scale": 0.8}); err != nil {
|
||||||
@@ -244,7 +220,7 @@ func TestUpdateTaste_MoodScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReset_RestoresShippedAndAudits(t *testing.T) {
|
func TestReset_RestoresShippedAndAudits(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
// Reset with nothing changed is a no-op (no audit row).
|
// Reset with nothing changed is a no-op (no audit row).
|
||||||
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
||||||
@@ -270,7 +246,7 @@ func TestReset_RestoresShippedAndAudits(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
|
func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
s := newService(t, pool)
|
s := newService(t, pool)
|
||||||
shipped := ShippedRadioWeights()
|
shipped := ShippedRadioWeights()
|
||||||
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
||||||
|
|||||||
@@ -2,15 +2,11 @@ package scrobble
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
||||||
@@ -18,22 +14,7 @@ import (
|
|||||||
|
|
||||||
func testPool(t *testing.T) (*pgxpool.Pool, *dbq.Queries) {
|
func testPool(t *testing.T) (*pgxpool.Pool, *dbq.Queries) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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, dbq.New(pool)
|
return pool, dbq.New(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,45 +3,19 @@ package secret_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every listed column exists, so a renamed or dropped column fails here
|
// Every listed column exists, so a renamed or dropped column fails here
|
||||||
// rather than leaving a credential unsealed.
|
// rather than leaving a credential unsealed.
|
||||||
func TestColumnsExistInTheSchema_Integration(t *testing.T) {
|
func TestColumnsExistInTheSchema_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
for _, c := range secret.Columns {
|
for _, c := range secret.Columns {
|
||||||
var n int
|
var n int
|
||||||
err := pool.QueryRow(context.Background(),
|
err := pool.QueryRow(context.Background(),
|
||||||
@@ -57,7 +31,7 @@ func TestColumnsExistInTheSchema_Integration(t *testing.T) {
|
|||||||
// A database written before sealing existed is sealed in place at startup,
|
// A database written before sealing existed is sealed in place at startup,
|
||||||
// every value opens to what it held, and a second pass has nothing to do.
|
// every value opens to what it held, and a second pass has nothing to do.
|
||||||
func TestSealStoredSealsWhatIsInTheClear_Integration(t *testing.T) {
|
func TestSealStoredSealsWhatIsInTheClear_Integration(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|
||||||
|
|||||||
@@ -7,18 +7,16 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/subsonic"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/subsonic"
|
||||||
)
|
)
|
||||||
@@ -165,18 +163,7 @@ func TestRouter_RestPathNotSwallowedBySPA(t *testing.T) {
|
|||||||
//
|
//
|
||||||
// Gated on MINSTREL_TEST_DATABASE_URL like other integration-leaning tests.
|
// Gated on MINSTREL_TEST_DATABASE_URL like other integration-leaning tests.
|
||||||
func TestRouter_AdminSubtreeNotShadowed(t *testing.T) {
|
func TestRouter_AdminSubtreeNotShadowed(t *testing.T) {
|
||||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
pool := dbtest.Open(t)
|
||||||
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)
|
|
||||||
|
|
||||||
// Seed a test admin user + session so the request actually reaches the
|
// Seed a test admin user + session so the request actually reaches the
|
||||||
// inner route lookup (auth-middleware short-circuits don't catch shadowing).
|
// inner route lookup (auth-middleware short-circuits don't catch shadowing).
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -18,7 +17,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/scrobble/listenbrainz"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/scrobble/listenbrainz"
|
||||||
@@ -26,22 +24,7 @@ import (
|
|||||||
|
|
||||||
func testPool(t *testing.T) (*pgxpool.Pool, *dbq.Queries) {
|
func testPool(t *testing.T) (*pgxpool.Pool, *dbq.Queries) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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, dbq.New(pool)
|
return pool, dbq.New(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,8 @@ package subsonic
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,7 +12,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
@@ -37,22 +33,7 @@ type playHistoryFixture struct {
|
|||||||
// E: played many times by another user (in neither list)
|
// E: played many times by another user (in neither list)
|
||||||
func seedPlayHistory(t *testing.T) playHistoryFixture {
|
func seedPlayHistory(t *testing.T) playHistoryFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|||||||
@@ -3,16 +3,10 @@ package subsonic
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
@@ -20,22 +14,7 @@ import (
|
|||||||
// A compilation's songs are credited to their own performers, as Subsonic
|
// A compilation's songs are credited to their own performers, as Subsonic
|
||||||
// clients show them, not all to Various Artists.
|
// clients show them, not all to Various Artists.
|
||||||
func TestGetAlbum_EachSongCreditsItsOwnArtist(t *testing.T) {
|
func TestGetAlbum_EachSongCreditsItsOwnArtist(t *testing.T) {
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -16,7 +15,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||||
@@ -63,25 +61,11 @@ type picksFixture struct {
|
|||||||
|
|
||||||
func seedPicks(t *testing.T) picksFixture {
|
func seedPicks(t *testing.T) picksFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
var err error
|
||||||
f := picksFixture{pool: pool, tracks: map[string]dbq.Track{}, albums: map[string]dbq.Album{}}
|
f := picksFixture{pool: pool, tracks: map[string]dbq.Track{}, albums: map[string]dbq.Album{}}
|
||||||
if f.user, err = q.CreateUser(ctx, dbq.CreateUserParams{
|
if f.user, err = q.CreateUser(ctx, dbq.CreateUserParams{
|
||||||
Username: dbtest.TestUserPrefix + "listener", PasswordHash: "x", ApiTokenHash: "x",
|
Username: dbtest.TestUserPrefix + "listener", PasswordHash: "x", ApiTokenHash: "x",
|
||||||
|
|||||||
@@ -7,13 +7,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||||
@@ -21,22 +19,7 @@ import (
|
|||||||
|
|
||||||
func testScrobblePool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track) {
|
func testScrobblePool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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)
|
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
||||||
|
|||||||
@@ -8,13 +8,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
||||||
@@ -22,22 +20,7 @@ import (
|
|||||||
|
|
||||||
func testStarPool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track, dbq.Album, dbq.Artist) {
|
func testStarPool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track, dbq.Album, dbq.Artist) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if testing.Short() {
|
pool := dbtest.Pool(t)
|
||||||
t.Skip("skipping 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)
|
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
u, _ := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
u, _ := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
||||||
|
|||||||
@@ -2,43 +2,17 @@ package sync_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogChange_WritesRowInTx(t *testing.T) {
|
func TestLogChange_WritesRowInTx(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
tx, err := pool.Begin(ctx)
|
tx, err := pool.Begin(ctx)
|
||||||
@@ -59,7 +33,7 @@ func TestLogChange_WritesRowInTx(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLogChange_RollbackDoesNotPersist(t *testing.T) {
|
func TestLogChange_RollbackDoesNotPersist(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ import (
|
|||||||
// what a newer row supersedes, and deletes past their retention, raising the
|
// what a newer row supersedes, and deletes past their retention, raising the
|
||||||
// trimmed mark past the newest delete it dropped.
|
// trimmed mark past the newest delete it dropped.
|
||||||
func TestCompact_KeepsEachEntitysLatestChange_Integration(t *testing.T) {
|
func TestCompact_KeepsEachEntitysLatestChange_Integration(t *testing.T) {
|
||||||
pool := testPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
old := time.Now().Add(-90 * 24 * time.Hour)
|
old := time.Now().Add(-90 * 24 * time.Hour)
|
||||||
recent := time.Now().Add(-time.Hour)
|
recent := time.Now().Add(-time.Hour)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -16,7 +15,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
)
|
)
|
||||||
@@ -25,27 +23,6 @@ var seedCounter uint64
|
|||||||
|
|
||||||
func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||||
|
|
||||||
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, discardLogger()); 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 {
|
func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
@@ -142,7 +119,7 @@ func tagWeight(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, tag string)
|
|||||||
// positive weight; an early-skipped artist earns a negative one. Genre tags
|
// positive weight; an early-skipped artist earns a negative one. Genre tags
|
||||||
// follow the same sign.
|
// follow the same sign.
|
||||||
func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) {
|
func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
u := seedUser(t, pool, "tp1")
|
u := seedUser(t, pool, "tp1")
|
||||||
|
|
||||||
@@ -176,7 +153,7 @@ func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) {
|
|||||||
// completed plays must NOT make a loved artist net-negative (the user's
|
// completed plays must NOT make a loved artist net-negative (the user's
|
||||||
// concern that a track-dislike shouldn't tank the artist).
|
// concern that a track-dislike shouldn't tank the artist).
|
||||||
func TestBuildTasteProfile_AggregationProtectsArtist(t *testing.T) {
|
func TestBuildTasteProfile_AggregationProtectsArtist(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
u := seedUser(t, pool, "tp2")
|
u := seedUser(t, pool, "tp2")
|
||||||
fav := seedArtist(t, pool, "Fav")
|
fav := seedArtist(t, pool, "Fav")
|
||||||
@@ -202,7 +179,7 @@ func TestBuildTasteProfile_AggregationProtectsArtist(t *testing.T) {
|
|||||||
// TestBuildTasteProfile_ArtistLikeBonus: an explicitly-liked artist with no
|
// TestBuildTasteProfile_ArtistLikeBonus: an explicitly-liked artist with no
|
||||||
// plays still earns a positive weight from the like bonus.
|
// plays still earns a positive weight from the like bonus.
|
||||||
func TestBuildTasteProfile_ArtistLikeBonus(t *testing.T) {
|
func TestBuildTasteProfile_ArtistLikeBonus(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
u := seedUser(t, pool, "tp3")
|
u := seedUser(t, pool, "tp3")
|
||||||
liked := seedArtist(t, pool, "LikedOnly")
|
liked := seedArtist(t, pool, "LikedOnly")
|
||||||
if _, err := pool.Exec(context.Background(),
|
if _, err := pool.Exec(context.Background(),
|
||||||
@@ -222,7 +199,7 @@ func TestBuildTasteProfile_ArtistLikeBonus(t *testing.T) {
|
|||||||
|
|
||||||
// TestBuildTasteProfile_AtomicReplace: a second build replaces, not appends.
|
// TestBuildTasteProfile_AtomicReplace: a second build replaces, not appends.
|
||||||
func TestBuildTasteProfile_AtomicReplace(t *testing.T) {
|
func TestBuildTasteProfile_AtomicReplace(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
u := seedUser(t, pool, "tp4")
|
u := seedUser(t, pool, "tp4")
|
||||||
|
|||||||
@@ -3,16 +3,12 @@ package tracks
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/db/dbq"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||||
@@ -20,27 +16,6 @@ import (
|
|||||||
|
|
||||||
// --- test seed helpers (inlined; mirrors quarantine patterns) -----------
|
// --- test seed helpers (inlined; mirrors quarantine patterns) -----------
|
||||||
|
|
||||||
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 seedAdmin(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
func seedAdmin(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||||
@@ -108,7 +83,7 @@ func markMissing(t *testing.T, pool *pgxpool.Pool, ids ...pgtype.UUID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestForgetTracks_NotFound(t *testing.T) {
|
func TestForgetTracks_NotFound(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := seedAdmin(t, pool, "alice")
|
admin := seedAdmin(t, pool, "alice")
|
||||||
var bogus pgtype.UUID
|
var bogus pgtype.UUID
|
||||||
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
||||||
@@ -123,7 +98,7 @@ func TestForgetTracks_NotFound(t *testing.T) {
|
|||||||
// A track that has its file plays, and is not forgotten; nor is the waiting
|
// A track that has its file plays, and is not forgotten; nor is the waiting
|
||||||
// track asked about with it.
|
// track asked about with it.
|
||||||
func TestForgetTracks_RefusesATrackThatHasItsFile(t *testing.T) {
|
func TestForgetTracks_RefusesATrackThatHasItsFile(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := seedAdmin(t, pool, "alice")
|
admin := seedAdmin(t, pool, "alice")
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
_, _, present := seedArtistAlbumTrack(t, pool, "Present", "", filepath.Join(dir, "p.mp3"))
|
_, _, present := seedArtistAlbumTrack(t, pool, "Present", "", filepath.Join(dir, "p.mp3"))
|
||||||
@@ -145,7 +120,7 @@ func TestForgetTracks_RefusesATrackThatHasItsFile(t *testing.T) {
|
|||||||
// Forget deletes the waiting tracks, tidies away the album and artist they
|
// Forget deletes the waiting tracks, tidies away the album and artist they
|
||||||
// leave empty, and names each in the audit log.
|
// leave empty, and names each in the audit log.
|
||||||
func TestForgetTracks_DeletesWaitingTracksAndAudits(t *testing.T) {
|
func TestForgetTracks_DeletesWaitingTracksAndAudits(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := seedAdmin(t, pool, "alice")
|
admin := seedAdmin(t, pool, "alice")
|
||||||
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
if _, err := pool.Exec(context.Background(), `DELETE FROM audit_log`); err != nil {
|
||||||
t.Fatalf("reset audit_log: %v", err)
|
t.Fatalf("reset audit_log: %v", err)
|
||||||
@@ -185,7 +160,7 @@ func TestForgetTracks_DeletesWaitingTracksAndAudits(t *testing.T) {
|
|||||||
|
|
||||||
// An album that keeps another track is left alone.
|
// An album that keeps another track is left alone.
|
||||||
func TestForgetTracks_KeepsAnAlbumThatStillHasTracks(t *testing.T) {
|
func TestForgetTracks_KeepsAnAlbumThatStillHasTracks(t *testing.T) {
|
||||||
pool := newPool(t)
|
pool := dbtest.Pool(t)
|
||||||
admin := seedAdmin(t, pool, "alice")
|
admin := seedAdmin(t, pool, "alice")
|
||||||
q := dbq.New(pool)
|
q := dbq.New(pool)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|||||||
Reference in New Issue
Block a user