sqldb

package
v0.0.0-...-a6b604f Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when an entity cannot be found.
	ErrNotFound = errors.Wrap(surf.ErrNotFound, "sql")

	// ErrConstraint is returned when an operation cannot be completed due
	// to declared constrained.
	ErrConstraint = errors.Wrap(surf.ErrConstraint, "sql")
)

Functions

func ClonePostgres

func ClonePostgres(t *testing.T, from string, o *DBOpts) (clonedb *sql.DB, cleanup func())

ClonePostgres creates clone of given database.

While this may speedup tests that require bootstraping with a lot of fixtures, be aware that content layout on the hard drive may differ from origin and default ordering may differ from original database.

func EnsurePostgres

func EnsurePostgres(t *testing.T, o *DBOpts) (testdb *sql.DB, cleanup func())

EnsurePostgres connects to PostgreSQL instance, create database and return connection to it. If connection to Postres cannot be established, test is skipped.

Unless option is provided, defaults are used:

  • Database name: test_database_<creation time in unix ns>
  • Host: localhost
  • Port: 5432
  • SSLMode: disable
  • User: postgres

Function connects to 'postgres' database first to create new database.

Types

type DBOpts

type DBOpts struct {
	User    string
	Port    int
	Host    string
	SSLMode string
	DBName  string
}

DBOpts defines options for test database connections

type Database

type Database interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) Row

	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
	Close() error
}

func PostgresDatabase

func PostgresDatabase(db *sql.DB) Database

type Row

type Row interface {
	Scan(...interface{}) error
}

type Transaction

type Transaction interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) Row

	Commit() error
	Rollback() error
}

Jump to

Keyboard shortcuts

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