-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mocks.go
369 lines (291 loc) · 8.92 KB
/
Mocks.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
/*
* Copyright (c) 2020. App Nerds LLC. All rights reserved
*/
package database
import (
"bytes"
"encoding/gob"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
)
type SessionMock struct {
DBFunc func(name string) Database
CloseFunc func()
}
func (s *SessionMock) DB(name string) Database {
return s.DBFunc(name)
}
func (s *SessionMock) Close() {
s.CloseFunc()
}
type DatabaseMock struct {
CFunc func(name string) Collection
GridFSFunc func(prefix string) GridFS
}
func (d *DatabaseMock) C(name string) Collection {
return d.CFunc(name)
}
func (d *DatabaseMock) GridFS(prefix string) GridFS {
return d.GridFSFunc(prefix)
}
type CollectionMock struct {
CountFunc func() (int, error)
DropAllIndexesFunc func() error
DropCollectionFunc func() error
DropIndexFunc func(key ...string) error
DropIndexNameFunc func(name string) error
EnsureIndexFunc func(index mgo.Index) error
EnsureIndexKeyFunc func(key ...string) error
FindFunc func(query interface{}) Query
FindIdFunc func(id interface{}) Query
FindWithPagingFunc func(query interface{}, skip, limit int) (Query, int, error)
IndexesFunc func() ([]mgo.Index, error)
InsertFunc func(docs ...interface{}) error
RemoveFunc func(selector interface{}) error
RemoveAllFunc func(selector interface{}) (*mgo.ChangeInfo, error)
RemoveIdFunc func(id interface{}) error
UpdateFunc func(selector interface{}, update interface{}) error
UpdateAllFunc func(selector interface{}, update interface{}) (*mgo.ChangeInfo, error)
UpdateIdFunc func(id interface{}, update interface{}) error
UpsertFunc func(selector interface{}, update interface{}) (*mgo.ChangeInfo, error)
UpsertIdFunc func(id interface{}, update interface{}) (*mgo.ChangeInfo, error)
}
func (c *CollectionMock) Count() (int, error) {
return c.CountFunc()
}
func (c *CollectionMock) DropAllIndexes() error {
return c.DropAllIndexesFunc()
}
func (c *CollectionMock) DropCollection() error {
return c.DropCollectionFunc()
}
func (c *CollectionMock) DropIndex(key ...string) error {
return c.DropIndexFunc(key...)
}
func (c *CollectionMock) DropIndexName(name string) error {
return c.DropIndexNameFunc(name)
}
func (c *CollectionMock) EnsureIndex(index mgo.Index) error {
return c.EnsureIndexFunc(index)
}
func (c *CollectionMock) EnsureIndexKey(key ...string) error {
return c.EnsureIndexKeyFunc(key...)
}
func (c *CollectionMock) Find(query interface{}) Query {
return c.FindFunc(query)
}
func (c *CollectionMock) FindId(id interface{}) Query {
return c.FindIdFunc(id)
}
func (c *CollectionMock) FindWithPaging(query interface{}, skip, limit int) (Query, int, error) {
return c.FindWithPagingFunc(query, skip, limit)
}
func (c *CollectionMock) Indexes() ([]mgo.Index, error) {
return c.IndexesFunc()
}
func (c *CollectionMock) Insert(docs ...interface{}) error {
return c.InsertFunc(docs...)
}
func (c *CollectionMock) Remove(selector interface{}) error {
return c.RemoveFunc(selector)
}
func (c *CollectionMock) RemoveAll(selector interface{}) (*mgo.ChangeInfo, error) {
return c.RemoveAllFunc(selector)
}
func (c *CollectionMock) RemoveId(id interface{}) error {
return c.RemoveIdFunc(id)
}
func (c *CollectionMock) Update(selector interface{}, update interface{}) error {
return c.UpdateFunc(selector, update)
}
func (c *CollectionMock) UpdateAll(selector interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.UpdateAllFunc(selector, update)
}
func (c *CollectionMock) UpdateId(id interface{}, update interface{}) error {
return c.UpdateIdFunc(id, update)
}
func (c *CollectionMock) Upsert(selector interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.UpsertFunc(selector, update)
}
func (c *CollectionMock) UpsertId(id interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.UpsertIdFunc(id, update)
}
type QueryMock struct {
AllFunc func(result interface{}) error
CountFunc func() (int, error)
DistinctFunc func(key string, result interface{}) error
LimitFunc func(n int) Query
OneFunc func(result interface{}) error
SelectFunc func(selector interface{}) Query
SkipFunc func(n int) Query
SortFunc func(fields ...string) Query
}
func (q *QueryMock) All(result interface{}) error {
return q.AllFunc(result)
}
func (q *QueryMock) Count() (int, error) {
return q.CountFunc()
}
func (q *QueryMock) Distinct(key string, result interface{}) error {
return q.DistinctFunc(key, result)
}
func (q *QueryMock) Limit(n int) Query {
return q.LimitFunc(n)
}
func (q *QueryMock) One(result interface{}) error {
return q.OneFunc(result)
}
func (q *QueryMock) Select(selector interface{}) Query {
return q.SelectFunc(selector)
}
func (q *QueryMock) Skip(n int) Query {
return q.SkipFunc(n)
}
func (q *QueryMock) Sort(fields ...string) Query {
return q.SortFunc(fields...)
}
type GridFSMock struct {
CreateFunc func(name string) (GridFile, error)
FindFunc func(query interface{}) Query
OpenFunc func(name string) (GridFile, error)
OpenIdFunc func(id interface{}) (GridFile, error)
RemoveFunc func(name string) (err error)
RemoveIdFunc func(id interface{}) error
}
func (g *GridFSMock) Create(name string) (GridFile, error) {
return g.CreateFunc(name)
}
func (g *GridFSMock) Find(query interface{}) Query {
return g.FindFunc(query)
}
func (g *GridFSMock) Open(name string) (GridFile, error) {
return g.OpenFunc(name)
}
func (g *GridFSMock) OpenId(id interface{}) (GridFile, error) {
return g.OpenIdFunc(id)
}
func (g *GridFSMock) Remove(name string) (err error) {
return g.RemoveFunc(name)
}
func (g *GridFSMock) RemoveId(id interface{}) error {
return g.RemoveIdFunc(id)
}
type GridFileMock struct {
AbortFunc func()
CloseFunc func() error
ContentTypeFunc func() string
GetMetaFunc func(result interface{}) error
IdFunc func() interface{}
MD5Func func() string
NameFunc func() string
ReadFunc func(b []byte) (int, error)
SeekFunc func(offset int64, whence int) (int64, error)
SetChunkSizeFunc func(bytes int)
SetContentTypeFunc func(ctype string)
SetIdFunc func(id interface{})
SetMetaFunc func(metadata interface{})
SetNameFunc func(name string)
SetUploadDateFunc func(t time.Time)
SizeFunc func() int64
WriteFunc func(data []byte) (int, error)
}
func (g *GridFileMock) Abort() {
g.AbortFunc()
}
func (g *GridFileMock) Close() error {
return g.CloseFunc()
}
func (g *GridFileMock) ContentType() string {
return g.ContentTypeFunc()
}
func (g *GridFileMock) GetMeta(result interface{}) error {
return g.GetMetaFunc(result)
}
func (g *GridFileMock) Id() interface{} {
return g.IdFunc()
}
func (g *GridFileMock) MD5() string {
return g.MD5Func()
}
func (g *GridFileMock) Name() string {
return g.NameFunc()
}
func (g *GridFileMock) Read(b []byte) (int, error) {
return g.ReadFunc(b)
}
func (g *GridFileMock) Seek(offset int64, whence int) (int64, error) {
return g.SeekFunc(offset, whence)
}
func (g *GridFileMock) SetChunkSize(bytes int) {
g.SetChunkSizeFunc(bytes)
}
func (g *GridFileMock) SetContentType(ctype string) {
g.SetContentTypeFunc(ctype)
}
func (g *GridFileMock) SetId(id interface{}) {
g.SetIdFunc(id)
}
func (g *GridFileMock) SetMeta(metadata interface{}) {
g.SetMetaFunc(metadata)
}
func (g *GridFileMock) SetName(name string) {
g.SetName(name)
}
func (g *GridFileMock) SetUploadDate(t time.Time) {
g.SetUploadDateFunc(t)
}
func (g *GridFileMock) Size() int64 {
return g.SizeFunc()
}
func (g *GridFileMock) Write(data []byte) (int, error) {
return g.WriteFunc(data)
}
type IterMock struct {
AllFunc func(result interface{}) error
CloseFunc func() error
DoneFunc func() bool
ErrFunc func() error
ForFunc func(result interface{}, f func() error) error
NextFunc func(result interface{}) bool
StateFunc func() (int64, []bson.Raw)
TimeoutFunc func() bool
}
func (i *IterMock) All(result interface{}) error {
return i.AllFunc(result)
}
func (i *IterMock) Close() error {
return i.CloseFunc()
}
func (i *IterMock) Done() bool {
return i.DoneFunc()
}
func (i *IterMock) Err() error {
return i.ErrFunc()
}
func (i *IterMock) For(result interface{}, f func() error) error {
return i.ForFunc(result, f)
}
func (i *IterMock) Next(result interface{}) bool {
return i.NextFunc(result)
}
func (i *IterMock) State() (int64, []bson.Raw) {
return i.StateFunc()
}
func (i *IterMock) Timeout() bool {
return i.TimeoutFunc()
}
func WriteToResultInterface(value, result interface{}) error {
var err error
var b bytes.Buffer
encoder := gob.NewEncoder(&b)
decoder := gob.NewDecoder(&b)
if err = encoder.Encode(value); err != nil {
return errors.Wrapf(err, "Error encoding value in WriteToResultInterface")
}
if err = decoder.Decode(result); err != nil {
return errors.Wrapf(err, "Error decoding value to result in WriteToResultInterface")
}
return nil
}