Skip to content

Commit 2ef7c14

Browse files
author
Nikita Koryabkin
committed
migration from custom namedValue to driver.NamedValue
1 parent 36d18c9 commit 2ef7c14

File tree

9 files changed

+30
-42
lines changed

9 files changed

+30
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/examples/blog/blog
22
/examples/orders/orders
33
/examples/basic/basic
4+
.idea/

expectations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ type queryBasedExpectation struct {
339339
args []driver.Value
340340
}
341341

342-
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
342+
func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
343343
// catch panic
344344
defer func() {
345345
if e := recover(); e != nil {

expectations_before_go18.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery {
1515
return e
1616
}
1717

18-
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
18+
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
1919
if nil == e.args {
2020
return nil
2121
}

expectations_go18.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package sqlmock
44

55
import (
66
"database/sql"
7+
"database/sql/driver"
78
"fmt"
89
"reflect"
910
)
@@ -19,7 +20,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
1920
return e
2021
}
2122

22-
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
23+
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
2324
if nil == e.args {
2425
return nil
2526
}

expectations_go18_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestQueryExpectationNamedArgComparison(t *testing.T) {
1212
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
13-
against := []namedValue{{Value: int64(5), Name: "id"}}
13+
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
1414
if err := e.argsMatches(against); err != nil {
1515
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
1616
}
@@ -24,7 +24,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
2424
t.Error("arguments should not match, since the size is not the same")
2525
}
2626

27-
against = []namedValue{
27+
against = []driver.NamedValue{
2828
{Value: int64(5), Name: "id"},
2929
{Value: "str", Name: "s"},
3030
}
@@ -33,7 +33,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
3333
t.Errorf("arguments should have matched, but it did not: %v", err)
3434
}
3535

36-
against = []namedValue{
36+
against = []driver.NamedValue{
3737
{Value: int64(5), Name: "id"},
3838
{Value: "str", Name: "username"},
3939
}
@@ -44,7 +44,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
4444

4545
e.args = []driver.Value{int64(5), "str"}
4646

47-
against = []namedValue{
47+
against = []driver.NamedValue{
4848
{Value: int64(5), Ordinal: 0},
4949
{Value: "str", Ordinal: 1},
5050
}
@@ -53,7 +53,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
5353
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
5454
}
5555

56-
against = []namedValue{
56+
against = []driver.NamedValue{
5757
{Value: int64(5), Ordinal: 1},
5858
{Value: "str", Ordinal: 2},
5959
}

expectations_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ import (
1111

1212
func TestQueryExpectationArgComparison(t *testing.T) {
1313
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
14-
against := []namedValue{{Value: int64(5), Ordinal: 1}}
14+
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
1515
if err := e.argsMatches(against); err != nil {
1616
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
1717
}
1818

1919
e.args = []driver.Value{5, "str"}
2020

21-
against = []namedValue{{Value: int64(5), Ordinal: 1}}
21+
against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
2222
if err := e.argsMatches(against); err == nil {
2323
t.Error("arguments should not match, since the size is not the same")
2424
}
2525

26-
against = []namedValue{
26+
against = []driver.NamedValue{
2727
{Value: int64(3), Ordinal: 1},
2828
{Value: "str", Ordinal: 2},
2929
}
3030
if err := e.argsMatches(against); err == nil {
3131
t.Error("arguments should not match, since the first argument (int value) is different")
3232
}
3333

34-
against = []namedValue{
34+
against = []driver.NamedValue{
3535
{Value: int64(5), Ordinal: 1},
3636
{Value: "st", Ordinal: 2},
3737
}
3838
if err := e.argsMatches(against); err == nil {
3939
t.Error("arguments should not match, since the second argument (string value) is different")
4040
}
4141

42-
against = []namedValue{
42+
against = []driver.NamedValue{
4343
{Value: int64(5), Ordinal: 1},
4444
{Value: "str", Ordinal: 2},
4545
}
@@ -51,7 +51,7 @@ func TestQueryExpectationArgComparison(t *testing.T) {
5151
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
5252
e.args = []driver.Value{5, tm}
5353

54-
against = []namedValue{
54+
against = []driver.NamedValue{
5555
{Value: int64(5), Ordinal: 1},
5656
{Value: tm, Ordinal: 2},
5757
}
@@ -69,31 +69,31 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
6969
var e *queryBasedExpectation
7070

7171
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
72-
against := []namedValue{
72+
against := []driver.NamedValue{
7373
{Value: true, Ordinal: 1},
7474
}
7575
if err := e.argsMatches(against); err != nil {
7676
t.Error("arguments should match, since arguments are the same")
7777
}
7878

7979
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
80-
against = []namedValue{
80+
against = []driver.NamedValue{
8181
{Value: false, Ordinal: 1},
8282
}
8383
if err := e.argsMatches(against); err != nil {
8484
t.Error("arguments should match, since argument are the same")
8585
}
8686

8787
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
88-
against = []namedValue{
88+
against = []driver.NamedValue{
8989
{Value: false, Ordinal: 1},
9090
}
9191
if err := e.argsMatches(against); err == nil {
9292
t.Error("arguments should not match, since argument is different")
9393
}
9494

9595
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
96-
against = []namedValue{
96+
against = []driver.NamedValue{
9797
{Value: true, Ordinal: 1},
9898
}
9999
if err := e.argsMatches(against); err == nil {

sqlmock.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ func (c *sqlmock) ExpectBegin() *ExpectedBegin {
247247

248248
// Exec meets http://golang.org/pkg/database/sql/driver/#Execer
249249
func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error) {
250-
namedArgs := make([]namedValue, len(args))
250+
namedArgs := make([]driver.NamedValue, len(args))
251251
for i, v := range args {
252-
namedArgs[i] = namedValue{
252+
namedArgs[i] = driver.NamedValue{
253253
Ordinal: i + 1,
254254
Value: v,
255255
}
@@ -266,7 +266,7 @@ func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error)
266266
return ex.result, nil
267267
}
268268

269-
func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
269+
func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, error) {
270270
var expected *ExpectedExec
271271
var fulfilled int
272272
var ok bool
@@ -401,17 +401,11 @@ func (c *sqlmock) ExpectPrepare(expectedSQL string) *ExpectedPrepare {
401401
return e
402402
}
403403

404-
type namedValue struct {
405-
Name string
406-
Ordinal int
407-
Value driver.Value
408-
}
409-
410404
// Query meets http://golang.org/pkg/database/sql/driver/#Queryer
411405
func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error) {
412-
namedArgs := make([]namedValue, len(args))
406+
namedArgs := make([]driver.NamedValue, len(args))
413407
for i, v := range args {
414-
namedArgs[i] = namedValue{
408+
namedArgs[i] = driver.NamedValue{
415409
Ordinal: i + 1,
416410
Value: v,
417411
}
@@ -428,7 +422,7 @@ func (c *sqlmock) Query(query string, args []driver.Value) (driver.Rows, error)
428422
return ex.rows, nil
429423
}
430424

431-
func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error) {
425+
func (c *sqlmock) query(query string, args []driver.NamedValue) (*ExpectedQuery, error) {
432426
var expected *ExpectedQuery
433427
var fulfilled int
434428
var ok bool

sqlmock_go18.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ var ErrCancelled = errors.New("canceling query due to user request")
1515

1616
// Implement the "QueryerContext" interface
1717
func (c *sqlmock) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
18-
namedArgs := make([]namedValue, len(args))
19-
for i, nv := range args {
20-
namedArgs[i] = namedValue(nv)
21-
}
22-
23-
ex, err := c.query(query, namedArgs)
18+
ex, err := c.query(query, args)
2419
if ex != nil {
2520
select {
2621
case <-time.After(ex.delay):
@@ -38,12 +33,7 @@ func (c *sqlmock) QueryContext(ctx context.Context, query string, args []driver.
3833

3934
// Implement the "ExecerContext" interface
4035
func (c *sqlmock) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
41-
namedArgs := make([]namedValue, len(args))
42-
for i, nv := range args {
43-
namedArgs[i] = namedValue(nv)
44-
}
45-
46-
ex, err := c.exec(query, namedArgs)
36+
ex, err := c.exec(query, args)
4737
if ex != nil {
4838
select {
4939
case <-time.After(ex.delay):

sqlmock_go19.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
// CheckNamedValue meets https://golang.org/pkg/database/sql/driver/#NamedValueChecker
1111
func (c *sqlmock) CheckNamedValue(nv *driver.NamedValue) (err error) {
1212
switch nv.Value.(type) {
13+
case sql.NamedArg:
14+
return nil
1315
case sql.Out:
1416
return nil
1517
default:

0 commit comments

Comments
 (0)