testing

command
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

testing

What this shows

Testing application code against a real PostgreSQL database with momotest, on a small bank-account service.

  • momotest.NewTx(t, momotest.WithMigrations(Migrations())) gives a test a transaction on a schema that already has the embedded migrations. Everything the test writes is rolled back when it ends, so parallel subtests start from the same two accounts and never see each other's transfers.
  • A refused overdraft is tested against the real CHECK constraint, inside a savepoint, so the test can look at the balances afterwards.
  • momotest.New gives a schema of its own, dropped when the test ends, for code that opens and commits its own transactions.
  • momotest.Seed with momotest.Insert writes the fixtures in order and reads the generated ids back into the rows.
  • The migrations are embedded with go:embed, so the binary and its tests carry the same schema.

The tests are skipped when MOMO_TEST_POSTGRES_URL is unset, and fail instead when MOMO_REQUIRE_INTEGRATION=1.

Run

cd examples/testing
export DATABASE_URL=postgres://user:pass@localhost:5432/scratch
momo migrate apply
go run .

To run the tests, point MOMO_TEST_POSTGRES_URL at a server and run go test -tags integration -race ./examples/testing.

Documentation

Overview

Command testing shows how to test application code against a real PostgreSQL database with momotest: a database or a transaction per test, the project's embedded migrations applied, seeded fixtures, and tests that run in parallel.

The program itself is a few lines of bank-account logic; read example_test.go for the point. Run it against a database that has the migrations applied:

DATABASE_URL=postgres://... go run ./examples/testing

Directories

Path Synopsis
Package models is the schema of the testing example: bank accounts whose balance the database refuses to take below zero.
Package models is the schema of the testing example: bank accounts whose balance the database refuses to take below zero.
account
Package account holds typed column descriptors and relation descriptors for the Account model.
Package account holds typed column descriptors and relation descriptors for the Account model.

Jump to

Keyboard shortcuts

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