-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepository.go
377 lines (315 loc) · 12.6 KB
/
repository.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package reltest
import (
"context"
"database/sql"
"runtime"
"github.com/go-rel/rel"
)
var (
// ErrConnectionClosed is alias for sql.ErrConnDone.
ErrConnectionClosed = sql.ErrConnDone
)
// Repository mock
type Repository struct {
repo rel.Repository
iterate iterate
count count
aggregate aggregate
find find
findAll findAll
findAndCountAll findAndCountAll
insert mutate
insertAll insertAll
update mutate
updateAny updateAny
delete delete
deleteAll deleteAll
deleteAny deleteAny
exec exec
preload preload
transaction Assert
ctxData ctxData
}
var _ rel.Repository = (*Repository)(nil)
// Adapter provides a mock function with given fields:
func (r *Repository) Adapter(ctx context.Context) rel.Adapter {
return r.repo.Adapter(ctx)
}
// Instrumentation provides a mock function with given fields: instrumenter
func (r *Repository) Instrumentation(instrumenter rel.Instrumenter) {
r.repo.Instrumentation(instrumenter)
}
// Ping database.
func (r *Repository) Ping(ctx context.Context) error {
return r.repo.Ping(ctx)
}
// Iterate through a collection of entities from database in batches.
// This function returns iterator that can be used to loop all entities.
// Limit, Offset and Sort query is automatically ignored.
func (r *Repository) Iterate(ctx context.Context, query rel.Query, options ...rel.IteratorOption) rel.Iterator {
return r.iterate.execute(ctx, query, options...)
}
// ExpectIterate apply mocks and expectations for Iterate
func (r *Repository) ExpectIterate(query rel.Query, options ...rel.IteratorOption) *MockIterate {
return r.iterate.register(r.ctxData, query, options...)
}
// Aggregate provides a mock function with given fields: query, aggregate, field
func (r *Repository) Aggregate(ctx context.Context, query rel.Query, aggregate string, field string) (int, error) {
r.repo.Aggregate(ctx, query, aggregate, field)
return r.aggregate.execute(ctx, query, aggregate, field)
}
// MustAggregate provides a mock function with given fields: query, aggregate, field
func (r *Repository) MustAggregate(ctx context.Context, query rel.Query, aggregate string, field string) int {
result, err := r.Aggregate(ctx, query, aggregate, field)
must(err)
return result
}
// ExpectAggregate apply mocks and expectations for Aggregate
func (r *Repository) ExpectAggregate(query rel.Query, aggregate string, field string) *MockAggregate {
return r.aggregate.register(r.ctxData, query, aggregate, field)
}
// Count provides a mock function with given fields: collection, queriers
func (r *Repository) Count(ctx context.Context, collection string, queriers ...rel.Querier) (int, error) {
r.repo.Count(ctx, collection, queriers...)
return r.count.execute(ctx, collection, queriers...)
}
// MustCount provides a mock function with given fields: collection, queriers
func (r *Repository) MustCount(ctx context.Context, collection string, queriers ...rel.Querier) int {
count, err := r.Count(ctx, collection, queriers...)
must(err)
return count
}
// ExpectCount apply mocks and expectations for Count
func (r *Repository) ExpectCount(collection string, queriers ...rel.Querier) *MockCount {
return r.count.register(r.ctxData, collection, queriers...)
}
// Find provides a mock function with given fields: entity, queriers
func (r *Repository) Find(ctx context.Context, entity any, queriers ...rel.Querier) error {
r.repo.Find(ctx, entity, queriers...)
return r.find.execute(ctx, entity, queriers...)
}
// MustFind provides a mock function with given fields: entity, queriers
func (r *Repository) MustFind(ctx context.Context, entity any, queriers ...rel.Querier) {
must(r.Find(ctx, entity, queriers...))
}
// ExpectFind apply mocks and expectations for Find
func (r *Repository) ExpectFind(queriers ...rel.Querier) *MockFind {
return r.find.register(r.ctxData, queriers...)
}
// FindAll provides a mock function with given fields: entities, queriers
func (r *Repository) FindAll(ctx context.Context, entities any, queriers ...rel.Querier) error {
r.repo.FindAll(ctx, entities, queriers...)
return r.findAll.execute(ctx, entities, queriers...)
}
// MustFindAll provides a mock function with given fields: entities, queriers
func (r *Repository) MustFindAll(ctx context.Context, entities any, queriers ...rel.Querier) {
must(r.FindAll(ctx, entities, queriers...))
}
// ExpectFindAll apply mocks and expectations for FindAll
func (r *Repository) ExpectFindAll(queriers ...rel.Querier) *MockFindAll {
return r.findAll.register(r.ctxData, queriers...)
}
// FindAndCountAll provides a mock function with given fields: entities, queriers
func (r *Repository) FindAndCountAll(ctx context.Context, entities any, queriers ...rel.Querier) (int, error) {
r.repo.FindAndCountAll(ctx, entities, queriers...)
return r.findAndCountAll.execute(ctx, entities, queriers...)
}
// MustFindAndCountAll provides a mock function with given fields: entities, queriers
func (r *Repository) MustFindAndCountAll(ctx context.Context, entities any, queriers ...rel.Querier) int {
count, err := r.FindAndCountAll(ctx, entities, queriers...)
must(err)
return count
}
// ExpectFindAndCountAll apply mocks and expectations for FindAndCountAll
func (r *Repository) ExpectFindAndCountAll(queriers ...rel.Querier) *MockFindAndCountAll {
return r.findAndCountAll.register(r.ctxData, queriers...)
}
// Insert provides a mock function with given fields: entity, mutators
func (r *Repository) Insert(ctx context.Context, entity any, mutators ...rel.Mutator) error {
ret := r.insert.execute("Insert", ctx, entity, mutators...)
r.repo.Insert(ctx, entity, mutators...)
return ret
}
// MustInsert provides a mock function with given fields: entity, mutators
func (r *Repository) MustInsert(ctx context.Context, entity any, mutators ...rel.Mutator) {
must(r.Insert(ctx, entity, mutators...))
}
// ExpectInsert apply mocks and expectations for Insert
func (r *Repository) ExpectInsert(mutators ...rel.Mutator) *MockMutate {
return r.insert.register("Insert", r.ctxData, mutators...)
}
// InsertAll entities.
func (r *Repository) InsertAll(ctx context.Context, entities any, mutators ...rel.Mutator) error {
r.repo.InsertAll(ctx, entities, mutators...)
return r.insertAll.execute(ctx, entities)
}
// MustInsertAll entities.
func (r *Repository) MustInsertAll(ctx context.Context, entities any, mutators ...rel.Mutator) {
must(r.InsertAll(ctx, entities, mutators...))
}
// ExpectInsertAll entities.
func (r *Repository) ExpectInsertAll() *MockInsertAll {
return r.insertAll.register(r.ctxData)
}
// Update provides a mock function with given fields: entity, mutators
func (r *Repository) Update(ctx context.Context, entity any, mutators ...rel.Mutator) error {
ret := r.update.execute("Update", ctx, entity, mutators...)
if err := r.repo.Update(ctx, entity, mutators...); err != nil {
return err
}
return ret
}
// MustUpdate provides a mock function with given fields: entity, mutators
func (r *Repository) MustUpdate(ctx context.Context, entity any, mutators ...rel.Mutator) {
must(r.Update(ctx, entity, mutators...))
}
// ExpectUpdate apply mocks and expectations for Update
func (r *Repository) ExpectUpdate(mutators ...rel.Mutator) *MockMutate {
return r.update.register("Update", r.ctxData, mutators...)
}
// UpdateAny provides a mock function with given fields: query
func (r *Repository) UpdateAny(ctx context.Context, query rel.Query, mutates ...rel.Mutate) (int, error) {
return r.updateAny.execute(ctx, query, mutates...)
}
// MustUpdateAny provides a mock function with given fields: query
func (r *Repository) MustUpdateAny(ctx context.Context, query rel.Query, mutates ...rel.Mutate) int {
r.repo.UpdateAny(ctx, query, mutates...)
updatedCount, err := r.UpdateAny(ctx, query, mutates...)
must(err)
return updatedCount
}
// ExpectUpdateAny apply mocks and expectations for UpdateAny
func (r *Repository) ExpectUpdateAny(query rel.Query, mutates ...rel.Mutate) *MockUpdateAny {
return r.updateAny.register(r.ctxData, query, mutates...)
}
// Delete provides a mock function with given fields: entity
func (r *Repository) Delete(ctx context.Context, entity any, options ...rel.Mutator) error {
r.repo.Delete(ctx, entity)
return r.delete.execute(ctx, entity, options...)
}
// MustDelete provides a mock function with given fields: entity
func (r *Repository) MustDelete(ctx context.Context, entity any, options ...rel.Mutator) {
must(r.Delete(ctx, entity, options...))
}
// ExpectDelete apply mocks and expectations for Delete
func (r *Repository) ExpectDelete(options ...rel.Mutator) *MockDelete {
return r.delete.register(r.ctxData, options...)
}
// DeleteAll provides DeleteAll mock function with given fields: entities
func (r *Repository) DeleteAll(ctx context.Context, entities any) error {
r.repo.DeleteAll(ctx, entities)
return r.deleteAll.execute(ctx, entities)
}
// MustDeleteAll provides a mock function with given fields: entity
func (r *Repository) MustDeleteAll(ctx context.Context, entity any) {
must(r.DeleteAll(ctx, entity))
}
// ExpectDeleteAll apply mocks and expectations for DeleteAll
func (r *Repository) ExpectDeleteAll() *MockDeleteAll {
return r.deleteAll.register(r.ctxData)
}
// DeleteAny provides a mock function with given fields: query
func (r *Repository) DeleteAny(ctx context.Context, query rel.Query) (int, error) {
r.repo.DeleteAny(ctx, query)
return r.deleteAny.execute(ctx, query)
}
// MustDeleteAny provides a mock function with given fields: query
func (r *Repository) MustDeleteAny(ctx context.Context, query rel.Query) int {
deletedCount, err := r.DeleteAny(ctx, query)
must(err)
return deletedCount
}
// ExpectDeleteAny apply mocks and expectations for DeleteAny
func (r *Repository) ExpectDeleteAny(query rel.Query) *MockDeleteAny {
return r.deleteAny.register(r.ctxData, query)
}
// Preload provides a mock function with given fields: entities, field, queriers
func (r *Repository) Preload(ctx context.Context, entities any, field string, queriers ...rel.Querier) error {
return r.preload.execute(ctx, entities, field, queriers...)
}
// MustPreload provides a mock function with given fields: entities, field, queriers
func (r *Repository) MustPreload(ctx context.Context, entities any, field string, queriers ...rel.Querier) {
must(r.Preload(ctx, entities, field, queriers...))
}
// ExpectPreload apply mocks and expectations for Preload
func (r *Repository) ExpectPreload(field string, queriers ...rel.Querier) *MockPreload {
return r.preload.register(r.ctxData, field, queriers...)
}
// Exec raw statement.
// Returns last inserted id, rows affected and error.
func (r *Repository) Exec(ctx context.Context, statement string, args ...any) (int, int, error) {
return r.exec.execute(ctx, statement, args)
}
// MustExec raw statement.
// Returns last inserted id, rows affected and error.
func (r *Repository) MustExec(ctx context.Context, statement string, args ...any) (int, int) {
lastInsertedId, rowsAffected, err := r.Exec(ctx, statement, args...)
must(err)
return lastInsertedId, rowsAffected
}
// ExpectExec for mocking Exec
func (r *Repository) ExpectExec(statement string, args ...any) *MockExec {
return r.exec.register(r.ctxData, statement, args...)
}
// Transaction provides a mock function with given fields: fn
func (r *Repository) Transaction(ctx context.Context, fn func(ctx context.Context) error) error {
ctxData := fetchContext(ctx)
r.transaction.call(ctx)
var err error
func() {
defer func() {
if p := recover(); p != nil {
switch e := p.(type) {
case runtime.Error:
panic(e)
case error:
err = e
default:
panic(e)
}
}
}()
ctxData.txDepth++
ctx = wrapContext(ctx, ctxData)
err = fn(ctx)
}()
return err
}
// ExpectTransaction declare expectation inside transaction.
func (r *Repository) ExpectTransaction(fn func(*Repository)) {
r.transaction.repeatability++
r.ctxData.txDepth++
fn(r)
r.ctxData.txDepth--
}
// AssertExpectations asserts that everything was in fact called as expected. Calls may have occurred in any order.
func (r *Repository) AssertExpectations(t TestingT) bool {
t.Helper()
return r.iterate.assert(t) &&
r.count.assert(t) &&
r.aggregate.assert(t) &&
r.find.assert(t) &&
r.findAll.assert(t) &&
r.findAndCountAll.assert(t) &&
r.insert.assert(t) &&
r.insertAll.assert(t) &&
r.update.assert(t) &&
r.updateAny.assert(t) &&
r.delete.assert(t) &&
r.deleteAll.assert(t) &&
r.deleteAny.assert(t) &&
r.exec.assert(t) &&
r.preload.assert(t)
// TODO: r.transaction.assert(t)
}
// New test repository.
func New() *Repository {
return &Repository{
repo: rel.New(&nopAdapter{}),
}
}
func must(err error) {
if err != nil {
panic(err)
}
}