Documentation ¶
Index ¶
- func Conn() driver.Conn
- func EnableTimeParsing(flag bool)
- func Reset()
- func RowsFromCSVString(columns []string, s string, c ...rune) driver.Rows
- func RowsFromSlice(columns []string, data [][]driver.Value) driver.Rows
- func SetBeginFunc(f func() (driver.Tx, error))
- func SetCommitFunc(f func() error)
- func SetExecFunc(f func(query string) (driver.Result, error))
- func SetExecWithArgsFunc(f func(query string, args []driver.Value) (driver.Result, error))
- func SetOpenFunc(f func(dsn string) (driver.Conn, error))
- func SetQueryFunc(f func(query string) (result driver.Rows, err error))
- func SetQueryWithArgsFunc(f func(query string, args []driver.Value) (result driver.Rows, err error))
- func SetRollbackFunc(f func() error)
- func StubBegin(tx driver.Tx, err error)
- func StubCommitError(err error)
- func StubExec(q string, r *Result)
- func StubExecError(q string, err error)
- func StubQuery(q string, rows driver.Rows)
- func StubQueryError(q string, err error)
- func StubRollbackError(err error)
- type Result
- type Tx
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnableTimeParsing ¶
func EnableTimeParsing(flag bool)
func RowsFromCSVString ¶
Example ¶
columns := []string{"id", "name", "age", "created"} result := ` 1,tim,20,2012-10-01 01:00:01 2,joe,25,2012-10-02 02:00:02 3,bob,30,2012-10-03 03:00:03 ` rows := RowsFromCSVString(columns, result) fmt.Println(rows.Columns())
Output: [id name age created]
func SetBeginFunc ¶
Set your own function to be executed when db.Begin() is called. You can either hand back a valid transaction, or an error. Conn() can be used to grab the global Conn object containing stubbed queries.
Example ¶
defer Reset() commitCalled := false rollbackCalled := false SetBeginFunc(func() (txn driver.Tx, err error) { t := &Tx{} t.SetCommitFunc(func() error { commitCalled = true return nil }) t.SetRollbackFunc(func() error { rollbackCalled = true return nil }) return t, nil }) db, _ := sql.Open("testdb", "") tx, _ := db.Begin() tx.Commit() fmt.Println("CommitCalled =", commitCalled) fmt.Println("RollbackCalled =", rollbackCalled)
Output: CommitCalled = true RollbackCalled = false
func SetCommitFunc ¶
func SetCommitFunc(f func() error)
Set your own function to be executed when tx.Commit() is called on the default transcation. Conn() can be used to grab the global Conn object containing stubbed queries.
Example ¶
defer Reset() SetCommitFunc(func() error { return errors.New("commit failed") }) db, _ := sql.Open("testdb", "") tx, _ := db.Begin() fmt.Println("CommitResult =", tx.Commit())
Output: CommitResult = commit failed
func SetExecFunc ¶
Set your own function to be executed when db.Exec is called. You can return an error or a Result object with the LastInsertId and RowsAffected
func SetExecWithArgsFunc ¶
Set your own function to be executed when db.Exec is called. You can return an error or a Result object with the LastInsertId and RowsAffected
Example ¶
defer Reset() SetExecWithArgsFunc(func(query string, args []driver.Value) (result driver.Result, err error) { if args[0] == "joe" { return testResult{1, 1}, nil } return testResult{1, 0}, nil }) db, _ := sql.Open("testdb", "") res, _ := db.Exec("UPDATE bar SET name = 'foo' WHERE name = ?", "joe") rowsAffected, _ := res.RowsAffected() fmt.Println("RowsAffected =", rowsAffected)
Output: RowsAffected = 1
func SetOpenFunc ¶
Set your own function to be executed when db.Open() is called. You can either hand back a valid connection, or an error. Conn() can be used to grab the global Conn object containing stubbed queries.
Example ¶
defer Reset() SetOpenFunc(func(dsn string) (driver.Conn, error) { // Conn() will return the same internal driver.Conn being used by the driver return Conn(), errors.New("test error") }) // err only returns from this if it's an unknown driver, we are stubbing opening a connection db, _ := sql.Open("testdb", "foo") _, err := db.Driver().Open("foo") if err != nil { fmt.Println("Stubbed error returned as expected: " + err.Error()) }
Output: Stubbed error returned as expected: test error
func SetQueryFunc ¶
Set your own function to be executed when db.Query() is called. As with StubQuery() you can use the RowsFromCSVString() method to easily generate the driver.Rows, or you can return your own.
Example ¶
defer Reset() columns := []string{"id", "name", "age", "created"} rows := "1,tim,20,2012-10-01 01:00:01\n2,joe,25,2012-10-02 02:00:02\n3,bob,30,2012-10-03 03:00:03" SetQueryFunc(func(query string) (result driver.Rows, err error) { return RowsFromCSVString(columns, rows), nil }) db, _ := sql.Open("testdb", "") res, _ := db.Query("SELECT foo FROM bar") for res.Next() { var u = new(user) res.Scan(&u.id, &u.name, &u.age, &u.created) fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10)) }
Output: tim - 20 joe - 25 bob - 30
Example (QueryRow) ¶
defer Reset() columns := []string{"id", "name", "age", "created"} rows := "1,tim,20,2012-10-01 01:00:01" SetQueryFunc(func(query string) (result driver.Rows, err error) { return RowsFromCSVString(columns, rows), nil }) db, _ := sql.Open("testdb", "") row := db.QueryRow("SELECT foo FROM bar") var u = new(user) row.Scan(&u.id, &u.name, &u.age, &u.created) fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))
Output: tim - 20
func SetQueryWithArgsFunc ¶
func SetQueryWithArgsFunc(f func(query string, args []driver.Value) (result driver.Rows, err error))
Set your own function to be executed when db.Query() is called. As with StubQuery() you can use the RowsFromCSVString() method to easily generate the driver.Rows, or you can return your own.
Example ¶
defer Reset() SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) { columns := []string{"id", "name", "age", "created"} rows := "" if args[0] == "joe" { rows = "2,joe,25,2012-10-02 02:00:02" } return RowsFromCSVString(columns, rows), nil }) db, _ := sql.Open("testdb", "") res, _ := db.Query("SELECT foo FROM bar WHERE name = $1", "joe") for res.Next() { var u = new(user) res.Scan(&u.id, &u.name, &u.age, &u.created) fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10)) }
Output: joe - 25
func SetRollbackFunc ¶
func SetRollbackFunc(f func() error)
Set your own function to be executed when tx.Rollback() is called on the default transcation. Conn() can be used to grab the global Conn object containing stubbed queries.
Example ¶
defer Reset() SetRollbackFunc(func() error { return errors.New("rollback failed") }) db, _ := sql.Open("testdb", "") tx, _ := db.Begin() fmt.Println("RollbackResult =", tx.Rollback())
Output: RollbackResult = rollback failed
func StubBegin ¶
Stubs the global driver.Conn to return the supplied tx and error when db.Begin() is called.
func StubCommitError ¶
func StubCommitError(err error)
Stubs the default transaction to return the supplied error when tx.Commit() is called.
func StubExec ¶
Stubs the global driver.Conn to return the supplied Result when db.Exec is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubExecError ¶
Stubs the global driver.Conn to return the supplied error when db.Exec() is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubQuery ¶
Stubs the global driver.Conn to return the supplied driver.Rows when db.Query() is called, query stubbing is case insensitive, and whitespace is also ignored.
Example ¶
defer Reset() db, _ := sql.Open("testdb", "") sql := "select id, name, age from users" columns := []string{"id", "name", "age", "created"} result := ` 1,tim,20,2012-10-01 01:00:01 2,joe,25,2012-10-02 02:00:02 3,bob,30,2012-10-03 03:00:03 ` StubQuery(sql, RowsFromCSVString(columns, result)) res, _ := db.Query(sql) for res.Next() { var u = new(user) res.Scan(&u.id, &u.name, &u.age, &u.created) fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10)) }
Output: tim - 20 joe - 25 bob - 30
Example (QueryRow) ¶
defer Reset() db, _ := sql.Open("testdb", "") sql := "select id, name, age from users" columns := []string{"id", "name", "age", "created"} result := ` 1,tim,20,2012-10-01 01:00:01 ` StubQuery(sql, RowsFromCSVString(columns, result)) row := db.QueryRow(sql) u := new(user) row.Scan(&u.id, &u.name, &u.age, &u.created) fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))
Output: tim - 20
func StubQueryError ¶
Stubs the global driver.Conn to return the supplied error when db.Query() is called, query stubbing is case insensitive, and whitespace is also ignored.
Example ¶
defer Reset() db, _ := sql.Open("testdb", "") sql := "select count(*) from error" StubQueryError(sql, errors.New("test error")) _, err := db.Query(sql) if err != nil { fmt.Println("Error returned: " + err.Error()) }
Output: Error returned: test error
func StubRollbackError ¶
func StubRollbackError(err error)
Stubs the default transaction to return the supplied error when tx.Rollback() is called.
Types ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (*Result) LastInsertId ¶
func (*Result) RowsAffected ¶
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}