Documentation
¶
Overview ¶
dbtest contains functionality for writing tests
Index ¶
- Variables
- func GenerateName() (string, error)
- type NewTestDatabaseFunc
- type TestDatabase
- type TestPostgres
- func (t *TestPostgres) Exec(ctx context.Context, q string, args ...interface{}) (pgconn.CommandTag, error)
- func (t *TestPostgres) Query(ctx context.Context, q string, args ...interface{}) (pgx.Rows, error)
- func (t *TestPostgres) Setup(ctx context.Context) error
- func (t *TestPostgres) Teardown(ctx context.Context) error
- func (t *TestPostgres) Truncate(ctx context.Context) error
- func (t *TestPostgres) URL() *url.URL
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ConnectionError = errors.NewCause(errors.BadRequestCategory, "connection_error")
ConnectionError occurs when something can't be done because the database is not connected
Functions ¶
func GenerateName ¶
Types ¶
type NewTestDatabaseFunc ¶
type NewTestDatabaseFunc func(string) (TestDatabase, error)
NewTestDatabaseFunc represents a constructor of a TestDatabase
type TestDatabase ¶
type TestDatabase interface { Setup(context.Context) error Teardown(context.Context) error Truncate(context.Context) error URL() *url.URL }
TestDatabase represents a test storage backend (e.g. Postgres, MySQL, SQLite, etc). Implementations are responsible for providing functionality for setting up and tearing down test environments for integration testing.
func New ¶
func New(dbURL string) (TestDatabase, error)
New returns an instance of a testing database for use in integration tests.
func NewTestPostgres ¶
func NewTestPostgres(rootURL string) (TestDatabase, error)
NewTestPostgres returns an instance of a TestPostgres struct
func Wrap ¶
func Wrap(db TestDatabase) TestDatabase
Wrap returns a wrapped test database setting it up to be a singleton-style object.
type TestPostgres ¶
type TestPostgres struct {
// contains filtered or unexported fields
}
TestPostgres implements the TestDatabase providing functionality for setting up a test environment for integration testing.
func (*TestPostgres) Exec ¶
func (t *TestPostgres) Exec(ctx context.Context, q string, args ...interface{}) (pgconn.CommandTag, error)
Exec exposes a method for executing a query against the database
func (*TestPostgres) Setup ¶
func (t *TestPostgres) Setup(ctx context.Context) error
Setup creates a randomly named database for testing
func (*TestPostgres) Teardown ¶
func (t *TestPostgres) Teardown(ctx context.Context) error
Teardown destroys the test database and closes any connection to the database.
func (*TestPostgres) Truncate ¶
func (t *TestPostgres) Truncate(ctx context.Context) error
Truncate truncates all tables inside the database resetting them back to empty while retaining any triggers or tables.
func (*TestPostgres) URL ¶
func (t *TestPostgres) URL() *url.URL
URL returns the connection string for the underlying test database.
type Wrapper ¶
type Wrapper struct {
// contains filtered or unexported fields
}
Wrapper is a TestDatabase providing a singleton-style experience allowing more than one test to use the same underlying database. The purpose is to save time on how long it takes to setup a database between tests.
Wrappers are kept in a centralized managed by the New function.
func (*Wrapper) Database ¶
func (w *Wrapper) Database() TestDatabase
Database returns a reference to the underlying TestDatabase
func (*Wrapper) Setup ¶
Setup calls the setup method on the TestDatabase if this is the first invocation
func (*Wrapper) Teardown ¶
Teardown calls the teardown method on the TestDatabase if this is the last invocation of the method.