Files
base/tests/fixture_test.go
zeekay 6a3066b08e Test apps open a migrated fixture, so core and apis fit the runner
tests/data lacked twelve migrations, and every test app ran them before
its test began: about four seconds an app, which put core and apis past
go test's ten minutes on the runner, and past thirty once the timeout
was raised. The fixture now holds them (go run ./tests/fixture writes
it), and TestTheFixtureIsMigrated fails, naming the migration, while
one is missing. On one host core went from 267 s to 26 s, apis from
432 s to 23 s, and the whole suite runs in 51 s.

The delete tests gave a background goroutine 100 ms to remove a
record's files, which a slow runner did not always meet. BaseApp
counts those deletions and WaitFileDeletes waits for them: the tests
wait on the count instead of the clock, and ResetBootstrapState waits
too, so a Base that stops no longer drops a deletion in flight.

TestSQLRun expected a CREATE TABLE to report one affected row. SQLite
answers a statement that writes no rows with the connection's last
count, and that row was the last migration's _migrations insert; the
handler now reads 0, the number it changed.

go test runs with its default timeout again, and crm_test.go is gofmt'd.
2026-10-19 09:08:39 -07:00

60 lines
1.4 KiB
Go

package tests
import (
"database/sql"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/hanzoai/base/core"
)
// The fixture every test app copies already carries every migration this tree
// registers, so no test app spends its first seconds running them. A migration
// added without `go run ./tests/fixture` fails here, naming it.
//
// It reads the fixture's own record of what was applied rather than opening an
// app on it, because opening one is what runs the migrations.
func TestTheFixtureIsMigrated(t *testing.T) {
_, file, _, _ := runtime.Caller(0)
db, err := sql.Open("sqlite", "file:"+filepath.Join(path.Dir(file), "data", "data.db")+"?mode=ro&immutable=1")
if err != nil {
t.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT file FROM _migrations")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
applied := map[string]bool{}
for rows.Next() {
var f string
if err := rows.Scan(&f); err != nil {
t.Fatal(err)
}
applied[f] = true
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
list := core.MigrationsList{}
list.Copy(core.SystemMigrations)
list.Copy(core.AppMigrations)
var missing []string
for _, m := range list.Items() {
if !applied[m.File] {
missing = append(missing, m.File)
}
}
if len(missing) > 0 {
t.Fatalf("tests/data lacks %d migrations, which every test app would run: %v\nrun `go run ./tests/fixture`", len(missing), missing)
}
}