...

Package pgxtest

import "github.com/jackc/pgx/v5/pgxtest"
Overview
Index

Overview ▾

Package pgxtest provides utilities for testing pgx and packages that integrate with pgx.

Variables

var AllQueryExecModes = []pgx.QueryExecMode{
    pgx.QueryExecModeCacheStatement,
    pgx.QueryExecModeCacheDescribe,
    pgx.QueryExecModeDescribeExec,
    pgx.QueryExecModeExec,
    pgx.QueryExecModeSimpleProtocol,
}

KnownOIDQueryExecModes is a slice of all query exec modes where the param and result OIDs are known before sending the query.

var KnownOIDQueryExecModes = []pgx.QueryExecMode{
    pgx.QueryExecModeCacheStatement,
    pgx.QueryExecModeCacheDescribe,
    pgx.QueryExecModeDescribeExec,
}

func RunValueRoundTripTests

func RunValueRoundTripTests(
    ctx context.Context,
    t testing.TB,
    ctr ConnTestRunner,
    modes []pgx.QueryExecMode,
    pgTypeName string,
    tests []ValueRoundTripTest,
)

func RunWithQueryExecModes

func RunWithQueryExecModes(ctx context.Context, t *testing.T, ctr ConnTestRunner, modes []pgx.QueryExecMode, f func(ctx context.Context, t testing.TB, conn *pgx.Conn))

RunWithQueryExecModes runs a f in a new test for each element of modes with a new connection created using connector. If modes is nil all pgx.QueryExecModes are tested.

func SkipCockroachDB

func SkipCockroachDB(t testing.TB, conn *pgx.Conn, msg string)

SkipCockroachDB calls Skip on t with msg if the connection is to a CockroachDB server.

func SkipPostgreSQLVersionLessThan

func SkipPostgreSQLVersionLessThan(t testing.TB, conn *pgx.Conn, minVersion int64)

type ConnTestRunner

ConnTestRunner controls how a *pgx.Conn is created and closed by tests. All fields are required. Use DefaultConnTestRunner to get a ConnTestRunner with reasonable default values.

type ConnTestRunner struct {
    // CreateConfig returns a *pgx.ConnConfig suitable for use with pgx.ConnectConfig.
    CreateConfig func(ctx context.Context, t testing.TB) *pgx.ConnConfig

    // AfterConnect is called after conn is established. It allows for arbitrary connection setup before a test begins.
    AfterConnect func(ctx context.Context, t testing.TB, conn *pgx.Conn)

    // AfterTest is called after the test is run. It allows for validating the state of the connection before it is closed.
    AfterTest func(ctx context.Context, t testing.TB, conn *pgx.Conn)

    // CloseConn closes conn.
    CloseConn func(ctx context.Context, t testing.TB, conn *pgx.Conn)
}

func DefaultConnTestRunner

func DefaultConnTestRunner() ConnTestRunner

DefaultConnTestRunner returns a new ConnTestRunner with all fields set to reasonable default values.

func (*ConnTestRunner) RunTest

func (ctr *ConnTestRunner) RunTest(ctx context.Context, t testing.TB, f func(ctx context.Context, t testing.TB, conn *pgx.Conn))

type ValueRoundTripTest

type ValueRoundTripTest struct {
    Param  any
    Result any
    Test   func(any) bool
}