forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectations_go19_test.go
43 lines (39 loc) · 964 Bytes
/
expectations_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
// +build go1.9
package sqlmock
import (
"context"
"testing"
)
func TestCustomValueConverterExec(t *testing.T) {
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
expectedQuery := "INSERT INTO tags \\(name,email,age,hobbies\\) VALUES \\(\\?,\\?,\\?,\\?\\)"
query := "INSERT INTO tags (name,email,age,hobbies) VALUES (?,?,?,?)"
name := "John"
email := "[email protected]"
age := 12
hobbies := []string{"soccer", "netflix"}
mock.ExpectBegin()
mock.ExpectPrepare(expectedQuery)
mock.ExpectExec(expectedQuery).WithArgs(name, email, age, hobbies).WillReturnResult(NewResult(1, 1))
mock.ExpectCommit()
ctx := context.Background()
tx, e := db.BeginTx(ctx, nil)
if e != nil {
t.Error(e)
return
}
stmt, e := db.PrepareContext(ctx, query)
if e != nil {
t.Error(e)
return
}
_, e = stmt.Exec(name, email, age, hobbies)
if e != nil {
t.Error(e)
return
}
tx.Commit()
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}