Documentation ¶
Overview ¶
Package go_mocket is way to mock DB for GORM and just sql package usage First you need to activate package somewhere in your tests code like this
package main import ( "database/sql" mocket "github.com/Selvatico/go-mocket" "github.com/jinzhu/gorm" ) var DB *gorm.DB func SetupTests() { mocket.Catcher.Register() // GORM db, err := gorm.Open("fake_test", "connection_string") // Could be any connection string DB = db // OR // Regular sql package usage db, err := sql.Open(driver, source) } func GetUsers(db *sql.DB) []map[string]interface {} { var res []map[string]interface{} age := 27 rows, err := db.Query("SELECT name FROM users WHERE age=?", age) if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { var name string var age string if err := rows.Scan(&name, &age); err != nil { log.Fatal(err) } //fmt.Printf("%s is %d\n", name, age) row := make(map[string]interface{}) row["name"] = name row["age"] = age res = append(res, row) } if err := rows.Err(); err != nil { log.Fatal(err) } return res }
Somewhere in you tests:
package main import ( "log" "testing" "database/sql" mocket "github.com/Selvatico/go-mocket" ) func TestResponses (t *testing.T) { mocket.Catcher.Register() db, _ := sql.Open("fake_test", "connection_string") // Could be any connection string DB = db t.Run("Simple SELECT caught by query", func(t *testing.T) { mocket.Catcher.Logging = true commonReply := []map[string]interface{}{{"name": "FirstLast", "age": "30"}} mocket.Catcher.Reset().NewMock().WithQuery(`SELECT name FROM users WHERE`).WithReply(commonReply) result := GetUsers(DB) if len(result) != 1 { t.Errorf("Returned sets is not equal to 1. Received %d", len(result)) } if result[0]["age"] != "30" { t.Errorf("Age is not equal. Got %v", result[0]["age"]) } }) }
For more information and use cases please check: https://github.com/Selvatico/go-mocket
Index ¶
- Constants
- Variables
- func NewFakeResult(insertId int64, rowsAffected int64) driver.Result
- type Exceptions
- type FakeConn
- func (c *FakeConn) Begin() (driver.Tx, error)
- func (c *FakeConn) Close() (err error)
- func (c *FakeConn) Exec(query string, args []driver.Value) (driver.Result, error)
- func (c *FakeConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
- func (c *FakeConn) Prepare(query string) (driver.Stmt, error)
- func (c *FakeConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)
- func (c *FakeConn) Query(query string, args []driver.Value) (driver.Rows, error)
- func (c *FakeConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
- type FakeDB
- type FakeDriver
- type FakeResponse
- func (fr *FakeResponse) IsMatch(query string, args []driver.NamedValue) bool
- func (fr *FakeResponse) MarkAsTriggered()
- func (fr *FakeResponse) OneTime() *FakeResponse
- func (fr *FakeResponse) WithArgs(vars ...interface{}) *FakeResponse
- func (fr *FakeResponse) WithCallback(f func(string, []driver.NamedValue)) *FakeResponse
- func (fr *FakeResponse) WithError(err error) *FakeResponse
- func (fr *FakeResponse) WithExecException() *FakeResponse
- func (fr *FakeResponse) WithId(id int64) *FakeResponse
- func (fr *FakeResponse) WithQuery(query string) *FakeResponse
- func (fr *FakeResponse) WithQueryException() *FakeResponse
- func (fr *FakeResponse) WithReply(response []map[string]interface{}) *FakeResponse
- func (fr *FakeResponse) WithRowsNum(num int64) *FakeResponse
- type FakeResult
- type FakeStmt
- func (s *FakeStmt) Close() error
- func (s *FakeStmt) ColumnConverter(idx int) driver.ValueConverter
- func (smt *FakeStmt) Exec(args []driver.Value) (driver.Result, error)deprecated
- func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
- func (s *FakeStmt) NumInput() int
- func (s *FakeStmt) Query(args []driver.Value) (driver.Rows, error)deprecated
- func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
- type FakeTx
- type MockCatcher
- func (mc *MockCatcher) Attach(fr []*FakeResponse)
- func (mc *MockCatcher) FindResponse(query string, args []driver.NamedValue) *FakeResponse
- func (mc *MockCatcher) NewMock() *FakeResponse
- func (mc *MockCatcher) Register()
- func (mc *MockCatcher) Reset() *MockCatcher
- func (mc *MockCatcher) SetLogging(l bool)
- type RowsCursor
Constants ¶
const (
DRIVER_NAME = "MOCK_FAKE_DRIVER"
)
Variables ¶
var HookBadCommit func() bool
hook to simulate broken connections
var HookBadRollback func() bool
hook to simulate broken connections
Functions ¶
Types ¶
type Exceptions ¶
Exceptions represents possible exceptions during query executions
type FakeConn ¶
type FakeConn struct {
// contains filtered or unexported fields
}
FakeConn implements connection
func (*FakeConn) ExecContext ¶
func (c *FakeConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
ExecContext is optional to implement and it returns skip
func (*FakeConn) PrepareContext ¶
PrepareContext returns a prepared statement, bound to this connection. context is for the preparation of the statement, it must not store the context within the statement itself.
type FakeDriver ¶
type FakeDriver struct {
// contains filtered or unexported fields
}
FakeDriver implements driver interface in sql package
type FakeResponse ¶
type FakeResponse struct { Pattern string // SQL query pattern to match with Args []interface{} // List args to be matched with Response []map[string]interface{} // Array of rows to be parsed as result Once bool // To trigger only once Triggered bool // If it was triggered at least once Callback func(string, []driver.NamedValue) // Callback to execute when response triggered RowsAffected int64 // Defines affected rows count LastInsertId int64 // ID to be returned for INSERT queries Error error // Any type of error which could happen dur *Exceptions // contains filtered or unexported fields }
FakeResponse represents mock of response with holding all required values to return mocked response
func (*FakeResponse) IsMatch ¶
func (fr *FakeResponse) IsMatch(query string, args []driver.NamedValue) bool
IsMatch checks if both query and args matcher's return true and if this is Once mock
func (*FakeResponse) MarkAsTriggered ¶
func (fr *FakeResponse) MarkAsTriggered()
MarkAsTriggered marks response as executed. For one time catches it will not make this possible to execute anymore
func (*FakeResponse) OneTime ¶
func (fr *FakeResponse) OneTime() *FakeResponse
OneTime sets current mock to be triggered only once
func (*FakeResponse) WithArgs ¶
func (fr *FakeResponse) WithArgs(vars ...interface{}) *FakeResponse
WithArgs attaches Args check for prepared statements
func (*FakeResponse) WithCallback ¶
func (fr *FakeResponse) WithCallback(f func(string, []driver.NamedValue)) *FakeResponse
WithCallback adds callback to be executed during matching
func (*FakeResponse) WithError ¶ added in v1.0.3
func (fr *FakeResponse) WithError(err error) *FakeResponse
WithError sets Error to FakeResponse struct to have it available on any statements executed example: WithError(sql.ErrNoRows)
func (*FakeResponse) WithExecException ¶
func (fr *FakeResponse) WithExecException() *FakeResponse
WithExecException says that if mock attached to non-SELECT query we need to trigger error there
func (*FakeResponse) WithId ¶
func (fr *FakeResponse) WithId(id int64) *FakeResponse
WithId sets ID to be considered as insert ID for INSERT statements
func (*FakeResponse) WithQuery ¶
func (fr *FakeResponse) WithQuery(query string) *FakeResponse
WithQuery adds SQL query pattern to match for
func (*FakeResponse) WithQueryException ¶
func (fr *FakeResponse) WithQueryException() *FakeResponse
WithQueryException adds to SELECT mocks triggering of error
func (*FakeResponse) WithReply ¶
func (fr *FakeResponse) WithReply(response []map[string]interface{}) *FakeResponse
WithReply adds to chain and assign some parts of response
func (*FakeResponse) WithRowsNum ¶
func (fr *FakeResponse) WithRowsNum(num int64) *FakeResponse
WithRowsNum specifies how many records to consider as affected
type FakeResult ¶
type FakeResult struct {
// contains filtered or unexported fields
}
FakeResult implementation of sql Result interface
func (*FakeResult) LastInsertId ¶
func (fr *FakeResult) LastInsertId() (int64, error)
LastInsertId required to give sql package ability get ID of inserted record
func (*FakeResult) RowsAffected ¶
func (fr *FakeResult) RowsAffected() (int64, error)
RowsAffected returns the number of rows affected
type FakeStmt ¶
type FakeStmt struct {
// contains filtered or unexported fields
}
FakeStmt is implementation of Stmt sql interfcae
func (*FakeStmt) ColumnConverter ¶
func (s *FakeStmt) ColumnConverter(idx int) driver.ValueConverter
ColumnConverter returns a ValueConverter for the provided column index.
func (*FakeStmt) ExecContext ¶
func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
ExecContext executes a query that doesn't return rows, such as an INSERT or UPDATE.
func (*FakeStmt) QueryContext ¶
func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
QueryContext executes a query that may return rows, such as a SELECT.
type FakeTx ¶
type FakeTx struct {
// contains filtered or unexported fields
}
FakeTx implements Tx interface
type MockCatcher ¶
type MockCatcher struct { Mocks []*FakeResponse // Slice of all mocks Logging bool // Do we need to log what we catching? PanicOnEmptyResponse bool // If not response matches - do we need to panic? // contains filtered or unexported fields }
MockCatcher is global entity to save all mocks aka FakeResponses
var Catcher *MockCatcher
Catcher is global instance of Catcher used for attaching all mocks to connection
func (*MockCatcher) Attach ¶
func (mc *MockCatcher) Attach(fr []*FakeResponse)
Attach several mocks to MockCather. Could be useful to attach mocks from some factories of mocks
func (*MockCatcher) FindResponse ¶
func (mc *MockCatcher) FindResponse(query string, args []driver.NamedValue) *FakeResponse
FindResponse finds suitable response by provided
func (*MockCatcher) NewMock ¶
func (mc *MockCatcher) NewMock() *FakeResponse
NewMock creates new FakeResponse and return for chains of attachments
func (*MockCatcher) Register ¶ added in v1.0.1
func (mc *MockCatcher) Register()
Register safely register FakeDriver
func (*MockCatcher) Reset ¶
func (mc *MockCatcher) Reset() *MockCatcher
Reset removes all Mocks to start process again
func (*MockCatcher) SetLogging ¶ added in v1.0.14
func (mc *MockCatcher) SetLogging(l bool)
type RowsCursor ¶
type RowsCursor struct {
// contains filtered or unexported fields
}
RowsCursor is implementation of Rows sql interface
func (*RowsCursor) ColumnTypeScanType ¶
func (rc *RowsCursor) ColumnTypeScanType(index int) reflect.Type
RowsColumnTypeScanType may be implemented by Rows. It should return the value type that can be used to scan types into.
func (*RowsCursor) Columns ¶
func (rc *RowsCursor) Columns() []string
Columns returns the names of the columns.
func (*RowsCursor) HasNextResultSet ¶
func (rc *RowsCursor) HasNextResultSet() bool
HasNextResultSet is called at the end of the current result set and reports whether there is another result set after the current one.
func (*RowsCursor) Next ¶
func (rc *RowsCursor) Next(accumulator []driver.Value) error
Next is called to populate the next row of data into the provided slice.
func (*RowsCursor) NextResultSet ¶
func (rc *RowsCursor) NextResultSet() error
NextResultSet advances the driver to the next result set even if there are remaining rows in the current result set.