intake/core/migrations_test.go
2025-01-18 14:00:53 -08:00

60 lines
1011 B
Go

package core
import (
"database/sql"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func EphemeralDb(t *testing.T) *sql.DB {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
if err = InitDatabase(db); err != nil {
t.Fatal(err)
}
if err = MigrateDatabase(db); err != nil {
t.Fatal(err)
}
return db
}
func TestInitIdempotency(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if err = InitDatabase(db); err != nil {
t.Fatal(err)
}
if err = InitDatabase(db); err != nil {
t.Fatal(err)
}
}
func TestMigrations(t *testing.T) {
db := EphemeralDb(t)
defer db.Close()
allMigrations, err := migrations.ReadDir("sql")
if err != nil {
t.Fatal(err)
}
rows, err := db.Query("select name from migrations")
if err != nil {
t.Fatal(err)
}
count := 0
for rows.Next() {
count += 1
}
if count != len(allMigrations) {
t.Fatalf("Expected %d migrations, got %d", len(allMigrations), count)
}
}