sqlmock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: 0BSD Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoMoreCalls = errors.New("no more expected calls")
	ErrBadFunction = errors.New("wrong function called")
	ErrBadQuery    = errors.New("wrong query")

	ErrArgCountMismatch = errors.New("argument count mismatch")
	ErrUnexpectedParam  = errors.New("unexpected parameter")
	ErrValueMismatch    = errors.New("value mismatch")
	ErrNilValueMismatch = errors.New("nil/non-nil value mismatch")
)
View Source
var DriverName = "sqltx_mock2"

Functions

This section is empty.

Types

type ConnMock

type ConnMock struct {
	Mock *Mock
}

ConnMock implements the driver.Conn interface.

func (*ConnMock) Begin

func (c *ConnMock) Begin() (driver.Tx, error)

Begin implements driver.Conn

func (*ConnMock) BeginTx

func (c *ConnMock) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx implements driver.ConnBeginTx

func (*ConnMock) Close

func (c *ConnMock) Close() error

Close implements driver.Conn

func (*ConnMock) ExecContext

func (c *ConnMock) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

ExecContext implements driver.ExecerContext

func (*ConnMock) Prepare

func (c *ConnMock) Prepare(query string) (driver.Stmt, error)

Prepare implements driver.Conn

func (*ConnMock) QueryContext

func (c *ConnMock) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

QueryContext implements driver.QueryerContext

type ExpectedCall

type ExpectedCall struct {
	// Mock is a reference back to the parent mock.
	Mock *Mock

	// Function is the type of the expected function.
	Function FuncType

	// Args are the expected arguments. (as driver.NamedValue)
	Args []driver.NamedValue

	// Query for statement preparation, execution, or querying.
	Query string

	// QueryRegex is used for regex matching of queries.
	QueryRegex *regexp.Regexp

	// UseRegexForQuery reports whether QueryRegex should be used for matching.
	UseRegexForQuery bool

	// Possible Return values.
	ReturnErr    *error
	ReturnResult driver.Result
	ReturnRows   driver.Rows
	ReturnTx     driver.Tx
	ReturnStmt   driver.Stmt
}

ExpectedCall represents an expected function call with its arguments and return values

func (*ExpectedCall) ReturnsEmptyResult

func (call *ExpectedCall) ReturnsEmptyResult() *ExpectedCall

func (*ExpectedCall) ReturnsError

func (call *ExpectedCall) ReturnsError(err error) *ExpectedCall

ReturnsError sets the error to return

func (*ExpectedCall) ReturnsNewStmt

func (call *ExpectedCall) ReturnsNewStmt() *ExpectedCall

ReturnsNewStmt creates and sets a new statement to return

func (*ExpectedCall) ReturnsNewTx

func (call *ExpectedCall) ReturnsNewTx() *ExpectedCall

ReturnsNewTx creates and sets a new transaction to return

func (*ExpectedCall) ReturnsNoError

func (call *ExpectedCall) ReturnsNoError() *ExpectedCall

ReturnsNoError sets no error to return.

func (*ExpectedCall) ReturnsResult

func (call *ExpectedCall) ReturnsResult(result driver.Result) *ExpectedCall

ReturnsResult sets the result to return

func (*ExpectedCall) ReturnsRows

func (call *ExpectedCall) ReturnsRows(rows driver.Rows) *ExpectedCall

ReturnsRows sets the rows to return

func (*ExpectedCall) WithArgs

func (call *ExpectedCall) WithArgs(args ...driver.Value) *ExpectedCall

WithArgs sets the expected arguments for the call Convert simple values to NamedValue with ordinal positions

func (*ExpectedCall) WithNamedArgs

func (call *ExpectedCall) WithNamedArgs(args ...driver.NamedValue) *ExpectedCall

WithNamedArgs sets the expected arguments as NamedValue

func (*ExpectedCall) WithQuery

func (call *ExpectedCall) WithQuery(query string) *ExpectedCall

WithQuery sets the expected exact query string for Prepare/Exec/Query calls

func (*ExpectedCall) WithQueryRegex

func (call *ExpectedCall) WithQueryRegex(pattern string) *ExpectedCall

WithQueryRegex sets a regex pattern for matching queries

type FuncType

type FuncType int

FuncType represents the type of database function call

