sonda/internal/storage/discovery_test.go
tgiachi b0a7a562a6 fix(discovery): purge lock, cleanup chains and rescans that cannot read
- fix: on PostgreSQL a purge could delete a discovered artist while a scan was
  adopting it. The scan then failed on the missing artist for the whole file,
  and the two could also deadlock. Purge now takes the same artist lock as the
  scanner and discovery before it removes anything. Saving a list or tags does
  too, and an artist that is gone by then is reported as not found instead of a
  foreign key error, so a user who opened an artist that was removed meanwhile
  gets a 404
- fix: a discovered artist that was only the source of a list stayed forever and
  kept the artists of its own list alive, so chains built up. Cleanup now removes
  such an artist and then follows the chain until nothing more goes
- fix: in a full rescan a file that came back but could not be read stayed
  marked missing. It is restored first, as in a normal scan, and read as well
- fix: ?full given twice is refused, so a typo cannot start a scan of another
  kind
- fix: genre cleaning lives in one place, used by the reader and the store
- docs: the files genres count the tracks the artist is credited on, and the spec
  says every files tag is a genre
- test: a purge colliding with a scan on both databases (it fails without the
  lock), a chain of discovered artists, saving for an artist that is gone, a
  file that returns unreadable during a full scan
2026-11-11 17:36:14 +01:00

1030 lines
41 KiB
Go

