Skip to content

Commit cb3e425

Browse files
author
Benjamin Rupp
committed
Moving Exec(...) test to 1.9 build as NamedValueChecker is only supported since 1.9
1 parent 4567d0a commit cb3e425

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

expectations_before_go18.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package sqlmock
44

55
import (
6+
"database/sql/driver"
67
"fmt"
78
"reflect"
89
)
@@ -39,6 +40,10 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
3940
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
4041
}
4142

43+
if !driver.IsValue(darg) {
44+
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
45+
}
46+
4247
if !reflect.DeepEqual(darg, v.Value) {
4348
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
4449
}

expectations_go19_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build go1.9
2+
3+
package sqlmock
4+
5+
import (
6+
"context"
7+
"testing"
8+
)
9+
10+
func TestCustomValueConverterExec(t *testing.T) {
11+
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
12+
expectedQuery := "INSERT INTO tags \\(name,email,age,hobbies\\) VALUES \\(\\?,\\?,\\?,\\?\\)"
13+
query := "INSERT INTO tags (name,email,age,hobbies) VALUES (?,?,?,?)"
14+
name := "John"
15+
email := "j@jj.j"
16+
age := 12
17+
hobbies := []string{"soccer", "netflix"}
18+
mock.ExpectBegin()
19+
mock.ExpectPrepare(expectedQuery)
20+
mock.ExpectExec(expectedQuery).WithArgs(name, email, age, hobbies).WillReturnResult(NewResult(1, 1))
21+
mock.ExpectCommit()
22+
23+
ctx := context.Background()
24+
tx, e := db.BeginTx(ctx, nil)
25+
if e != nil {
26+
t.Error(e)
27+
return
28+
}
29+
stmt, e := db.PrepareContext(ctx, query)
30+
if e != nil {
31+
t.Error(e)
32+
return
33+
}
34+
_, e = stmt.Exec(name, email, age, hobbies)
35+
if e != nil {
36+
t.Error(e)
37+
return
38+
}
39+
tx.Commit()
40+
if err := mock.ExpectationsWereMet(); err != nil {
41+
t.Error(err)
42+
}
43+
}

expectations_test.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sqlmock
22

33
import (
4-
"context"
54
"database/sql/driver"
65
"errors"
76
"fmt"
@@ -193,39 +192,4 @@ func TestCustomValueConverterQueryScan(t *testing.T) {
193192
if err := mock.ExpectationsWereMet(); err != nil {
194193
t.Error(err)
195194
}
196-
}
197-
198-
func TestCustomValueConverterExec(t *testing.T) {
199-
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
200-
expectedQuery := "INSERT INTO tags \\(name,email,age,hobbies\\) VALUES \\(\\?,\\?,\\?,\\?\\)"
201-
query := "INSERT INTO tags (name,email,age,hobbies) VALUES (?,?,?,?)"
202-
name := "John"
203-
email := "j@jj.j"
204-
age := 12
205-
hobbies := []string{"soccer", "netflix"}
206-
mock.ExpectBegin()
207-
mock.ExpectPrepare(expectedQuery)
208-
mock.ExpectExec(expectedQuery).WithArgs(name, email, age, hobbies).WillReturnResult(NewResult(1, 1))
209-
mock.ExpectCommit()
210-
211-
ctx := context.Background()
212-
tx, e := db.BeginTx(ctx, nil)
213-
if e != nil {
214-
t.Error(e)
215-
return
216-
}
217-
stmt, e := db.PrepareContext(ctx, query)
218-
if e != nil {
219-
t.Error(e)
220-
return
221-
}
222-
_, e = stmt.Exec(name, email, age, hobbies)
223-
if e != nil {
224-
t.Error(e)
225-
return
226-
}
227-
tx.Commit()
228-
if err := mock.ExpectationsWereMet(); err != nil {
229-
t.Error(err)
230-
}
231-
}
195+
}

0 commit comments

Comments
 (0)