Skip to content

Commit e64ef33

Browse files
authored
Merge pull request #195 from berupp/custom_types_exec_fix
Fix for Exec(...) mocking with non-standard SQL data types ([]string)
2 parents e98392b + 181c3c5 commit e64ef33

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

expectations_go18.go

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

55
import (
66
"database/sql"
7-
"database/sql/driver"
87
"fmt"
98
"reflect"
109
)
@@ -54,10 +53,6 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
5453
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
5554
}
5655

57-
if !driver.IsValue(darg) {
58-
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
59-
}
60-
6156
if !reflect.DeepEqual(darg, v.Value) {
6257
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
6358
}

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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package sqlmock
22

33
import (
44
"database/sql/driver"
5+
"errors"
56
"fmt"
7+
"reflect"
68
"testing"
79
"time"
810
)
@@ -137,3 +139,57 @@ func TestBuildQuery(t *testing.T) {
137139
t.Error(err)
138140
}
139141
}
142+
143+
type CustomConverter struct{}
144+
145+
func (s CustomConverter) ConvertValue(v interface{}) (driver.Value, error) {
146+
switch v.(type) {
147+
case string:
148+
return v.(string), nil
149+
case []string:
150+
return v.([]string), nil
151+
case int:
152+
return v.(int), nil
153+
default:
154+
return nil, errors.New(fmt.Sprintf("cannot convert %T with value %v", v, v))
155+
}
156+
}
157+
func TestCustomValueConverterQueryScan(t *testing.T) {
158+
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
159+
query := `
160+
SELECT
161+
name,
162+
email,
163+
address,
164+
anotherfield
165+
FROM user
166+
where
167+
name = 'John'
168+
and
169+
address = 'Jakarta'
170+
171+
`
172+
expectedStringValue := "ValueOne"
173+
expectedIntValue := 2
174+
expectedArrayValue := []string{"Three", "Four"}
175+
mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"}))
176+
row := db.QueryRow(query)
177+
var stringValue string
178+
var intValue int
179+
var arrayValue []string
180+
if e := row.Scan(&stringValue, &intValue, &arrayValue); e != nil {
181+
t.Error(e)
182+
}
183+
if stringValue != expectedStringValue {
184+
t.Errorf("Expectation %s does not met: %s", expectedStringValue, stringValue)
185+
}
186+
if intValue != expectedIntValue {
187+
t.Errorf("Expectation %d does not met: %d", expectedIntValue, intValue)
188+
}
189+
if !reflect.DeepEqual(expectedArrayValue, arrayValue) {
190+
t.Errorf("Expectation %v does not met: %v", expectedArrayValue, arrayValue)
191+
}
192+
if err := mock.ExpectationsWereMet(); err != nil {
193+
t.Error(err)
194+
}
195+
}

0 commit comments

Comments
 (0)