riverdbtest

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MPL-2.0 Imports: 20 Imported by: 1

Documentation

Overview

Package riverdbtest contains testing infrastructure for the River project itself that creates isolated schemas suitable for use within a single case.

This package is for internal use and should not be considered stable. Changes to functions and types in this package WILL NOT be considered breaking changes for purposes of River's semantic versioning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestSchema

func TestSchema[TTx any](ctx context.Context, tb testutil.TestingTB, driver riverdriver.Driver[TTx], opts *TestSchemaOpts) string

TestSchema generates an isolated schema for use during a single test run. Migrations are run in the schema (this adds ~50 ms of overhead) to prepare it for River testing. AFter a test run, the schema in use is checked back into a pool for potential reuse. When a schema is reused, tables in TruncateTables are truncated to leave a clean state for the next test.

Use of a schema doesn't happen in River clients automatically. TestSchema returns the name of a schema to use. This should be set in River clients, used as a parameter for testfactory functions, and anywhere else where database operations are issued.

Despite reasonably fast migrations and efficient reuse, test schemas still have more overhead than a test transaction, so prefer the use of riversharedtest.TestTx where a full database isn't needed (TestSchema is commonly needed where tests rely on database notifications).

tb is an interface that tolerates not having a cleanup hook so constructs like testutil.PanicTB can be used. If Cleanup isn't available, schemas aren't checked back in for reuse.

Where Cleanup is available, the function also performs a post-flight check that makes sure all tables in TruncateTables are empty. This helps detect problems where test cases accidentally inject data into the default schema rather than the one returned by this function.

func TestTx

func TestTx[TTx any](ctx context.Context, tb testing.TB, driver riverdriver.Driver[TTx], opts *TestTxOpts) (TTx, string)

TestTx starts a test transaction that's rolled back automatically as the test case is cleaning itself up.

The function invokes TestSchema to create a single schema where this test transaction and all future test transactions for this package test run will run.

`search_path` is set to the name of the transaction schema so that it's not necessary to specify an explicit schema for database operations. (This is somewhat of a legacy decision

The included driver determines what migrations are run to prepare the test transaction schema.

func TestTxPgx

func TestTxPgx(ctx context.Context, tb testing.TB) pgx.Tx

TestTxPgx starts a test transaction that's rolled back automatically as the test case is cleaning itself up.

This variant starts a transaction for the standard pgx/v5 driver most commonly used throughout most of River.

func TestTxPgxDriver added in v0.26.0

func TestTxPgxDriver(ctx context.Context, tb testing.TB, driver riverdriver.Driver[pgx.Tx], opts *TestTxOpts) (pgx.Tx, string)

TestTxPgxDriver starts a test transaction that's rolled back automatically as the test case is cleaning itself up. Unlike TestTxPgx, this variant takes a driver and options for greater flexibility, including allowing for Pro drivers, while still sharing common setup like schema search path.

Types

type TestSchemaOpts

type TestSchemaOpts struct {
	// LineTargetVersions specify target versions for migration lines being
	// migrated. By default all lines are migrated all the way up, but this lets
	// tests migrate to an only partially applied version. This option is rarely
	// required.
	LineTargetVersions map[string]int

	// Lines are migration lines to run. By default, the migration lines
	// specified by the driver's GetMigrationDefaultLines function are run.
	//
	// Set to an empty non-nil slice like `[]string{}` to run no migrations.
	Lines []string

	// ProcurePool returns a database pool that will be set to the input driver
	// using Driver.PoolSet. This is an optional parameter and should usuall be
	// left unset. It exists for use with SQLite to generate a database pool for
	// use in testing after a test schema is available because unlike other
	// databases, test schemas in SQLite (which are actually test databases) are
	// not available between databases.
	//
	// The input schema is the name of the test schema to use. After the pool's
	// been procured, it'll either be migrated to current version or have its
	// tables truncated, depending on whether it's been reused or not.
	//
	// Returns a database pool (*sql.DB for SQLite) and a "user-facing schema
	// name", which is the schema used in River operations. In practice, this is
	// an empty string for SQLite because unlike other databases, schemas can't
	// be reference by their qualified name (operations must use `river_job`
	// instead of `schema.river_job`).
	ProcurePool func(ctx context.Context, schema string) (any, string)
	// contains filtered or unexported fields
}

TestSchemaOpts are options for TestSchema. Most of the time these can be left as nil.

type TestTxOpts

type TestTxOpts struct {
	// DisableSchemaSharing prevents test transactions from trying to share a
	// schema, and instead falls back to procuring schemas for each transaction
	// run using TestSchema. This is meant for environments where parallelism
	// doesn't work as well, like SQLite, which will emit "busy" errors when
	// multiple clients try to share a schema, even when they're in separate
	// transactions.
	DisableSchemaSharing bool

	// IsTestTxHelper should be set to true for if TestTx is being called from
	// within a secondary helper that's in a common testing package. This causes
	// an extra stack frame to be skipped when determining the name of the test
	// schema being used for test transactions. So instead of `riverdbtest` or
	// `riverprodbtest` we get the real name of the package being tested (e.g.
	// `river` or `riverpro`).
	IsTestTxHelper bool

	// ProcurePool returns a database pool that will be set to the input driver
	// using Driver.PoolSet. This is an optional parameter and should usually be
	// left unset. It exists for use with SQLite to generate a database pool for
	// use in testing after a test schema is available because unlike other
	// databases, test schemas in SQLite (which are actually test databases) are
	// not available between databases.
	//
	// The input schema is the name of the test schema to use. After the pool's
	// been procured, it'll either be migrated to current version or have its
	// tables truncated, depending on whether it's been reused or not.
	//
	// Returns a database pool (*sql.DB for SQLite) and a "user-facing schema
	// name", which is the schema used in River operations. In practice, this is
	// an empty string for SQLite because unlike other databases, schemas can't
	// be reference by their qualified name (operations must use `river_job`
	// instead of `schema.river_job`).
	ProcurePool func(ctx context.Context, schema string) (any, string)
	// contains filtered or unexported fields
}

TestTxOpts are options for TestTx. Most of the time these can be left as nil.

Jump to

Keyboard shortcuts

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