1
2
3
4 package sqlmock
5
6 import (
7 "database/sql/driver"
8 "testing"
9 "time"
10 )
11
12 func TestQueryExpectationArgComparison(t *testing.T) {
13 e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
14 against := []namedValue{{Value: int64(5), Ordinal: 1}}
15 if err := e.argsMatches(against); err == nil {
16 t.Error("arguments should not match, since argument was passed, but noArgs was set")
17 }
18
19 e.noArgs = false
20 if err := e.argsMatches(against); err != nil {
21 t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
22 }
23
24 e.args = []driver.Value{5, "str"}
25
26 against = []namedValue{{Value: int64(5), Ordinal: 1}}
27 if err := e.argsMatches(against); err == nil {
28 t.Error("arguments should not match, since the size is not the same")
29 }
30
31 against = []namedValue{
32 {Value: int64(3), Ordinal: 1},
33 {Value: "str", Ordinal: 2},
34 }
35 if err := e.argsMatches(against); err == nil {
36 t.Error("arguments should not match, since the first argument (int value) is different")
37 }
38
39 against = []namedValue{
40 {Value: int64(5), Ordinal: 1},
41 {Value: "st", Ordinal: 2},
42 }
43 if err := e.argsMatches(against); err == nil {
44 t.Error("arguments should not match, since the second argument (string value) is different")
45 }
46
47 against = []namedValue{
48 {Value: int64(5), Ordinal: 1},
49 {Value: "str", Ordinal: 2},
50 }
51 if err := e.argsMatches(against); err != nil {
52 t.Errorf("arguments should match, but it did not: %s", err)
53 }
54
55 const longForm = "Jan 2, 2006 at 3:04pm (MST)"
56 tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
57 e.args = []driver.Value{5, tm}
58
59 against = []namedValue{
60 {Value: int64(5), Ordinal: 1},
61 {Value: tm, Ordinal: 2},
62 }
63 if err := e.argsMatches(against); err != nil {
64 t.Error("arguments should match, but it did not")
65 }
66
67 e.args = []driver.Value{5, AnyArg()}
68 if err := e.argsMatches(against); err != nil {
69 t.Errorf("arguments should match, but it did not: %s", err)
70 }
71 }
72
73 func TestQueryExpectationArgComparisonBool(t *testing.T) {
74 var e *queryBasedExpectation
75
76 e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
77 against := []namedValue{
78 {Value: true, Ordinal: 1},
79 }
80 if err := e.argsMatches(against); err != nil {
81 t.Error("arguments should match, since arguments are the same")
82 }
83
84 e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
85 against = []namedValue{
86 {Value: false, Ordinal: 1},
87 }
88 if err := e.argsMatches(against); err != nil {
89 t.Error("arguments should match, since argument are the same")
90 }
91
92 e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
93 against = []namedValue{
94 {Value: false, Ordinal: 1},
95 }
96 if err := e.argsMatches(against); err == nil {
97 t.Error("arguments should not match, since argument is different")
98 }
99
100 e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
101 against = []namedValue{
102 {Value: true, Ordinal: 1},
103 }
104 if err := e.argsMatches(against); err == nil {
105 t.Error("arguments should not match, since argument is different")
106 }
107 }
108
109 type panicConverter struct {
110 }
111
112 func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
113 panic(v)
114 }
115
116 func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
117 e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
118 values := []namedValue{
119 {Ordinal: 1, Name: "test", Value: "test"},
120 }
121 if err := e.attemptArgMatch(values); err == nil {
122 t.Errorf("error expected")
123 }
124 }
125
View as plain text