- A search key apart from the key that identifies artists, albums and tracks: lower case, accents taken off (Latin, Greek and Cyrillic marks only, so kana and Hangul stay intact), and letters like ß, æ, ø, ł written plain. - GET /artists, /albums and /tracks look in it, so "bjork" finds "Björk" and "strasse" finds "Straße". - "Beyoncé" and "Beyonce" stay two artists: the identity key keeps accents. - Migration 0016 adds the column on SQLite and PostgreSQL; RefreshMatchKeys fills it for existing rows at start. - golang.org/x/text is now a direct dependency.
554 lines
20 KiB
Go
554 lines
20 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/catalog"
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
)
|
|
|
|
// seeded describes the library built by seedCatalog.
|
|
type seeded struct {
|
|
rock, jazz int64 // library ids
|
|
track map[string]int64 // track id by title
|
|
repo *CatalogRepo
|
|
}
|
|
|
|
func pf(rel, title, album, albumArtist string, trackNo, discNo int, format string, artists ...string) library.ParsedFile {
|
|
return library.ParsedFile{
|
|
FileInfo: library.FileInfo{RelPath: rel, Size: 100, MTimeUnix: 1},
|
|
Metadata: library.Metadata{
|
|
Tags: library.Tags{
|
|
Title: title, Album: album, AlbumArtist: albumArtist,
|
|
Artists: artists, TrackNo: trackNo, DiscNo: discNo,
|
|
},
|
|
Properties: library.Properties{Format: format, DurationMs: 60000, SampleRate: 44100},
|
|
},
|
|
}
|
|
}
|
|
|
|
// seedCatalog builds two libraries:
|
|
//
|
|
// rock: Radiohead - OK Computer (Airbag, Paranoid Android, Karma Police, and
|
|
// Ghost whose only file is missing), Björk - Post and a loose track,
|
|
// "100% Pure", "Under_score", "Ünïcode Trck", and an album of only
|
|
// missing files.
|
|
// jazz: Miles Davis - Kind of Blue (tracks listed out of order), a
|
|
// compilation "Various Hits" credited to two artists, and a second
|
|
// encoding of Airbag.
|
|
func seedCatalog(t *testing.T, db *bun.DB) seeded {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
lib := NewLibraryRepo(db)
|
|
s := seeded{track: map[string]int64{}, repo: NewCatalogRepo(db)}
|
|
|
|
rock, err := lib.CreateLibrary(ctx, "rock", "/lib/rock")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jazz, err := lib.CreateLibrary(ctx, "jazz", "/lib/jazz")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.rock, s.jazz = rock.ID, jazz.ID
|
|
|
|
save := func(libID int64, f library.ParsedFile) {
|
|
t.Helper()
|
|
if _, err := lib.SaveFile(ctx, libID, f, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
save(s.rock, pf("r/1.flac", "Airbag", "OK Computer", "Radiohead", 1, 1, "flac", "Radiohead"))
|
|
save(s.rock, pf("r/2.flac", "Paranoid Android", "OK Computer", "Radiohead", 2, 1, "flac", "Radiohead"))
|
|
save(s.rock, pf("r/3.flac", "Karma Police", "OK Computer", "Radiohead", 3, 1, "flac", "Radiohead"))
|
|
save(s.rock, pf("r/4.flac", "Ghost", "OK Computer", "Radiohead", 4, 1, "flac", "Radiohead"))
|
|
save(s.rock, pf("b/1.flac", "Army of Me", "Post", "Björk", 1, 1, "flac", "Björk"))
|
|
save(s.rock, pf("b/2.flac", "Loose Song", "", "", 0, 0, "flac", "Björk"))
|
|
save(s.rock, pf("c/1.flac", "100% Pure", "", "", 0, 0, "flac", "Wild Cards"))
|
|
save(s.rock, pf("c/2.flac", "Under_score", "", "", 0, 0, "flac", "Wild Cards"))
|
|
save(s.rock, pf("c/3.flac", "Ünïcode Trck", "", "", 0, 0, "flac", "Ünïcode Band"))
|
|
save(s.rock, pf("h/1.flac", "Hidden Song", "Hidden Album", "Only Missing", 1, 1, "flac", "Only Missing"))
|
|
|
|
save(s.jazz, pf("m/3.mp3", "Blue in Green", "Kind of Blue", "Miles Davis", 3, 1, "mpeg", "Miles Davis"))
|
|
save(s.jazz, pf("m/1.mp3", "So What", "Kind of Blue", "Miles Davis", 1, 1, "mpeg", "Miles Davis"))
|
|
save(s.jazz, pf("m/2.mp3", "Freddie Freeloader", "Kind of Blue", "Miles Davis", 2, 1, "mpeg", "Miles Davis"))
|
|
save(s.jazz, pf("v/1.mp3", "Song A", "Various Hits", "Various Artists", 1, 1, "mpeg", "Artist A"))
|
|
save(s.jazz, pf("v/2.mp3", "Song B", "Various Hits", "Various Artists", 2, 1, "mpeg", "Artist B"))
|
|
|
|
rows, err := db.Query("SELECT id, title FROM tracks")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for rows.Next() {
|
|
var id int64
|
|
var title string
|
|
if err := rows.Scan(&id, &title); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.track[title] = id
|
|
}
|
|
_ = rows.Close()
|
|
|
|
// A second encoding of Airbag in the jazz library.
|
|
if _, err := db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
|
|
VALUES (?, ?, 'airbag.mp3', 'mpeg', 1, 1)`, s.track["Airbag"], s.jazz); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Files that are gone: Ghost and the whole hidden album.
|
|
if _, err := db.Exec(`UPDATE audio_files SET missing_at = ? WHERE track_id IN (?, ?)`,
|
|
time.Now().Unix(), s.track["Ghost"], s.track["Hidden Song"]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func artistNames(p catalog.Page[catalog.Artist]) []string {
|
|
var out []string
|
|
for _, a := range p.Items {
|
|
out = append(out, a.Name)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func trackTitles(p catalog.Page[catalog.Track]) []string {
|
|
var out []string
|
|
for _, tr := range p.Items {
|
|
out = append(out, tr.Title)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func albumTitles(p catalog.Page[catalog.Album]) []string {
|
|
var out []string
|
|
for _, a := range p.Items {
|
|
out = append(out, a.Title)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func equal(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func page(limit int) catalog.Filter { return catalog.Filter{Limit: limit} }
|
|
|
|
// ---- artists ----
|
|
|
|
func TestCatalogArtists(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
got, err := s.repo.ListArtists(context.Background(), page(50))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Sorted by name in plain byte order, the same on both databases (so
|
|
// the accented name comes last). "Only Missing" has nothing playable;
|
|
// "Various Artists" is only an album artist but its album is playable.
|
|
want := []string{"Artist A", "Artist B", "Björk", "Miles Davis", "Radiohead", "Various Artists", "Wild Cards", "Ünïcode Band"}
|
|
if !equal(artistNames(got), want) || got.Total != len(want) {
|
|
t.Fatalf("artists = %v (total %d), want %v", artistNames(got), got.Total, want)
|
|
}
|
|
|
|
byName := map[string]catalog.Artist{}
|
|
for _, a := range got.Items {
|
|
byName[a.Name] = a
|
|
}
|
|
if r := byName["Radiohead"]; r.TrackCount != 3 || r.AlbumCount != 1 {
|
|
t.Errorf("Radiohead = %+v, want 3 tracks (Ghost is missing) and 1 album", r)
|
|
}
|
|
if v := byName["Various Artists"]; v.TrackCount != 0 || v.AlbumCount != 1 {
|
|
t.Errorf("Various Artists = %+v, want 0 credited tracks and 1 album", v)
|
|
}
|
|
if b := byName["Björk"]; b.TrackCount != 2 || b.AlbumCount != 1 {
|
|
t.Errorf("Björk = %+v, want 2 tracks (one without album) and 1 album", b)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCatalogPaging(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
first, _ := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3})
|
|
second, _ := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3, Offset: 3})
|
|
if len(first.Items) != 3 || len(second.Items) != 3 || first.Total != 8 || second.Total != 8 {
|
|
t.Fatalf("pages: %d and %d items, totals %d/%d", len(first.Items), len(second.Items), first.Total, second.Total)
|
|
}
|
|
if first.Items[2].ID == second.Items[0].ID {
|
|
t.Error("pages overlap")
|
|
}
|
|
if first.Limit != 3 || second.Offset != 3 {
|
|
t.Errorf("limit/offset not echoed: %d %d", first.Limit, second.Offset)
|
|
}
|
|
|
|
past, err := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3, Offset: 100})
|
|
if err != nil || past.Items == nil || len(past.Items) != 0 || past.Total != 8 {
|
|
t.Errorf("past the end: %+v err=%v (want an empty, non-nil slice and the real total)", past, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCatalogSearch(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
search := func(q string) catalog.Filter { return catalog.Filter{Query: q, Limit: 50} }
|
|
|
|
// Case folding, accented letters included, is the same on both databases.
|
|
for _, q := range []string{"björk", "BJÖRK", "jö"} {
|
|
if got, _ := s.repo.ListArtists(ctx, search(q)); !equal(artistNames(got), []string{"Björk"}) {
|
|
t.Errorf("artists %q = %v", q, artistNames(got))
|
|
}
|
|
}
|
|
if got, _ := s.repo.ListArtists(ctx, search("ÜNÏ")); !equal(artistNames(got), []string{"Ünïcode Band"}) {
|
|
t.Errorf("artists ÜNÏ = %v", artistNames(got))
|
|
}
|
|
if got, _ := s.repo.ListTracks(ctx, search("ÜNÏCODE")); !equal(trackTitles(got), []string{"Ünïcode Trck"}) {
|
|
t.Errorf("tracks ÜNÏCODE = %v", trackTitles(got))
|
|
}
|
|
if got, _ := s.repo.ListAlbums(ctx, search("kind OF")); !equal(albumTitles(got), []string{"Kind of Blue"}) {
|
|
t.Errorf("albums kind OF = %v", albumTitles(got))
|
|
}
|
|
if got, _ := s.repo.ListArtists(ctx, search("zzz")); got.Total != 0 || len(got.Items) != 0 {
|
|
t.Errorf("no match: %+v", got)
|
|
}
|
|
|
|
// LIKE wildcards in the query are plain characters.
|
|
if got, _ := s.repo.ListTracks(ctx, search("100%")); !equal(trackTitles(got), []string{"100% Pure"}) {
|
|
t.Errorf(`"100%%" = %v, want only the title containing a percent sign`, trackTitles(got))
|
|
}
|
|
if got, _ := s.repo.ListTracks(ctx, search("%")); !equal(trackTitles(got), []string{"100% Pure"}) {
|
|
t.Errorf(`"%%" = %v, must not match everything`, trackTitles(got))
|
|
}
|
|
if got, _ := s.repo.ListTracks(ctx, search("_")); !equal(trackTitles(got), []string{"Under_score"}) {
|
|
t.Errorf(`"_" = %v, must not match every one-character position`, trackTitles(got))
|
|
}
|
|
if got, _ := s.repo.ListTracks(ctx, search("!")); got.Total != 0 {
|
|
t.Errorf(`"!" (the escape character) = %v`, trackTitles(got))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCatalogLibraryFilter(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
jazz, _ := s.repo.ListArtists(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
|
|
// Radiohead is here only because Airbag also has a file in this library.
|
|
want := []string{"Artist A", "Artist B", "Miles Davis", "Radiohead", "Various Artists"}
|
|
if !equal(artistNames(jazz), want) {
|
|
t.Fatalf("jazz artists = %v, want %v", artistNames(jazz), want)
|
|
}
|
|
for _, a := range jazz.Items {
|
|
if a.Name == "Radiohead" && (a.TrackCount != 1 || a.AlbumCount != 1) {
|
|
t.Errorf("counts must follow the filter: Radiohead = %+v, want 1 track", a)
|
|
}
|
|
}
|
|
|
|
tracks, _ := s.repo.ListTracks(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
|
|
if tracks.Total != 6 { // 3 + 2 + Airbag
|
|
t.Errorf("jazz tracks = %d, want 6: %v", tracks.Total, trackTitles(tracks))
|
|
}
|
|
albums, _ := s.repo.ListAlbums(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
|
|
if !equal(albumTitles(albums), []string{"Kind of Blue", "OK Computer", "Various Hits"}) {
|
|
t.Errorf("jazz albums = %v", albumTitles(albums))
|
|
}
|
|
for _, a := range albums.Items {
|
|
if a.Title == "OK Computer" && a.TrackCount != 1 {
|
|
t.Errorf("OK Computer in jazz = %+v, want only Airbag", a)
|
|
}
|
|
}
|
|
|
|
if none, _ := s.repo.ListTracks(ctx, catalog.Filter{LibraryID: 999, Limit: 50}); none.Total != 0 {
|
|
t.Errorf("unknown library: %d tracks", none.Total)
|
|
}
|
|
})
|
|
}
|
|
|
|
// ---- albums ----
|
|
|
|
func TestCatalogAlbums(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
got, err := s.repo.ListAlbums(ctx, page(50))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// "Hidden Album" has only missing files.
|
|
if want := []string{"Kind of Blue", "OK Computer", "Post", "Various Hits"}; !equal(albumTitles(got), want) || got.Total != 4 {
|
|
t.Fatalf("albums = %v (total %d), want %v", albumTitles(got), got.Total, want)
|
|
}
|
|
|
|
by := map[string]catalog.Album{}
|
|
for _, a := range got.Items {
|
|
by[a.Title] = a
|
|
}
|
|
ok := by["OK Computer"]
|
|
if ok.TrackCount != 3 || ok.DurationMs != 180000 || ok.Artist == nil || ok.Artist.Name != "Radiohead" {
|
|
t.Errorf("OK Computer = %+v, want 3 tracks, 3 minutes, Radiohead", ok)
|
|
}
|
|
if v := by["Various Hits"]; v.Artist == nil || v.Artist.Name != "Various Artists" || v.TrackCount != 2 {
|
|
t.Errorf("Various Hits = %+v", v)
|
|
}
|
|
|
|
byArtist, _ := s.repo.ListAlbums(ctx, catalog.Filter{ArtistID: ok.Artist.ID, Limit: 50})
|
|
if !equal(albumTitles(byArtist), []string{"OK Computer"}) {
|
|
t.Errorf("albums of Radiohead = %v", albumTitles(byArtist))
|
|
}
|
|
})
|
|
}
|
|
|
|
// ---- tracks ----
|
|
|
|
func TestCatalogTracks(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
all, err := s.repo.ListTracks(ctx, page(50))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
titles := trackTitles(all)
|
|
count := map[string]int{}
|
|
for _, ti := range titles {
|
|
count[ti]++
|
|
}
|
|
if count["Airbag"] != 1 {
|
|
t.Errorf("Airbag appears %d times: a track with two files must be listed once", count["Airbag"])
|
|
}
|
|
if count["Ghost"] != 0 || count["Hidden Song"] != 0 {
|
|
t.Errorf("tracks without a playable file were listed: %v", titles)
|
|
}
|
|
if all.Total != 13 || len(all.Items) != 13 {
|
|
t.Errorf("total = %d, items = %d, want 13: %v", all.Total, len(all.Items), titles)
|
|
}
|
|
|
|
// Albums in title order; inside an album by disc, track number, title.
|
|
var kind []string
|
|
for _, tr := range all.Items {
|
|
if tr.Album != nil && tr.Album.Title == "Kind of Blue" {
|
|
kind = append(kind, tr.Title)
|
|
}
|
|
}
|
|
if !equal(kind, []string{"So What", "Freddie Freeloader", "Blue in Green"}) {
|
|
t.Errorf("Kind of Blue order = %v", kind)
|
|
}
|
|
|
|
by := map[string]catalog.Track{}
|
|
for _, tr := range all.Items {
|
|
by[tr.Title] = tr
|
|
}
|
|
airbag := by["Airbag"]
|
|
if airbag.Album == nil || airbag.Album.Title != "OK Computer" || len(airbag.Artists) != 1 ||
|
|
airbag.Artists[0].Name != "Radiohead" || airbag.TrackNo != 1 || airbag.DiscNo != 1 || airbag.DurationMs != 60000 {
|
|
t.Errorf("Airbag = %+v", airbag)
|
|
}
|
|
if airbag.Format != "flac" {
|
|
t.Errorf("Airbag format = %q, want the first playable file's (flac)", airbag.Format)
|
|
}
|
|
if f := by["So What"].Format; f != "mp3" {
|
|
t.Errorf("format = %q, want mpeg shown as mp3", f)
|
|
}
|
|
if loose := by["Loose Song"]; loose.Album != nil || loose.TrackNo != 0 || len(loose.Artists) != 1 {
|
|
t.Errorf("a track without album: %+v", loose)
|
|
}
|
|
if a := by["Song A"].Artists; len(a) != 1 || a[0].Name != "Artist A" {
|
|
t.Errorf("compilation track credits = %+v", a)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCatalogTrackFilters(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
albums, _ := s.repo.ListAlbums(ctx, catalog.Filter{Query: "ok comp", Limit: 5})
|
|
ok := albums.Items[0]
|
|
byAlbum, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: ok.ID, Limit: 50})
|
|
if !equal(trackTitles(byAlbum), []string{"Airbag", "Paranoid Android", "Karma Police"}) {
|
|
t.Errorf("tracks of OK Computer = %v", trackTitles(byAlbum))
|
|
}
|
|
|
|
artists, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "artist a", Limit: 5})
|
|
byArtist, _ := s.repo.ListTracks(ctx, catalog.Filter{ArtistID: artists.Items[0].ID, Limit: 50})
|
|
if !equal(trackTitles(byArtist), []string{"Song A"}) {
|
|
t.Errorf("tracks of Artist A = %v", trackTitles(byArtist))
|
|
}
|
|
|
|
both, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: ok.ID, Query: "karma", Limit: 50})
|
|
if !equal(trackTitles(both), []string{"Karma Police"}) {
|
|
t.Errorf("filters combine with AND: %v", trackTitles(both))
|
|
}
|
|
if none, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: 999, Limit: 50}); none.Total != 0 {
|
|
t.Errorf("unknown album: %d tracks", none.Total)
|
|
}
|
|
})
|
|
}
|
|
|
|
// ---- details ----
|
|
|
|
func TestCatalogDetails(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
tr, err := s.repo.GetTrack(ctx, s.track["Airbag"])
|
|
if err != nil || tr.Title != "Airbag" || tr.Album == nil || len(tr.Artists) != 1 || tr.Format != "flac" {
|
|
t.Fatalf("GetTrack = %+v err=%v", tr, err)
|
|
}
|
|
if _, err := s.repo.GetTrack(ctx, s.track["Ghost"]); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("a track with only missing files: err = %v, want ErrNotFound", err)
|
|
}
|
|
if _, err := s.repo.GetTrack(ctx, 99999); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("unknown track: err = %v", err)
|
|
}
|
|
|
|
artists, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "various", Limit: 5})
|
|
va, err := s.repo.GetArtist(ctx, artists.Items[0].ID)
|
|
if err != nil || va.Name != "Various Artists" || va.AlbumCount != 1 {
|
|
t.Errorf("an album artist without credits: %+v err=%v", va, err)
|
|
}
|
|
var hiddenArtist int64
|
|
if err := db.QueryRow("SELECT id FROM artists WHERE name = 'Only Missing'").Scan(&hiddenArtist); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.repo.GetArtist(ctx, hiddenArtist); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("an artist with nothing playable: err = %v, want ErrNotFound", err)
|
|
}
|
|
|
|
var hiddenAlbum, okAlbum int64
|
|
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'Hidden Album'").Scan(&hiddenAlbum)
|
|
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&okAlbum)
|
|
if _, err := s.repo.GetAlbum(ctx, hiddenAlbum); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("an album with nothing playable: err = %v", err)
|
|
}
|
|
al, err := s.repo.GetAlbum(ctx, okAlbum)
|
|
if err != nil || al.TrackCount != 3 || al.Artist == nil {
|
|
t.Errorf("GetAlbum = %+v err=%v", al, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A page must cost a fixed number of queries, never one per row.
|
|
func TestCatalogListsDoNotQueryPerRow(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
var queries atomic.Int64
|
|
db.AddQueryHook(countingHook{&queries})
|
|
|
|
for name, run := range map[string]func() error{
|
|
"artists": func() error { _, err := s.repo.ListArtists(context.Background(), page(50)); return err },
|
|
"albums": func() error { _, err := s.repo.ListAlbums(context.Background(), page(50)); return err },
|
|
"tracks": func() error { _, err := s.repo.ListTracks(context.Background(), page(50)); return err },
|
|
} {
|
|
queries.Store(0)
|
|
if err := run(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := queries.Load(); n > 6 {
|
|
t.Errorf("%s: %d queries for one page, want a small constant", name, n)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
type countingHook struct{ n *atomic.Int64 }
|
|
|
|
func (countingHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.Context { return ctx }
|
|
func (h countingHook) AfterQuery(context.Context, *bun.QueryEvent) { h.n.Add(1) }
|
|
|
|
// Two albums can share a title ("Greatest Hits", "Live"). Their tracks must not
|
|
// interleave in a listing.
|
|
func TestCatalogKeepsAlbumsWithTheSameTitleApart(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
lib := NewLibraryRepo(db)
|
|
l, err := lib.CreateLibrary(ctx, "m", "/lib/m")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, f := range []library.ParsedFile{
|
|
pf("a1.flac", "A one", "Greatest Hits", "Artist A", 1, 1, "flac", "Artist A"),
|
|
pf("b1.flac", "B one", "Greatest Hits", "Artist B", 1, 1, "flac", "Artist B"),
|
|
pf("a2.flac", "A two", "Greatest Hits", "Artist A", 2, 1, "flac", "Artist A"),
|
|
pf("b2.flac", "B two", "Greatest Hits", "Artist B", 2, 1, "flac", "Artist B"),
|
|
} {
|
|
if _, err := lib.SaveFile(ctx, l.ID, f, 0); err != nil {
|
|
t.Fatalf("file %d: %v", i, err)
|
|
}
|
|
}
|
|
repo := NewCatalogRepo(db)
|
|
|
|
all, err := repo.ListTracks(ctx, page(50))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := trackTitles(all)
|
|
a := []string{"A one", "A two", "B one", "B two"}
|
|
b := []string{"B one", "B two", "A one", "A two"}
|
|
if !equal(got, a) && !equal(got, b) {
|
|
t.Fatalf("tracks = %v, want each album's tracks together", got)
|
|
}
|
|
|
|
// The same holds across a page boundary.
|
|
first, _ := repo.ListTracks(ctx, catalog.Filter{Limit: 2})
|
|
second, _ := repo.ListTracks(ctx, catalog.Filter{Limit: 2, Offset: 2})
|
|
if got := append(trackTitles(first), trackTitles(second)...); !equal(got, a) && !equal(got, b) {
|
|
t.Errorf("paged tracks = %v", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
// The album count of an artist and the albums listed for that artist must be
|
|
// the same set, otherwise "3 albums" leads to an empty or shorter list.
|
|
func TestCatalogArtistAlbumCountMatchesTheirAlbumList(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
|
|
for _, libraryID := range []int64{0, s.rock, s.jazz} {
|
|
artists, err := s.repo.ListArtists(ctx, catalog.Filter{LibraryID: libraryID, Limit: 50})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, a := range artists.Items {
|
|
albums, err := s.repo.ListAlbums(ctx, catalog.Filter{ArtistID: a.ID, LibraryID: libraryID, Limit: 50})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if albums.Total != a.AlbumCount {
|
|
t.Errorf("library %d, %s: album_count = %d but /albums?artist_id lists %d (%v)",
|
|
libraryID, a.Name, a.AlbumCount, albums.Total, albumTitles(albums))
|
|
}
|
|
}
|
|
}
|
|
|
|
// The compilation is reachable from a credited artist, not only from the album artist.
|
|
artistA, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "artist a", Limit: 5})
|
|
albums, _ := s.repo.ListAlbums(ctx, catalog.Filter{ArtistID: artistA.Items[0].ID, Limit: 50})
|
|
if !equal(albumTitles(albums), []string{"Various Hits"}) {
|
|
t.Errorf("albums of a credited artist = %v", albumTitles(albums))
|
|
}
|
|
})
|
|
}
|