testdb

package
v14.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfig added in v14.7.0

func GetConfig(t testing.TB, database string) config.DB

GetConfig returns the database configuration determined by environment variables. See NewDB() for the list of variables.

func SetHealthyNodes

func SetHealthyNodes(t testing.TB, ctx context.Context, db glsql.Querier, healthyNodes map[string]map[string][]string)

SetHealthyNodes sets the healthy nodes in the database as determined by the passed in map. The healthyNodes map is keyed by praefect name -> virtual storage -> storage. On each run, it clears all previous health checks from the table, so the passed in nodes are the only ones considered healthy after the function. As the healthy nodes are determined by the time of the last successful health check, this should be run in the same transastion as the tested query to prevent flakiness.

func SetMigrations added in v14.8.0

func SetMigrations(t testing.TB, db DB, cfg config.Config, up int)

SetMigrations ensures the requested number of migrations are up and the remainder are down.

func WaitForBlockedQuery added in v14.7.0

func WaitForBlockedQuery(ctx context.Context, t testing.TB, db glsql.Querier, queryPrefix string)

WaitForBlockedQuery is a helper that waits until a blocked query matching the prefix is present in the database. This is useful for ensuring another transaction is blocking a query when testing concurrent execution of multiple queries.

Types

type DB added in v14.7.0

type DB struct {
	*sql.DB
	// Name is a name of the database.
	Name string
}

DB is a helper struct that should be used only for testing purposes.

func New added in v14.7.0

func New(t testing.TB) DB

New returns a wrapper around the database connection pool. Must be used only for testing. The new database with empty relations will be created for each call of this function. It uses env vars:

PGHOST - required, URL/socket/dir
PGPORT - required, binding port
PGUSER - optional, user - `$ whoami` would be used if not provided

Once the test is completed the database will be dropped on test cleanup execution.

func (DB) Begin added in v14.7.0

func (db DB) Begin(t testing.TB) *TxWrapper

Begin starts a new transaction and returns it wrapped into TxWrapper.

func (DB) Close added in v14.7.0

func (db DB) Close() error

Close removes schema if it was used and releases connection pool.

func (DB) MustExec added in v14.7.0

func (db DB) MustExec(t testing.TB, q string, args ...interface{})

MustExec executes `q` with `args` and verifies there are no errors.

func (DB) RequireRowsInTable added in v14.7.0

func (db DB) RequireRowsInTable(t *testing.T, tname string, n int)

RequireRowsInTable verifies that `tname` table has `n` amount of rows in it.

func (DB) Truncate added in v14.7.0

func (db DB) Truncate(t testing.TB, tables ...string)

Truncate removes all data from the list of tables and restarts identities for them.

func (DB) TruncateAll added in v14.7.0

func (db DB) TruncateAll(t testing.TB)

TruncateAll removes all data from known set of tables.

type TxWrapper added in v14.7.0

type TxWrapper struct {
	*sql.Tx
}

TxWrapper is a simple wrapper around *sql.Tx.

func (*TxWrapper) Commit added in v14.7.0

func (txw *TxWrapper) Commit(t testing.TB)

Commit executes Commit operation on the wrapped *sql.Tx if it is set. After execution is sets Tx to nil to prevent errors on the deferred invocations (useful for testing when Rollback is deferred).

func (*TxWrapper) Rollback added in v14.7.0

func (txw *TxWrapper) Rollback(t testing.TB)

Rollback executes Rollback operation on the wrapped *sql.Tx if it is set. After execution is sets Tx to nil to prevent errors on the repeated invocations (useful for testing when Rollback is deferred).

Jump to

Keyboard shortcuts

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