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"
|
|
|
|
)
|
|
|
|
|
2025-01-16 21:46:30 +00:00
|
|
|
func EphemeralDb(t *testing.T) *sql.DB {
|
2025-01-16 19:46:37 +00:00
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-16 21:46:30 +00:00
|
|
|
func TestMigrations(t *testing.T) {
|
|
|
|
db := EphemeralDb(t)
|
|
|
|
defer db.Close()
|
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
|
|
|
}
|
|
|
|
}
|