sonda/internal/storage/about_test.go
tgiachi 48cb903115 feat(about): artist descriptions from Wikipedia
- GET /artists/{id}/description shows what Wikipedia says about an artist: a short
  text with its page, a link and the licence (CC BY-SA 4.0).
- New provider "wikipedia" (off by default, no key, contact in the User-Agent, which
  Wikimedia requires). The way to the text is a chain of ids, never a name: the
  artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the Wikipedia
  page Wikidata names in the first configured language that has one. A page is used
  only if it is an article about that same item.
- [wikipedia] languages in the config (one to five codes of two or three lower case
  letters, checked before they are sent: an unknown site makes Wikidata answer with
  every language).
- Stored 30 days (a day when there was nothing to find), served stale if Wikipedia
  fails, removed with the provider's data (the purge now reports descriptions).
- MusicBrainz client: WikidataID. Wikipedia client for Wikidata and the summary,
  which also reads Wikidata's errors that come with HTTP 200.
- artist_descriptions table (migration 0014, SQLite and PostgreSQL).
- Spec, contract test, README and example config updated.

Closes #39
2026-11-11 21:36:27 +01:00

115 lines
4.4 KiB
Go

package storage
import (
"context"
"errors"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/about"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/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)
}
})
}