statedbtest

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
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

func AddFutureColumn(tb testing.TB, dbPath, table, value string)

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 Dir

func Dir(tb testing.TB) string

Dir returns a new temporary directory whose project database is already migrated.

func FutureValues

func FutureValues(tb testing.TB, dbPath, table, key string) map[string]string

FutureValues reads FutureColumn from every row of table, keyed by key, an SQL expression naming the row: `id`, or `identity || '/' || resource`.

func Open

func Open(tb testing.TB) *statedb.DB

Open opens a fully migrated, empty database at a new temporary path and closes it when the test ends.

func Path

func Path(tb testing.TB) string

Path returns the path of a fully migrated, empty database in a new temporary directory.

func Seed

func Seed(tb testing.TB, dbPath string) bool

Seed puts a fully migrated, empty state.db at dbPath unless a file is already there, and reports whether it wrote one.

func SeedDir

func SeedDir(tb testing.TB, dir string)

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

func SetFutureValue(tb testing.TB, dbPath, table, value, where string)

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.

func WantFutureValues

func WantFutureValues(tb testing.TB, dbPath, table, key, after string, want map[string]string)

WantFutureValues fails tb unless table's rows are exactly want: the same keys, each holding the value given. after names the write being checked.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL