mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-19 20:57:50 +02:00
Merge pull request #195 from berupp/custom_types_exec_fix
Fix for Exec(...) mocking with non-standard SQL data types ([]string)
This commit is contained in:
commit
e64ef33e8b
@ -4,7 +4,6 @@ package sqlmock
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@ -54,10 +53,6 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
|
|||||||
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
|
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !driver.IsValue(darg) {
|
|
||||||
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(darg, v.Value) {
|
if !reflect.DeepEqual(darg, v.Value) {
|
||||||
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
|
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
|
||||||
}
|
}
|
||||||
|
43
expectations_go19_test.go
Normal file
43
expectations_go19_test.go
Normal file
@ -0,0 +1,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 := "j@jj.j"
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,9 @@ package sqlmock
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -137,3 +139,57 @@ func TestBuildQuery(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CustomConverter struct{}
|
||||||
|
|
||||||
|
func (s CustomConverter) ConvertValue(v interface{}) (driver.Value, error) {
|
||||||
|
switch v.(type) {
|
||||||
|
case string:
|
||||||
|
return v.(string), nil
|
||||||
|
case []string:
|
||||||
|
return v.([]string), nil
|
||||||
|
case int:
|
||||||
|
return v.(int), nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New(fmt.Sprintf("cannot convert %T with value %v", v, v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestCustomValueConverterQueryScan(t *testing.T) {
|
||||||
|
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
|
||||||
|
query := `
|
||||||
|
SELECT
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
address,
|
||||||
|
anotherfield
|
||||||
|
FROM user
|
||||||
|
where
|
||||||
|
name = 'John'
|
||||||
|
and
|
||||||
|
address = 'Jakarta'
|
||||||
|
|
||||||
|
`
|
||||||
|
expectedStringValue := "ValueOne"
|
||||||
|
expectedIntValue := 2
|
||||||
|
expectedArrayValue := []string{"Three", "Four"}
|
||||||
|
mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"}))
|
||||||
|
row := db.QueryRow(query)
|
||||||
|
var stringValue string
|
||||||
|
var intValue int
|
||||||
|
var arrayValue []string
|
||||||
|
if e := row.Scan(&stringValue, &intValue, &arrayValue); e != nil {
|
||||||
|
t.Error(e)
|
||||||
|
}
|
||||||
|
if stringValue != expectedStringValue {
|
||||||
|
t.Errorf("Expectation %s does not met: %s", expectedStringValue, stringValue)
|
||||||
|
}
|
||||||
|
if intValue != expectedIntValue {
|
||||||
|
t.Errorf("Expectation %d does not met: %d", expectedIntValue, intValue)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedArrayValue, arrayValue) {
|
||||||
|
t.Errorf("Expectation %v does not met: %v", expectedArrayValue, arrayValue)
|
||||||
|
}
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user