dreltest

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin

func Begin(t testingTB, engine *drel.Engine) *drel.Tx

Begin opens a transaction-scoped sandbox against a SQLite (or libSQL) engine: every write made through the returned *drel.Tx is rolled back when the test completes, giving per-test isolation without recreating the schema. Unlike a raw SAVEPOINT through the connection pool, this drives the engine's own single-connection transaction, so isolation holds for file-backed SQLite too (not just in-memory). Begin fails the test on a non-SQLite engine.

func CreateSchema added in v0.5.0

func CreateSchema(t testingTB, engine *drel.Engine, ddl ...string)

CreateSchema executes each DDL statement against the engine in order, failing the test on the first error. Convenience for tests that already hold an engine (it is the helper the PRD's hand-waved createSchema(t, engine) refers to).

func NewSQLite

func NewSQLite(t testingTB, opts ...Option) *drel.Engine

NewSQLite creates an in-memory SQLite engine for testing, applying any options (WithMigrations, WithSchema, WithSeed) in that order. The engine is automatically closed when the test completes.

func WithRollback added in v0.6.0

func WithRollback(t testingTB, engine *drel.Engine, fn func(ctx context.Context))

WithRollback runs fn inside a transaction and rolls that transaction back. The transaction travels in the context, so code that calls Engine.WithTx joins it through a savepoint instead of opening its own transaction. Nothing fn writes reaches the next test.

Use it for every test that touches the database. On Postgres the test functions can then run in parallel against one database, and no test needs to recreate the schema.

func TestPlaceOrder(t *testing.T) {
    dreltest.WithRollback(t, engine, func(ctx context.Context) {
        err := service.PlaceOrder(ctx, order) // calls db.WithTx inside
        require.NoError(t, err)
    })
}

The rollback runs at test cleanup, so it also runs after t.Fatal. A failed test does not leak an open transaction.

Three limits hold.

A test that needs a second connection to read fn's writes cannot use WithRollback, because nothing is committed.

On Postgres the transaction holds one pooled connection for the whole test, so the pool must be larger than the count of parallel tests.

A SQLite engine drives one connection. Two parked transactions therefore cannot exist at one time, and two parallel WithRollback calls on one SQLite engine deadlock. Run the SQLite tests one after another, or give each test its own engine with NewSQLite.

Types

type Option

type Option func(*config)

Option configures NewSQLite.

func WithMigrations added in v0.5.0

func WithMigrations(dir string) Option

WithMigrations applies all pending up-migrations from dir against the test engine. The migration files must be written for SQLite (the test engine's dialect); see CreateSchema for raw-DDL setup.

func WithSchema added in v0.5.0

func WithSchema(ddl ...string) Option

WithSchema applies raw DDL statements (e.g. CREATE TABLE) after the database is created, each in order. A failing statement fails the test.

func WithSeed

func WithSeed(fn func(*drel.Engine) error) Option

WithSeed runs a seed function after the database is created and after any schema/migrations have been applied. A non-nil error fails the test.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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