pqtest

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin

func Begin(t testing.TB, db *sql.DB) *sql.Tx

Begin a new transaction, calling t.Fatal() if this fails.

func DB

func DB(conninfo ...string) (*sql.DB, error)

DB connects to the test database. The caller must call db.Close().

func DSN

func DSN(conninfo string) string

func ErrorContains

func ErrorContains(have error, want string) bool

ErrorContains checks if the error message in have contains the text in want.

This is safe when have is nil. Use an empty string for want if you want to test that err is nil.

func Exec

func Exec(t testing.TB, db interface {
	Exec(string, ...any) (sql.Result, error)
}, q string, args ...any)

Exec calls db.Exec(), calling t.Fatal if this fails.

func ForceBinaryParameters

func ForceBinaryParameters() bool

func InvalidCertificate

func InvalidCertificate(err error) bool

InvalidCertificate reports if this error is an "invalid certificate" error.

func MustDB

func MustDB(t testing.TB, conninfo ...string) *sql.DB

MustDB connects to the test database, calling t.Fatal() if this fails. The connection is closed in t.Cleanup().

func NormalizeIndent

func NormalizeIndent(in string) string

NormalizeIndent removes tab indentation from every line.

This is useful for "inline" multiline strings:

  cases := []struct {
      string in
  }{
      `
	 	    Hello,
	 	    world!
      `,
  }

This is nice and readable, but the downside is that every line will now have two extra tabs. This will remove those two tabs from every line.

The amount of tabs to remove is based only on the first line, any further tabs will be preserved.

func Pgbouncer

func Pgbouncer() bool

func Pgpool

func Pgpool() bool

func Prepare

func Prepare(t testing.TB, db interface {
	Prepare(string) (*sql.Stmt, error)
}, q string) *sql.Stmt

Prepare a new statement, calling t.Fatal() if this fails.

func Ptr added in v1.11.1

func Ptr[T any](t T) *T

Ptr gets a pointer to any value.

TODO: replace with new(..) once pq requires Go 1.26.

func Query

func Query[T any](t testing.TB, db interface {
	Query(string, ...any) (*sql.Rows, error)
}, q string, args ...any) []map[string]T

Query calls db.Query(), calling t.Fatal if this fails.

The resulting rows are scanned to the type T.

func Read

func Read(t *testing.T, paths ...string) []byte

Read data from a file.

func SkipPgbouncer

func SkipPgbouncer(t testing.TB)

func SkipPgpool

func SkipPgpool(t testing.TB)

func TempFile

func TempFile(t *testing.T, name, data string) string

TempFile creates a new temporary file and returns the path.

Types

type Fake

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

func NewFake

func NewFake(t testing.TB, fun func(Fake, net.Conn)) Fake

NewFake creates a new "fake" PostgreSQL server. You need to accept connections with [Fake.Accept].

This can also be tested against libpq with something like:

f := pqtest.NewFake(t)
f.Accept(..)

fmt.Println("\n" + f.DSN())
time.Sleep(9 * time.Minute)

func (Fake) Close

func (f Fake) Close()

func (Fake) DSN

func (f Fake) DSN() string

DSN is the DSN to connect to for this server.

func (Fake) Host

func (f Fake) Host() string

Host returns the hostname for this server.

func (Fake) Port

func (f Fake) Port() string

Port returns the port for this server.

func (Fake) ReadMsg

func (f Fake) ReadMsg(cn net.Conn) (proto.RequestCode, []byte, bool)

ReadMsg reads a message from the client (frontend).

func (Fake) ReadStartup

func (f Fake) ReadStartup(cn net.Conn) (map[string]string, bool)

ReadStartup reads the startup message.

func (Fake) SimpleQuery

func (f Fake) SimpleQuery(cn net.Conn, tag string, values ...any)

SimpleQuery responds to a simpleQuery workflow; values are as a col, value pair:

f.SimpleQuery(cn, "SELECT",
	"colname", "val",
	"int", 2)

Currently only supports string, int, and bool for values

func (Fake) Startup

func (f Fake) Startup(cn net.Conn, params map[string]string)

Startup reads the startup message from the server with [f.ReadStartup] and sends proto.AuthenticationRequest and proto.ReadyForQuery.

func (Fake) WriteMsg

func (f Fake) WriteMsg(cn net.Conn, code proto.ResponseCode, msg string)

WriteMsg writes a message to the client (frontend).

func (Fake) WriteStartup

func (f Fake) WriteStartup(cn net.Conn, params map[string]string)

WriteStartup writes startup parameters.

Jump to

Keyboard shortcuts

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