ormtest

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ormtest opens a database for a test with the models' tables made: in-memory SQLite by default, or, with ORMTEST_DRIVER and ORMTEST_DSN set, a schema (Postgres) or database (MySQL) of its own on a real server, dropped when the test ends.

func TestSignup(t *testing.T) {
	ctx := ormtest.Open(t, Users, Posts)
	ormtest.Seed(t, ctx, Users, &User{Name: "Ali"})
	…
}

The program's tests import the driver of a real server they point it at (_ "github.com/vuka-lang/orm/postgres"); SQLite is linked here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountQueries

func CountQueries(ctx context.Context) (context.Context, func() int64)

CountQueries is ctx counting the statements its queries run (not those the ORM refused to send), and how many so far.

func Driver

func Driver() string

Driver is the database Open gives: "sqlite", or ORMTEST_DRIVER's.

func Exec

func Exec(t testing.TB, ctx context.Context, query string, args ...any)

Exec runs SQL on ctx's database (test data the models can't write), failing the test when it can't.

func Mirror

func Mirror(t testing.TB, ctx context.Context, models ...orm.AnyManager) (mirrored, onMirror context.Context)

Mirror opens a second database with the models' tables and has ctx's writes mirrored to it (orm.WithMirror), for testing a model moving between databases. It is in-memory SQLite, or ORMTEST_MIRROR_DRIVER's with ORMTEST_MIRROR_DSN: MySQL as Open's and Postgres as the mirror's is the move the ORM's mirror is for. The returned context reads the mirror, to check what reached it.

func Open

func Open(t testing.TB, models ...orm.AnyManager) context.Context

Open is a context whose queries use a fresh database with the models' tables (and their many-to-many tables) made.

func Seed

func Seed[T any](t testing.TB, ctx context.Context, m *orm.Manager[T], rows ...*T)

Seed inserts rows, failing the test when it can't.

Types

This section is empty.

Jump to

Keyboard shortcuts

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