Documentation ¶
Index ¶
- Constants
- func BeginAll() ([]*sql.Tx, error)
- func CreateTestDatabase(ctx context.Context, opts ...db.Option) (*db.Connection, error)
- func DefaultDB() *db.Connection
- func DefaultDBs() []*db.Connection
- func DropTestDatabase(ctx context.Context, conn *db.Connection, opts ...db.Option) (err error)
- func NowRounded(it assertions, locationName string, precision time.Duration) time.Time
- func ResolveDBConfig(ctx context.Context, c *db.Config) error
- func RollbackAll(txs ...*sql.Tx) error
- func ValidatePool(ctx context.Context, pool *db.Connection, hints string) error
- type AlwaysFailDB
- type ErrorProducer
- type Option
- type PseudoQueryDB
- func (pqd *PseudoQueryDB) ExecContext(_ context.Context, _ string, _ ...interface{}) (sql.Result, error)
- func (pqd *PseudoQueryDB) QueryContext(ctx context.Context, _ string, _ ...interface{}) (*sql.Rows, error)
- func (pqd *PseudoQueryDB) QueryRowContext(_ context.Context, _ string, _ ...interface{}) *sql.Row
- type SingleError
- type SliceErrors
- type Suite
- type SuiteAction
Constants ¶
const ( SuiteFailureTests = 1 SuiteFailureBefore = 2 SuiteFailureAfter = 3 )
FailureCodes
Variables ¶
This section is empty.
Functions ¶
func BeginAll ¶
BeginAll begins a transaction in each of the underlying connections. If an error is raised by *any* of the connections, t
func CreateTestDatabase ¶
CreateTestDatabase creates a randomized test database.
func DefaultDB ¶
func DefaultDB() *db.Connection
DefaultDB returns a default database connection for tests.
func DefaultDBs ¶
func DefaultDBs() []*db.Connection
DefaultDBs returns a default set database connections for tests.
func DropTestDatabase ¶
DropTestDatabase drops a database.
func NowRounded ¶ added in v1.20210826.18
NowRounded returns the current time, but rounded to a given precision and then placed into a timezone given by a location name from the IANA Time Zone database (or "Local").
This is useful in situations where timestamps are written to and then read back from a foreign system, like a database. For example, a `TIMESTAMP WITH TIME ZONE` column in `postgres` will truncate to microsecond precision and will return a "bare" timezone even if the timezone written was UTC.
func ResolveDBConfig ¶ added in v1.20210826.18
ResolveDBConfig is intended to be used to help debug issues resolving a `db.Config` from the environment.
In the case of failure, this wraps the `Resolve()` error with a helpful message and a list of all relevant environment variables.
func RollbackAll ¶
RollbackAll calls `Rollback` on a set of transactions.
func ValidatePool ¶ added in v1.20210826.18
ValidatePool validates that - the connection string is valid - the selected `sql` driver can be used - a simple ping can be sent over the connection (is the DB reachable?)
In the case of failure, this tries to diagnose the connection error and produce helpful tips on how to resolve.
Types ¶
type AlwaysFailDB ¶ added in v1.20210826.18
type AlwaysFailDB struct {
Errors ErrorProducer
}
AlwaysFailDB implements the `db.DB` interface, but each method always fails.
func (*AlwaysFailDB) ExecContext ¶ added in v1.20210826.18
ExecContext implements the `db.DB` interface and returns and error.
func (*AlwaysFailDB) QueryContext ¶ added in v1.20210826.18
QueryContext implements the `db.DB` interface and returns and error.
func (*AlwaysFailDB) QueryRowContext ¶ added in v1.20210826.18
QueryRowContext implements the `db.DB` interface; the error value is embedded in the `sql.Row` value returned.
type ErrorProducer ¶ added in v1.20210826.18
type ErrorProducer interface {
NextError() error
}
ErrorProducer is an interface that defines an error factory.
type Option ¶
type Option func(*Suite)
Option is a mutator for a test suite.
func OptWithDefaultDB ¶
func OptWithDefaultDB() Option
OptWithDefaultDB runs a test suite with a dedicated database connection.
func OptWithDefaultDBs ¶
OptWithDefaultDBs runs a test suite with a count of database connections. Note: this type of connection pool is used in rare circumstances for performance reasons; you probably want to use `OptWithDefaultDB` for your tests.
func OptWithStatementLabelRequired ¶ added in v1.20210603.3
func OptWithStatementLabelRequired() Option
OptWithStatementLabelRequired adds a defaultdb interceptor that enforces that statement labels must be present on all statements.
type PseudoQueryDB ¶ added in v1.20210826.18
PseudoQueryDB implements the `db.DB` interface, it intercepts calls to `QueryContext` and replaces the `query` / `args` arguments with custom values.
func (*PseudoQueryDB) ExecContext ¶ added in v1.20210826.18
func (pqd *PseudoQueryDB) ExecContext(_ context.Context, _ string, _ ...interface{}) (sql.Result, error)
ExecContext implements the `db.DB` interface; this is not supported in `PseudoQueryDB`. It will **always** return a "not implemented" error.
func (*PseudoQueryDB) QueryContext ¶ added in v1.20210826.18
func (pqd *PseudoQueryDB) QueryContext(ctx context.Context, _ string, _ ...interface{}) (*sql.Rows, error)
QueryContext implements the `db.DB` interface. It intercepts the **actual** query and arguments and replaces them with the query and arguments stored on the current `PseudoQueryDB`.
func (*PseudoQueryDB) QueryRowContext ¶ added in v1.20210826.18
QueryRowContext implements the `db.DB` interface; this is not supported in `PseudoQueryDB`. It will **always** return a `sql.Row` with a "not implemented" error set on the row.
type SingleError ¶ added in v1.20210826.18
type SingleError struct {
Error error
}
SingleError satisfies ErrorProducer for a single error.
func (*SingleError) NextError ¶ added in v1.20210826.18
func (se *SingleError) NextError() error
NextError produces the "next" error.
type SliceErrors ¶ added in v1.20210826.18
SliceErrors satisfies ErrorProducer for a slice of errors.
func (*SliceErrors) NextError ¶ added in v1.20210826.18
func (se *SliceErrors) NextError() error
NextError produces the "next" error. (This is not concurrency safe.)
type Suite ¶
type Suite struct { M *testing.M Log logger.Log Before []SuiteAction After []SuiteAction }
Suite is a set of before and after actions for a given package tests.
type SuiteAction ¶
SuiteAction is a step that can be run either before or after package tests.