package storage import ( "context" "errors" "testing" "time" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/about" "git.kmalloc.org/squid/sonda/internal/discovery" "git.kmalloc.org/squid/sonda/internal/providers/lastfm" ) func TestDescriptionSaveAndRead(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.Description(ctx, radiohead) if err != nil || snap.Fetched { t.Fatalf("never asked: %+v err=%v", snap, err) } d := &about.Description{Text: "Un gruppo. Con è e ö.", Short: "gruppo musicale", Language: "it", Title: "Radiohead", URL: "https://it.wikipedia.org/wiki/Radiohead"} if err := repo.SaveDescription(ctx, radiohead, at, "Q44190", d); err != nil { t.Fatal(err) } snap, err = repo.Description(ctx, radiohead) if err != nil || !snap.Fetched || snap.None || !snap.RetrievedAt.Equal(at) || snap.Description != *d { t.Fatalf("saved: %+v err=%v, want %+v", snap, err, *d) } // A new answer replaces it, including "nothing found". if err := repo.SaveDescription(ctx, radiohead, at.Add(time.Hour), "Q44190", nil); err != nil { t.Fatal(err) } snap, _ = repo.Description(ctx, radiohead) if !snap.Fetched || !snap.None || !snap.RetrievedAt.Equal(at.Add(time.Hour)) || snap.Description != (about.Description{}) { t.Errorf("after 'nothing': %+v", snap) } var qid string if err := db.QueryRow("SELECT wikidata_id FROM artist_descriptions WHERE artist_id = ?", radiohead).Scan(&qid); err != nil || qid != "Q44190" { t.Errorf("wikidata id = %q err=%v", qid, err) } // Another artist is separate. if snap, _ := repo.Description(ctx, artistID(t, db, "Björk")); snap.Fetched { t.Error("another artist has a description") } }) } func TestDescriptionOfAnArtistThatWentIsNotFoundAndLeavesNothing(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.SaveDescription(ctx, 99999, at, "", nil); !errors.Is(err, discovery.ErrNotFound) { t.Errorf("err = %v, want not found", err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_descriptions"); n != 0 { t.Errorf("%d rows for an artist that does not exist", n) } // Deleting an artist deletes its description (one without albums: Staind, named by a list). if err := repo.SaveSimilar(ctx, artistID(t, db, "Radiohead"), "lastfm", at, true, []lastfm.Similar{{Name: "Staind", Match: 1}}); err != nil { t.Fatal(err) } staind := artistID(t, db, "Staind") if err := repo.SaveDescription(ctx, staind, at, "Q1", &about.Description{Text: "x", Language: "en", Title: "T", URL: "https://en.wikipedia.org/wiki/T"}); err != nil { t.Fatal(err) } if _, err := db.Exec("DELETE FROM artists WHERE id = ?", staind); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_descriptions"); n != 0 { t.Errorf("%d descriptions left for a deleted artist", n) } }) } func TestPurgingWikipediaRemovesDescriptionsAndNothingElse(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") if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Staind", Match: 1}}); err != nil { t.Fatal(err) } _ = repo.SaveDescription(ctx, radiohead, at, "Q44190", &about.Description{Text: "x", Language: "en", Title: "T", URL: "https://en.wikipedia.org/wiki/T"}) _ = repo.SaveDescription(ctx, artistID(t, db, "Björk"), at, "", nil) // Purging another provider leaves the descriptions. if _, err := repo.Purge(ctx, "musicbrainz"); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_descriptions"); n != 2 { t.Fatalf("a purge of MusicBrainz left %d descriptions, want 2", n) } out, err := repo.Purge(ctx, "wikipedia") if err != nil { t.Fatal(err) } if out.Descriptions != 2 || out.SimilarArtists != 0 || out.Artists != 0 { t.Errorf("purged = %+v, want 2 descriptions and nothing else", out) } if n := count(t, db, "SELECT COUNT(*) FROM artist_descriptions"); n != 0 { t.Errorf("%d descriptions left", n) } if n := count(t, db, "SELECT COUNT(*) FROM artist_similar"); n != 1 { t.Errorf("Last.fm's list was touched: %d rows", n) } }) }