Documentation
¶
Overview ¶
Package statedbtest gives tests a fully migrated, empty state.db without migrating one.
It binds internal/dbtemplate to statedb.Open: the first test in a binary that asks for a database pays for one migration from scratch, and every test after it gets a copy of the result. A copy is the file statedb.Open would have produced — same schema, same schema_migrations rows, same file mode — so the code under test takes the path a hub takes when it opens an existing project, and none of the 56 migrations run again. Why that matters, in numbers, is in internal/dbtemplate's package comment.
Use it wherever a test needs a project or a database to exist and is not about how one comes to exist:
dir := statedbtest.Dir(t) // a project directory, database migrated statedbtest.SeedDir(t, dir) // the same for a directory you already have db := statedbtest.Open(t) // a migrated *statedb.DB, closed at cleanup
Do not use it in a test about migrations, schema adoption, version skew or a first open racing another: those must build their database from scratch, or they stop testing what they are named for. pkg/statedb's in-package tests cannot import this package (it imports statedb); they bind dbtemplate themselves, in template_test.go. Its external statedb_test files use this package like everyone else.
The template is keyed on a SHA-256 of the migration files, and a freshly built template is checked against those files before it is ever copied: its schema_migrations must record exactly them, in order, and nothing else. It is built in memory, once per test binary, from the migrations that binary embeds, and is never written anywhere that outlives the binary — so it cannot go stale, and the check is what proves it rather than assumes it.
Index ¶
- Constants
- func AddFutureColumn(tb testing.TB, dbPath, table, value string)
- func Dir(tb testing.TB) string
- func FutureValues(tb testing.TB, dbPath, table, key string) map[string]string
- func Open(tb testing.TB) *statedb.DB
- func Path(tb testing.TB) string
- func Seed(tb testing.TB, dbPath string) bool
- func SeedDir(tb testing.TB, dir string)
- func SetFutureValue(tb testing.TB, dbPath, table, value, where string)
- func Template() *dbtemplate.Template
- func WantFutureValues(tb testing.TB, dbPath, table, key, after string, want map[string]string)
Constants ¶
const FutureColumn = "from_a_newer_build"
FutureColumn is the column a newer build added. No build of this tree knows it, so no statement this tree issues names it.
Variables ¶
This section is empty.
Functions ¶
func AddFutureColumn ¶
AddFutureColumn appends FutureColumn to table in the database at dbPath, in the shape a migration recorded additive takes, and sets it in every row already there to value, an SQL expression over the row — `'kept by ' || id`.
func FutureValues ¶
FutureValues reads FutureColumn from every row of table, keyed by key, an SQL expression naming the row: `id`, or `identity || '/' || resource`.
func Open ¶
Open opens a fully migrated, empty database at a new temporary path and closes it when the test ends.
func Seed ¶
Seed puts a fully migrated, empty state.db at dbPath unless a file is already there, and reports whether it wrote one.
func SeedDir ¶
SeedDir seeds the database of the project at dir: dir/.cloop/state.db, which is where state.Init, state.Save, state.Load and statedb.Open(state.DBPath(dir)) find it for a project outside a session. Call it before anything opens a database there.
func SetFutureValue ¶
SetFutureValue sets FutureColumn to value, an SQL expression, in the rows of table that where selects.
func Template ¶
func Template() *dbtemplate.Template
Template returns the state.db template, creating it on first use.
Types ¶
This section is empty.