testdb

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package testdb spins up a real database instance (TiDB, MySQL, or PostgreSQL) for integration tests.

Tests use CHETTER_TEST_DSN to point at an existing database (e.g. a CI service container). When that env var is unset, the package starts a one-shot Docker container for the test run and tears it down on exit.

Set CHETTER_TEST_DB_DIALECT to "mysql" or "postgres" to use that container instead of the default TiDB container.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PackageDB

type PackageDB struct {
	// contains filtered or unexported fields
}

PackageDB manages a single database Docker container shared across all tests in one package. Use StartPackageDB in TestMain for fast integration tests.

func StartPackageDB

func StartPackageDB(m *testing.M) *PackageDB

StartPackageDB starts a database container once per package. Call from TestMain. Pass *testing.M so cleanup runs after all tests complete via os.Exit.

func (*PackageDB) AdminDB

func (p *PackageDB) AdminDB(t testing.TB) *sql.DB

AdminDB returns a connection to the database server for creating test databases.

func (*PackageDB) Close

func (p *PackageDB) Close()

Close shuts down the shared TiDB container.

func (*PackageDB) NewTestDB

func (p *PackageDB) NewTestDB(t testing.TB) (*TestDB, func())

NewTestDB creates a new isolated test database on the shared container, applies the schema, and returns a TestDB ready for test use.

type TestDB

type TestDB struct {
	DB  *sql.DB
	DSN string
	// contains filtered or unexported fields
}

TestDB owns a connection to a real database instance plus the cleanup hooks the test should defer.

func NewForTesting

func NewForTesting(t *testing.T) (*TestDB, func())

NewForTesting prepares a fresh, isolated database on a TiDB instance. The returned TestDB is ready to use and the cleanup func drops the database and tears down the Docker container when one was started.

func PostgresSchemaParityDBs

func PostgresSchemaParityDBs(t testing.TB) (bootstrapDB, gooseDB *TestDB, cleanup func())

PostgresSchemaParityDBs creates two PostgreSQL databases on the same server: one with the runtime bootstrap schema (via store.ApplySchema) and one with Goose migrations. Use this to compare the two schema paths for drift.

func (*TestDB) DatabaseName

func (tdb *TestDB) DatabaseName() string

DatabaseName returns the per-run database name.

func (*TestDB) Dialect

func (tdb *TestDB) Dialect() store.Dialect

Dialect returns the database dialect for this test database.

func (*TestDB) OpenStore

func (tdb *TestDB) OpenStore(t *testing.T) (*store.Store, func())

OpenStore returns a *store.Store backed by this test database. Useful for tests that exercise the legacy store API alongside the new sqlc repository.

func (*TestDB) Truncate

func (tdb *TestDB) Truncate(t *testing.T)

Truncate wipes all rows from chetter tables but keeps the schema.

Jump to

Keyboard shortcuts

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