const (
	FuncBegin FuncType = iota
	FuncPrepare
	FuncExec
	FuncQuery
	FuncCommit
	FuncRollback
)

func (FuncType) String

func (f FuncType) String() string

String returns the string representation of FuncType

type Mock

type Mock struct {
	// Expected is a slice of expected function calls in order
	Expected []*ExpectedCall

	// CallIndex is the index of the current call
	CallIndex int
	// contains filtered or unexported fields
}

Mock contains the mock configuration and expectations.

func NewMock

func NewMock() (string, *Mock)

NewMock creates a new Mock and registers it with the driver pool.

func NewMockDB

func NewMockDB() (*sql.DB, *Mock, error)

NewMockDB creates a new sql.DB with the mock driver

func NewMockDBTx

func NewMockDBTx() (*sql.DB, *sql.Tx, *Mock, error)

NewMockDBTx creates a new sql.DB and sql.Tx with the mock driver

func (*Mock) ExpectBegin

func (m *Mock) ExpectBegin() *ExpectedCall

ExpectBegin adds a new `Begin` call expectation.

func (*Mock) ExpectCommit

func (m *Mock) ExpectCommit() *ExpectedCall

ExpectCommit adds a new `Commit` call expectation.

func (*Mock) ExpectExec

func (m *Mock) ExpectExec() *ExpectedCall

ExpectExec adds a new `Exec` call expectation.

func (*Mock) ExpectPrepare

func (m *Mock) ExpectPrepare() *ExpectedCall

ExpectPrepare adds a new `Prepare` call expectation.

func (*Mock) ExpectQuery

func (m *Mock) ExpectQuery() *ExpectedCall

ExpectQuery adds a new `Query` call expectation.

func (*Mock) ExpectRollback

func (m *Mock) ExpectRollback() *ExpectedCall

ExpectRollback adds a new `Rollback` call expectation.

func (*Mock) VerifyExpectations

func (m *Mock) VerifyExpectations() error

VerifyExpectations checks if all expected calls were made

type MockDriver

type MockDriver struct {
	// contains filtered or unexported fields
}

MockDriver implements the driver.Driver interface

var Pool *MockDriver

func (*MockDriver) Open

func (d *MockDriver) Open(name string) (driver.Conn, error)

Open implements driver.Driver

type ResultMock

type ResultMock struct{ LastID, RowsAffectedValue int64 }

ResultMock implements sql.Result for testing

func NewSqlResultMock

func NewSqlResultMock(lastID int64, rowsAffectedValue int64) *ResultMock

func (*ResultMock) LastInsertId

func (m *ResultMock) LastInsertId() (int64, error)

func (*ResultMock) RowsAffected

func (m *ResultMock) RowsAffected() (int64, error)

type RowsMock

type RowsMock struct{}

RowsMock implements the driver.Rows interface

func NewRowsMock

func NewRowsMock() *RowsMock

func (*RowsMock) Close

func (m *RowsMock) Close() error

func (*RowsMock) Columns

func (m *RowsMock) Columns() []string

func (*RowsMock) Next

func (m *RowsMock) Next([]driver.Value) error

type StmtMock

type StmtMock struct {
	// Mock is the Mock object that created this StmtMock.
	Mock *Mock
	// contains filtered or unexported fields
}

StmtMock implements the driver.Stmt interface

func (*StmtMock) Close

func (m *StmtMock) Close() error

Close implements driver.Stmt

func (*StmtMock) Exec

func (m *StmtMock) Exec(args []driver.Value) (driver.Result, error)

Exec implements driver.Stmt

func (*StmtMock) NumInput

func (m *StmtMock) NumInput() int

NumInput implements driver.Stmt

func (*StmtMock) Query

func (m *StmtMock) Query(args []driver.Value) (driver.Rows, error)

Query implements driver.Stmt

type TransactionMock

type TransactionMock struct {
	Mock *Mock
}

TransactionMock implements the driver.Tx interface.

func (*TransactionMock) Commit

func (tx *TransactionMock) Commit() error

Commit implements the driver.Tx interface.

func (*TransactionMock) Rollback

func (tx *TransactionMock) Rollback() error

Rollback implements the driver.Tx interface.

Jump to

Keyboard shortcuts

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