-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
210 lines (178 loc) · 5.06 KB
/
testing.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package bunoffe
import (
"context"
"database/sql"
"fmt"
"reflect"
"github.com/DATA-DOG/go-sqlmock"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
)
type (
// MockQueryExecutor is an Executor that ignores the queries
// passed to its methods (Exec, Scan, and Exists). Instead,
// the returned values and values assigned to the model are
// the ones provided to operations (Ops field).
MockQueryExecutor struct {
// Ops is a slice of operations. Each time an Executor method
// is called, next operation in line (starting with the first)
// will be executed.
Ops []MockedQueryOperation
idx int
}
// MockedQueryOperation is interface that works as common type
// for all mock operations.
MockedQueryOperation interface {
doNothing()
}
// MockExecOperation is a type to mock a Exec call.
MockExecOperation struct {
// If Model is not nil and Error is nil, when Exec is called, it will
// contain the value passed to the query method `.Model(&m)`.
Model any
// If Args is not nil and Error is nil, when Exec is called, each of
// its values will be assigned to parameter `...args`.
Args []any
// If Result is not nil and Error is nil, when Exec is called, it will
// return Result.
Result sql.Result
// If Error is not nil, Exec will return a nil sql.Result and this
// Error.
Error error
}
// MockScanOperation is a type to mock a Scan call.
MockScanOperation struct {
// If Model is not nil and Error is nil, when Scan is called, it will
// be assigned the value passed to the query method `.Model(&m)`.
Model any
// If Args is not nil and Error is nil, when Exec is called, each of
// its values will be assigned to parameter `...args`.
Args []any
// If Error is not nil, Scan will return it.
Error error
}
MockExistsOperation struct {
// If Error is not nil, this value will be returned when Exists is
// called. Otherwise false is returned.
Exists bool
// If Error is not nil, Scan will return it.
Error error
}
MockQueryResult struct {
LastInsertIdValue int64
LastInsertIdError error
RowsAffectedValue int64
RowsAffectedError error
}
)
func (MockExecOperation) doNothing() {}
func (MockScanOperation) doNothing() {}
func (MockExistsOperation) doNothing() {}
// Creates a *bun.DB with a mocked database.
func NewMockedBunDB() (*bun.DB, error) {
sqldb, _, err := sqlmock.New()
if err != nil {
return nil, err
}
return bun.NewDB(sqldb, sqlitedialect.New()), nil
}
// Exec mocks a query.Exec call. See the MockExecOperation documentation for details.
func (ex *MockQueryExecutor) Exec(
ctx context.Context,
q ExecQuery,
args ...any,
) (sql.Result, error) {
nop := ex.nextOp()
op, ok := nop.(MockExecOperation)
if !ok {
panic(opCastError("MockExec", nop))
}
if op.Error != nil {
return nil, op.Error
}
if op.Model != nil {
assign(
reflect.ValueOf(op.Model),
reflect.ValueOf(q.GetModel().Value()),
)
}
if len(op.Args) > 0 && len(op.Args) != len(args) {
panic("operation.Args and args should have the same length")
}
for i, val := range op.Args {
assign(
reflect.ValueOf(args[i]),
reflect.ValueOf(val),
)
}
return op.Result, nil
}
// Exec mocks a query.Scan call. See the MockScanOperation documentation for details.
func (ex *MockQueryExecutor) Scan(ctx context.Context, q ScanQuery, args ...any) error {
nop := ex.nextOp()
op, ok := nop.(MockScanOperation)
if !ok {
panic(opCastError("MockScan", nop))
}
if op.Error != nil {
return op.Error
}
if op.Model != nil {
assign(
reflect.ValueOf(q.GetModel().Value()),
reflect.ValueOf(op.Model),
)
}
for i, val := range op.Args {
assign(
reflect.ValueOf(args[i]),
reflect.ValueOf(val),
)
}
return nil
}
// Exec mocks a query.Exists call. See the MockExistsOperation documentation for details.
func (ex *MockQueryExecutor) Exists(ctx context.Context, q ExistsQuery) (bool, error) {
nop := ex.nextOp()
op, ok := nop.(MockExistsOperation)
if !ok {
panic(opCastError("MockExists", nop))
}
if op.Error != nil {
return false, op.Error
}
return op.Exists, nil
}
func (ex *MockQueryExecutor) nextOp() MockedQueryOperation {
if len(ex.Ops) <= ex.idx {
s := fmt.Sprintf(
"mocked query requested operation #%v, but test only contains %v",
ex.idx,
len(ex.Ops),
)
panic(s)
}
ex.idx++
return ex.Ops[ex.idx-1]
}
func (r MockQueryResult) LastInsertId() (int64, error) {
return r.LastInsertIdValue, r.LastInsertIdError
}
func (r MockQueryResult) RowsAffected() (int64, error) {
return r.RowsAffectedValue, r.RowsAffectedError
}
func opCastError(expected string, found any) string {
return fmt.Sprintf("expected '%v' operation, but found '%T'", expected, found)
}
func assign(dest reflect.Value, src reflect.Value) {
switch {
case dest.Kind() == reflect.Ptr && src.Kind() == reflect.Ptr:
dest.Elem().Set(src.Elem())
case dest.Kind() == reflect.Ptr && src.Kind() != reflect.Ptr:
dest.Elem().Set(src)
case dest.Kind() != reflect.Ptr && src.Kind() == reflect.Ptr:
dest.Set(src.Elem())
case dest.Kind() != reflect.Ptr && src.Kind() != reflect.Ptr:
dest.Set(src.Elem())
}
}