Documentation
¶
Overview ¶
Package pgsandpit gives each test a PostgreSQL session wrapped in a transaction that is rolled back when the test ends, and hands the test *sql.DB values that run on that session.
var sandpit = pgsandpit.New("postgres://postgres@localhost:5432/postgres")
func TestFoo(t *testing.T) {
t.Parallel()
s := sandpit.Session(t)
db := s.DB()
ro := s.With().ReadOnly().DB()
// db and ro share the session: ro sees what db wrote, and nothing is
// left in the database after the test.
}
Index ¶
Constants ¶
const DefaultTimeout = 10 * time.Second
DefaultTimeout is how long a call waits for the session while another transaction holds it, unless the *sql.DB was built with Builder.Timeout.
Variables ¶
var ( // ErrTimeout is returned, wrapped, when a call waited longer than the // timeout for the session. It usually means a transaction was left open, // or the *sql.DB was used outside a transaction while that transaction was // open in the same goroutine. ErrTimeout = errors.New("pgsandpit: timed out waiting for the session") // ErrSessionClosed is returned when a *sql.DB is used after its session // ended. ErrSessionClosed = errors.New("pgsandpit: session is closed") )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds a *sql.DB for a Session. Each method returns a new Builder, so a Builder can be shared and branched.
func (*Builder) DB ¶
DB returns the *sql.DB. It is closed when the test ends; closing it earlier does not end the session.
type Sandpit ¶
type Sandpit struct {
// contains filtered or unexported fields
}
Sandpit opens sessions on one database. Create it once per test package.
func New ¶
New returns a Sandpit for the database dsn points to. It does not connect until the first Session call.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one PostgreSQL connection with one transaction open on it. Every *sql.DB made from a Session runs on that connection, so they all see each other's changes. Only one statement, or one application transaction, runs on the session at a time; the others wait.