Skip to content

Commit f3f5a5d

Browse files
author
Nikita Koryabkin
committed
added support for mammoths
1 parent 2ef7c14 commit f3f5a5d

8 files changed

+311
-125
lines changed

expectations.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,3 @@ type queryBasedExpectation struct {
338338
converter driver.ValueConverter
339339
args []driver.Value
340340
}
341-
342-
func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
343-
// catch panic
344-
defer func() {
345-
if e := recover(); e != nil {
346-
_, ok := e.(error)
347-
if !ok {
348-
err = fmt.Errorf(e.(string))
349-
}
350-
}
351-
}()
352-
353-
err = e.argsMatches(args)
354-
return
355-
}

expectations_before_go18.go

Lines changed: 16 additions & 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 []driver.NamedValue) error {
18+
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
1919
if nil == e.args {
2020
return nil
2121
}
@@ -50,3 +50,18 @@ func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
5050
}
5151
return nil
5252
}
53+
54+
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
55+
// catch panic
56+
defer func() {
57+
if e := recover(); e != nil {
58+
_, ok := e.(error)
59+
if !ok {
60+
err = fmt.Errorf(e.(string))
61+
}
62+
}
63+
}()
64+
65+
err = e.argsMatches(args)
66+
return
67+
}

expectations_before_go18_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// +build !go1.8
2+
3+
package sqlmock
4+
5+
func TestQueryExpectationArgComparison(t *testing.T) {
6+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
7+
against := []namedValue{{Value: int64(5), Ordinal: 1}}
8+
if err := e.argsMatches(against); err != nil {
9+
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
10+
}
11+
12+
e.args = []driver.Value{5, "str"}
13+
14+
against = []namedValue{{Value: int64(5), Ordinal: 1}}
15+
if err := e.argsMatches(against); err == nil {
16+
t.Error("arguments should not match, since the size is not the same")
17+
}
18+
19+
against = []namedValue{
20+
{Value: int64(3), Ordinal: 1},
21+
{Value: "str", Ordinal: 2},
22+
}
23+
if err := e.argsMatches(against); err == nil {
24+
t.Error("arguments should not match, since the first argument (int value) is different")
25+
}
26+
27+
against = []namedValue{
28+
{Value: int64(5), Ordinal: 1},
29+
{Value: "st", Ordinal: 2},
30+
}
31+
if err := e.argsMatches(against); err == nil {
32+
t.Error("arguments should not match, since the second argument (string value) is different")
33+
}
34+
35+
against = []namedValue{
36+
{Value: int64(5), Ordinal: 1},
37+
{Value: "str", Ordinal: 2},
38+
}
39+
if err := e.argsMatches(against); err != nil {
40+
t.Errorf("arguments should match, but it did not: %s", err)
41+
}
42+
43+
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
44+
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
45+
e.args = []driver.Value{5, tm}
46+
47+
against = []namedValue{
48+
{Value: int64(5), Ordinal: 1},
49+
{Value: tm, Ordinal: 2},
50+
}
51+
if err := e.argsMatches(against); err != nil {
52+
t.Error("arguments should match, but it did not")
53+
}
54+
55+
e.args = []driver.Value{5, AnyArg()}
56+
if err := e.argsMatches(against); err != nil {
57+
t.Errorf("arguments should match, but it did not: %s", err)
58+
}
59+
}
60+
61+
func TestQueryExpectationArgComparisonBool(t *testing.T) {
62+
var e *queryBasedExpectation
63+
64+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
65+
against := []namedValue{
66+
{Value: true, Ordinal: 1},
67+
}
68+
if err := e.argsMatches(against); err != nil {
69+
t.Error("arguments should match, since arguments are the same")
70+
}
71+
72+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
73+
against = []namedValue{
74+
{Value: false, Ordinal: 1},
75+
}
76+
if err := e.argsMatches(against); err != nil {
77+
t.Error("arguments should match, since argument are the same")
78+
}
79+
80+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
81+
against = []namedValue{
82+
{Value: false, Ordinal: 1},
83+
}
84+
if err := e.argsMatches(against); err == nil {
85+
t.Error("arguments should not match, since argument is different")
86+
}
87+
88+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
89+
against = []namedValue{
90+
{Value: true, Ordinal: 1},
91+
}
92+
if err := e.argsMatches(against); err == nil {
93+
t.Error("arguments should not match, since argument is different")
94+
}
95+
}

