Help us shape the future of Go! Take the 2026 Go Developer Survey

dbtest

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package dbtest runs the CMS's store tests against real database engines in throwaway containers.

The suite exists because the CMS speaks SQL to more than one engine: a test that passes on Postgres proves nothing about MySQL. Each registers a subtest per engine, so a single test body becomes the conformance check for every engine the CMS claims to support.

Tests written against this harness should drive stores through their public API rather than raw SQL. A test that reaches for SQL directly has to be written once per dialect, which is exactly the duplication the harness is meant to remove.

Containers are started lazily, once per engine per test process, and shared by every test in the package. Between tests, Truncate empties the cms_ tables — far cheaper than a fresh database per test. When Docker is not available the whole suite skips rather than fails, so `go test ./...` still passes on a machine without it.

Index

Constants

This section is empty.

Variables

View Source
var MySQLEngines = []Engine{MySQL, MariaDB}

MySQLEngines are the engines that speak the MySQL dialect.

Functions

func Each

func Each(t *testing.T, fn func(t *testing.T, db *sqldb.DB))

Each runs fn as a subtest against every engine under test, handing it a migrated, empty database. Use it as the entry point for any test that touches storage:

func TestPageInsert(t *testing.T) {
    dbtest.Each(t, func(t *testing.T, db *sqldb.DB) {
        store := content.NewStore(db, "en")
        ...
    })
}

func OpenMySQL added in v1.2.5

func OpenMySQL(t *testing.T, engine Engine, params ...string) *sqldb.DB

OpenMySQL opens a second pool on engine's container — MySQL or MariaDB — with exactly the DSN parameters given, for tests of what the CMS does with a DSN that lacks some of mysqlParams. The schema is the shared pool's, already migrated. The pool closes when the test ends.

func RequiredMySQLParams added in v1.2.5

func RequiredMySQLParams() []string

RequiredMySQLParams returns a copy of the DSN parameters every MySQL pool the CMS uses must carry, for a test to drop one of.

func SkipWithoutDocker added in v0.9.5

func SkipWithoutDocker(t *testing.T)

SkipWithoutDocker skips the calling test when there is no usable Docker daemon, so the unit-test suite still runs on machines without one.

func Truncate

func Truncate(t *testing.T, db *sqldb.DB)

Truncate empties every cms_ table, leaving the schema in place. The table list is read from the database rather than hardcoded, so a new migration needs no change here.

Types

type Engine

type Engine string

Engine names one database engine under test.

const (
	Postgres Engine = "postgres"
	MySQL    Engine = "mysql"
	MariaDB  Engine = "mariadb"
)

Jump to

Keyboard shortcuts

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