package pqtest
import "github.com/lib/pq/internal/pqtest"
Index ¶
- func Begin(t testing.TB, db *sql.DB) *sql.Tx
- func Chmod(t *testing.T, mode fs.FileMode, paths ...string)
- func DB(t testing.TB, 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 Home(t *testing.T) string
- 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 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
- func Write(t *testing.T, data []byte, paths ...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) (float32, 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) WriteBackendKeyData(cn net.Conn, pid int, secretKey []byte)
- func (f Fake) WriteMsg(cn net.Conn, code proto.ResponseCode, msg string)
- func (f Fake) WriteNegotiateProtocolVersion(cn net.Conn, newestMinor int, options []string)
- func (f Fake) WriteStartup(cn net.Conn, params map[string]string)
- type Stmt
Functions ¶
func Begin ¶
Begin a new transaction, calling t.Fatal() if this fails.
func Chmod ¶
Chmod a file
func DB ¶
DB connects to the test database and returns the Ping error. The connection is closed in t.Cleanup().
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 Home ¶
Home sets the HOME directory to a temporary directly and makes sure the .postgresql directory exists.
func InvalidCertificate ¶
InvalidCertificate reports if this error is an "invalid certificate" error.
func MustDB ¶
MustDB connects and pings 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 Ptr ¶
func Ptr[T any](t T) *T
Ptr gets a pointer to any value.
TODO(go1.26): 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.
func Write ¶
Write data to a file.
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) WriteBackendKeyData ¶
WriteBackendKeyData sends a BackendKeyData message with the given process ID and secret key (variable length).
func (Fake) WriteMsg ¶
WriteMsg writes a message to the client (frontend).
func (Fake) WriteNegotiateProtocolVersion ¶
WriteNegotiateProtocolVersion sends a NegotiateProtocolVersion message.
func (Fake) WriteStartup ¶
WriteStartup writes startup parameters.
type Stmt ¶
func Prepare ¶
Prepare a new statement, calling t.Fatal() if this fails.
func (*Stmt) MustClose ¶
MustClose calls Stmt.Close(), calling t.Fatal if this fails.
func (*Stmt) MustExec ¶
MustExec calls Stmt.Exec(), calling t.Fatal if this fails.
Source Files ¶
- Version
- v1.12.0
- Published
- Mar 18, 2026
- Platform
- darwin/amd64
- Imports
- 19 packages
- Last checked
- 2 hours ago –
Tools for package owners.