Documentation
¶
Overview ¶
Package dbmock provides dbmock functionality.
Index ¶
- Variables
- type ArgMatcher
- type Call
- type CallKind
- type Conn
- type ExecExpectation
- func (b *ExecExpectation) Consume() error
- func (b *ExecExpectation) DescribeMismatch(call Call) string
- func (b *ExecExpectation) IsSatisfied() bool
- func (b *ExecExpectation) Kind() CallKind
- func (b *ExecExpectation) Match(call Call) error
- func (e *ExecExpectation) WillReturnError(err error) *ExecExpectation
- func (e *ExecExpectation) WillReturnResult(rowsAffected, lastInsertID int64) *ExecExpectation
- type ExecResult
- type Expectation
- type ExpectationQueue
- func (q *ExpectationQueue) Dequeue() (Expectation, error)
- func (q *ExpectationQueue) Enqueue(exp Expectation)
- func (q *ExpectationQueue) Len() int
- func (q *ExpectationQueue) MatchNext(call Call) (Expectation, error)
- func (q *ExpectationQueue) Peek() (Expectation, error)
- func (q *ExpectationQueue) Snapshot() []Expectation
- type Mock
- func (m *Mock) Calls() []Call
- func (m *Mock) Conn() *Conn
- func (m *Mock) ExpectBegin() *TxExpectation
- func (m *Mock) ExpectCommit() *TxExpectation
- func (m *Mock) ExpectExec(query string, args ...any) *ExecExpectation
- func (m *Mock) ExpectPrepare(query string, args ...any) *PrepareExpectation
- func (m *Mock) ExpectQuery(query string, args ...any) *QueryExpectation
- func (m *Mock) ExpectRollback() *TxExpectation
- func (m *Mock) ExpectationsWereMet() error
- type Option
- type PrepareExpectation
- func (b *PrepareExpectation) Consume() error
- func (b *PrepareExpectation) DescribeMismatch(call Call) string
- func (e *PrepareExpectation) ExpectExec(args ...any) *ExecExpectation
- func (e *PrepareExpectation) ExpectQuery(args ...any) *QueryExpectation
- func (b *PrepareExpectation) IsSatisfied() bool
- func (b *PrepareExpectation) Kind() CallKind
- func (b *PrepareExpectation) Match(call Call) error
- func (e *PrepareExpectation) ValidatePlaceholderCount() *PrepareExpectation
- func (e *PrepareExpectation) WillReturnError(err error) *PrepareExpectation
- type QueryExpectation
- func (b *QueryExpectation) Consume() error
- func (b *QueryExpectation) DescribeMismatch(call Call) string
- func (b *QueryExpectation) IsSatisfied() bool
- func (b *QueryExpectation) Kind() CallKind
- func (b *QueryExpectation) Match(call Call) error
- func (e *QueryExpectation) WillReturnError(err error) *QueryExpectation
- func (e *QueryExpectation) WillReturnRows(rows Rows) *QueryExpectation
- type Rows
- type Stmt
- type Tx
- type TxExpectation
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnexpectedCall is an exported package value. ErrUnexpectedCall = errors.New("unexpected call") // ErrKindMismatch is an exported package value. ErrKindMismatch = errors.New("call kind mismatch") // ErrPreparedModeMismatch reports an unexpected prepared statement mode. ErrPreparedModeMismatch = errors.New("prepared mode mismatch") // ErrSQLMismatch reports an unexpected SQL statement. ErrSQLMismatch = errors.New("sql mismatch") // ErrArgCountMismatch reports an unexpected argument count. ErrArgCountMismatch = errors.New("argument count mismatch") // ErrArgMismatch reports an unexpected argument value. ErrArgMismatch = errors.New("argument mismatch") // ErrPlaceholderCountMismatch reports a placeholder and argument count mismatch. ErrPlaceholderCountMismatch = errors.New("placeholder count mismatch") // ErrExpectationAlreadyConsumed reports a reused expectation. ErrExpectationAlreadyConsumed = errors.New("expectation already consumed") // ErrEmptyExpectationQueue reports that no expectation is available. ErrEmptyExpectationQueue = errors.New("expectation queue is empty") // ErrStatementClosed reports use of a closed statement. ErrStatementClosed = errors.New("statement is closed") // ErrTransactionDone reports use of an already finalized transaction. ErrTransactionDone = errors.New("transaction is already finalized") )
Functions ¶
This section is empty.
Types ¶
type ArgMatcher ¶
ArgMatcher defines argument matching behavior for expectations.
func AnyArg ¶
func AnyArg() ArgMatcher
AnyArg matches any value at the corresponding argument position.
func Predicate ¶
func Predicate(name string, predicate func(any) bool) ArgMatcher
Predicate matches arguments for which predicate(value) returns true.
func TypeOf ¶
func TypeOf(sample any) ArgMatcher
TypeOf matches arguments of the same dynamic type as sample.
type Call ¶
Call is a normalized representation of an intercepted DB operation.
type CallKind ¶
type CallKind string
CallKind identifies the database operation class.
const ( // CallExec is an exported package constant. CallExec CallKind = "exec" // CallQuery is an exported package constant. CallQuery = "query" // CallPrepare identifies a prepared statement operation. CallPrepare CallKind = "prepare" // CallBegin identifies a transaction begin operation. CallBegin CallKind = "begin" // CallCommit identifies a transaction commit operation. CallCommit CallKind = "commit" // CallRollback identifies a transaction rollback operation. CallRollback CallKind = "rollback" )
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn executes DB operations against mock expectations.
func (*Conn) Exec ¶
func (c *Conn) Exec(query string, args ...any) (ExecResult, error)
Exec matches and executes the next expected exec call.
type ExecExpectation ¶
type ExecExpectation struct {
// contains filtered or unexported fields
}
ExecExpectation matches an exec call.
func NewExecExpectation ¶
func NewExecExpectation(query string, args ...any) *ExecExpectation
NewExecExpectation creates an exec expectation.
func (*ExecExpectation) DescribeMismatch ¶
func (*ExecExpectation) IsSatisfied ¶
func (b *ExecExpectation) IsSatisfied() bool
func (*ExecExpectation) WillReturnError ¶
func (e *ExecExpectation) WillReturnError(err error) *ExecExpectation
WillReturnError configures the error returned by a matched exec call.
func (*ExecExpectation) WillReturnResult ¶
func (e *ExecExpectation) WillReturnResult(rowsAffected, lastInsertID int64) *ExecExpectation
WillReturnResult configures the result returned by a matched exec call.
type ExecResult ¶
ExecResult represents an exec operation result.
type Expectation ¶
type Expectation interface {
Kind() CallKind
Match(call Call) error
Consume() error
IsSatisfied() bool
DescribeMismatch(call Call) string
}
Expectation defines matching and lifecycle behavior for one expected DB call.
type ExpectationQueue ¶
type ExpectationQueue struct {
// contains filtered or unexported fields
}
ExpectationQueue provides synchronized expectation ordering and consumption.
func NewExpectationQueue ¶
func NewExpectationQueue() *ExpectationQueue
NewExpectationQueue creates an empty expectation queue.
func NewExpectationQueueWithOrdering ¶
func NewExpectationQueueWithOrdering(ordered bool) *ExpectationQueue
NewExpectationQueueWithOrdering creates an empty expectation queue with configured ordering mode.
func (*ExpectationQueue) Dequeue ¶
func (q *ExpectationQueue) Dequeue() (Expectation, error)
Dequeue removes and returns the first expectation.
func (*ExpectationQueue) Enqueue ¶
func (q *ExpectationQueue) Enqueue(exp Expectation)
Enqueue appends an expectation at the tail.
func (*ExpectationQueue) Len ¶
func (q *ExpectationQueue) Len() int
Len returns the number of queued expectations.
func (*ExpectationQueue) MatchNext ¶
func (q *ExpectationQueue) MatchNext(call Call) (Expectation, error)
MatchNext validates and consumes the next expected call.
func (*ExpectationQueue) Peek ¶
func (q *ExpectationQueue) Peek() (Expectation, error)
Peek returns the first expectation without consuming it.
func (*ExpectationQueue) Snapshot ¶
func (q *ExpectationQueue) Snapshot() []Expectation
Snapshot returns a shallow copy of currently queued expectations.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock coordinates expectations and captured calls.
func (*Mock) ExpectBegin ¶
func (m *Mock) ExpectBegin() *TxExpectation
ExpectBegin appends a begin expectation.
func (*Mock) ExpectCommit ¶
func (m *Mock) ExpectCommit() *TxExpectation
ExpectCommit appends a commit expectation.
func (*Mock) ExpectExec ¶
func (m *Mock) ExpectExec(query string, args ...any) *ExecExpectation
ExpectExec appends an exec expectation.
func (*Mock) ExpectPrepare ¶
func (m *Mock) ExpectPrepare(query string, args ...any) *PrepareExpectation
ExpectPrepare appends a prepare expectation.
func (*Mock) ExpectQuery ¶
func (m *Mock) ExpectQuery(query string, args ...any) *QueryExpectation
ExpectQuery appends a query expectation.
func (*Mock) ExpectRollback ¶
func (m *Mock) ExpectRollback() *TxExpectation
ExpectRollback appends a rollback expectation.
func (*Mock) ExpectationsWereMet ¶
ExpectationsWereMet fails if queued expectations remain unmatched.
type Option ¶
type Option func(*options)
Option configures mock behavior.
func WithUnorderedExpectations ¶
func WithUnorderedExpectations() Option
WithUnorderedExpectations enables matching calls in any order.
type PrepareExpectation ¶
type PrepareExpectation struct {
// contains filtered or unexported fields
}
PrepareExpectation matches a prepare call.
func NewPrepareExpectation ¶
func NewPrepareExpectation(query string, args ...any) *PrepareExpectation
NewPrepareExpectation creates a prepare expectation.
func (*PrepareExpectation) DescribeMismatch ¶
func (*PrepareExpectation) ExpectExec ¶
func (e *PrepareExpectation) ExpectExec(args ...any) *ExecExpectation
ExpectExec chains an expected prepared statement exec call.
func (*PrepareExpectation) ExpectQuery ¶
func (e *PrepareExpectation) ExpectQuery(args ...any) *QueryExpectation
ExpectQuery chains an expected prepared statement query call.
func (*PrepareExpectation) IsSatisfied ¶
func (b *PrepareExpectation) IsSatisfied() bool
func (*PrepareExpectation) ValidatePlaceholderCount ¶
func (e *PrepareExpectation) ValidatePlaceholderCount() *PrepareExpectation
ValidatePlaceholderCount enables placeholder-count checks for prepared stmt exec/query calls.
func (*PrepareExpectation) WillReturnError ¶
func (e *PrepareExpectation) WillReturnError(err error) *PrepareExpectation
WillReturnError configures the error returned by a matched prepare call.
type QueryExpectation ¶
type QueryExpectation struct {
// contains filtered or unexported fields
}
QueryExpectation matches a query call.
func NewQueryExpectation ¶
func NewQueryExpectation(query string, args ...any) *QueryExpectation
NewQueryExpectation creates a query expectation.
func (*QueryExpectation) DescribeMismatch ¶
func (*QueryExpectation) IsSatisfied ¶
func (b *QueryExpectation) IsSatisfied() bool
func (*QueryExpectation) WillReturnError ¶
func (e *QueryExpectation) WillReturnError(err error) *QueryExpectation
WillReturnError configures the error returned by a matched query call.
func (*QueryExpectation) WillReturnRows ¶
func (e *QueryExpectation) WillReturnRows(rows Rows) *QueryExpectation
WillReturnRows configures rows returned by a matched query call.
type Rows ¶
Rows is a simple query result container for tests.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt executes prepared statement operations against mock expectations.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx executes transaction lifecycle operations against mock expectations.
func (*Tx) Exec ¶
func (tx *Tx) Exec(query string, args ...any) (ExecResult, error)
Exec matches and executes an exec call inside the active transaction.
type TxExpectation ¶
type TxExpectation struct {
// contains filtered or unexported fields
}
TxExpectation matches a transaction lifecycle call.
func NewTxExpectation ¶
func NewTxExpectation(kind CallKind) *TxExpectation
NewTxExpectation creates a transaction expectation (begin, commit, rollback).
func (*TxExpectation) DescribeMismatch ¶
func (*TxExpectation) IsSatisfied ¶
func (b *TxExpectation) IsSatisfied() bool
func (*TxExpectation) WillReturnError ¶
func (e *TxExpectation) WillReturnError(err error) *TxExpectation
WillReturnError configures the error returned by a matched tx call.