package pqtest
import "github.com/lib/pq/internal/pqtest"
Index ¶
- func Begin(t testing.TB, db *sql.DB) *sql.Tx
- func DB(conninfo ...string) (*sql.DB, error)
- func DSN(conninfo string) string
- func ErrorContains(have error, want string) bool
- func Exec(t testing.TB, db interface { Exec(string, ...any) (sql.Result, error) }, q string, args ...any)
- func ForceBinaryParameters() bool
- func InvalidCertificate(err error) bool
- func MustDB(t testing.TB, conninfo ...string) *sql.DB
- func NormalizeIndent(in string) string
- func Pgbouncer() bool
- func Pgpool() bool
- func Prepare(t testing.TB, db interface { Prepare(string) (*sql.Stmt, error) }, q string) *sql.Stmt
- func Ptr[T any](t T) *T
- func Query[T any](t testing.TB, db interface { Query(string, ...any) (*sql.Rows, error) }, q string, args ...any) []map[string]T
- func Read(t *testing.T, paths ...string) []byte
- func SkipPgbouncer(t testing.TB)
- func SkipPgpool(t testing.TB)
- func TempFile(t *testing.T, name, data string) string
- type Fake
- func NewFake(t testing.TB, fun func(Fake, net.Conn)) Fake
- func (f Fake) Close()
- func (f Fake) DSN() string
- func (f Fake) Host() string
- func (f Fake) Port() string
- func (f Fake) ReadMsg(cn net.Conn) (proto.RequestCode, []byte, bool)
- func (f Fake) ReadStartup(cn net.Conn) (map[string]string, bool)
- func (f Fake) SimpleQuery(cn net.Conn, tag string, values ...any)
- func (f Fake) Startup(cn net.Conn, params map[string]string)
- func (f Fake) WriteMsg(cn net.Conn, code proto.ResponseCode, msg string)
- func (f Fake) WriteStartup(cn net.Conn, params map[string]string)
Functions ¶
func Begin ¶
Begin a new transaction, calling t.Fatal() if this fails.
func DB ¶
DB connects to the test database. The caller must call db.Close().
func DSN ¶
func ErrorContains ¶
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 ¶
InvalidCertificate reports if this error is an "invalid certificate" error.
func MustDB ¶
MustDB connects to the test database, calling t.Fatal() if this fails. The connection is closed in t.Cleanup().
func NormalizeIndent ¶
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 ¶
Prepare a new statement, calling t.Fatal() if this fails.
func Ptr ¶
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 ¶
Read data from a file.
func SkipPgbouncer ¶
func SkipPgpool ¶
func TempFile ¶
TempFile creates a new temporary file and returns the path.
Types ¶
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
func NewFake ¶
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 ¶
DSN is the DSN to connect to for this server.
func (Fake) Host ¶
Host returns the hostname for this server.
func (Fake) Port ¶
Port returns the port for this server.
func (Fake) ReadMsg ¶
ReadMsg reads a message from the client (frontend).
func (Fake) ReadStartup ¶
ReadStartup reads the startup message.
func (Fake) SimpleQuery ¶
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 ¶
Startup reads the startup message from the server with [f.ReadStartup] and sends proto.AuthenticationRequest and proto.ReadyForQuery.
func (Fake) WriteMsg ¶
WriteMsg writes a message to the client (frontend).
func (Fake) WriteStartup ¶
WriteStartup writes startup parameters.
Source Files ¶
- Version
- v1.11.1
- Published
- Jan 29, 2026
- Platform
- windows/amd64
- Imports
- 17 packages
- Last checked
- 1 hour ago –
Tools for package owners.