mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
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.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Command fixture brings tests/data up to the migrations this tree registers.
|
|
//
|
|
// go run ./tests/fixture
|
|
//
|
|
// Every test app is a copy of tests/data, and a copy that lacks a migration runs
|
|
// it before the test begins. Run per app, a dozen migrations cost each one about
|
|
// four seconds, which is how core and apis stopped fitting the runner's limit: the
|
|
// work was the same in every test and none of it was the test. The fixture records
|
|
// the schema the migrations produce, so a test app opens it instead of building it,
|
|
// and tests.TestTheFixtureIsMigrated fails when a new migration is not in it yet.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/hanzoai/base/tests"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "fixture:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
_, file, _, _ := runtime.Caller(0)
|
|
dir := filepath.Join(filepath.Dir(file), "..", "data")
|
|
|
|
// a test app is the fixture with every pending migration applied
|
|
app, err := tests.NewTestApp()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer app.Cleanup()
|
|
|
|
for _, db := range []struct {
|
|
name string
|
|
db dbx.Builder
|
|
}{
|
|
{"data.db", app.NonconcurrentDB()},
|
|
{"auxiliary.db", app.AuxNonconcurrentDB()},
|
|
} {
|
|
dst := filepath.Join(dir, db.name)
|
|
tmp := dst + ".next"
|
|
_ = os.Remove(tmp)
|
|
|
|
// VACUUM INTO writes one self-contained file, with nothing left in a WAL
|
|
if _, err := db.db.NewQuery("VACUUM INTO {:path}").Bind(dbx.Params{"path": tmp}).Execute(); err != nil {
|
|
return fmt.Errorf("%s: %w", db.name, err)
|
|
}
|
|
if err := os.Rename(tmp, dst); err != nil {
|
|
return fmt.Errorf("%s: %w", db.name, err)
|
|
}
|
|
fmt.Println("wrote", dst)
|
|
}
|
|
|
|
return nil
|
|
}
|