The test package provides utilities for integration testing with PostgreSQL using testcontainers-go. It automatically starts a PostgreSQL container, creates a connection pool, and cleans up after tests complete.
For test suites that share a single container across all tests in a package:
package mypackage_test
import (
"testing"
pg "github.com/mutablelogic/go-pg"
pgtest "github.com/mutablelogic/go-pg/pkg/test"
)
var conn pgtest.Conn
func TestMain(m *testing.M) {
pgtest.Main(m, func(pool pg.PoolConn) (func(), error) {
conn = pgtest.Conn{PoolConn: pool}
return nil, nil
})
}
func TestSomething(t *testing.T) {
c := conn.Begin(t)
defer c.Close()
// Use c.PoolConn for database operations
err := c.Exec(ctx, "CREATE TABLE test (id SERIAL PRIMARY KEY)")
// ...
}For isolated tests that need their own container:
func TestWithManager(t *testing.T) {
mgr := pgtest.NewManager(t)
defer mgr.Close()
// Use mgr.Manager for PostgreSQL management operations
roles, err := mgr.ListRoles(ctx, schema.RoleListRequest{})
// ...
}When creating containers directly, you can customize the configuration:
container, err := pgtest.NewContainer(ctx, "mytest", "postgres:16",
pgtest.WithEnv("POSTGRES_PASSWORD", "secret"),
pgtest.WithEnv("POSTGRES_DB", "testdb"),
pgtest.WithPort("5432/tcp"),
pgtest.WithWaitLog("database system is ready to accept connections"),
)| Option | Description |
|---|---|
WithEnv(key, value) |
Set environment variable |
WithPort(port) |
Expose a port (e.g., "5432/tcp") |
WithWaitLog(message) |
Wait for log message before considering container ready |
WithWaitPort(port) |
Wait for port to be available |
The NewPgxContainer function provides a preconfigured PostgreSQL container:
container, pool, err := pgtest.NewPgxContainer(ctx, "mytest", verbose, traceFn)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
defer container.Close(ctx)With optional schema search path:
container, pool, err := pgtest.NewPgxContainer(ctx, "mytest", verbose, traceFn, "myschema", "public")
if err != nil {
t.Fatal(err)
}
defer pool.Close()
defer container.Close(ctx)Parameters:
ctx- Context with timeoutname- Container name prefix (timestamp is appended)verbose- Enable verbose SQL loggingtraceFn- Optional trace function for SQL queriessearchPath- Optional variadic parameter to set schema search path
When running tests with -v, SQL queries are logged:
go test -v ./...The trace function receives all executed SQL with arguments and any errors.