1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-06-06 23:26:14 +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/blog/blog
/examples/orders/orders /examples/orders/orders
/examples/basic/basic /examples/basic/basic
.idea/

View File

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

View File

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

View File

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

View File

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

View File

@ -11,19 +11,19 @@ import (
func TestQueryExpectationArgComparison(t *testing.T) { func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter} 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 { if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err) t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
} }
e.args = []driver.Value{5, "str"} 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 { if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same") t.Error("arguments should not match, since the size is not the same")
} }
against = []namedValue{ against = []driver.NamedValue{
{Value: int64(3), Ordinal: 1}, {Value: int64(3), Ordinal: 1},
{Value: "str", Ordinal: 2}, {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") 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: int64(5), Ordinal: 1},
{Value: "st", Ordinal: 2}, {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") 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: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2}, {Value: "str", Ordinal: 2},
} }
@ -51,7 +51,7 @@ func TestQueryExpectationArgComparison(t *testing.T) {
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
e.args = []driver.Value{5, tm} e.args = []driver.Value{5, tm}
against = []namedValue{ against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1}, {Value: int64(5), Ordinal: 1},
{Value: tm, Ordinal: 2}, {Value: tm, Ordinal: 2},
} }
@ -69,7 +69,7 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation var e *queryBasedExpectation
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter} e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against := []namedValue{ against := []driver.NamedValue{
{Value: true, Ordinal: 1}, {Value: true, Ordinal: 1},
} }
if err := e.argsMatches(against); err != nil { 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} e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{ against = []driver.NamedValue{
{Value: false, Ordinal: 1}, {Value: false, Ordinal: 1},
} }
if err := e.argsMatches(against); err != nil { 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} e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against = []namedValue{ against = []driver.NamedValue{
{Value: false, Ordinal: 1}, {Value: false, Ordinal: 1},
} }
if err := e.argsMatches(against); err == nil { 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} e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{ against = []driver.NamedValue{
{Value: true, Ordinal: 1}, {Value: true, Ordinal: 1},
} }
if err := e.argsMatches(against); err == nil { 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 // Exec meets http://golang.org/pkg/database/sql/driver/#Execer
func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error) { 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 { for i, v := range args {
namedArgs[i] = namedValue{ namedArgs[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, Value: v,
} }
@ -266,7 +266,7 @@ func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error)
return ex.result, nil 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 expected *ExpectedExec
var fulfilled int var fulfilled int
var ok bool var ok bool
@ -401,17 +401,11 @@ func (c *sqlmock) ExpectPrepare(expectedSQL string) *ExpectedPrepare {
return e return e
} }
type namedValue struct {
Name string
Ordinal int
Value driver.Value
}
// Query meets http://golang.org/pkg/database/sql/driver/#Queryer // Query meets http://golang.org/pkg/database/sql/driver/#Queryer
func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error) { 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 { for i, v := range args {
namedArgs[i] = namedValue{ namedArgs[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, Value: v,
} }
@ -428,7 +422,7 @@ func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error)
return ex.rows, nil 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 expected *ExpectedQuery
var fulfilled int var fulfilled int
var ok bool var ok bool

View File

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

View File

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