mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-05-15 22:06:43 +02:00
Moving Exec(...) test to 1.9 build as NamedValueChecker is only supported since 1.9
This commit is contained in:
parent
4567d0a670
commit
cb3e425163
@ -3,6 +3,7 @@
|
|||||||
package sqlmock
|
package sqlmock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@ -39,6 +40,10 @@ 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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package sqlmock
|
package sqlmock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -194,38 +193,3 @@ func TestCustomValueConverterQueryScan(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user