forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 8
/
result_test.go
62 lines (56 loc) · 1.58 KB
/
result_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sqlmock
import (
"fmt"
"testing"
)
// used for examples
var mock = &sqlmock{}
func ExampleNewErrorResult() {
db, mock, _ := New()
result := NewErrorResult(fmt.Errorf("some error"))
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
res, _ := db.Exec("INSERT something")
_, err := res.LastInsertId()
fmt.Println(err)
// Output: some error
}
func ExampleNewResult() {
var lastInsertID, affected int64
result := NewResult(lastInsertID, affected)
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
fmt.Println(mock.ExpectationsWereMet())
// Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec or ExecContext which:
// - matches sql: '^INSERT (.+)'
// - is without arguments
// - should return Result having:
// LastInsertId: 0
// RowsAffected: 0
}
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 TestShouldReturnErrorSqlDriverResult(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")
}
}