dbtest

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

dbtest contains functionality for writing tests

Index

Constants

This section is empty.

Variables

View Source
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

func GenerateName() (string, error)

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) Query

func (t *TestPostgres) Query(ctx context.Context, q string, args ...interface{}) (pgx.Rows, error)

Query exposes a method for querying the test database directly

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

func (w *Wrapper) Setup(ctx context.Context) error

Setup calls the setup method on the TestDatabase if this is the first invocation

func (*Wrapper) Teardown

func (w *Wrapper) Teardown(ctx context.Context) error

Teardown calls the teardown method on the TestDatabase if this is the last invocation of the method.

func (*Wrapper) Truncate

func (w *Wrapper) Truncate(ctx context.Context) error

Truncate is just a pass through to the TestDatabase's truncation method. It does not prevent truncation from being called more than once.

It's assumed this responsibility is managed by the underlying database.

func (*Wrapper) URL

func (w *Wrapper) URL() *url.URL

URL returns the underlying URL of the Test Database

Jump to

Keyboard shortcuts

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