Documentation ¶
Overview ¶
package pgxmock is a mock library implementing pgx connector. Which has one and only purpose - to simulate pgx driver behavior in tests, without needing a real database connection. It helps to maintain correct **TDD** workflow.
It does not require (almost) any modifications to your source code in order to test and mock database operations. Supports concurrency and multiple database mocking.
The driver allows to mock any pgx driver method behavior.
Index ¶
- Variables
- func NewResult(op string, rowsAffected int64) pgconn.CommandTag
- func QueryMatcherOption(queryMatcher QueryMatcher) func(*pgxmock) error
- type Argument
- type CallModifier
- type ExpectedBatch
- func (e *ExpectedBatch) ExpectExec(query string) *ExpectedExec
- func (e *ExpectedBatch) ExpectQuery(query string) *ExpectedQuery
- func (e *ExpectedBatch) Maybe() CallModifier
- func (e *ExpectedBatch) String() string
- func (e *ExpectedBatch) Times(n uint) CallModifier
- func (e *ExpectedBatch) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedBatch) WillPanic(v any)
- func (e *ExpectedBatch) WillReturnError(err error)
- type ExpectedBegin
- func (e *ExpectedBegin) Maybe() CallModifier
- func (e *ExpectedBegin) String() string
- func (e *ExpectedBegin) Times(n uint) CallModifier
- func (e *ExpectedBegin) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedBegin) WillPanic(v any)
- func (e *ExpectedBegin) WillReturnError(err error)
- type ExpectedClose
- func (e *ExpectedClose) Maybe() CallModifier
- func (e *ExpectedClose) String() string
- func (e *ExpectedClose) Times(n uint) CallModifier
- func (e *ExpectedClose) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedClose) WillPanic(v any)
- func (e *ExpectedClose) WillReturnError(err error)
- type ExpectedCommit
- func (e *ExpectedCommit) Maybe() CallModifier
- func (e *ExpectedCommit) String() string
- func (e *ExpectedCommit) Times(n uint) CallModifier
- func (e *ExpectedCommit) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedCommit) WillPanic(v any)
- func (e *ExpectedCommit) WillReturnError(err error)
- type ExpectedCopyFrom
- func (e *ExpectedCopyFrom) Maybe() CallModifier
- func (e *ExpectedCopyFrom) String() string
- func (e *ExpectedCopyFrom) Times(n uint) CallModifier
- func (e *ExpectedCopyFrom) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedCopyFrom) WillPanic(v any)
- func (e *ExpectedCopyFrom) WillReturnError(err error)
- func (e *ExpectedCopyFrom) WillReturnResult(result int64) *ExpectedCopyFrom
- type ExpectedDeallocate
- func (e *ExpectedDeallocate) Maybe() CallModifier
- func (e *ExpectedDeallocate) String() string
- func (e *ExpectedDeallocate) Times(n uint) CallModifier
- func (e *ExpectedDeallocate) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedDeallocate) WillPanic(v any)
- func (e *ExpectedDeallocate) WillReturnError(err error)
- type ExpectedExec
- func (e *ExpectedExec) Maybe() CallModifier
- func (e *ExpectedExec) String() string
- func (e *ExpectedExec) Times(n uint) CallModifier
- func (e *ExpectedExec) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedExec) WillPanic(v any)
- func (e *ExpectedExec) WillReturnError(err error)
- func (e *ExpectedExec) WillReturnResult(result pgconn.CommandTag) *ExpectedExec
- func (e *ExpectedExec) WithArgs(args ...interface{}) *ExpectedExec
- func (e *ExpectedExec) WithRewrittenSQL(sql string) *ExpectedExec
- type ExpectedPing
- type ExpectedPrepare
- func (e *ExpectedPrepare) Maybe() CallModifier
- func (e *ExpectedPrepare) String() string
- func (e *ExpectedPrepare) Times(n uint) CallModifier
- func (e *ExpectedPrepare) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedPrepare) WillPanic(v any)
- func (e *ExpectedPrepare) WillReturnError(err error)
- type ExpectedQuery
- func (e *ExpectedQuery) Maybe() CallModifier
- func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery
- func (e *ExpectedQuery) String() string
- func (e *ExpectedQuery) Times(n uint) CallModifier
- func (e *ExpectedQuery) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedQuery) WillPanic(v any)
- func (e *ExpectedQuery) WillReturnError(err error)
- func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery
- func (e *ExpectedQuery) WithArgs(args ...interface{}) *ExpectedQuery
- func (e *ExpectedQuery) WithRewrittenSQL(sql string) *ExpectedQuery
- type ExpectedReset
- func (e *ExpectedReset) Maybe() CallModifier
- func (e *ExpectedReset) String() string
- func (e *ExpectedReset) Times(n uint) CallModifier
- func (e *ExpectedReset) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedReset) WillPanic(v any)
- func (e *ExpectedReset) WillReturnError(err error)
- type ExpectedRollback
- func (e *ExpectedRollback) Maybe() CallModifier
- func (e *ExpectedRollback) String() string
- func (e *ExpectedRollback) Times(n uint) CallModifier
- func (e *ExpectedRollback) WillDelayFor(duration time.Duration) CallModifier
- func (e *ExpectedRollback) WillPanic(v any)
- func (e *ExpectedRollback) WillReturnError(err error)
- type Expecter
- type PgxCommonIface
- type PgxConnIface
- type PgxPoolIface
- type QueryMatcher
- type QueryMatcherFunc
- type Rows
- func (r *Rows) AddCommandTag(tag pgconn.CommandTag) *Rows
- func (r *Rows) AddRow(values ...any) *Rows
- func (r *Rows) AddRows(values ...[]any) *Rows
- func (r *Rows) CloseError(err error) *Rows
- func (r *Rows) FromCSVString(s string) *Rows
- func (r *Rows) Kind() pgx.Rows
- func (r *Rows) RowError(row int, err error) *Rows
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CSVColumnParser = func(s string) interface{} { switch { case strings.ToLower(s) == "null": return nil } return s }
CSVColumnParser is a function which converts trimmed csv column string to a []byte representation. Currently transforms NULL to nil
Functions ¶
func NewResult ¶
func NewResult(op string, rowsAffected int64) pgconn.CommandTag
NewResult creates a new pgconn.CommandTag result for Exec based query mocks.
func QueryMatcherOption ¶
func QueryMatcherOption(queryMatcher QueryMatcher) func(*pgxmock) error
QueryMatcherOption allows to customize SQL query matcher and match SQL query strings in more sophisticated ways. The default QueryMatcher is QueryMatcherRegexp.
Types ¶
type Argument ¶
type Argument interface {
Match(interface{}) bool
}
Argument interface allows to match any argument in specific way when used with ExpectedQuery and ExpectedExec expectations.
type CallModifier ¶
type CallModifier interface { // Maybe allows the expected method call to be optional. // Not calling an optional method will not cause an error while asserting expectations Maybe() CallModifier // Times indicates that that the expected method should only fire the indicated number of times. // Zero value is ignored and means the same as one. Times(n uint) CallModifier // WillDelayFor allows to specify duration for which it will delay // result. May be used together with Context WillDelayFor(duration time.Duration) CallModifier // WillReturnError allows to set an error for the expected method WillReturnError(err error) // WillPanic allows to force the expected method to panic WillPanic(v any) }
CallModifier interface represents common interface for all expectations supported
type ExpectedBatch ¶
type ExpectedBatch struct {
// contains filtered or unexported fields
}
ExpectedBatch is used to manage pgx.Batch expectations. Returned by pgxmock.ExpectBatch.
func (*ExpectedBatch) ExpectExec ¶
func (e *ExpectedBatch) ExpectExec(query string) *ExpectedExec
ExpectExec allows to expect Queue().Exec() on this batch.
func (*ExpectedBatch) ExpectQuery ¶
func (e *ExpectedBatch) ExpectQuery(query string) *ExpectedQuery
ExpectQuery allows to expect Queue().Query() or Queue().QueryRow() on this batch.
func (*ExpectedBatch) Maybe ¶
func (e *ExpectedBatch) Maybe() CallModifier
func (*ExpectedBatch) String ¶
func (e *ExpectedBatch) String() string
String returns string representation
func (*ExpectedBatch) Times ¶
func (e *ExpectedBatch) Times(n uint) CallModifier
func (*ExpectedBatch) WillDelayFor ¶
func (e *ExpectedBatch) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedBatch) WillReturnError ¶
func (e *ExpectedBatch) WillReturnError(err error)
type ExpectedBegin ¶
type ExpectedBegin struct {
// contains filtered or unexported fields
}
ExpectedBegin is used to manage *pgx.Begin expectation returned by pgxmock.ExpectBegin.
func (*ExpectedBegin) Maybe ¶
func (e *ExpectedBegin) Maybe() CallModifier
func (*ExpectedBegin) String ¶
func (e *ExpectedBegin) String() string
String returns string representation
func (*ExpectedBegin) Times ¶
func (e *ExpectedBegin) Times(n uint) CallModifier
func (*ExpectedBegin) WillDelayFor ¶
func (e *ExpectedBegin) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedBegin) WillReturnError ¶
func (e *ExpectedBegin) WillReturnError(err error)
type ExpectedClose ¶
type ExpectedClose struct {
// contains filtered or unexported fields
}
ExpectedClose is used to manage pgx.Close expectation returned by pgxmock.ExpectClose
func (*ExpectedClose) Maybe ¶
func (e *ExpectedClose) Maybe() CallModifier
func (*ExpectedClose) String ¶
func (e *ExpectedClose) String() string
String returns string representation
func (*ExpectedClose) Times ¶
func (e *ExpectedClose) Times(n uint) CallModifier
func (*ExpectedClose) WillDelayFor ¶
func (e *ExpectedClose) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedClose) WillReturnError ¶
func (e *ExpectedClose) WillReturnError(err error)
type ExpectedCommit ¶
type ExpectedCommit struct {
// contains filtered or unexported fields
}
ExpectedCommit is used to manage pgx.Tx.Commit expectation returned by pgxmock.ExpectCommit.
func (*ExpectedCommit) Maybe ¶
func (e *ExpectedCommit) Maybe() CallModifier
func (*ExpectedCommit) String ¶
func (e *ExpectedCommit) String() string
String returns string representation
func (*ExpectedCommit) Times ¶
func (e *ExpectedCommit) Times(n uint) CallModifier
func (*ExpectedCommit) WillDelayFor ¶
func (e *ExpectedCommit) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedCommit) WillReturnError ¶
func (e *ExpectedCommit) WillReturnError(err error)
type ExpectedCopyFrom ¶
type ExpectedCopyFrom struct {
// contains filtered or unexported fields
}
ExpectedCopyFrom is used to manage *pgx.Conn.CopyFrom expectations. Returned by *Pgxmock.ExpectCopyFrom.
func (*ExpectedCopyFrom) Maybe ¶
func (e *ExpectedCopyFrom) Maybe() CallModifier
func (*ExpectedCopyFrom) String ¶
func (e *ExpectedCopyFrom) String() string
String returns string representation
func (*ExpectedCopyFrom) Times ¶
func (e *ExpectedCopyFrom) Times(n uint) CallModifier
func (*ExpectedCopyFrom) WillDelayFor ¶
func (e *ExpectedCopyFrom) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedCopyFrom) WillReturnError ¶
func (e *ExpectedCopyFrom) WillReturnError(err error)
func (*ExpectedCopyFrom) WillReturnResult ¶
func (e *ExpectedCopyFrom) WillReturnResult(result int64) *ExpectedCopyFrom
WillReturnResult arranges for an expected CopyFrom() to return a number of rows affected
type ExpectedDeallocate ¶
type ExpectedDeallocate struct {
// contains filtered or unexported fields
}
ExpectedDeallocate is used to manage pgx.Deallocate and pgx.DeallocateAll expectations. Returned by pgxmock.ExpectDeallocate(string) and pgxmock.ExpectDeallocateAll().
func (*ExpectedDeallocate) Maybe ¶
func (e *ExpectedDeallocate) Maybe() CallModifier
func (*ExpectedDeallocate) String ¶
func (e *ExpectedDeallocate) String() string
String returns string representation
func (*ExpectedDeallocate) Times ¶
func (e *ExpectedDeallocate) Times(n uint) CallModifier
func (*ExpectedDeallocate) WillDelayFor ¶
func (e *ExpectedDeallocate) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedDeallocate) WillReturnError ¶
func (e *ExpectedDeallocate) WillReturnError(err error)
type ExpectedExec ¶
type ExpectedExec struct {
// contains filtered or unexported fields
}
ExpectedExec is used to manage pgx.Exec, pgx.Tx.Exec or pgx.Stmt.Exec expectations. Returned by pgxmock.ExpectExec.
Example ¶
mock, _ := NewConn() ex := mock.ExpectExec("^INSERT (.+)").WillReturnResult(NewResult("INSERT", 15)) ex.WillDelayFor(time.Second).Maybe().Times(2) fmt.Print(ex) res, _ := mock.Exec(ctx, "INSERT something") fmt.Println(res) ex.WithArgs(42) fmt.Print(ex) res, _ = mock.Exec(ctx, "INSERT something", 42) fmt.Print(res)
Output: ExpectedExec => expecting call to Exec(): - matches sql: '^INSERT (.+)' - is without arguments - returns result: INSERT 15 - delayed execution for: 1s - execution is optional - execution calls awaited: 2 INSERT 15 ExpectedExec => expecting call to Exec(): - matches sql: '^INSERT (.+)' - is with arguments: 0 - 42 - returns result: INSERT 15 - delayed execution for: 1s - execution is optional - execution calls awaited: 2 INSERT 15
func (*ExpectedExec) Maybe ¶
func (e *ExpectedExec) Maybe() CallModifier
func (*ExpectedExec) String ¶
func (e *ExpectedExec) String() string
String returns string representation
func (*ExpectedExec) Times ¶
func (e *ExpectedExec) Times(n uint) CallModifier
func (*ExpectedExec) WillDelayFor ¶
func (e *ExpectedExec) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedExec) WillReturnError ¶
func (e *ExpectedExec) WillReturnError(err error)
func (*ExpectedExec) WillReturnResult ¶
func (e *ExpectedExec) WillReturnResult(result pgconn.CommandTag) *ExpectedExec
WillReturnResult arranges for an expected Exec() to return a particular result, there is pgxmock.NewResult(op string, rowsAffected int64) method to build a corresponding result.
func (*ExpectedExec) WithArgs ¶
func (e *ExpectedExec) WithArgs(args ...interface{}) *ExpectedExec
WithArgs will match given expected args to actual database exec operation arguments. if at least one argument does not match, it will return an error. For specific arguments an pgxmock.Argument interface can be used to match an argument.
func (*ExpectedExec) WithRewrittenSQL ¶
func (e *ExpectedExec) WithRewrittenSQL(sql string) *ExpectedExec
WithRewrittenSQL will match given expected expression to a rewritten SQL statement by an pgx.QueryRewriter argument
type ExpectedPing ¶
type ExpectedPing struct {
// contains filtered or unexported fields
}
ExpectedPing is used to manage Ping() expectations
func (*ExpectedPing) Maybe ¶
func (e *ExpectedPing) Maybe() CallModifier
func (*ExpectedPing) String ¶
func (e *ExpectedPing) String() string
String returns string representation
func (*ExpectedPing) Times ¶
func (e *ExpectedPing) Times(n uint) CallModifier
func (*ExpectedPing) WillDelayFor ¶
func (e *ExpectedPing) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedPing) WillReturnError ¶
func (e *ExpectedPing) WillReturnError(err error)
type ExpectedPrepare ¶
type ExpectedPrepare struct {
// contains filtered or unexported fields
}
ExpectedPrepare is used to manage pgx.Prepare or pgx.Tx.Prepare expectations. Returned by pgxmock.ExpectPrepare.
func (*ExpectedPrepare) Maybe ¶
func (e *ExpectedPrepare) Maybe() CallModifier
func (*ExpectedPrepare) String ¶
func (e *ExpectedPrepare) String() string
String returns string representation
func (*ExpectedPrepare) Times ¶
func (e *ExpectedPrepare) Times(n uint) CallModifier
func (*ExpectedPrepare) WillDelayFor ¶
func (e *ExpectedPrepare) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedPrepare) WillReturnError ¶
func (e *ExpectedPrepare) WillReturnError(err error)
type ExpectedQuery ¶
type ExpectedQuery struct {
// contains filtered or unexported fields
}
ExpectedQuery is used to manage *pgx.Conn.Query, *pgx.Conn.QueryRow, *pgx.Tx.Query, *pgx.Tx.QueryRow, *pgx.Stmt.Query or *pgx.Stmt.QueryRow expectations
func (*ExpectedQuery) Maybe ¶
func (e *ExpectedQuery) Maybe() CallModifier
func (*ExpectedQuery) RowsWillBeClosed ¶
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery
RowsWillBeClosed expects this query rows to be closed.
func (*ExpectedQuery) String ¶
func (e *ExpectedQuery) String() string
String returns string representation
func (*ExpectedQuery) Times ¶
func (e *ExpectedQuery) Times(n uint) CallModifier
func (*ExpectedQuery) WillDelayFor ¶
func (e *ExpectedQuery) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedQuery) WillReturnError ¶
func (e *ExpectedQuery) WillReturnError(err error)
func (*ExpectedQuery) WillReturnRows ¶
func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery
WillReturnRows specifies the set of resulting rows that will be returned by the triggered query
func (*ExpectedQuery) WithArgs ¶
func (e *ExpectedQuery) WithArgs(args ...interface{}) *ExpectedQuery
WithArgs will match given expected args to actual database query arguments. if at least one argument does not match, it will return an error. For specific arguments an pgxmock.Argument interface can be used to match an argument.
func (*ExpectedQuery) WithRewrittenSQL ¶
func (e *ExpectedQuery) WithRewrittenSQL(sql string) *ExpectedQuery
WithRewrittenSQL will match given expected expression to a rewritten SQL statement by an pgx.QueryRewriter argument
type ExpectedReset ¶
type ExpectedReset struct {
// contains filtered or unexported fields
}
ExpectedReset is used to manage pgx.Reset expectation
func (*ExpectedReset) Maybe ¶
func (e *ExpectedReset) Maybe() CallModifier
func (*ExpectedReset) String ¶
func (e *ExpectedReset) String() string
func (*ExpectedReset) Times ¶
func (e *ExpectedReset) Times(n uint) CallModifier
func (*ExpectedReset) WillDelayFor ¶
func (e *ExpectedReset) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedReset) WillReturnError ¶
func (e *ExpectedReset) WillReturnError(err error)
type ExpectedRollback ¶
type ExpectedRollback struct {
// contains filtered or unexported fields
}
ExpectedRollback is used to manage pgx.Tx.Rollback expectation returned by pgxmock.ExpectRollback.
func (*ExpectedRollback) Maybe ¶
func (e *ExpectedRollback) Maybe() CallModifier
func (*ExpectedRollback) String ¶
func (e *ExpectedRollback) String() string
String returns string representation
func (*ExpectedRollback) Times ¶
func (e *ExpectedRollback) Times(n uint) CallModifier
func (*ExpectedRollback) WillDelayFor ¶
func (e *ExpectedRollback) WillDelayFor(duration time.Duration) CallModifier
func (*ExpectedRollback) WillReturnError ¶
func (e *ExpectedRollback) WillReturnError(err error)
type Expecter ¶
type Expecter interface { // ExpectationsWereMet checks whether all queued expectations // were met in order (unless MatchExpectationsInOrder set to false). // If any of them was not met - an error is returned. ExpectationsWereMet() error // ExpectBatch expects pgx.Batch to be called. The *ExpectedBatch // allows to mock database response ExpectBatch() *ExpectedBatch // ExpectClose queues an expectation for this database // action to be triggered. The *ExpectedClose allows // to mock database response ExpectClose() *ExpectedClose // ExpectPrepare expects Prepare() to be called with expectedSQL query. ExpectPrepare(expectedStmtName, expectedSQL string) *ExpectedPrepare // ExpectDeallocate expects Deallocate() to be called with expectedStmtName. // The *ExpectedDeallocate allows to mock database response ExpectDeallocate(expectedStmtName string) *ExpectedDeallocate ExpectDeallocateAll() *ExpectedDeallocate // ExpectQuery expects Query() or QueryRow() to be called with expectedSQL query. // the *ExpectedQuery allows to mock database response. ExpectQuery(expectedSQL string) *ExpectedQuery // ExpectExec expects Exec() to be called with expectedSQL query. // the *ExpectedExec allows to mock database response ExpectExec(expectedSQL string) *ExpectedExec // ExpectBegin expects pgx.Conn.Begin to be called. // the *ExpectedBegin allows to mock database response ExpectBegin() *ExpectedBegin // ExpectBeginTx expects expects BeginTx() to be called with expectedSQL // query. The *ExpectedBegin allows to mock database response. ExpectBeginTx(txOptions pgx.TxOptions) *ExpectedBegin // ExpectCommit expects pgx.Tx.Commit to be called. // the *ExpectedCommit allows to mock database response ExpectCommit() *ExpectedCommit // ExpectReset expects pgxpool.Reset() to be called. // The *ExpectedReset allows to mock database response ExpectReset() *ExpectedReset // ExpectRollback expects pgx.Tx.Rollback to be called. // the *ExpectedRollback allows to mock database response ExpectRollback() *ExpectedRollback // ExpectPing expected Ping() to be called. // The *ExpectedPing allows to mock database response ExpectPing() *ExpectedPing // ExpectCopyFrom expects pgx.CopyFrom to be called. // The *ExpectCopyFrom allows to mock database response ExpectCopyFrom(expectedTableName pgx.Identifier, expectedColumns []string) *ExpectedCopyFrom // MatchExpectationsInOrder gives an option whether to match all // expectations in the order they were set or not. // // By default it is set to - true. But if you use goroutines // to parallelize your query executation, that option may // be handy. // // This option may be turned on anytime during tests. As soon // as it is switched to false, expectations will be matched // in any order. Or otherwise if switched to true, any unmatched // expectations will be expected in order MatchExpectationsInOrder(bool) // NewRows allows Rows to be created from a []string slice. NewRows(columns []string) *Rows // NewRowsWithColumnDefinition allows Rows to be created from a // pgconn.FieldDescription slice with a definition of sql metadata NewRowsWithColumnDefinition(columns ...pgconn.FieldDescription) *Rows // New Column allows to create a Column NewColumn(name string) *pgconn.FieldDescription }
Expecter interface serves to create expectations for any kind of database action in order to mock and test real database behavior.
type PgxCommonIface ¶
type PgxCommonIface interface { Expecter pgx.Tx BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) Ping(context.Context) error }
PgxCommonIface represents common interface for all pgx connection interfaces: pgxpool.Pool, pgx.Conn and pgx.Tx
type PgxConnIface ¶
type PgxConnIface interface { PgxCommonIface Close(ctx context.Context) error Deallocate(ctx context.Context, name string) error DeallocateAll(ctx context.Context) error Config() *pgx.ConnConfig PgConn() *pgconn.PgConn }
PgxConnIface represents pgx.Conn specific interface
func NewConn ¶
func NewConn(options ...func(*pgxmock) error) (PgxConnIface, error)
NewConn creates PgxConnIface database connection and a mock to manage expectations. Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
type PgxPoolIface ¶
type PgxPoolIface interface { PgxCommonIface Acquire(ctx context.Context) (*pgxpool.Conn, error) AcquireAllIdle(ctx context.Context) []*pgxpool.Conn AcquireFunc(ctx context.Context, f func(*pgxpool.Conn) error) error AsConn() PgxConnIface Close() Stat() *pgxpool.Stat Reset() Config() *pgxpool.Config }
PgxPoolIface represents pgxpool.Pool specific interface
func NewPool ¶
func NewPool(options ...func(*pgxmock) error) (PgxPoolIface, error)
NewPool creates PgxPoolIface pool of database connections and a mock to manage expectations. Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
type QueryMatcher ¶
type QueryMatcher interface { // Match expected SQL query string without whitespace to // actual SQL. Match(expectedSQL, actualSQL string) error }
QueryMatcher is an SQL query string matcher interface, which can be used to customize validation of SQL query strings. As an example, external library could be used to build and validate SQL ast, columns selected.
pgxmock can be customized to implement a different QueryMatcher configured through an option when pgxmock.New or pgxmock.NewWithDSN is called, default QueryMatcher is QueryMatcherRegexp.
Example ¶
// configure to use case sensitive SQL query matcher // instead of default regular expression matcher mock, err := NewConn(QueryMatcherOption(QueryMatcherEqual)) if err != nil { fmt.Println("failed to open pgxmock database:", err) } // defer db.Close() rows := NewRows([]string{"id", "title"}). AddRow(1, "one"). AddRow(2, "two") mock.ExpectQuery("SELECT * FROM users").WillReturnRows(rows) rs, err := mock.Query(context.Background(), "SELECT * FROM users") if err != nil { fmt.Println("failed to match expected query") return } defer rs.Close() for rs.Next() { var id int var title string _ = rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and title: one scanned id: 2 and title: two
var QueryMatcherEqual QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) if actual != expect { return fmt.Errorf(`actual sql: "%s" does not equal to expected "%s"`, actual, expect) } return nil })
QueryMatcherEqual is the SQL query matcher which simply tries a case sensitive match of expected and actual SQL strings without whitespace.
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) re, err := regexp.Compile(expect) if err != nil { return err } if !re.MatchString(actual) { return fmt.Errorf(`could not match actual sql: "%s" with expected regexp "%s"`, actual, re.String()) } return nil })
QueryMatcherRegexp is the default SQL query matcher used by pgxmock. It parses expectedSQL to a regular expression and attempts to match actualSQL.
type QueryMatcherFunc ¶
QueryMatcherFunc type is an adapter to allow the use of ordinary functions as QueryMatcher. If f is a function with the appropriate signature, QueryMatcherFunc(f) is a QueryMatcher that calls f.
func (QueryMatcherFunc) Match ¶
func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error
Match implements the QueryMatcher
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is a mocked collection of rows to return for Query result
Example ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } defer mock.Close(context.Background()) rows := NewRows([]string{"id", "title"}). AddRow(1, "one"). AddRow(2, "two"). AddCommandTag(pgconn.NewCommandTag("SELECT 2")) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, _ := mock.Query(context.Background(), "SELECT") defer rs.Close() fmt.Println("command tag:", rs.CommandTag()) if len(rs.FieldDescriptions()) != 2 { fmt.Println("got wrong number of fields") } for rs.Next() { var id int var title string _ = rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: command tag: SELECT 2 scanned id: 1 and title: one scanned id: 2 and title: two
Example (CustomDriverValue) ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } defer mock.Close(context.Background()) rows := NewRows([]string{"id", "null_int"}). AddRow(5, pgtype.Int8{Int64: 5, Valid: true}). AddRow(2, pgtype.Int8{Valid: false}) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, _ := mock.Query(context.Background(), "SELECT") defer rs.Close() for rs.Next() { var id int var num pgtype.Int8 _ = rs.Scan(&id, &num) fmt.Println("scanned id:", id, "and null int64:", num) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 5 and null int64: {5 true} scanned id: 2 and null int64: {0 false}
Example (ExpectToBeClosed) ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } defer mock.Close(context.Background()) row := NewRows([]string{"id", "title"}).AddRow(1, "john") rows := NewRowsWithColumnDefinition( pgconn.FieldDescription{Name: "id"}, pgconn.FieldDescription{Name: "title"}). AddRow(1, "john").AddRow(2, "anna") mock.ExpectQuery("SELECT").WillReturnRows(row, rows).RowsWillBeClosed() _, _ = mock.Query(context.Background(), "SELECT") _, _ = mock.Query(context.Background(), "SELECT") if err := mock.ExpectationsWereMet(); err != nil { fmt.Println("got error:", err) } /*Output: got error: expected query rows to be closed, but it was not: ExpectedQuery => expecting call to Query() or to QueryRow(): - matches sql: 'SELECT' - is without arguments - returns data: result set: 0 row 0: [1 john] result set: 1 row 0: [1 john] row 1: [2 anna] */
Output: got error: expected query rows to be closed, but it was not: ExpectedQuery => expecting call to Query() or to QueryRow(): - matches sql: 'SELECT' - is without arguments - returns data: result set: 0 row 0: [1 john] result set: 1 row 0: [1 john] row 1: [2 anna]
Example (RawValues) ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } defer mock.Close(context.Background()) rows := NewRows([]string{"raw"}). AddRow([]byte(`one binary value with some text!`)). AddRow([]byte(`two binary value with even more text than the first one`)). AddRow([]byte{}) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, err := mock.Query(context.Background(), "SELECT") if err != nil { fmt.Print(err) return } defer rs.Close() for rs.Next() { var rawValue []byte if err := json.Unmarshal(rs.RawValues()[0], &rawValue); err != nil { fmt.Print(err) } fmt.Println(string(rawValue)) }
Output: one binary value with some text! two binary value with even more text than the first one
Example (RowError) ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } // defer mock.Close(context.Background()) rows := NewRows([]string{"id", "title"}). AddRow(0, "one"). AddRow(1, "two"). RowError(1, fmt.Errorf("row error")) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, _ := mock.Query(context.Background(), "SELECT") defer rs.Close() for rs.Next() { var id int var title string _ = rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) } }
Output: scanned id: 0 and title: one scanned id: 1 and title: two got rows error: row error
Example (Values) ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open pgxmock database:", err) return } defer mock.Close(context.Background()) rows := NewRows([]string{"raw"}). AddRow(`one string value with some text!`). AddRow(`two string value with even more text than the first one`). AddRow([]byte{}) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, err := mock.Query(context.Background(), "SELECT") if err != nil { fmt.Print(err) return } defer rs.Close() for rs.Next() { v, e := rs.Values() fmt.Println(v[0], e) }
Output: one string value with some text! <nil> two string value with even more text than the first one <nil> [] <nil>
func NewRows ¶
NewRows allows Rows to be created from a sql interface{} slice or from the CSV string and to be used as sql driver.Rows. Use pgxmock.NewRows instead if using a custom converter
func NewRowsWithColumnDefinition ¶
func NewRowsWithColumnDefinition(columns ...pgconn.FieldDescription) *Rows
NewRowsWithColumnDefinition return rows with columns metadata
func (*Rows) AddCommandTag ¶
func (r *Rows) AddCommandTag(tag pgconn.CommandTag) *Rows
AddCommandTag will add a command tag to the result set
func (*Rows) AddRow ¶
AddRow composed from database interface{} slice return the same instance to perform subsequent actions. Note that the number of values must match the number of columns
func (*Rows) AddRows ¶
AddRows adds multiple rows composed from any slice and returns the same instance to perform subsequent actions.
Example ¶
mock, err := NewConn() if err != nil { fmt.Println("failed to open sqlmock database:", err) return } defer mock.Close(context.Background()) values := [][]any{ { 1, "one", }, { 2, "two", }, } rows := NewRows([]string{"id", "title"}).AddRows(values...) mock.ExpectQuery("SELECT").WillReturnRows(rows) rs, _ := mock.Query(context.Background(), "SELECT") defer rs.Close() for rs.Next() { var id int var title string _ = rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and title: one scanned id: 2 and title: two
func (*Rows) CloseError ¶
CloseError allows to set an error which will be returned by rows.Close function.
The close error will be triggered only in cases when rows.Next() EOF was not yet reached, that is a default sql library behavior
func (*Rows) FromCSVString ¶
FromCSVString build rows from csv string. return the same instance to perform subsequent actions. Note that the number of values must match the number of columns