Skip to content

Commit cfb2877

Browse files
committed
tests Context sql driver extensions
1 parent 965003d commit cfb2877

File tree

8 files changed

+529
-215
lines changed

8 files changed

+529
-215
lines changed

arg_matcher_before_go18.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build !go1.8
2+
3+
package sqlmock
4+
5+
import (
6+
"database/sql/driver"
7+
"fmt"
8+
"reflect"
9+
)
10+
11+
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
12+
if nil == e.args {
13+
return nil
14+
}
15+
if len(args) != len(e.args) {
16+
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args))
17+
}
18+
for k, v := range args {
19+
// custom argument matcher
20+
matcher, ok := e.args[k].(Argument)
21+
if ok {
22+
// @TODO: does it make sense to pass value instead of named value?
23+
if !matcher.Match(v.Value) {
24+
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k])
25+
}
26+
continue
27+
}
28+
29+
dval := e.args[k]
30+
// convert to driver converter
31+
darg, err := driver.DefaultParameterConverter.ConvertValue(dval)
32+
if err != nil {
33+
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
34+
}
35+
36+
if !driver.IsValue(darg) {
37+
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
38+
}
39+
40+
if !reflect.DeepEqual(darg, v.Value) {
41+
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
42+
}
43+
}
44+
return nil
45+
}

arg_matcher_go18.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// +build go1.8
2+
3+
package sqlmock
4+
5+
import (
6+
"database/sql"
7+
"database/sql/driver"
8+
"fmt"
9+
"reflect"
10+
)
11+
12+
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
13+
if nil == e.args {
14+
return nil
15+
}
16+
if len(args) != len(e.args) {
17+
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args))
18+
}
19+
// @TODO should we assert either all args are named or ordinal?
20+
for k, v := range args {
21+
// custom argument matcher
22+
matcher, ok := e.args[k].(Argument)
23+
if ok {
24+
// @TODO: does it make sense to pass value instead of named value?
25+
if !matcher.Match(v.Value) {
26+
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k])
27+
}
28+
continue
29+
}
30+
31+
dval := e.args[k]
32+
if named, isNamed := dval.(sql.NamedArg); isNamed {
33+
dval = named.Value
34+
if v.Name != named.Name {
35+
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name)
36+
}
37+
}
38+
39+
// convert to driver converter
40+
darg, err := driver.DefaultParameterConverter.ConvertValue(dval)
41+
if err != nil {
42+
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
43+
}
44+
45+
if !driver.IsValue(darg) {
46+
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
47+
}
48+
49+
if !reflect.DeepEqual(darg, v.Value) {
50+
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
51+
}
52+
}
53+
return nil
54+
}

expectations.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sqlmock
33
import (
44
"database/sql/driver"
55
"fmt"
6-
"reflect"
76
"regexp"
87
"strings"
98
"sync"
@@ -355,49 +354,3 @@ func (e *queryBasedExpectation) attemptMatch(sql string, args []namedValue) (err
355354
func (e *queryBasedExpectation) queryMatches(sql string) bool {
356355
return e.sqlRegex.MatchString(sql)
357356
}
358-
359-
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
360-
if nil == e.args {
361-
return nil
362-
}
363-
if len(args) != len(e.args) {
364-
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args))
365-
}
366-
for k, v := range args {
367-
// custom argument matcher
368-
matcher, ok := e.args[k].(Argument)
369-
if ok {
370-
// @TODO: does it make sense to pass value instead of named value?
371-
if !matcher.Match(v.Value) {
372-
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k])
373-
}
374-
continue
375-
}
376-
377-
dval := e.args[k]
378-
if named, isNamed := dval.(namedValue); isNamed {
379-
dval = named.Value
380-
if v.Name != named.Name {
381-
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name)
382-
}
383-
if v.Ordinal != named.Ordinal {
384-
return fmt.Errorf("named argument %d: ordinal position: \"%d\" does not match expected: \"%d\"", k, v.Ordinal, named.Ordinal)
385-
}
386-
}
387-
388-
// convert to driver converter
389-
darg, err := driver.DefaultParameterConverter.ConvertValue(dval)
390-
if err != nil {
391-
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err)
392-
}
393-
394-
if !driver.IsValue(darg) {
395-
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg)
396-
}
397-
398-
if !reflect.DeepEqual(darg, v.Value) {
399-
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)
400-
}
401-
}
402-
return nil
403-
}

