intake/core/migrations_test.go

68 lines
1.2 KiB
Go
Raw Normal View History

2025-01-16 22:53:04 +00:00
package core
2025-01-16 19:46:37 +00:00
import (
"database/sql"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func EphemeralDb(t *testing.T) *DB {
// We don't use OpenDb here because you can't open two connections to the same memory mem
mem, err := sql.Open("sqlite3", ":memory:")
2025-01-16 19:46:37 +00:00
if err != nil {
t.Fatal(err)
}
if _, err = defaultPragma(mem); err != nil {
t.Fatal(err)
}
db := new(DB)
db.ro = mem
db.rw = mem
2025-01-16 21:46:30 +00:00
if err = InitDatabase(db); err != nil {
2025-01-16 19:46:37 +00:00
t.Fatal(err)
}
2025-01-16 21:46:30 +00:00
if err = MigrateDatabase(db); err != nil {
2025-01-16 19:46:37 +00:00
t.Fatal(err)
}
2025-01-16 21:46:30 +00:00
return db
}
2025-01-17 05:11:07 +00:00
func TestInitIdempotency(t *testing.T) {
mem, err := sql.Open("sqlite3", ":memory:")
2025-01-17 05:11:07 +00:00
if err != nil {
t.Fatal(err)
}
db := new(DB)
db.ro = mem
db.rw = mem
2025-01-17 05:11:07 +00:00
if err = InitDatabase(db); err != nil {
t.Fatal(err)
}
if err = InitDatabase(db); err != nil {
t.Fatal(err)
}
}
2025-01-16 21:46:30 +00:00
func TestMigrations(t *testing.T) {
db := EphemeralDb(t)
2025-01-16 19:46:37 +00:00
2025-01-17 05:11:07 +00:00
allMigrations, err := migrations.ReadDir("sql")
if err != nil {
t.Fatal(err)
}
rows, err := db.Query("select name from migrations")
if err != nil {
2025-01-16 21:46:30 +00:00
t.Fatal(err)
}
2025-01-17 05:11:07 +00:00
count := 0
for rows.Next() {
count += 1
}
if count != len(allMigrations) {
t.Fatalf("Expected %d migrations, got %d", len(allMigrations), count)
2025-01-16 19:46:37 +00:00
}
}