Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/examples/blog/blog
/examples/orders/orders
/examples/basic/basic
.idea/
15 changes: 0 additions & 15 deletions expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,6 @@ type queryBasedExpectation struct {
args []driver.Value
}

func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
// catch panic
defer func() {
if e := recover(); e != nil {
_, ok := e.(error)
if !ok {
err = fmt.Errorf(e.(string))
}
}
}()

err = e.argsMatches(args)
return
}

// ExpectedPing is used to manage *sql.DB.Ping expectations.
// Returned by *Sqlmock.ExpectPing.
type ExpectedPing struct {
Expand Down
15 changes: 15 additions & 0 deletions expectations_before_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
}
return nil
}

func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
// catch panic
defer func() {
if e := recover(); e != nil {
_, ok := e.(error)
if !ok {
err = fmt.Errorf(e.(string))
}
}
}()

err = e.argsMatches(args)
return
}
118 changes: 118 additions & 0 deletions expectations_before_go18_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// +build !go1.8

package sqlmock

import (
"database/sql/driver"
"testing"
"time"
)

func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
against := []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}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same")
}

against = []namedValue{
{Value: int64(3), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the first argument (int value) is different")
}

against = []namedValue{
{Value: int64(5), Ordinal: 1},
{Value: "st", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the second argument (string value) is different")
}

against = []namedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}

const longForm = "Jan 2, 2006 at 3:04pm (MST)"
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
e.args = []driver.Value{5, tm}

against = []namedValue{
{Value: int64(5), Ordinal: 1},
{Value: tm, Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, but it did not")
}

e.args = []driver.Value{5, AnyArg()}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}
}

func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation

e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against := []namedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since arguments are the same")
}

e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument are the same")
}

e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against = []namedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}

e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []namedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}
}

type panicConverter struct {
}

func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
panic(v)
}

func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
values := []namedValue{
{Ordinal: 1, Name: "test", Value: "test"},
}
if err := e.attemptArgMatch(values); err == nil {
t.Errorf("error expected")
}
}
18 changes: 17 additions & 1 deletion expectations_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sqlmock

import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -59,3 +60,18 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
}
return nil
}

func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
// catch panic
defer func() {
if e := recover(); e != nil {
_, ok := e.(error)
if !ok {
err = fmt.Errorf(e.(string))
}
}
}()

err = e.argsMatches(args)
return
}
120 changes: 115 additions & 5 deletions expectations_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,104 @@ import (
"database/sql"
"database/sql/driver"
"testing"
"time"
)

func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
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 = []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 = []driver.NamedValue{
{Value: int64(3), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the first argument (int value) is different")
}

against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "st", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the second argument (string value) is different")
}

against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}

const longForm = "Jan 2, 2006 at 3:04pm (MST)"
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
e.args = []driver.Value{5, tm}

against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: tm, Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, but it did not")
}

e.args = []driver.Value{5, AnyArg()}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}
}

func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation

e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against := []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since arguments are the same")
}

e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument are the same")
}

e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}

e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}
}

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)
}
Expand All @@ -24,7 +117,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"},
}
Expand All @@ -33,7 +126,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"},
}
Expand All @@ -44,7 +137,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},
}
Expand All @@ -53,7 +146,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},
}
Expand All @@ -62,3 +155,20 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
}

type panicConverter struct {
}

func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
panic(v)
}

func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
values := []driver.NamedValue{
{Ordinal: 1, Name: "test", Value: "test"},
}
if err := e.attemptArgMatch(values); err == nil {
t.Errorf("error expected")
}
}
Loading