Documentation
¶
Overview ¶
Package postgrestest provides a test harness that starts an ephemeral PostgreSQL server. PostgreSQL must be installed locally for this package to work.
Example ¶
var t *testing.T // passed into your testing function
// Start up the PostgreSQL server. This can take a few seconds, so better to
// do it once per test run.
ctx := context.Background()
srv, err := postgrestest.Start(ctx)
if err != nil {
t.Fatal(err)
}
t.Cleanup(srv.Cleanup)
// Each of your subtests can have their own database:
t.Run("Test1", func(t *testing.T) {
db, err := srv.NewPGXConn(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(ctx, `CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
t.Fatal(err)
}
// ...
})
t.Run("Test2", func(t *testing.T) {
db, err := srv.NewPGXConn(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(ctx, `CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
t.Fatal(err)
}
// ...
})
Index ¶
- type Server
- func (srv *Server) Cleanup()
- func (srv *Server) CreateDatabase(ctx context.Context) (string, error)
- func (srv *Server) DefaultDatabase() string
- func (srv *Server) NewDatabaseDB(ctx context.Context) (*sql.DB, error)
- func (srv *Server) NewPGXConn(ctx context.Context) (*pgx.Conn, error)
- func (srv *Server) NewPGXPool(ctx context.Context) (*pgxpool.Pool, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
A Server represents a running PostgreSQL server.
func Start ¶
Start starts a PostgreSQL server with an empty database and waits for it to accept connections.
Start looks for the programs "pg_ctl" and "initdb" in PATH. If these are not found, then Start searches for them in /usr/lib/postgresql/*/bin, preferring the highest version found.
func (*Server) Cleanup ¶
func (srv *Server) Cleanup()
Cleanup shuts down the server and deletes any on-disk files the server used.
func (*Server) CreateDatabase ¶
CreateDatabase creates a new database on the server and returns its data source name.
func (*Server) DefaultDatabase ¶
DefaultDatabase returns the data source name of the default "postgres" database.
func (*Server) NewDatabaseDB ¶
NewDatabaseDB opens a connection to a freshly created database on the server.
func (*Server) NewPGXConn ¶
NewPGXConn opens a connection to a freshly created database on the server.