56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package sqlite_test
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.zaynet.fr/DoGaMa/DoGaMa-serv/internal/persistence/sqlite"
|
|
)
|
|
|
|
func TestOpenAppliesMigrationsAndConfiguration(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "dogama.db")
|
|
db, err := sqlite.Open(context.Background(), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
var count int
|
|
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&count); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 2 {
|
|
t.Fatalf("got %d migrations, want 2", count)
|
|
}
|
|
var foreignKeys, busyTimeout int
|
|
var journalMode string
|
|
if err := db.QueryRow("PRAGMA foreign_keys").Scan(&foreignKeys); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow("PRAGMA busy_timeout").Scan(&busyTimeout); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow("PRAGMA journal_mode").Scan(&journalMode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if foreignKeys != 1 || busyTimeout != 5000 || journalMode != "wal" {
|
|
t.Fatalf("unexpected pragmas: foreign_keys=%d busy_timeout=%d journal_mode=%s", foreignKeys, busyTimeout, journalMode)
|
|
}
|
|
|
|
if err := db.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db, err = sqlite.Open(context.Background(), path)
|
|
if err != nil {
|
|
t.Fatalf("reopen migrated database: %v", err)
|
|
}
|
|
defer db.Close()
|
|
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&count); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 2 {
|
|
t.Fatalf("reopened database has %d migrations, want 2", count)
|
|
}
|
|
}
|