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

testdb

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package testdb gives each test a fresh database for the run's Test dialect, so one test suite runs unchanged against SQLite, PostgreSQL or MySQL.

The run's dialect comes from one variable, TANGO_TEST_DSN, read by its scheme through db.ParseDSN, the same grammar an app's TANGO_DB_DSN uses:

unset or sqlite://:memory:   SQLite, in memory
sqlite://<any path>          SQLite, a new file per test under t.TempDir()
postgres://… postgresql://…  PostgreSQL, a new schema per test
mysql://…                    MySQL, a new database per test

One scheme-qualified variable, rather than a dialect variable beside a DSN, means the two can never disagree, and anyone who can configure an app already knows how to point its tests somewhere. The SQLite file path only selects file-backed mode; it is never opened, because a file shared between tests would leak rows from one into the next.

A MySQL run creates a database for each Open and drops it when the test ends, so the account in the DSN needs the CREATE and DROP privileges; raw SQL written with numbered placeholders is rewritten to "?" for it.

A PostgreSQL run gets a schema created for each Open, set as every connection's search_path and dropped with CASCADE when the test ends, so tests may commit, run DDL and run in parallel without seeing each other. An unreachable server or a malformed DSN fails the test: a PostgreSQL or MySQL run never passes by skipping. Tests that cannot run on one dialect say so with SQLiteOnly, PostgresOnly or MySQLOnly and a reason, so every exemption is greppable.

testdb imports the modernc.org/sqlite, pgx and MySQL drivers itself, so tests need no driver imports of their own.

Raw SQL in a test can use PostgreSQL's numbered placeholders ($1, $2, …) and TRUE/FALSE on every dialect: modernc.org/sqlite binds $N by number, and a MySQL run rewrites them to "?".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dialect

func Dialect() db.Dialect

Dialect reports the run's Test dialect. It has no test to fail, so an unsupported TANGO_TEST_DSN panics with the message Open, Store, SQLiteOnly and PostgresOnly fail the test with.

func MySQLOnly added in v0.6.0

func MySQLOnly(t testing.TB, reason string)

MySQLOnly skips the test unless the run's Test dialect is MySQL. The reason says what the test needs that SQLite and PostgreSQL cannot give it.

func NotMySQL added in v0.6.0

func NotMySQL(t testing.TB, reason string)

NotMySQL skips the test when the run's Test dialect is MySQL. The reason says what the test needs that MySQL cannot give it.

func Open

func Open(t testing.TB) (*sql.DB, db.Dialect)

Open returns a fresh database for this test and the Dialect to use with it.

func PostgresOnly

func PostgresOnly(t testing.TB, reason string)

PostgresOnly skips the test unless the run's Test dialect is PostgreSQL. The reason says what the test needs that the other dialects cannot give it.

func SQLiteOnly

func SQLiteOnly(t testing.TB, reason string)

SQLiteOnly skips the test unless the run's Test dialect is SQLite. The reason says what the test needs that the other dialects cannot give it.

func Store

func Store(t testing.TB) *db.Store

Store returns a db.Store over a fresh database for this test.

Types

This section is empty.

Jump to

Keyboard shortcuts

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