testutils

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testutils contains some common utilities used exclusively by the test suite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateUniqueTestDatabase

func CreateUniqueTestDatabase(t *testing.T) (string, *sql.DB)

CreateUniqueTestDatabase creates a unique database for a test and returns both the database name and a *sql.DB connection scoped to that database. The connection and database are automatically cleaned up when the test finishes.

func DSN

func DSN() string

func DSNForDatabase

func DSNForDatabase(dbName string) string

DSNForDatabase returns a DSN for a specific database name

func EvenOddHasher added in v0.10.2

func EvenOddHasher(colAny any) (uint64, error)

EvenOddHasher is a test hash function that shards assuming -80 and 80- shards. even goes to -80, odd goes to 80-

func RunSQL

func RunSQL(t *testing.T, stmt string)

func RunSQLInDatabase

func RunSQLInDatabase(t *testing.T, dbName, stmt string)

RunSQLInDatabase runs SQL in a specific database

func WaitForReplicaHealthy added in v0.13.0

func WaitForReplicaHealthy(t *testing.T, dsn string, timeout time.Duration)

WaitForReplicaHealthy polls SHOW REPLICA STATUS until both the IO and SQL threads report Yes, or the timeout elapses. On timeout it fails the test with an infra-attribution message so a broken CI replica setup is clearly distinguishable from a Spirit migration bug.

Types

type TestTable added in v0.13.0

type TestTable struct {
	Name string
	DB   *sql.DB
}

TestTable manages a test table's lifecycle: creation, cleanup, and provides a DB connection for verification queries after migration.

func NewTestTable added in v0.13.0

func NewTestTable(t *testing.T, name string, createSQL string) *TestTable

NewTestTable creates a test table and registers cleanup to drop it and all Spirit artifacts (_new, _old, _chkpnt) when the test finishes.

Example:

tt := testutils.NewTestTable(t, "mytable",
    `CREATE TABLE mytable (
        id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL
    )`)

// Use tt.DB for verification queries after migration
var count int
tt.DB.QueryRowContext(t.Context(), "SELECT COUNT(*) FROM mytable").Scan(&count)

func (*TestTable) SeedRows added in v0.13.0

func (tt *TestTable) SeedRows(t *testing.T, insertSelectSQL string, targetRows int)

SeedRows populates the table by doubling rows until reaching approximately targetRows. The insertSelectSQL should be an INSERT INTO ... SELECT statement WITHOUT a FROM clause. SeedRows appends "FROM dual" for the initial insert, then "FROM <table>" for each doubling iteration.

Example:

// Simple seeding — produces ~4096 identical rows (different auto-increment IDs)
tt.SeedRows(t, "INSERT INTO mytable (name, val) SELECT 'seed', 1", 4096)

// With SQL functions — each row gets unique random data
tt.SeedRows(t, "INSERT INTO mytable (pad) SELECT RANDOM_BYTES(1024)", 100000)

Jump to

Keyboard shortcuts

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