Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
261 lines
9.9 KiB
Go
261 lines
9.9 KiB
Go
package storage
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
|
||
"github.com/uptrace/bun"
|
||
|
||
"git.kmalloc.org/squid/sonda/internal/catalog"
|
||
)
|
||
|
||
func TestSearchKeyIgnoresCaseAndAccents(t *testing.T) {
|
||
for in, want := range map[string]string{
|
||
"Björk": "bjork",
|
||
"BJÖRK": "bjork",
|
||
"Beyoncé": "beyonce",
|
||
" Café ": "cafe",
|
||
"Mötley Crüe": "motley crue",
|
||
"Straße": "strasse",
|
||
"Ørsted": "orsted",
|
||
"Æon": "aeon",
|
||
"Łódź": "lodz",
|
||
"Sigur Rós": "sigur ros",
|
||
"Ελλάδα": "ελλαδα",
|
||
"ñandú": "nandu",
|
||
"AC/DC": "ac/dc",
|
||
"plain": "plain",
|
||
"宇多田ヒカル": "宇多田ヒカル",
|
||
"ガガ": "ガガ", // the marks of kana are part of the letter
|
||
"한글": "한글",
|
||
"Dvořák": "dvorak",
|
||
"Beyoncé Knowles": "beyonce knowles",
|
||
"école": "ecole", // an e and a separate accent
|
||
"İstanbul": "istanbul",
|
||
"PHỞ": "pho",
|
||
"ÉCOLE": "ecole", // a capital E and a separate accent
|
||
"ΣΟΦΟΣ": "σοφοσ",
|
||
"σοφός": "σοφοσ", // the final sigma is a sigma
|
||
"fire": "fire",
|
||
"ABC": "abc",
|
||
"́": "", // a lone accent says nothing
|
||
} {
|
||
if got := searchKey(in); got != want {
|
||
t.Errorf("searchKey(%q) = %q, want %q", in, got, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestSearchFindsNamesWithoutTheAccentsAndTheOtherWayRound(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} }
|
||
|
||
for _, q := range []string{"bjork", "BJORK", "björk", "BJÖRK", "bjö", "jork"} {
|
||
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("unicode")); !equal(artistNames(got), []string{"Ünïcode Band"}) {
|
||
t.Errorf("artists unicode = %v", artistNames(got))
|
||
}
|
||
if got, _ := s.repo.ListTracks(ctx, search("unicode trck")); !equal(trackTitles(got), []string{"Ünïcode Trck"}) {
|
||
t.Errorf("tracks = %v", trackTitles(got))
|
||
}
|
||
if got, _ := s.repo.ListAlbums(ctx, search("post")); !equal(albumTitles(got), []string{"Post"}) {
|
||
t.Errorf("albums = %v", albumTitles(got))
|
||
}
|
||
// The wildcards of the text are still literal.
|
||
if got, _ := s.repo.ListTracks(ctx, search("100%")); !equal(trackTitles(got), []string{"100% Pure"}) {
|
||
t.Errorf("tracks 100%% = %v", trackTitles(got))
|
||
}
|
||
})
|
||
}
|
||
|
||
// "Beyoncé" and "Beyonce" are two names as far as identity goes: the scanner must not merge
|
||
// them; only the search treats them alike.
|
||
func TestAccentsDoNotMergeArtistsButTheSearchFindsBoth(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
ctx := context.Background()
|
||
repo := NewLibraryRepo(db)
|
||
lib := mkLib(t, repo, "m")
|
||
if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Halo", "I Am", "Beyoncé"), 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err := repo.SaveFile(ctx, lib, parsed("2.flac", "Other", "Other Album", "Beyonce"), 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 2 {
|
||
t.Fatalf("artists = %d, want 2: accents identify an artist", n)
|
||
}
|
||
got, err := NewCatalogRepo(db).ListArtists(ctx, catalog.Filter{Query: "beyonce", Limit: 10})
|
||
if err != nil || got.Total != 2 {
|
||
t.Errorf("search = %v err = %v, want both", artistNames(got), err)
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestRefreshMatchKeysFillsTheSearchKeyOfOldRows(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
ctx := context.Background()
|
||
repo := NewLibraryRepo(db)
|
||
lib := mkLib(t, repo, "m")
|
||
if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Army of Me", "Post", "Björk"), 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// Rows from before the column existed have none.
|
||
for _, q := range []string{"UPDATE artists SET search_key = ''", "UPDATE albums SET search_key = ''", "UPDATE tracks SET search_key = ''"} {
|
||
if _, err := db.Exec(q); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
if got, _ := NewCatalogRepo(db).ListArtists(ctx, catalog.Filter{Query: "bjork", Limit: 10}); got.Total != 0 {
|
||
t.Fatal("found an artist that has no search key")
|
||
}
|
||
if err := repo.RefreshMatchKeys(ctx); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := repo.RefreshMatchKeys(ctx); err != nil { // idempotent
|
||
t.Fatal(err)
|
||
}
|
||
cat := NewCatalogRepo(db)
|
||
if got, _ := cat.ListArtists(ctx, catalog.Filter{Query: "bjork", Limit: 10}); got.Total != 1 {
|
||
t.Errorf("artists = %d, want 1", got.Total)
|
||
}
|
||
if got, _ := cat.ListAlbums(ctx, catalog.Filter{Query: "post", Limit: 10}); got.Total != 1 {
|
||
t.Errorf("albums = %d, want 1", got.Total)
|
||
}
|
||
if got, _ := cat.ListTracks(ctx, catalog.Filter{Query: "army", Limit: 10}); got.Total != 1 {
|
||
t.Errorf("tracks = %d, want 1", got.Total)
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestADiscoveredArtistGetsASearchKey(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
ctx := context.Background()
|
||
if err := db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||
_, err := insertDiscovered(ctx, tx, "Sigur Rós", "")
|
||
return err
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var key string
|
||
if err := db.QueryRow("SELECT search_key FROM artists WHERE name = 'Sigur Rós'").Scan(&key); err != nil || key != "sigur ros" {
|
||
t.Errorf("search key = %q err = %v", key, err)
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestAQueryThatSaysNothingOnceFoldedMatchesNothing(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
s := seedCatalog(t, db)
|
||
ctx := context.Background()
|
||
for _, q := range []string{"́", "́́"} {
|
||
if got, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: q, Limit: 50}); got.Total != 0 {
|
||
t.Errorf("artists %q = %v, want none", q, artistNames(got))
|
||
}
|
||
if got, _ := s.repo.ListTracks(ctx, catalog.Filter{Query: q, Limit: 50}); got.Total != 0 {
|
||
t.Errorf("tracks %q = %v, want none", q, trackTitles(got))
|
||
}
|
||
if got, _ := s.repo.ListAlbums(ctx, catalog.Filter{Query: q, Limit: 50}); got.Total != 0 {
|
||
t.Errorf("albums %q = %v, want none", q, albumTitles(got))
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestARetitledTrackIsFoundByItsNewTitle(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
ctx := context.Background()
|
||
repo := NewLibraryRepo(db)
|
||
lib := mkLib(t, repo, "m")
|
||
if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Old Name", "Album", "Band"), 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
existing, err := repo.ExistingFiles(ctx, lib)
|
||
if err != nil || len(existing) != 1 {
|
||
t.Fatalf("existing = %v err = %v", existing, err)
|
||
}
|
||
if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Café Nuovo", "Album", "Band"), existing[0].ID); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
cat := NewCatalogRepo(db)
|
||
if got, _ := cat.ListTracks(ctx, catalog.Filter{Query: "cafe", Limit: 10}); !equal(trackTitles(got), []string{"Café Nuovo"}) {
|
||
t.Errorf("by the new title = %v", trackTitles(got))
|
||
}
|
||
if got, _ := cat.ListTracks(ctx, catalog.Filter{Query: "old name", Limit: 10}); got.Total != 0 {
|
||
t.Errorf("by the old title = %v", trackTitles(got))
|
||
}
|
||
})
|
||
}
|
||
|
||
// A refresh fixes both keys of a row together, and never merges what the accents keep apart.
|
||
func TestRefreshKeepsAccentedAndPlainArtistsApart(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||
ctx := context.Background()
|
||
repo := NewLibraryRepo(db)
|
||
lib := mkLib(t, repo, "m")
|
||
for i, name := range []string{"Beyoncé", "Beyonce"} {
|
||
if _, err := repo.SaveFile(ctx, lib, parsed(string(rune('a'+i))+".flac", "Song", "Album "+name, name), 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
if _, err := db.Exec("UPDATE artists SET name_key = 'wrong', search_key = ''"); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := repo.RefreshMatchKeys(ctx); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var keys []string
|
||
rows, err := db.QueryContext(ctx, "SELECT name_key || '/' || search_key FROM artists ORDER BY id")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer rows.Close()
|
||
for rows.Next() {
|
||
var k string
|
||
_ = rows.Scan(&k)
|
||
keys = append(keys, k)
|
||
}
|
||
if !equal(keys, []string{"beyoncé/beyonce", "beyonce/beyonce"}) {
|
||
t.Errorf("keys = %v, want the identity key with the accent and the search key without", keys)
|
||
}
|
||
})
|
||
}
|
||
|
||
// The rows to fix go in batches: a library with thousands of stale rows ends up with every key
|
||
// right, the rows that were already right keep their value, and a second run changes nothing.
|
||
func TestRefreshMatchKeysFixesEveryRowAcrossBatches(t *testing.T) {
|
||
forEachBackend(t, func(t *testing.T, db *bun.DB, driver string) {
|
||
ctx := context.Background()
|
||
const n = 1250 // more than two batches
|
||
var insert string
|
||
if driver == DriverPostgres {
|
||
insert = "INSERT INTO artists (name, name_key, search_key) SELECT 'Sigur Rós ' || g, CASE WHEN g % 5 = 0 THEN 'sigur rós ' || g ELSE 'stale' END, CASE WHEN g % 5 = 0 THEN 'sigur ros ' || g ELSE '' END FROM generate_series(1, ?) g"
|
||
} else {
|
||
insert = "WITH RECURSIVE c(g) AS (SELECT 1 UNION ALL SELECT g+1 FROM c WHERE g < ?) INSERT INTO artists (name, name_key, search_key) SELECT 'Sigur Rós ' || g, CASE WHEN g % 5 = 0 THEN 'sigur rós ' || g ELSE 'stale' END, CASE WHEN g % 5 = 0 THEN 'sigur ros ' || g ELSE '' END FROM c"
|
||
}
|
||
if _, err := db.ExecContext(ctx, insert, n); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
repo := NewLibraryRepo(db)
|
||
for i := 0; i < 2; i++ {
|
||
if err := repo.RefreshMatchKeys(ctx); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
var wrong int
|
||
if err := db.QueryRow("SELECT count(*) FROM artists WHERE name_key <> lower(name) OR search_key <> replace(lower(name), 'ó', 'o')").Scan(&wrong); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if wrong != 0 {
|
||
t.Errorf("%d artists have a wrong key", wrong)
|
||
}
|
||
var total int
|
||
if err := db.QueryRow("SELECT count(*) FROM artists").Scan(&total); err != nil || total != n {
|
||
t.Errorf("artists = %d err = %v, want %d", total, err, n)
|
||
}
|
||
})
|
||
}
|