Documentation
¶
Overview ¶
Package testutils contains some common utilities used exclusively by the test suite.
Index ¶
- func CreateUniqueTestDatabase(t *testing.T) (string, *sql.DB)
- func DSN() string
- func DSNForDatabase(dbName string) string
- func EvenOddHasher(colAny any) (uint64, error)
- func RunSQL(t *testing.T, stmt string)
- func RunSQLInDatabase(t *testing.T, dbName, stmt string)
- func WaitForReplicaHealthy(t *testing.T, dsn string, timeout time.Duration)
- type TestTable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateUniqueTestDatabase ¶
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 DSNForDatabase ¶
DSNForDatabase returns a DSN for a specific database name
func EvenOddHasher ¶ added in v0.10.2
EvenOddHasher is a test hash function that shards assuming -80 and 80- shards. even goes to -80, odd goes to 80-
func RunSQLInDatabase ¶
RunSQLInDatabase runs SQL in a specific database
func WaitForReplicaHealthy ¶ added in v0.13.0
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
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
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
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)