testutil

package
v1.20210908.5 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	SuiteFailureTests  = 1
	SuiteFailureBefore = 2
	SuiteFailureAfter  = 3
)

FailureCodes

Variables

This section is empty.

Functions

func BeginAll

func BeginAll() ([]*sql.Tx, error)

BeginAll begins a transaction in each of the underlying connections. If an error is raised by *any* of the connections, t

func CreateTestDatabase

func CreateTestDatabase(ctx context.Context, opts ...db.Option) (*db.Connection, error)

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

func DropTestDatabase(ctx context.Context, conn *db.Connection, opts ...db.Option) (err error)

DropTestDatabase drops a database.

func NowRounded added in v1.20210826.18

func NowRounded(it assertions, locationName string, precision time.Duration) time.Time

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

func ResolveDBConfig(ctx context.Context, c *db.Config) error

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

func RollbackAll(txs ...*sql.Tx) error

RollbackAll calls `Rollback` on a set of transactions.

func ValidatePool added in v1.20210826.18

func ValidatePool(ctx context.Context, pool *db.Connection, hints string) error

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

func (afd *AlwaysFailDB) ExecContext(context.Context, string, ...interface{}) (sql.Result, error)

ExecContext implements the `db.DB` interface and returns and error.

func (*AlwaysFailDB) QueryContext added in v1.20210826.18

func (afd *AlwaysFailDB) QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)

QueryContext implements the `db.DB` interface and returns and error.

func (*AlwaysFailDB) QueryRowContext added in v1.20210826.18

func (afd *AlwaysFailDB) QueryRowContext(context.Context, string, ...interface{}) *sql.Row

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 OptAfter

func OptAfter(steps ...SuiteAction) Option

OptAfter appends after run actions.

func OptBefore

func OptBefore(steps ...SuiteAction) Option

OptBefore appends before run actions.

func OptLog

func OptLog(log logger.Log) Option

OptLog sets the suite logger.

func OptWithDefaultDB

func OptWithDefaultDB() Option

OptWithDefaultDB runs a test suite with a dedicated database connection.

func OptWithDefaultDBs

func OptWithDefaultDBs(count int) Option

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

type PseudoQueryDB struct {
	DB    *sql.DB
	Query string
	Args  []interface{}
}

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

func (pqd *PseudoQueryDB) QueryRowContext(_ context.Context, _ string, _ ...interface{}) *sql.Row

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

type SliceErrors struct {
	Errors []error
	Index  int
}

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.

func New

func New(m *testing.M, opts ...Option) *Suite

New returns a new test suite.

func (Suite) Run

func (s Suite) Run()

Run runs tests and returns the exit code.

type SuiteAction

type SuiteAction func(context.Context) error

SuiteAction is a step that can be run either before or after package tests.

Jump to

Keyboard shortcuts

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