forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlmock_go19_test.go
70 lines (61 loc) · 1.54 KB
/
sqlmock_go19_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
63
64
65
66
67
68
69
70
// +build go1.9
package sqlmock
import (
"database/sql"
"database/sql/driver"
"errors"
"testing"
)
func TestStatementTX(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
prep := mock.ExpectPrepare("SELECT")
mock.ExpectBegin()
prep.ExpectQuery().WithArgs(1).WillReturnError(errors.New("fast fail"))
stmt, err := db.Prepare("SELECT title, body FROM articles WHERE id = ?")
if err != nil {
t.Fatalf("unexpected error on prepare: %v", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatalf("unexpected error on begin: %v", err)
}
// upgrade connection for statement
txStmt := tx.Stmt(stmt)
_, err = txStmt.Query(1)
if err == nil || err.Error() != "fast fail" {
t.Fatalf("unexpected result: %v", err)
}
}
func Test_sqlmock_CheckNamedValue(t *testing.T) {
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
tests := []struct {
name string
arg *driver.NamedValue
wantErr bool
}{
{
arg: &driver.NamedValue{Name: "test", Value: "test"},
wantErr: false,
},
{
arg: &driver.NamedValue{Name: "test", Value: sql.Out{}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := mock.(*sqlmock).CheckNamedValue(tt.arg); (err != nil) != tt.wantErr {
t.Errorf("CheckNamedValue() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}