1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2024-11-21 17:17:08 +02:00

migration from custom namedValue to driver.NamedValue

This commit is contained in:
Nikita Koryabkin 2019-11-28 13:51:27 +03:00
parent 36d18c96ee
commit 2ef7c147be
9 changed files with 30 additions and 42 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/examples/blog/blog
/examples/orders/orders
/examples/basic/basic
.idea/

View File

@ -339,7 +339,7 @@ type queryBasedExpectation struct {
args []driver.Value
}
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
// catch panic
defer func() {
if e := recover(); e != nil {

View File

@ -15,7 +15,7 @@ func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery {
return e
}
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
if nil == e.args {
return nil
}

View File

@ -4,6 +4,7 @@ package sqlmock
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
)
@ -19,7 +20,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
return e
}
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
if nil == e.args {
return nil
}

View File

@ -10,7 +10,7 @@ import (
func TestQueryExpectationNamedArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
against := []namedValue{{Value: int64(5), Name: "id"}}
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
}
@ -24,7 +24,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
t.Error("arguments should not match, since the size is not the same")
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Name: "id"},
{Value: "str", Name: "s"},
}
@ -33,7 +33,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Name: "id"},
{Value: "str", Name: "username"},
}
@ -44,7 +44,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
e.args = []driver.Value{int64(5), "str"}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 0},
{Value: "str", Ordinal: 1},
}
@ -53,7 +53,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}

View File

@ -11,19 +11,19 @@ import (
func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
against := []namedValue{{Value: int64(5), Ordinal: 1}}
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
}
e.args = []driver.Value{5, "str"}
against = []namedValue{{Value: int64(5), Ordinal: 1}}
against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same")
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(3), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
@ -31,7 +31,7 @@ func TestQueryExpectationArgComparison(t *testing.T) {
t.Error("arguments should not match, since the first argument (int value) is different")
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "st", Ordinal: 2},
}
@ -39,7 +39,7 @@ func TestQueryExpectationArgComparison(t *testing.T) {
t.Error("arguments should not match, since the second argument (string value) is different")
}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
@ -51,7 +51,7 @@ func TestQueryExpectationArgComparison(t *testing.T) {
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
e.args = []driver.Value{5, tm}
against = []namedValue{
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: tm, Ordinal: 2},
}
@ -69,7 +69,7 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against := []namedValue{
against := []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
@ -77,7 +77,7 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
}
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
@ -85,7 +85,7 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
}
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against = []namedValue{
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
@ -93,7 +93,7 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
}
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{
against = []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {

View File

@ -247,9 +247,9 @@ func (c *sqlmock) ExpectBegin() *ExpectedBegin {
// Exec meets http://golang.org/pkg/database/sql/driver/#Execer
func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error) {
namedArgs := make([]namedValue, len(args))
namedArgs := make([]driver.NamedValue, len(args))
for i, v := range args {
namedArgs[i] = namedValue{
namedArgs[i] = driver.NamedValue{
Ordinal: i + 1,
Value: v,
}
@ -266,7 +266,7 @@ func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error)
return ex.result, nil
}
func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, error) {
var expected *ExpectedExec
var fulfilled int
var ok bool
@ -401,17 +401,11 @@ func (c *sqlmock) ExpectPrepare(expectedSQL string) *ExpectedPrepare {
return e
}
type namedValue struct {
Name string
Ordinal int
Value driver.Value
}
// Query meets http://golang.org/pkg/database/sql/driver/#Queryer
func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error) {
namedArgs := make([]namedValue, len(args))
namedArgs := make([]driver.NamedValue, len(args))
for i, v := range args {
namedArgs[i] = namedValue{
namedArgs[i] = driver.NamedValue{
Ordinal: i + 1,
Value: v,
}
@ -428,7 +422,7 @@ func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error)
return ex.rows, nil
}
func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error) {
func (c *sqlmock) query(query string, args []driver.NamedValue) (*ExpectedQuery, error) {
var expected *ExpectedQuery
var fulfilled int
var ok bool

View File

@ -15,12 +15,7 @@ var ErrCancelled = errors.New("canceling query due to user request")
// Implement the "QueryerContext" interface
func (c *sqlmock) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
namedArgs := make([]namedValue, len(args))
for i, nv := range args {
namedArgs[i] = namedValue(nv)
}
ex, err := c.query(query, namedArgs)
ex, err := c.query(query, args)
if ex != nil {
select {
case <-time.After(ex.delay):
@ -38,12 +33,7 @@ func (c *sqlmock) QueryContext(ctx context.Context, query string, args []driver.
// Implement the "ExecerContext" interface
func (c *sqlmock) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
namedArgs := make([]namedValue, len(args))
for i, nv := range args {
namedArgs[i] = namedValue(nv)
}
ex, err := c.exec(query, namedArgs)
ex, err := c.exec(query, args)
if ex != nil {
select {
case <-time.After(ex.delay):

View File

@ -10,6 +10,8 @@ import (
// CheckNamedValue meets https://golang.org/pkg/database/sql/driver/#NamedValueChecker
func (c *sqlmock) CheckNamedValue(nv *driver.NamedValue) (err error) {
switch nv.Value.(type) {
case sql.NamedArg:
return nil
case sql.Out:
return nil
default: