databasetest

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package databasetest provides a conformance suite for database.Driver implementations.

Run executes the documented behavior of the contract against a driver the caller supplies: affected-row counts, positional and named queries, cursors, database.ErrNoRows mapping, sessions, transactions, locks, and context cancellation. Session checks require one physical connection across transactions, lexical handle/cursor expiration, automatic lock cleanup, and non-reentrant ownership that survives nested rollback. Use a pool with enough capacity for independent contenders; retained scopes intentionally hold theirs. A schema suite also creates a table with every portable column type from [schema.Create], writes a Go value through the query builder, and requires the same value back. Each subtest gets a fresh driver and its own uniquely named table, created and dropped with portable SQL, so the suite runs against any database, including the SQLite driver used for development and tests.

Positional placeholders are written as ? and rewritten for the dialect the driver reports through database.Driver.Dialect; a driver whose dialect is [query.ANSI] must accept ?.

A typical driver test:

func TestConformance(t *testing.T) {
	databasetest.Run(t, func(t *testing.T) database.Driver {
		driver, err := sqlite.New(":memory:")
		if err != nil {
			t.Fatal(err)
		}
		t.Cleanup(func() {
			_ = driver.Stop(context.Background())
		})

		return driver
	})
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(t *testing.T, newDriver func(t *testing.T) database.Driver, opts ...Option)

Run executes the conformance suite against drivers returned by newDriver.

newDriver is called once per subtest and must return a driver connected to an empty or disposable database, registering any cleanup on t. Run creates and drops its own tables and never touches other ones.

Run reports every deviation with t.Errorf or t.Fatalf in a subtest named after the behavior.

Example:

databasetest.Run(t, func(t *testing.T) database.Driver {
	return newTestDriver(t)
}, databasetest.WithTimeout(time.Minute))
Example

ExampleRun shows a driver test running the conformance suite. The factory connects to a disposable database and registers its cleanup with t.Cleanup.

package main

import (
	"testing"

	"arkoren.dev/core/service/database"
	"arkoren.dev/core/service/database/databasetest"
)

func main() {
	_ = func(t *testing.T, factory func(*testing.T) database.Driver) {
		databasetest.Run(t, factory)
	}
}

Types

type Option

type Option func(*settings)

Option configures Run.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout bounds every subtest's operations, for slow remote databases. A zero or negative timeout keeps the default of 30 seconds.

Example

ExampleWithTimeout shows how to give a slow remote database more time.

package main

import (
	"testing"
	"time"

	"arkoren.dev/core/service/database"
	"arkoren.dev/core/service/database/databasetest"
)

func main() {
	_ = func(t *testing.T, factory func(*testing.T) database.Driver) {
		databasetest.Run(t, factory, databasetest.WithTimeout(2*time.Minute))
	}
}

Directories

Path Synopsis
internal
suite
Package suite implements database driver checks and their private fixtures.
Package suite implements database driver checks and their private fixtures.

Jump to

Keyboard shortcuts

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