testdb

package
v16.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfig

func GetConfig(tb 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(tb 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

func SetMigrations(tb 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

func WaitForBlockedQuery(tb testing.TB, ctx context.Context, 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

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

func New(tb 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

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

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

func (DB) Close

func (db DB) Close() error

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

func (DB) MustExec

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

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

func (DB) RequireRowsInTable

func (db DB) RequireRowsInTable(tb testing.TB, tname string, n int)

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

func (DB) Truncate

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

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

func (DB) TruncateAll

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

TruncateAll removes all data from known set of tables.

type TxWrapper

type TxWrapper struct {
	*sql.Tx
}

TxWrapper is a simple wrapper around *sql.Tx.

func (*TxWrapper) Commit

func (txw *TxWrapper) Commit(tb 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

func (txw *TxWrapper) Rollback(tb 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