sqliface

package module
v0.0.0-...-f8e6c8b Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

README

sqliface

GoDoc Build Status

sqliface is a package for the Go programming language that contains a set of interfaces and implementations that help make code utilizing database/sql more testable. This package has only been tested with a MySQL database. Pull Requests for utilizing other databases' custom types are appreciated (i.e. mysql.NullTime).

For documentation check out the GoDoc page.

Examples

Check out the examples directory for examples on how to use this package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecCloser

type ExecCloser interface {
	Execer
	io.Closer
}

ExecCloser implements the Execer interface with an additional Close method. Using this will allow you to test simple uses of sql.DB in a service.

type Execer

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

Execer is an interface that both sql.Tx and sql.DB implement. Using this interface will allow you to pass either into a function.

type MockRow

type MockRow []interface{}

MockRow is database row that implements Row to help test applications using database/sql.

func (MockRow) Scan

func (mr MockRow) Scan(dest ...interface{}) error

Scan is an implementation for a fake database row.

type MockRows

type MockRows struct {
	Rows   []MockRow
	Index  int
	Error  error
	Closed bool
}

MockRows is a set of database rows that implements Rows to help test applications using database/sql.

func NewMockRows

func NewMockRows(rows ...MockRow) *MockRows

NewMockRows will create a new MockRows instance with the given MockRow set.

func (*MockRows) Close

func (ms *MockRows) Close() error

Close will set the Close flag in MockRows so users can verify the method gets called.

func (*MockRows) Err

func (ms *MockRows) Err() error

Err will return the given Error in the MockRows object.

func (*MockRows) Next

func (ms *MockRows) Next() bool

Next will shift the Index of MockRows to the next MockRow in the set.

func (*MockRows) Scan

func (ms *MockRows) Scan(dest ...interface{}) error

Scan will attempt to scan the current MockRow into the given interfaces.

type Row

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

Row is an interface implemented by sql.Row, sql.Rows, and MockRow.

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...interface{}) error
	Err() error
	Close() error
}

Rows is an interface implemented by sql.Rows and MockRows.

type TypeError

type TypeError struct {
	Expected string
	Got      interface{}
}

TypeError is returned by MockRow.Scan when the type it's attempting to scan into does not match the type in the MockRow.

func NewTypeError

func NewTypeError(exp string, got interface{}) *TypeError

NewTypeError returns a TypeError instance. This can helpful to use when testing for errors while scanning from databases.

func (*TypeError) Error

func (t *TypeError) Error() string

Error allows TypeError to implement the error interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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