expectations_go18.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,18 @@ func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
6060
}
6161
return nil
6262
}
63+
64+
func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
65+
// catch panic
66+
defer func() {
67+
if e := recover(); e != nil {
68+
_, ok := e.(error)
69+
if !ok {
70+
err = fmt.Errorf(e.(string))
71+
}
72+
}
73+
}()
74+
75+
err = e.argsMatches(args)
76+
return
77+
}

expectations_go18_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,101 @@ import (
66
"database/sql"
77
"database/sql/driver"
88
"testing"
9+
"time"
910
)
1011

12+
func TestQueryExpectationArgComparison(t *testing.T) {
13+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
14+
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
15+
if err := e.argsMatches(against); err != nil {
16+
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
17+
}
18+
19+
e.args = []driver.Value{5, "str"}
20+
21+
against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
22+
if err := e.argsMatches(against); err == nil {
23+
t.Error("arguments should not match, since the size is not the same")
24+
}
25+
26+
against = []driver.NamedValue{
27+
{Value: int64(3), Ordinal: 1},
28+
{Value: "str", Ordinal: 2},
29+
}
30+
if err := e.argsMatches(against); err == nil {
31+
t.Error("arguments should not match, since the first argument (int value) is different")
32+
}
33+
34+
against = []driver.NamedValue{
35+
{Value: int64(5), Ordinal: 1},
36+
{Value: "st", Ordinal: 2},
37+
}
38+
if err := e.argsMatches(against); err == nil {
39+
t.Error("arguments should not match, since the second argument (string value) is different")
40+
}
41+
42+
against = []driver.NamedValue{
43+
{Value: int64(5), Ordinal: 1},
44+
{Value: "str", Ordinal: 2},
45+
}
46+
if err := e.argsMatches(against); err != nil {
47+
t.Errorf("arguments should match, but it did not: %s", err)
48+
}
49+
50+
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
51+
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
52+
e.args = []driver.Value{5, tm}
53+
54+
against = []driver.NamedValue{
55+
{Value: int64(5), Ordinal: 1},
56+
{Value: tm, Ordinal: 2},
57+
}
58+
if err := e.argsMatches(against); err != nil {
59+
t.Error("arguments should match, but it did not")
60+
}
61+
62+
e.args = []driver.Value{5, AnyArg()}
63+
if err := e.argsMatches(against); err != nil {
64+
t.Errorf("arguments should match, but it did not: %s", err)
65+
}
66+
}
67+
68+
func TestQueryExpectationArgComparisonBool(t *testing.T) {
69+
var e *queryBasedExpectation
70+
71+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
72+
against := []driver.NamedValue{
73+
{Value: true, Ordinal: 1},
74+
}
75+
if err := e.argsMatches(against); err != nil {
76+
t.Error("arguments should match, since arguments are the same")
77+
}
78+
79+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
80+
against = []driver.NamedValue{
81+
{Value: false, Ordinal: 1},
82+
}
83+
if err := e.argsMatches(against); err != nil {
84+
t.Error("arguments should match, since argument are the same")
85+
}
86+
87+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
88+
against = []driver.NamedValue{
89+
{Value: false, Ordinal: 1},
90+
}
91+
if err := e.argsMatches(against); err == nil {
92+
t.Error("arguments should not match, since argument is different")
93+
}
94+
95+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
96+
against = []driver.NamedValue{
97+
{Value: true, Ordinal: 1},
98+
}
99+
if err := e.argsMatches(against); err == nil {
100+
t.Error("arguments should not match, since argument is different")
101+
}
102+
}
103+
11104
func TestQueryExpectationNamedArgComparison(t *testing.T) {
12105
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
13106
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}

expectations_go19_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ package sqlmock
44

55
import (
66
"context"
7+
"database/sql/driver"
8+
"errors"
9+
"fmt"
710
"testing"
811
)
912

13+
type CustomConverter struct{}
14+
15+
func (s CustomConverter) ConvertValue(v interface{}) (driver.Value, error) {
16+
switch v.(type) {
17+
case string:
18+
return v.(string), nil
19+
case []string:
20+
return v.([]string), nil
21+
case int:
22+
return v.(int), nil
23+
default:
24+
return nil, errors.New(fmt.Sprintf("cannot convert %T with value %v", v, v))
25+
}
26+
}
27+
1028
func TestCustomValueConverterExec(t *testing.T) {
1129
db, mock, _ := New(ValueConverterOption(CustomConverter{}))
1230
expectedQuery := "INSERT INTO tags \\(name,email,age,hobbies\\) VALUES \\(\\?,\\?,\\?,\\?\\)"

0 commit comments

Comments
 (0)