Foundation: config, schema/migrations, auth, stable IDs, metadata probing
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func openTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "test.db")
|
||||
db, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
}
|
||||
|
||||
func TestOpenAppliesPragmas(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
|
||||
var mode string
|
||||
if err := db.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil {
|
||||
t.Fatalf("journal_mode: %v", err)
|
||||
}
|
||||
if mode != "wal" {
|
||||
t.Errorf("journal_mode = %q, want wal", mode)
|
||||
}
|
||||
|
||||
var fk int
|
||||
if err := db.QueryRow("PRAGMA foreign_keys").Scan(&fk); err != nil {
|
||||
t.Fatalf("foreign_keys: %v", err)
|
||||
}
|
||||
if fk != 1 {
|
||||
t.Errorf("foreign_keys = %d, want 1", fk)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAppliesMigrations(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
|
||||
tables := []string{
|
||||
"users", "sessions", "api_keys",
|
||||
"music_folders", "media_files", "scan_state",
|
||||
"artists", "genres", "albums", "album_artists", "album_genres",
|
||||
"songs", "song_artists", "contributors", "song_genres",
|
||||
"cover_art", "lyrics", "lyrics_lines", "lyrics_cues",
|
||||
"playlists", "playlist_entries",
|
||||
"stars", "ratings", "scrobbles", "playback_history", "now_playing",
|
||||
"bookmarks", "play_queues",
|
||||
"shares", "share_items",
|
||||
"podcast_channels", "podcast_episodes",
|
||||
"internet_radio_stations", "chat_messages",
|
||||
"transcode_profiles", "audio_features", "settings",
|
||||
}
|
||||
for _, name := range tables {
|
||||
var got string
|
||||
err := db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name=?", name).Scan(&got)
|
||||
if err != nil {
|
||||
t.Errorf("table %q missing: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateIsIdempotent(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
if err := Migrate(db); err != nil {
|
||||
t.Fatalf("second Migrate call failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForeignKeysEnforced(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
_, err := db.Exec(`INSERT INTO sessions (id, user_id, expires_at) VALUES ('s1', 'no-such-user', '2999-01-01T00:00:00Z')`)
|
||||
if err == nil {
|
||||
t.Fatal("expected foreign key violation inserting session with unknown user_id")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user