1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2024-11-24 08:12:13 +02:00
This commit is contained in:
gedi 2014-08-14 20:35:18 +03:00
parent 4c6f0e69c3
commit 60b2068e00
4 changed files with 53 additions and 4 deletions

View File

@ -2,8 +2,10 @@ language: go
go:
- 1.1
- 1.2
- 1.3
- release
- tip
script:
- go vet ./...
- go test -v ./...

View File

@ -320,6 +320,8 @@ Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)
## Changes
- **2014-08-14** added **sqlmock.NewErrorResult** which gives an option to return driver.Result with errors for
interface methods, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/5)
- **2014-05-29** allow to match arguments in more sophisticated ways, by providing an **sqlmock.Argument** interface
- **2014-04-21** introduce **sqlmock.New()** to open a mock database connection for tests. This method
calls sql.DB.Ping to ensure that connection is open, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/4).

View File

@ -10,21 +10,30 @@ import (
type result struct {
insertID int64
rowsAffected int64
err error
}
// NewResult creates a new sql driver Result
// for Exec based query mocks.
func NewResult(lastInsertID int64, rowsAffected int64) driver.Result {
return &result{
lastInsertID,
rowsAffected,
insertID: lastInsertID,
rowsAffected: rowsAffected,
}
}
// NewErrorResult creates a new sql driver Result
// which returns an error given for both interface methods
func NewErrorResult(err error) driver.Result {
return &result{
err: err,
}
}
func (r *result) LastInsertId() (int64, error) {
return r.insertID, nil
return r.insertID, r.err
}
func (r *result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
return r.rowsAffected, r.err
}

36
result_test.go Normal file
View File

@ -0,0 +1,36 @@
package sqlmock
import (
"fmt"
"testing"
)
func TestShouldReturnValidSqlDriverResult(t *testing.T) {
result := NewResult(1, 2)
id, err := result.LastInsertId()
if 1 != id {
t.Errorf("Expected last insert id to be 1, but got: %d", id)
}
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
affected, err := result.RowsAffected()
if 2 != affected {
t.Errorf("Expected affected rows to be 2, but got: %d", affected)
}
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
}
func TestShouldReturnErroeSqlDriverResult(t *testing.T) {
result := NewErrorResult(fmt.Errorf("some error"))
_, err := result.LastInsertId()
if err == nil {
t.Error("expected error, but got none")
}
_, err = result.RowsAffected()
if err == nil {
t.Error("expected error, but got none")
}
}