Documentation ¶
Overview ¶
Package pgtest provides support functions for tests that need to use Postgres. Most clients will just call NewTx; those that need more control can call NewDB.
func TestSimple(t *testing.T) { dbtx := pgtest.NewTx(t) ... } func TestComplex(t *testing.T) { _, db := pgtest.NewDB(t, pgtest.SchemaPath) ... dbtx, err := db.Begin(ctx) ... }
Prefer NewTx when the caller (usually a test function) can run in exactly one transaction. It's significantly faster than NewDB.
Index ¶
- Constants
- Variables
- func CloneDB(ctx context.Context, baseURL string) (newURL string, err error)
- func Dump(t testing.TB, dbURL string, includeSchema bool, excludingTables ...string) string
- func Exec(ctx context.Context, db pg.DB, t testing.TB, q string, args ...interface{})
- func NewDB(t testing.TB, schemaPath string) (url string, db *sql.DB)
- func NewTx(t testing.TB) *sql.Tx
- func WrapDB(t testing.TB, url string, wrapFn func(string)) *sql.DB
Constants ¶
const DefaultURL = "postgres:///postgres?sslmode=disable"
DefaultURL is used by NewTX and NewDB if DBURL is the empty string.
Variables ¶
var ( // DBURL should be a URL of the form "postgres://...". // If it is the empty string, DefaultURL will be used. // The functions NewTx and NewDB use it to create and connect // to new databases by replacing the database name component // with a randomized name. DBURL = os.Getenv("DB_URL_TEST") // SchemaPath is a file containing a schema to initialize // a database in NewTx. SchemaPath = os.Getenv("CHAIN") + "/core/schema.sql" )
Functions ¶
func CloneDB ¶
CloneDB creates a new database, using the database at the provided URL as a template. It returns the URL of the database clone.
func Exec ¶
Exec executes q in the database or transaction in ctx. If there is an error, it fails t.
func NewDB ¶
NewDB creates a database initialized with the schema in schemaPath. It returns the resulting *sql.DB with its URL.
It also registers a finalizer for the DB, so callers can discard it without closing it explicitly, and the test program is nevertheless unlikely to run out of connection slots in the server.
Prefer NewTx whenever the caller can do its work in exactly one transaction.
func NewTx ¶
NewTx returns a new transaction on a database initialized with the schema in SchemaPath.
It also registers a finalizer for the Tx, so callers can discard it without rolling back explicitly, and the test program is nevertheless unlikely to run out of connection slots in the server. The caller should not commit the returned Tx; doing so will prevent the underlying database from being reused and so cause future calls to NewTx to be slower.
Types ¶
This section is empty.