sonda/internal/storage/storage_test.go
tgiachi ed32aac228 feat: the radio stations are in the apps only when the person asks
A new setting, radio_in_apps, off until the person turns it on (Settings, Apps and
devices, or PATCH /me/settings). With it off, the list of playlists of an OpenSubsonic
app is only the playlists of the person, and a station asked for by id is not found.
With it on, the stations come as playlists called Radio · Rock.

- migration 0036: user_settings.radio_in_apps
- usersettings, the settings API and the OpenAPI spec have the new field; the PATCH
  now keeps each setting apart (naming one no longer sets the other)
- Settings has a switch
2026-11-16 12:33:09 +01:00

281 lines
8 KiB
Go

package storage
import (
"context"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"testing/fstest"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/migrations"
)
// postgresDSNEnv points the tests at a throwaway PostgreSQL server, e.g.
// postgres://postgres:sonda@127.0.0.1:55432/sonda?sslmode=disable
// Without it only the SQLite tests run, so the default suite needs no network.
const postgresDSNEnv = "SONDA_TEST_POSTGRES_DSN"
var schemaCounter atomic.Int64
// openBackend opens a fresh, empty database for driver. Postgres tests get
// their own schema, dropped when the test ends.
func openBackend(t *testing.T, driver string) *bun.DB {
t.Helper()
switch driver {
case DriverSQLite:
db, err := Open(DriverSQLite, filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close() })
return db
case DriverPostgres:
admin, err := Open(DriverPostgres, os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
schema := fmt.Sprintf("t_%d_%d", os.Getpid(), schemaCounter.Add(1))
if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil {
t.Fatal(err)
}
u, err := url.Parse(os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
q := u.Query()
q.Set("search_path", schema)
u.RawQuery = q.Encode()
db, err := Open(DriverPostgres, u.String())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = db.Close()
_, _ = admin.Exec("DROP SCHEMA " + schema + " CASCADE")
_ = admin.Close()
})
return db
}
t.Fatalf("unknown driver %q", driver)
return nil
}
// forEachBackend runs fn once per available backend with a migrated database.
func forEachBackend(t *testing.T, fn func(t *testing.T, db *bun.DB, driver string)) {
t.Helper()
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
for _, driver := range drivers {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatal(err)
}
fn(t, db, driver)
})
}
}
func tableExists(t *testing.T, db *bun.DB, driver, table string) bool {
t.Helper()
var n int
var err error
if driver == DriverPostgres {
err = db.QueryRow(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = ?",
table).Scan(&n)
} else {
err = db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?", table).Scan(&n)
}
if err != nil {
t.Fatal(err)
}
return n == 1
}
func TestOpenRejectsUnknownDriver(t *testing.T) {
if _, err := Open("mysql", "x"); err == nil {
t.Fatal("expected error for unknown driver")
}
}
func TestSQLiteOpenSetsPragmas(t *testing.T) {
db := openBackend(t, DriverSQLite)
var fk int
if err := db.QueryRow("PRAGMA foreign_keys").Scan(&fk); err != nil {
t.Fatal(err)
}
if fk != 1 {
t.Errorf("foreign_keys = %d, want 1", fk)
}
var mode string
if err := db.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil {
t.Fatal(err)
}
if mode != "wal" {
t.Errorf("journal_mode = %q, want wal", mode)
}
var busy int
if err := db.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil {
t.Fatal(err)
}
if busy <= 0 {
t.Errorf("busy_timeout = %d, want > 0", busy)
}
}
func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, driver string) {
fsys, _ := migrations.For(driver)
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatalf("second Migrate: %v", err)
}
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends", "federation_requests", "track_peaks", "mastodon_accounts"} {
if !tableExists(t, db, driver, table) {
t.Errorf("table %s missing", table)
}
}
var applied int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 36 {
t.Errorf("applied migrations = %d, want 36", applied)
}
})
}
func TestSchemaEnforcesForeignKeys(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title, album_id) VALUES ('x', 999)"); err == nil {
t.Fatal("expected foreign key violation")
}
})
}
func TestSchemaAllowsUnknownMBIDsButRejectsDuplicates(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
// Several artists without an MBID must be allowed.
for _, name := range []string{"A", "B"} {
if _, err := db.Exec("INSERT INTO artists (name) VALUES (?)", name); err != nil {
t.Fatalf("insert %s: %v", name, err)
}
}
// The same MBID twice must not.
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('C', 'm1')"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('D', 'm1')"); err == nil {
t.Fatal("expected unique violation on mbid")
}
})
}
func TestAudioFilePathIsUniquePerLibrary(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('t')"); err != nil {
t.Fatal(err)
}
var a, b int64
for name, dst := range map[string]*int64{"a": &a, "b": &b} {
err := db.QueryRow("INSERT INTO libraries (name, path, created_at) VALUES (?, ?, 1) RETURNING id",
name, "/music/"+name).Scan(dst)
if err != nil {
t.Fatal(err)
}
}
insert := func(lib int64, rel string) error {
_, err := db.Exec(
`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
VALUES (1, ?, ?, 'mp3', 1, 1)`, lib, rel)
return err
}
if err := insert(a, "x/y.mp3"); err != nil {
t.Fatal(err)
}
// Same relative path in another library is a different file.
if err := insert(b, "x/y.mp3"); err != nil {
t.Fatalf("same rel_path in another library should be allowed: %v", err)
}
// Same library and same relative path is a duplicate.
if err := insert(a, "x/y.mp3"); err == nil {
t.Fatal("expected unique violation on (library_id, rel_path)")
}
// A file must belong to a real library.
if err := insert(999, "z.mp3"); err == nil {
t.Fatal("expected foreign key violation for an unknown library")
}
})
}
func TestMigrateRollsBackFailedMigration(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys := fstest.MapFS{
"0001_ok.sql": {Data: []byte("CREATE TABLE a (id INTEGER);")},
"0002_bad.sql": {Data: []byte("CREATE TABLE b (id INTEGER); THIS IS NOT SQL;")},
}
if err := Migrate(context.Background(), db, fsys); err == nil {
t.Fatal("expected error from bad migration")
}
if tableExists(t, db, driver, "b") {
t.Error("table b exists: failed migration was not rolled back")
}
var n int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&n); err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("applied = %d, want 1 (only 0001)", n)
}
})
}
}
func availableDrivers() []string {
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
return drivers
}
func TestMigrationFoldersMatch(t *testing.T) {
names := func(driver string) string {
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
entries, err := fs.ReadDir(fsys, ".")
if err != nil {
t.Fatal(err)
}
var out []string
for _, e := range entries {
out = append(out, e.Name())
}
return strings.Join(out, ",")
}
if a, b := names(DriverSQLite), names(DriverPostgres); a != b {
t.Errorf("migration files differ: sqlite=%s postgres=%s", a, b)
}
}