expectations_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -64,64 +64,6 @@ func TestQueryExpectationArgComparison(t *testing.T) {
6464
}
6565
}
6666

67-
func TestQueryExpectationNamedArgComparison(t *testing.T) {
68-
e := &queryBasedExpectation{}
69-
against := []namedValue{{Value: int64(5), Name: "id"}}
70-
if err := e.argsMatches(against); err != nil {
71-
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
72-
}
73-
74-
e.args = []driver.Value{
75-
namedValue{Name: "id", Value: int64(5)},
76-
namedValue{Name: "s", Value: "str"},
77-
}
78-
79-
if err := e.argsMatches(against); err == nil {
80-
t.Error("arguments should not match, since the size is not the same")
81-
}
82-
83-
against = []namedValue{
84-
{Value: int64(5), Name: "id"},
85-
{Value: "str", Name: "s"},
86-
}
87-
88-
if err := e.argsMatches(against); err != nil {
89-
t.Errorf("arguments should have matched, but it did not: %v", err)
90-
}
91-
92-
against = []namedValue{
93-
{Value: int64(5), Name: "id"},
94-
{Value: "str", Name: "username"},
95-
}
96-
97-
if err := e.argsMatches(against); err == nil {
98-
t.Error("arguments matched, but it should have not due to Name")
99-
}
100-
101-
e.args = []driver.Value{
102-
namedValue{Ordinal: 1, Value: int64(5)},
103-
namedValue{Ordinal: 2, Value: "str"},
104-
}
105-
106-
against = []namedValue{
107-
{Value: int64(5), Ordinal: 0},
108-
{Value: "str", Ordinal: 1},
109-
}
110-
111-
if err := e.argsMatches(against); err == nil {
112-
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
113-
}
114-
115-
against = []namedValue{
116-
{Value: int64(5), Ordinal: 1},
117-
{Value: "str", Ordinal: 2},
118-
}
119-
120-
if err := e.argsMatches(against); err != nil {
121-
t.Errorf("arguments should have matched, but it did not: %v", err)
122-
}
123-
}
124-
12567
func TestQueryExpectationArgComparisonBool(t *testing.T) {
12668
var e *queryBasedExpectation
12769

expectations_test_go18.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// +build go1.8
2+
3+
package sqlmock
4+
5+
import (
6+
"database/sql"
7+
"database/sql/driver"
8+
"testing"
9+
)
10+
11+
func TestQueryExpectationNamedArgComparison(t *testing.T) {
12+
e := &queryBasedExpectation{}
13+
against := []namedValue{{Value: int64(5), Name: "id"}}
14+
if err := e.argsMatches(against); err != nil {
15+
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
16+
}
17+
18+
e.args = []driver.Value{
19+
sql.Named("id", 5),
20+
sql.Named("s", "str"),
21+
}
22+
23+
if err := e.argsMatches(against); err == nil {
24+
t.Error("arguments should not match, since the size is not the same")
25+
}
26+
27+
against = []namedValue{
28+
{Value: int64(5), Name: "id"},
29+
{Value: "str", Name: "s"},
30+
}
31+
32+
if err := e.argsMatches(against); err != nil {
33+
t.Errorf("arguments should have matched, but it did not: %v", err)
34+
}
35+
36+
against = []namedValue{
37+
{Value: int64(5), Name: "id"},
38+
{Value: "str", Name: "username"},
39+
}
40+
41+
if err := e.argsMatches(against); err == nil {
42+
t.Error("arguments matched, but it should have not due to Name")
43+
}
44+
45+
e.args = []driver.Value{int64(5), "str"}
46+
47+
against = []namedValue{
48+
{Value: int64(5), Ordinal: 0},
49+
{Value: "str", Ordinal: 1},
50+
}
51+
52+
if err := e.argsMatches(against); err == nil {
53+
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
54+
}
55+
56+
against = []namedValue{
57+
{Value: int64(5), Ordinal: 1},
58+
{Value: "str", Ordinal: 2},
59+
}
60+
61+
if err := e.argsMatches(against); err != nil {
62+
t.Errorf("arguments should have matched, but it did not: %v", err)
63+
}
64+
}

0 commit comments

Comments
 (0)