package storage
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
"codeberg.org/squidd/sonda/internal/catalog"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/library"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/providers/lastfm"
)
var at = time.Unix(1_700_000_000, 0)
func artistID(t *testing.T, db *bun.DB, name string) int64 {
t.Helper()
var id int64
if err := db.QueryRow("SELECT id FROM artists WHERE name = ?", name).Scan(&id); err != nil {
t.Fatalf("artist %q: %v", name, err)
}
return id
}
func similarNames(s discovery.Snapshot) []string {
var out []string
for _, it := range s.Items {
out = append(out, it.Artist.Name)
}
return out
}
func TestDiscoveryArtistLookup(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
a, err := repo.Artist(ctx, radiohead)
if err != nil || a.Name != "Radiohead" || a.MBID != "" {
t.Fatalf("Artist = %+v err=%v", a, err)
}
if _, err := db.Exec("UPDATE artists SET mbid = ? WHERE id = ?", "a74b1b7f-71a5-4011-9441-d0b5e4122711", radiohead); err != nil {
t.Fatal(err)
}
if a, _ = repo.Artist(ctx, radiohead); a.MBID != "a74b1b7f-71a5-4011-9441-d0b5e4122711" {
t.Errorf("mbid = %q", a.MBID)
}
// Known only by name: nothing playable, nothing discovered.
if _, err := repo.Artist(ctx, artistID(t, db, "Only Missing")); !errors.Is(err, discovery.ErrNotFound) {
t.Errorf("an artist with nothing playable: err = %v", err)
}
if _, err := repo.Artist(ctx, 99999); !errors.Is(err, discovery.ErrNotFound) {
t.Errorf("unknown artist: err = %v", err)
}
})
}
func TestDiscoverySaveAndReadSimilar(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
snap, err := repo.Similar(ctx, radiohead, "lastfm", 10)
if err != nil || snap.Fetched || len(snap.Items) != 0 {
t.Fatalf("never asked: %+v err=%v", snap, err)
}
before := count(t, db, "SELECT COUNT(*) FROM artists")
cands := []lastfm.Similar{
{Name: "Muse", MBID: "9C9F1380-2516-4FC9-A3E6-F9F61941D090", Match: 1},
{Name: " björk ", MBID: "", Match: 0.8}, // a local artist, by name
{Name: "Elbow", Match: 0.6}, // unknown, no id
{Name: "Miles Davis", MBID: "feedface-0000-0000-0000-000000000001", Match: 0.4}, // a local name with an id of its own
}
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, cands); err != nil {
t.Fatal(err)
}
snap, err = repo.Similar(ctx, radiohead, "lastfm", 10)
if err != nil {
t.Fatal(err)
}
if !snap.Fetched || snap.NotFound || !snap.RetrievedAt.Equal(at) {
t.Fatalf("snapshot = %+v", snap)
}
// The local namesake that has no id is the same artist; Miles Davis with an
// id the local one lacks is also linked to it (an unknown id never says
// "different"), so only Muse and Elbow are new.
if got := similarNames(snap); !equal(got, []string{"Muse", "Björk", "Elbow", "Miles Davis"}) {
t.Fatalf("items = %v", got)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists") - before; n != 2 {
t.Errorf("%d artists were created, want 2 (Muse and Elbow)", n)
}
for i, want := range []struct {
rank int
score float64
inLibrary bool
}{{1, 1, false}, {2, 0.8, true}, {3, 0.6, false}, {4, 0.4, true}} {
it := snap.Items[i]
if it.Rank != want.rank || it.Score != want.score || it.InLibrary != want.inLibrary {
t.Errorf("item %d = %+v, want %+v", i, it, want)
}
}
// The ids are kept lower-case, and the local artists were not given one.
var mbid string
if err := db.QueryRow("SELECT mbid FROM artists WHERE name = 'Muse'").Scan(&mbid); err != nil || mbid != "9c9f1380-2516-4fc9-a3e6-f9f61941d090" {
t.Errorf("Muse mbid = %q err=%v", mbid, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name IN ('Björk', 'Miles Davis') AND mbid IS NOT NULL"); n != 0 {
t.Error("an id from the provider was written onto a local artist")
}
// The page size limits what is read back.
two, _ := repo.Similar(ctx, radiohead, "lastfm", 2)
if got := similarNames(two); !equal(got, []string{"Muse", "Björk"}) {
t.Errorf("limited items = %v", got)
}
})
}
// Two artists can share a name. When both sides have an id and the ids differ,
// they are different people.
func TestDiscoveryKeepsNamesakesWithDifferentIdsApart(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
bjork := artistID(t, db, "Björk")
if _, err := db.Exec("UPDATE artists SET mbid = ? WHERE id = ?", "aaaaaaaa-0000-0000-0000-00000000000b", bjork); err != nil {
t.Fatal(err)
}
cands := []lastfm.Similar{
{Name: "Björk", MBID: "AAAAAAAA-0000-0000-0000-00000000000B", Match: 1}, // the same id, upper case
{Name: "Björk", MBID: "bbbbbbbb-0000-0000-0000-00000000000b", Match: 0.5}, // a namesake
}
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, cands); err != nil {
t.Fatal(err)
}
snap, _ := repo.Similar(ctx, radiohead, "lastfm", 10)
if len(snap.Items) != 2 || snap.Items[0].Artist.ID != bjork || !snap.Items[0].InLibrary {
t.Fatalf("items = %+v", snap.Items)
}
if snap.Items[1].Artist.ID == bjork || snap.Items[1].InLibrary {
t.Errorf("the namesake was taken for the local artist: %+v", snap.Items[1])
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'Björk'"); n != 2 {
t.Errorf("artists named Björk = %d, want 2", n)
}
})
}
func TestDiscoveryResolvesTheSameArtistOnlyOnce(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
cands := []lastfm.Similar{
{Name: "Elbow", Match: 1},
{Name: "elbow", Match: 0.9}, // the same artist spelled differently
{Name: "Radiohead", Match: 0.8}, // the artist itself
{Name: "Muse", Match: 0.7},
}
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, cands); err != nil {
t.Fatal(err)
}
snap, _ := repo.Similar(ctx, radiohead, "lastfm", 10)
if got := similarNames(snap); !equal(got, []string{"Elbow", "Muse"}) {
t.Errorf("items = %v", got)
}
// Ranks stay consecutive.
if snap.Items[0].Rank != 1 || snap.Items[1].Rank != 2 {
t.Errorf("ranks = %d, %d", snap.Items[0].Rank, snap.Items[1].Rank)
}
})
}
func TestDiscoverySaveReplacesWhatWasThere(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Muse", Match: 1}, {Name: "Elbow", Match: 0.5}})
later := at.Add(8 * 24 * time.Hour)
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", later, true, []lastfm.Similar{{Name: "Elbow", Match: 1}}); err != nil {
t.Fatal(err)
}
snap, _ := repo.Similar(ctx, radiohead, "lastfm", 10)
if got := similarNames(snap); !equal(got, []string{"Elbow"}) || snap.Items[0].Score != 1 || !snap.RetrievedAt.Equal(later) {
t.Errorf("snapshot = %+v", snap)
}
if n := count(t, db, "SELECT COUNT(*) FROM provider_fetches"); n != 1 {
t.Errorf("ledger rows = %d, want 1", n)
}
// "Not found" clears the items and says so.
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", later, false, nil); err != nil {
t.Fatal(err)
}
snap, _ = repo.Similar(ctx, radiohead, "lastfm", 10)
if !snap.Fetched || !snap.NotFound || len(snap.Items) != 0 {
t.Errorf("not found: %+v", snap)
}
// Another provider's data is not touched.
_ = repo.SaveSimilar(ctx, radiohead, "other", at, true, []lastfm.Similar{{Name: "Muse", Match: 1}})
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", later, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
if other, _ := repo.Similar(ctx, radiohead, "other", 10); len(other.Items) != 1 {
t.Errorf("another provider's items = %+v", other.Items)
}
})
}
func TestDiscoveredArtistsAreKnownButNotListed(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}}); err != nil {
t.Fatal(err)
}
elbow := artistID(t, db, "Elbow")
// A discovered artist can be shown and asked about...
got, err := s.repo.GetArtist(ctx, elbow)
if err != nil || got.Name != "Elbow" || got.TrackCount != 0 || got.AlbumCount != 0 {
t.Errorf("GetArtist = %+v err=%v", got, err)
}
if a, err := repo.Artist(ctx, elbow); err != nil || a.Name != "Elbow" {
t.Errorf("Artist = %+v err=%v", a, err)
}
// ...but the lists of the library stay local.
p, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "elbow", Limit: 10})
if p.Total != 0 {
t.Errorf("a discovered artist is listed: %v", artistNames(p))
}
all, _ := s.repo.ListArtists(ctx, catalog.Filter{Limit: 100})
for _, n := range artistNames(all) {
if n == "Elbow" {
t.Error("a discovered artist is in the artist list")
}
}
// An artist with nothing playable that nobody discovered is still not found.
if _, err := s.repo.GetArtist(ctx, artistID(t, db, "Only Missing")); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("err = %v, want ErrNotFound", err)
}
})
}
// When music that was only known from a provider arrives in a library, the scan
// finds the artist that is already there instead of making a second one.
func TestAScannedFileAdoptsADiscoveredArtist(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", MBID: "ebebebeb-0000-0000-0000-000000000001", Match: 1}})
elbow := artistID(t, db, "Elbow")
lib := NewLibraryRepo(db)
if _, err := lib.SaveFile(ctx, s.rock, pf("e/1.flac", "Grounds for Divorce", "", "", 0, 0, "flac", "ELBOW"), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = 'elbow'"); n != 1 {
t.Fatalf("artists named elbow = %d, want 1", n)
}
snap, _ := repo.Similar(ctx, radiohead, "lastfm", 10)
if len(snap.Items) != 1 || snap.Items[0].Artist.ID != elbow || !snap.Items[0].InLibrary {
t.Errorf("items = %+v", snap.Items)
}
if got, err := s.repo.GetArtist(ctx, elbow); err != nil || got.TrackCount != 1 {
t.Errorf("GetArtist = %+v err=%v", got, err)
}
// The artist is now the user's: it carries the name from their tags, and
// nothing a provider said about it (nobody checked that id).
a, err := repo.Artist(ctx, elbow)
if err != nil || a.Name != "ELBOW" || a.MBID != "" {
t.Errorf("after the scan the artist is %+v err=%v, want name ELBOW and no id", a, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+fmt.Sprint(elbow)+" AND mbid IS NOT NULL"); n != 0 {
t.Error("the provider's id stayed on the artist")
}
// A second scan of the same artist changes nothing more.
if _, err := lib.SaveFile(ctx, s.rock, pf("e/2.flac", "Another", "", "", 0, 0, "flac", "elbow"), 0); err != nil {
t.Fatal(err)
}
if a, _ = repo.Artist(ctx, elbow); a.Name != "ELBOW" {
t.Errorf("name = %q: only the first scan renames a discovered artist", a.Name)
}
})
}
func TestDeletingAnArtistRemovesWhatWasKnownAboutIt(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
if _, err := db.Exec("DELETE FROM artists WHERE id = ?", artistID(t, db, "Elbow")); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artist_similar"); n != 0 {
t.Errorf("edges left = %d", n)
}
})
}
// Several people opening artists at the same time, and a scan running meanwhile,
// must not make each other fail, and must not leave two rows for one artist.
func TestConcurrentSavesDoNotFailOrDuplicate(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
lib := NewLibraryRepo(db)
sources := []int64{artistID(t, db, "Radiohead"), artistID(t, db, "Björk"), artistID(t, db, "Miles Davis"), artistID(t, db, "Wild Cards")}
const rounds = 15
errs := make(chan error, 1000)
for round := 0; round < rounds; round++ {
var wg sync.WaitGroup // one round at a time: five writers at once, not seventy-five
// Every source names the same two new artists: one with an id, one without.
shared := []lastfm.Similar{
{Name: fmt.Sprintf("Shared With Id %d", round), MBID: fmt.Sprintf("00000000-0000-0000-0000-%012d", round), Match: 1},
{Name: fmt.Sprintf("Shared No Id %d", round), Match: 0.5},
}
for _, src := range sources {
wg.Add(1)
go func() {
defer wg.Done()
if err := repo.SaveSimilar(ctx, src, "lastfm", at, true, shared); err != nil {
errs <- fmt.Errorf("SaveSimilar: %w", err)
}
}()
}
// A scan adding files at the same time.
wg.Add(1)
go func() {
defer wg.Done()
// A new artist, and one that an earlier round only discovered (adopted by
// name, which drops its id), as the second of the file's artists.
artists := []string{fmt.Sprintf("Scanned Artist %d", round)}
if round > 0 {
artists = append(artists, fmt.Sprintf("Shared With Id %d", round-1))
}
f := pf(fmt.Sprintf("scan/%d.flac", round), fmt.Sprintf("Scanned %d", round), "", "", 0, 0, "flac", artists...)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
errs <- fmt.Errorf("SaveFile: %w", err)
}
}()
wg.Wait()
}
close(errs)
for err := range errs {
t.Error(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM (SELECT name_key FROM artists WHERE name LIKE 'Shared %' GROUP BY name_key HAVING COUNT(*) > 1) d"); n != 0 {
t.Errorf("%d artists exist more than once", n)
}
// The lists are replaced every round, so the artists of earlier rounds that no
// list names are gone; the ones a scan adopted ("Shared With Id" of rounds
// 0 to rounds-2) are real artists now and stay, plus the two of the last round.
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name LIKE 'Shared %'"); n != rounds+1 {
t.Errorf("shared artists = %d, want %d", n, rounds+1)
}
// Each save replaces the source's edges, so what is left is the last round.
if n := count(t, db, "SELECT COUNT(*) FROM artist_similar WHERE provider = 'lastfm'"); n != len(sources)*2 {
t.Errorf("edges = %d, want %d", n, len(sources)*2)
}
})
}
// ---- MusicBrainz ids that come from the files ----
// withIDs is pf with the ids a tagger wrote: one per artist, and the album artist's.
func withIDs(f library.ParsedFile, albumArtistID string, ids ...string) library.ParsedFile {
f.ArtistMBIDs, f.AlbumArtistMBID = ids, albumArtistID
return f
}
func mbidOf(t *testing.T, db *bun.DB, id int64) string {
t.Helper()
var m sql.NullString
if err := db.QueryRow("SELECT mbid FROM artists WHERE id = ?", id).Scan(&m); err != nil {
t.Fatal(err)
}
return m.String
}
func TestFileIDsAreKeptOnTheArtistsOfTheFiles(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
lib := NewLibraryRepo(db)
const idNew = "aaaaaaaa-0000-4000-8000-000000000001"
const idAlbum = "aaaaaaaa-0000-4000-8000-000000000002"
// A new artist is created with its id, and so is the album artist.
f := withIDs(pf("n/1.flac", "N1", "Album", "Album Artist", 1, 1, "flac", "New Artist"), idAlbum, idNew)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
t.Fatal(err)
}
newID, albumID := artistID(t, db, "New Artist"), artistID(t, db, "Album Artist")
if mbidOf(t, db, newID) != idNew || mbidOf(t, db, albumID) != idAlbum {
t.Errorf("ids = %q / %q", mbidOf(t, db, newID), mbidOf(t, db, albumID))
}
// The same id under another spelling is the same artist.
f = withIDs(pf("n/2.flac", "N2", "", "", 0, 0, "flac", "NEW ARTIST (feat. nobody)"), "", idNew)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM track_artists WHERE artist_id = "+fmt.Sprint(newID)); n != 2 {
t.Errorf("tracks credited to the artist = %d, want 2: the id says it is the same artist", n)
}
// A later file without the id keeps what is known.
if _, err := lib.SaveFile(ctx, s.rock, pf("n/3.flac", "N3", "", "", 0, 0, "flac", "new artist"), 0); err != nil {
t.Fatal(err)
}
if mbidOf(t, db, newID) != idNew {
t.Error("a file without an id erased the id")
}
// An artist that was there without an id gets the one the file says.
const idBjork = "aaaaaaaa-0000-4000-8000-000000000003"
bjork := artistID(t, db, "Björk")
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("b/3.flac", "B3", "", "", 0, 0, "flac", "Björk"), "", idBjork), 0); err != nil {
t.Fatal(err)
}
if mbidOf(t, db, bjork) != idBjork {
t.Errorf("Björk's id = %q", mbidOf(t, db, bjork))
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = 'björk'"); n != 1 {
t.Errorf("artists named Björk = %d, want 1", n)
}
// Another id under the same name is another artist.
const idOther = "aaaaaaaa-0000-4000-8000-000000000004"
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("b/4.flac", "B4", "", "", 0, 0, "flac", "Björk"), "", idOther), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = 'björk'"); n != 2 {
t.Errorf("artists named Björk = %d, want 2: the ids differ", n)
}
if mbidOf(t, db, bjork) != idBjork {
t.Error("the first Björk lost her id")
}
})
}
// A file whose tags name the same id as a provider did confirms that artist, and
// the id stays. A file with another id under the same name is somebody else.
func TestFileIDsAndDiscoveredArtists(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
lib := NewLibraryRepo(db)
radiohead := artistID(t, db, "Radiohead")
const same = "bbbbbbbb-0000-4000-8000-000000000001"
const other = "bbbbbbbb-0000-4000-8000-000000000002"
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{
{Name: "Confirmed", MBID: same, Match: 1}, {Name: "Namesake", MBID: same[:35] + "3", Match: 0.5},
})
confirmed, namesake := artistID(t, db, "Confirmed"), artistID(t, db, "Namesake")
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("c/9.flac", "C9", "", "", 0, 0, "flac", "CONFIRMED"), "", same), 0); err != nil {
t.Fatal(err)
}
a, _ := repo.Artist(ctx, confirmed)
if a.Name != "CONFIRMED" || a.MBID != same {
t.Errorf("confirmed artist = %+v, want the file's spelling and the id that both sides agree on", a)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = 'confirmed'"); n != 1 {
t.Errorf("artists named Confirmed = %d", n)
}
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("c/10.flac", "C10", "", "", 0, 0, "flac", "Namesake"), "", other), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = 'namesake'"); n != 2 {
t.Errorf("artists named Namesake = %d, want 2: a file with another id is another artist", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+fmt.Sprint(namesake)+" AND discovered = "+boolLit(db, true)); n != 1 {
t.Error("the discovered Namesake was taken by a file that is not it")
}
})
}
func boolLit(db *bun.DB, v bool) string {
if db.Dialect().Name() == dialect.PG {
return fmt.Sprint(v)
}
if v {
return "1"
}
return "0"
}
// ---- tags ----
func TestTagsSaveAndRead(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
snap, err := repo.Tags(ctx, radiohead, "lastfm")
if err != nil || snap.Fetched || len(snap.Tags) != 0 {
t.Fatalf("never asked: %+v err=%v", snap, err)
}
tags := []discovery.Tag{{Name: "rock", Weight: 100}, {Name: "alternative rock", Weight: 54.5, Genre: true}, {Name: "british", Weight: 54.5}}
if err := repo.SaveTags(ctx, radiohead, "lastfm", at, true, tags); err != nil {
t.Fatal(err)
}
snap, err = repo.Tags(ctx, radiohead, "lastfm")
if err != nil || !snap.Fetched || snap.NotFound || !snap.RetrievedAt.Equal(at) {
t.Fatalf("snapshot = %+v err=%v", snap, err)
}
// Heaviest first; equal weights in name order, so the order is stable.
want := []discovery.Tag{{Name: "rock", Weight: 100}, {Name: "alternative rock", Weight: 54.5, Genre: true}, {Name: "british", Weight: 54.5}}
if len(snap.Tags) != 3 {
t.Fatalf("tags = %+v", snap.Tags)
}
for i := range want {
if snap.Tags[i] != want[i] {
t.Errorf("tag %d = %+v, want %+v", i, snap.Tags[i], want[i])
}
}
// Saving again replaces; another provider is kept apart; "not found" clears.
_ = repo.SaveTags(ctx, radiohead, "musicbrainz", at, true, []discovery.Tag{{Name: "art rock", Weight: 30, Genre: true}})
later := at.Add(8 * 24 * time.Hour)
if err := repo.SaveTags(ctx, radiohead, "lastfm", later, true, []discovery.Tag{{Name: "grunge", Weight: 10}}); err != nil {
t.Fatal(err)
}
snap, _ = repo.Tags(ctx, radiohead, "lastfm")
if len(snap.Tags) != 1 || snap.Tags[0].Name != "grunge" || !snap.RetrievedAt.Equal(later) {
t.Errorf("after replacing: %+v", snap)
}
if other, _ := repo.Tags(ctx, radiohead, "musicbrainz"); len(other.Tags) != 1 || other.Tags[0].Name != "art rock" {
t.Errorf("another provider's tags = %+v", other)
}
if err := repo.SaveTags(ctx, radiohead, "lastfm", later, false, nil); err != nil {
t.Fatal(err)
}
snap, _ = repo.Tags(ctx, radiohead, "lastfm")
if !snap.Fetched || !snap.NotFound || len(snap.Tags) != 0 {
t.Errorf("not found: %+v", snap)
}
// The ledger for tags and for similar artists do not touch each other.
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
if s, _ := repo.Similar(ctx, radiohead, "lastfm", 10); !s.Fetched || s.NotFound || !s.RetrievedAt.Equal(at) {
t.Errorf("similar artists were disturbed by tags: %+v", s)
}
if n := count(t, db, "SELECT COUNT(*) FROM provider_fetches"); n != 3 {
t.Errorf("ledger rows = %d, want 3 (tags lastfm, tags musicbrainz, similar lastfm)", n)
}
})
}
func TestDeletingAnArtistRemovesItsTags(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
elbow := artistID(t, db, "Elbow") // an artist with no albums, so it can be deleted
_ = repo.SaveTags(ctx, elbow, "lastfm", at, true, []discovery.Tag{{Name: "rock", Weight: 1}})
if _, err := db.Exec("DELETE FROM artists WHERE id = ?", elbow); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artist_tags"); n != 0 {
t.Errorf("tags left = %d", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM provider_fetches WHERE kind = 'tags'"); n != 0 {
t.Errorf("ledger rows left = %d", n)
}
})
}
// ---- review: adopting by id ----
func artistsNamed(t *testing.T, db *bun.DB, key string) int {
t.Helper()
return count(t, db, "SELECT COUNT(*) FROM artists WHERE name_key = '"+key+"'")
}
// The key follows the name: an artist adopted under another spelling must be
// found by that spelling afterwards.
func TestAnAdoptedArtistIsFoundByItsNewSpelling(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db)
const m = "cccccccc-0000-4000-8000-000000000001"
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Beyonce", MBID: m, Match: 1}})
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("y/1.flac", "Y1", "", "", 0, 0, "flac", "Beyoncé"), "", m), 0); err != nil {
t.Fatal(err)
}
if _, err := lib.SaveFile(ctx, s.rock, pf("y/2.flac", "Y2", "", "", 0, 0, "flac", "Beyoncé"), 0); err != nil {
t.Fatal(err)
}
if n := artistsNamed(t, db, "beyoncé"); n != 1 {
t.Errorf("artists with the key of Beyoncé = %d, want 1", n)
}
if n := artistsNamed(t, db, "beyonce"); n != 0 {
t.Errorf("the old key is still in use by %d artists", n)
}
})
}
// A real artist that has no id yet must receive it, not be split from the
// discovered artist that already holds it.
func TestAFileIDGoesToTheRealArtistNotToTheDiscoveredNamesake(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db)
const m = "cccccccc-0000-4000-8000-000000000002"
// The user already has The Beatles, without an id.
if _, err := lib.SaveFile(ctx, s.rock, pf("bt/1.flac", "B1", "Abbey Road", "The Beatles", 1, 1, "flac", "The Beatles"), 0); err != nil {
t.Fatal(err)
}
real := artistID(t, db, "The Beatles")
// Last.fm names "Beatles" with the id the real band has.
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Beatles", MBID: m, Match: 1}})
discovered := artistID(t, db, "Beatles")
// A new file of the real band carries that id.
f := withIDs(pf("bt/2.flac", "B2", "Abbey Road", "The Beatles", 2, 1, "flac", "The Beatles"), m, m)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
t.Fatal(err)
}
if n := artistsNamed(t, db, "the beatles"); n != 1 {
t.Fatalf("artists named The Beatles = %d, want 1", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM albums WHERE title_key = 'abbey road'"); n != 1 {
t.Errorf("albums = %d, want 1", n)
}
if mbidOf(t, db, real) != m {
t.Errorf("the real artist's id = %q, want %q", mbidOf(t, db, real), m)
}
if mbidOf(t, db, discovered) != "" {
t.Error("the discovered namesake still holds the id")
}
// The next look at Last.fm's answer lands on the real artist.
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Beatles", MBID: m, Match: 1}})
snap, _ := repo.Similar(ctx, artistID(t, db, "Radiohead"), "lastfm", 5)
if len(snap.Items) != 1 || snap.Items[0].Artist.ID != real || !snap.Items[0].InLibrary {
t.Errorf("after the next refresh = %+v, want the real artist", snap.Items)
}
})
}
// Whatever was read about an artist under the provider's id is not known to be
// about the user's artist once that id is dropped.
func TestDroppingAProviderIDAlsoDropsWhatWasReadWithIt(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db)
radiohead := artistID(t, db, "Radiohead")
const m = "cccccccc-0000-4000-8000-000000000003"
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Namesake", MBID: m, Match: 1}})
ns := artistID(t, db, "Namesake")
_ = repo.SaveTags(ctx, ns, "musicbrainz", at, true, []discovery.Tag{{Name: "folk", Weight: 5}})
_ = repo.SaveSimilar(ctx, ns, "lastfm", at, true, []lastfm.Similar{{Name: "Someone Else", Match: 1}})
// A file with no id adopts it by name: the id is dropped.
if _, err := lib.SaveFile(ctx, s.rock, pf("n/1.flac", "N1", "", "", 0, 0, "flac", "Namesake"), 0); err != nil {
t.Fatal(err)
}
if mbidOf(t, db, ns) != "" {
t.Fatal("the id stayed")
}
if tg, _ := repo.Tags(ctx, ns, "musicbrainz"); tg.Fetched || len(tg.Tags) != 0 {
t.Errorf("tags read with the dropped id are still served: %+v", tg)
}
if sm, _ := repo.Similar(ctx, ns, "lastfm", 5); sm.Fetched || len(sm.Items) != 0 {
t.Errorf("similar artists read with the dropped id are still served: %+v", sm)
}
// What others said about it stays: Radiohead's list still names it.
if sm, _ := repo.Similar(ctx, radiohead, "lastfm", 5); len(sm.Items) != 1 || sm.Items[0].Artist.ID != ns || !sm.Items[0].InLibrary {
t.Errorf("the link from Radiohead = %+v", sm.Items)
}
})
}
// When the file confirms the very same id, what was read is kept.
func TestConfirmingTheIDKeepsWhatWasRead(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db)
const m = "cccccccc-0000-4000-8000-000000000004"
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Confirmed Two", MBID: m, Match: 1}})
c := artistID(t, db, "Confirmed Two")
_ = repo.SaveTags(ctx, c, "musicbrainz", at, true, []discovery.Tag{{Name: "folk", Weight: 5}})
if _, err := lib.SaveFile(ctx, s.rock, withIDs(pf("n/2.flac", "N2", "", "", 0, 0, "flac", "Confirmed Two"), "", m), 0); err != nil {
t.Fatal(err)
}
if tg, _ := repo.Tags(ctx, c, "musicbrainz"); !tg.Fetched || len(tg.Tags) != 1 {
t.Errorf("tags = %+v", tg)
}
})
}
// ---- genres from the files ----
func genresOf(t *testing.T, db *bun.DB, title string) []string {
t.Helper()
rows, err := db.Query("SELECT g.genre FROM track_genres g JOIN tracks t ON t.id = g.track_id WHERE t.title = ? ORDER BY g.genre", title)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var out []string
for rows.Next() {
var g string
if err := rows.Scan(&g); err != nil {
t.Fatal(err)
}
out = append(out, g)
}
return out
}
func withGenres(f library.ParsedFile, genres ...string) library.ParsedFile {
f.Genres = genres
return f
}
func TestScanKeepsTheGenresOfEachFile(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
lib := NewLibraryRepo(db)
f := withGenres(pf("g/1.flac", "Genre Song", "", "", 0, 0, "flac", "Genre Artist"), "grunge", "rock")
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
t.Fatal(err)
}
if got := genresOf(t, db, "Genre Song"); !equal(got, []string{"grunge", "rock"}) {
t.Fatalf("genres = %v", got)
}
// Reading the file again replaces them, including with none.
var fileID int64
_ = db.QueryRow("SELECT id FROM audio_files WHERE rel_path = 'g/1.flac'").Scan(&fileID)
f = withGenres(pf("g/1.flac", "Genre Song", "", "", 0, 0, "flac", "Genre Artist"), "metal")
if _, err := lib.SaveFile(ctx, s.rock, f, fileID); err != nil {
t.Fatal(err)
}
if got := genresOf(t, db, "Genre Song"); !equal(got, []string{"metal"}) {
t.Fatalf("after the update genres = %v", got)
}
f.Genres = nil
if _, err := lib.SaveFile(ctx, s.rock, f, fileID); err != nil {
t.Fatal(err)
}
if got := genresOf(t, db, "Genre Song"); len(got) != 0 {
t.Errorf("after clearing genres = %v", got)
}
})
}
func TestFileGenresCountTheArtistsPlayableTracks(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
lib, repo := NewLibraryRepo(db), NewDiscoveryRepo(db)
save := func(rel, title string, artists []string, genres ...string) {
t.Helper()
f := withGenres(pf(rel, title, "", "", 0, 0, "flac", artists...), genres...)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
t.Fatal(err)
}
}
save("g/a.flac", "GA", []string{"Genre Band"}, "grunge", "rock")
save("g/b.flac", "GB", []string{"Genre Band"}, "rock")
save("g/c.flac", "GC", []string{"Genre Band", "Other Band"}, "rock", "post-grunge")
save("g/d.flac", "GD", []string{"Other Band"}, "jazz")
save("g/e.flac", "GE", []string{"Genre Band"}, "metal") // its file will be missing
if _, err := db.Exec("UPDATE audio_files SET missing_at = 1 WHERE rel_path = 'g/e.flac'"); err != nil {
t.Fatal(err)
}
got, err := repo.FileGenres(ctx, artistID(t, db, "Genre Band"))
if err != nil {
t.Fatal(err)
}
want := []discovery.Tag{{Name: "rock", Weight: 3, Genre: true}, {Name: "grunge", Weight: 1, Genre: true}, {Name: "post-grunge", Weight: 1, Genre: true}}
if len(got) != len(want) {
t.Fatalf("genres = %+v, want %+v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("genre %d = %+v, want %+v", i, got[i], want[i])
}
}
// An artist nobody has files for has none.
_ = repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
if got, err := repo.FileGenres(ctx, artistID(t, db, "Elbow")); err != nil || len(got) != 0 {
t.Errorf("a discovered artist: %+v err=%v", got, err)
}
})
}
// ---- taking back what a provider said ----
func TestPurgeRemovesWhatOneProviderGaveAndWhatOnlyItNamed(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead, bjork := artistID(t, db, "Radiohead"), artistID(t, db, "Björk")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}, {Name: "Muse", Match: 0.5}, {Name: "Björk", Match: 0.2}})
_ = repo.SaveSimilar(ctx, bjork, "lastfm", at, true, []lastfm.Similar{{Name: "Elbow", Match: 1}})
elbow := artistID(t, db, "Elbow")
_ = repo.SaveTags(ctx, radiohead, "lastfm", at, true, []discovery.Tag{{Name: "rock", Weight: 100}})
_ = repo.SaveTags(ctx, elbow, "lastfm", at, true, []discovery.Tag{{Name: "indie", Weight: 100}})
_ = repo.SaveTags(ctx, radiohead, "musicbrainz", at, true, []discovery.Tag{{Name: "art rock", Weight: 30}, {Name: "rock", Weight: 19}})
// Purging MusicBrainz takes its tags and its records, and nothing else.
got, err := repo.Purge(ctx, "musicbrainz")
if err != nil {
t.Fatal(err)
}
if got.Tags != 2 || got.SimilarArtists != 0 || got.Artists != 0 {
t.Errorf("musicbrainz purge = %+v", got)
}
if tg, _ := repo.Tags(ctx, radiohead, "musicbrainz"); tg.Fetched || len(tg.Tags) != 0 {
t.Errorf("musicbrainz tags are still there: %+v", tg)
}
if tg, _ := repo.Tags(ctx, radiohead, "lastfm"); !tg.Fetched || len(tg.Tags) != 1 {
t.Errorf("lastfm tags were touched: %+v", tg)
}
// Purging Last.fm takes everything it gave, and the artists only it named.
got, err = repo.Purge(ctx, "lastfm")
if err != nil {
t.Fatal(err)
}
if got.SimilarArtists != 4 || got.Tags != 2 || got.Artists != 2 {
t.Errorf("lastfm purge = %+v, want 4 similar artists, 2 tags and the 2 artists Elbow and Muse", got)
}
for _, table := range []string{"artist_similar", "artist_tags", "provider_fetches"} {
if n := count(t, db, "SELECT COUNT(*) FROM "+table); n != 0 {
t.Errorf("%s has %d rows left", table, n)
}
}
for _, name := range []string{"Elbow", "Muse"} {
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = '"+name+"'"); n != 0 {
t.Errorf("the discovered artist %s is still there", name)
}
}
// The user's own artists, tracks and files are as they were.
for _, name := range []string{"Radiohead", "Björk", "Miles Davis"} {
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = '"+name+"'"); n != 1 {
t.Errorf("%s: %d rows, want 1", name, n)
}
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files"); n == 0 {
t.Error("the purge touched the files")
}
// Doing it again finds nothing.
if again, err := repo.Purge(ctx, "lastfm"); err != nil || again != (providers.Purged{}) {
t.Errorf("second purge = %+v err=%v", again, err)
}
})
}
// A discovered artist that nothing points to any more is not kept: otherwise
// every list that changes leaves some behind.
func TestReplacingAListRemovesTheArtistsNoOneNamesAnymore(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead, bjork := artistID(t, db, "Radiohead"), artistID(t, db, "Björk")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{
{Name: "Dropped", Match: 1}, {Name: "Shared", Match: 0.8}, {Name: "Kept", Match: 0.6}, {Name: "Miles Davis", Match: 0.4}})
_ = repo.SaveSimilar(ctx, bjork, "lastfm", at, true, []lastfm.Similar{{Name: "Shared", Match: 1}})
_ = repo.SaveTags(ctx, artistID(t, db, "Dropped"), "lastfm", at, true, []discovery.Tag{{Name: "x", Weight: 1}})
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at.Add(time.Hour), true, []lastfm.Similar{{Name: "Kept", Match: 1}}); err != nil {
t.Fatal(err)
}
has := func(name string) bool {
return count(t, db, "SELECT COUNT(*) FROM artists WHERE name = '"+name+"'") == 1
}
if has("Dropped") {
t.Error("Dropped is no longer named by anyone but is still there")
}
if !has("Shared") {
t.Error("Shared is still named by Björk's list and was removed")
}
if !has("Kept") {
t.Error("Kept is still in the list and was removed")
}
if !has("Miles Davis") {
t.Error("a real artist was removed because a list stopped naming it")
}
if n := count(t, db, "SELECT COUNT(*) FROM artist_tags"); n != 0 {
t.Errorf("tags of the removed artist left: %d", n)
}
})
}
// ---- review: purge, cleanup chains and deleted artists ----
// A purge deleting a discovered artist while a scan adopts it must not leave the
// scan with the id of an artist that is gone.
func TestPurgeAndAdoptionDoNotCollide(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db)
radiohead := artistID(t, db, "Radiohead")
errs := make(chan error, 100)
for round := 0; round < 25; round++ {
name := fmt.Sprintf("Collide %d", round)
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{
{Name: name, MBID: fmt.Sprintf("dddddddd-0000-4000-8000-%012d", round), Match: 1}}); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
if _, err := repo.Purge(ctx, "lastfm"); err != nil {
errs <- fmt.Errorf("Purge: %w", err)
}
}()
go func() {
defer wg.Done()
f := pf(fmt.Sprintf("col/%d.flac", round), fmt.Sprintf("Collide song %d", round), "", "", 0, 0, "flac", name)
if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil {
errs <- fmt.Errorf("SaveFile: %w", err)
}
}()
wg.Wait()
}
close(errs)
for err := range errs {
t.Error(err)
}
})
}
// An artist that was only a source of a list, and that no list names any more,
// goes too, and so do the artists only its own list kept alive.
func TestAChainOfDiscoveredArtistsIsCleanedUpTogether(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
radiohead := artistID(t, db, "Radiohead")
_ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Dee", Match: 1}, {Name: "Keep", Match: 0.5}})
dee := artistID(t, db, "Dee")
_ = repo.SaveSimilar(ctx, dee, "lastfm", at, true, []lastfm.Similar{{Name: "Eee", Match: 1}})
eee := artistID(t, db, "Eee")
_ = repo.SaveSimilar(ctx, eee, "lastfm", at, true, []lastfm.Similar{{Name: "Fff", Match: 1}})
if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at.Add(time.Hour), true, []lastfm.Similar{{Name: "Keep", Match: 1}}); err != nil {
t.Fatal(err)
}
for _, name := range []string{"Dee", "Eee", "Fff"} {
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = '"+name+"'"); n != 0 {
t.Errorf("%s is kept alive only by a list nobody can reach, and is still there", name)
}
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'Keep'"); n != 1 {
t.Error("an artist the list still names was removed")
}
if n := count(t, db, "SELECT COUNT(*) FROM artist_similar"); n != 1 {
t.Errorf("edges left = %d, want 1", n)
}
})
}
// Writing about an artist that was removed meanwhile is "not found", not an
// obscure foreign key error.
func TestSavingForAnArtistThatIsGoneIsNotFound(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewDiscoveryRepo(db)
if err := repo.SaveSimilar(ctx, 99999, "lastfm", at, true, []lastfm.Similar{{Name: "X", Match: 1}}); !errors.Is(err, discovery.ErrNotFound) {
t.Errorf("SaveSimilar: %v", err)
}
if err := repo.SaveTags(ctx, 99999, "lastfm", at, true, []discovery.Tag{{Name: "x", Weight: 1}}); !errors.Is(err, discovery.ErrNotFound) {
t.Errorf("SaveTags: %v", err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'X'"); n != 0 {
t.Error("an artist was created for a list nobody can read")
}
})
}