-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
argument_test.go
190 lines (154 loc) · 5.3 KB
/
argument_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package pgxmock
import (
"context"
"errors"
"testing"
"time"
pgx "github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
)
type AnyTime struct{}
// Match satisfies pgxmock.Argument interface
func (a AnyTime) Match(v interface{}) bool {
_, ok := v.(time.Time)
return ok
}
func TestAnyTimeArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec("INSERT INTO users").
WithArgs("john", AnyTime{}).
WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(), "INSERT INTO users(name, created_at) VALUES (?, ?)", "john", time.Now())
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestAnyTimeNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec("INSERT INTO users").
WithArgs(pgx.NamedArgs{"name": "john", "time": AnyTime{}}).
WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(),
"INSERT INTO users(name, created_at) VALUES (@name, @time)",
pgx.NamedArgs{"name": "john", "time": time.Now()},
)
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestByteSliceArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
username := []byte("user")
mock.ExpectExec("INSERT INTO users").WithArgs(username).WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (?)", username)
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
type failQryRW struct {
pgx.QueryRewriter
}
func (fqrw failQryRW) RewriteQuery(_ context.Context, _ *pgx.Conn, sql string, _ []any) (newSQL string, newArgs []any, err error) {
return "", nil, errors.New("cannot rewrite query " + sql)
}
func TestExpectQueryRewriterFail(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectQuery(`INSERT INTO users\(username\) VALUES \(\@user\)`).
WithRewrittenSQL(`INSERT INTO users\(username\) VALUES \(\$1\)`).
WithArgs(failQryRW{})
_, err = mock.Query(context.Background(), "INSERT INTO users(username) VALUES (@user)", "baz")
assert.Error(t, err)
}
func TestQueryRewriterFail(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec(`INSERT INTO .+`).WithArgs("foo")
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (@user)", failQryRW{})
assert.Error(t, err)
}
func TestByteSliceNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
username := []byte("user")
mock.ExpectExec(`INSERT INTO users\(username\) VALUES \(\@user\)`).
WithArgs(pgx.NamedArgs{"user": username}).
WithRewrittenSQL(`INSERT INTO users\(username\) VALUES \(\$1\)`).
WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(),
"INSERT INTO users(username) VALUES (@user)",
pgx.NamedArgs{"user": username},
)
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestAnyArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec("INSERT INTO users").
WithArgs("john", AnyArg()).
WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(), "INSERT INTO users(name, created_at) VALUES (?, ?)", "john", time.Now())
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestAnyNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec("INSERT INTO users").
WithArgs("john", AnyArg()).
WillReturnResult(NewResult("INSERT", 1))
_, err = mock.Exec(context.Background(), "INSERT INTO users(name, created_at) VALUES (@name, @created)",
pgx.NamedArgs{"name": "john", "created": time.Now()},
)
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}