1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-03-17 20:48:01 +02:00
go-sqlmock/expectations_go18_test.go

175 lines
4.9 KiB
Go
Raw Permalink Normal View History

2017-02-07 15:03:05 +02:00
// +build go1.8
package sqlmock
import (
"database/sql"
"database/sql/driver"
"testing"
2019-12-02 14:39:36 +03:00
"time"
2017-02-07 15:03:05 +02:00
)
2019-12-02 14:39:36 +03:00
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")
}
}
2017-02-07 15:03:05 +02:00
func TestQueryExpectationNamedArgComparison(t *testing.T) {
2018-08-06 22:29:24 +02:00
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
2017-02-07 15:03:05 +02:00
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{
sql.Named("id", 5),
sql.Named("s", "str"),
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same")
}
against = []driver.NamedValue{
2017-02-07 15:03:05 +02:00
{Value: int64(5), Name: "id"},
{Value: "str", Name: "s"},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
against = []driver.NamedValue{
2017-02-07 15:03:05 +02:00
{Value: int64(5), Name: "id"},
{Value: "str", Name: "username"},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments matched, but it should have not due to Name")
}
e.args = []driver.Value{int64(5), "str"}
against = []driver.NamedValue{
2017-02-07 15:03:05 +02:00
{Value: int64(5), Ordinal: 0},
{Value: "str", Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
}
against = []driver.NamedValue{
2017-02-07 15:03:05 +02:00
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
}
2019-12-04 09:02:10 +03:00
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")
}
}