postgrestest

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package postgrestest is a small fake PostgreSQL server for tests, in this package and in others. It speaks the simple query protocol only, so a test must use the driver's default query_mode (simple). It does not parse SQL: the test gives it a Handler that answers each SQL text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Name string
	OID  uint32
}

Column is one column of a Result. OID is the PostgreSQL type, such as 25 for text, 23 for int4 and 16 for bool. Use Text and Int for the usual ones.

func Int

func Int(name string) Column

Int makes an int4 column.

func Text

func Text(name string) Column

Text and Int make a text column and an int4 column.

type Handler

type Handler func(sql string) []Result

Handler answers one query text. It returns one Result for each statement in the text. No result at all means an empty query.

type Result

type Result struct {
	Columns []Column
	Rows    [][]any
	// Tag is the command tag, such as "SELECT 2" or "UPDATE 1".
	Tag string
	// ErrCode and ErrMessage, when set, make the statement fail. The statements
	// after it in the same query are not run.
	ErrCode    string
	ErrMessage string
}

Result is the answer to one statement. A cell is a string, or nil for NULL.

type Server

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

Server is a running fake server.

func Start

func Start(t testing.TB, h Handler) *Server

Start starts a server on a free local port and stops it when the test ends.

func StartWithPassword

func StartWithPassword(t testing.TB, h Handler, password string) *Server

StartWithPassword is Start, but a client must log in with this password.

func (*Server) Addr

func (s *Server) Addr() string

Addr is host:port.

func (*Server) ConnCount

func (s *Server) ConnCount() int

ConnCount is how many connections have been opened.

func (*Server) Queries

func (s *Server) Queries() []string

Queries are the SQL texts received, in order.

func (*Server) Startups

func (s *Server) Startups() []map[string]string

Startups are the startup parameters of each connection (user, database, application_name, and any other setting the client sent).

func (*Server) URL

func (s *Server) URL() string

URL is the target to give the driver.

Jump to

Keyboard shortcuts

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