-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbexpectations.go
667 lines (594 loc) · 18.1 KB
/
dbexpectations.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package kivikmock
import (
"encoding/json"
"fmt"
"github.com/go-kivik/kivik/v4/driver"
)
// ExpectedDBClose is used to manage *kivik.Client.Close expectation returned
// by Mock.ExpectClose.
type ExpectedDBClose struct {
commonExpectation
callback func() error
}
func (e *ExpectedDBClose) method(v bool) string {
if v {
return "DB.Close(ctx)"
}
return "DB.Close()"
}
func (e *ExpectedDBClose) met(ex expectation) bool {
return true
}
// WillReturnError allows setting an error for *kivik.Client.Close action.
func (e *ExpectedDBClose) WillReturnError(err error) *ExpectedDBClose {
e.err = err
return e
}
// WillExecute sets a callback function to be called with any inputs to the
// original function. Any values returned by the callback will be returned as
// if generated by the driver.
func (e *ExpectedDBClose) WillExecute(cb func() error) *ExpectedDBClose {
e.callback = cb
return e
}
func (e *ExpectedDBClose) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).Close()", e.dbo().name, e.dbo().id)
extra := delayString(e.delay)
extra += errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedAllDocs) String() string {
var rets []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("AllDocs", &e.commonExpectation, withOptions, nil, rets)
}
func jsonDoc(i interface{}) string {
jsonText, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("<invalid json:%s>", err)
}
return string(jsonText)
}
func (e *ExpectedBulkGet) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).BulkGet() which:", e.dbo().name, e.dbo().id)
if e.arg0 == nil {
msg += "\n\t- has any doc references"
} else {
msg += fmt.Sprintf("\n\t- has doc references: %v", jsonDoc(e.arg0))
}
msg += optionsString(e.options)
if e.ret0 != nil {
msg += fmt.Sprintf("\n\t- should return: %d results", e.ret0.count())
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedFind) String() string {
var opts, rets []string
if e.arg0 == nil {
opts = append(opts, "has any query")
} else {
opts = append(opts, fmt.Sprintf("has query: %v", e.arg0))
}
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("Find", &e.commonExpectation, withOptions, opts, rets)
}
// WithQuery sets the expected query for the Find() call.
func (e *ExpectedFind) WithQuery(query interface{}) *ExpectedFind {
e.arg0 = query
return e
}
func (e *ExpectedCreateIndex) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).CreateIndex() which:", e.dbo().name, e.dbo().id)
if e.arg0 == "" {
msg += "\n\t- has any ddoc"
} else {
msg += "\n\t- has ddoc: " + e.arg0
}
if e.arg1 == "" {
msg += "\n\t- has any name"
} else {
msg += "\n\t- has name: " + e.arg1
}
if e.arg2 == nil {
msg += "\n\t- has any index"
} else {
msg += fmt.Sprintf("\n\t- has index: %v", e.arg2)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
// WithDDocID sets the expected ddocID value for the DB.CreateIndex() call.
func (e *ExpectedCreateIndex) WithDDocID(ddocID string) *ExpectedCreateIndex {
e.arg0 = ddocID
return e
}
// WithName sets the expected name value for the DB.CreateIndex() call.
func (e *ExpectedCreateIndex) WithName(name string) *ExpectedCreateIndex {
e.arg1 = name
return e
}
// WithIndex sets the expected index value for the DB.CreateIndex() call.
func (e *ExpectedCreateIndex) WithIndex(index interface{}) *ExpectedCreateIndex {
e.arg2 = index
return e
}
func (e *ExpectedGetIndexes) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).GetIndexes()", e.dbo().name, e.dbo().id)
var extra string
if e.ret0 != nil {
extra += fmt.Sprintf("\n\t- should return indexes: %v", e.ret0)
}
extra += delayString(e.delay)
extra += errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedDeleteIndex) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).DeleteIndex() which:", e.dbo().name, e.dbo().id)
msg += fieldString("ddoc", e.arg0)
msg += fieldString("name", e.arg1)
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
// WithDDoc sets the expected ddoc to be passed to the DB.DeleteIndex() call.
func (e *ExpectedDeleteIndex) WithDDoc(ddoc string) *ExpectedDeleteIndex {
e.arg0 = ddoc
return e
}
// WithName sets the expected name to be passed to the DB.DeleteIndex() call.
func (e *ExpectedDeleteIndex) WithName(name string) *ExpectedDeleteIndex {
e.arg1 = name
return e
}
func (e *ExpectedExplain) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).Explain() which:", e.dbo().name, e.dbo().id)
if e.arg0 == nil {
msg += "\n\t- has any query"
} else {
msg += fmt.Sprintf("\n\t- has query: %v", e.arg0)
}
if e.ret0 != nil {
msg += fmt.Sprintf("\n\t- should return query plan: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
// WithQuery sets the expected query for the Explain() call.
func (e *ExpectedExplain) WithQuery(query interface{}) *ExpectedExplain {
e.arg0 = query
return e
}
func (e *ExpectedCreateDoc) String() string {
msg := fmt.Sprintf("call to DB(%s#%d).CreateDoc() which:", e.dbo().name, e.dbo().id)
if e.arg0 == nil {
msg += "\n\t- has any doc"
} else {
msg += fmt.Sprintf("\n\t- has doc: %s", jsonDoc(e.arg0))
}
msg += optionsString(e.options)
if e.ret0 != "" {
msg += "\n\t- should return docID: " + e.ret0
}
if e.ret1 != "" {
msg += "\n\t- should return rev: " + e.ret1
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
// WithDoc sets the expected doc for the call to CreateDoc().
func (e *ExpectedCreateDoc) WithDoc(doc interface{}) *ExpectedCreateDoc {
e.arg0 = doc
return e
}
const (
withOptions = 1 << iota
)
func dbStringer(methodName string, e *commonExpectation, flags int, opts []string, rets []string) string {
msg := fmt.Sprintf("call to DB(%s#%d).%s()", e.db.name, e.db.id, methodName)
var extra string
for _, c := range opts {
extra += "\n\t- " + c
}
if flags&withOptions > 0 {
extra += optionsString(e.options)
}
for _, c := range rets {
extra += "\n\t- " + c
}
extra += delayString(e.delay)
extra += errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedCompact) String() string {
return dbStringer("Compact", &e.commonExpectation, 0, nil, nil)
}
func (e *ExpectedViewCleanup) String() string {
return dbStringer("ViewCleanup", &e.commonExpectation, 0, nil, nil)
}
func (e *ExpectedPut) String() string {
custom := []string{}
if e.arg0 == "" {
custom = append(custom, "has any docID")
} else {
custom = append(custom, fmt.Sprintf("has docID: %s", e.arg0))
}
if e.arg1 == nil {
custom = append(custom, "has any doc")
} else {
custom = append(custom, fmt.Sprintf("has doc: %s", jsonDoc(e.arg1)))
}
return dbStringer("Put", &e.commonExpectation, withOptions, custom, nil)
}
// WithDocID sets the expectation for the docID passed to the DB.Put() call.
func (e *ExpectedPut) WithDocID(docID string) *ExpectedPut {
e.arg0 = docID
return e
}
// WithDoc sets the expectation for the doc passed to the DB.Put() call.
func (e *ExpectedPut) WithDoc(doc interface{}) *ExpectedPut {
e.arg1 = doc
return e
}
func (e *ExpectedGetRev) String() string {
var opts []string
if e.arg0 == "" {
opts = append(opts, "has any docID")
} else {
opts = append(opts, fmt.Sprintf("has docID: %s", e.arg0))
}
var rets []string
if e.ret0 != "" {
rets = append(rets, "should return rev: "+e.ret0)
}
return dbStringer("GetRev", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.GetRev call.
func (e *ExpectedGetRev) WithDocID(docID string) *ExpectedGetRev {
e.arg0 = docID
return e
}
func (e *ExpectedFlush) String() string {
return dbStringer("Flush", &e.commonExpectation, 0, nil, nil)
}
func (e *ExpectedDeleteAttachment) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any docID")
} else {
opts = append(opts, "has docID: "+e.arg0)
}
if e.arg1 == "" {
opts = append(opts, "has any filename")
} else {
opts = append(opts, "has filename: "+e.arg1)
}
if e.ret0 != "" {
rets = append(rets, "should return rev: "+e.ret0)
}
return dbStringer("DeleteAttachment", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.DeleteAttachment() call.
func (e *ExpectedDeleteAttachment) WithDocID(docID string) *ExpectedDeleteAttachment {
e.arg0 = docID
return e
}
// WithFilename sets the expectation for the filename passed to the DB.DeleteAttachment() call.
func (e *ExpectedDeleteAttachment) WithFilename(filename string) *ExpectedDeleteAttachment {
e.arg1 = filename
return e
}
func (e *ExpectedDelete) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any docID")
} else {
opts = append(opts, "has docID: "+e.arg0)
}
if e.ret0 != "" {
rets = append(rets, "should return rev: "+e.ret0)
}
return dbStringer("Delete", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.Delete() call.
func (e *ExpectedDelete) WithDocID(docID string) *ExpectedDelete {
e.arg0 = docID
return e
}
func (e *ExpectedCopy) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any targetID")
} else {
opts = append(opts, "has targetID: "+e.arg0)
}
if e.arg1 == "" {
opts = append(opts, "has any sourceID")
} else {
opts = append(opts, "has sourceID: "+e.arg1)
}
if e.ret0 != "" {
rets = append(rets, "should return rev: "+e.ret0)
}
return dbStringer("Copy", &e.commonExpectation, withOptions, opts, rets)
}
// WithTargetID sets the expectation for the docID passed to the DB.Copy() call.
func (e *ExpectedCopy) WithTargetID(docID string) *ExpectedCopy {
e.arg0 = docID
return e
}
// WithSourceID sets the expectation for the docID passed to the DB.Copy() call.
func (e *ExpectedCopy) WithSourceID(docID string) *ExpectedCopy {
e.arg1 = docID
return e
}
func (e *ExpectedCompactView) String() string {
var opts []string
if e.arg0 == "" {
opts = []string{"has any ddocID"}
} else {
opts = []string{"has ddocID: " + e.arg0}
}
return dbStringer("CompactView", &e.commonExpectation, 0, opts, nil)
}
// WithDDoc sets the expected design doc name for the call to DB.CompactView().
func (e *ExpectedCompactView) WithDDoc(ddocID string) *ExpectedCompactView {
e.arg0 = ddocID
return e
}
func (e *ExpectedGet) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = []string{"has any docID"}
} else {
opts = []string{"has docID: " + e.arg0}
}
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return document with rev: %s", e.ret0.Rev)}
}
return dbStringer("Get", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expected docID for the DB.Get() call.
func (e *ExpectedGet) WithDocID(docID string) *ExpectedGet {
e.arg0 = docID
return e
}
func (e *ExpectedGetAttachmentMeta) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = []string{"has any docID"}
} else {
opts = []string{"has docID: " + e.arg0}
}
if e.arg1 == "" {
opts = append(opts, "has any filename")
} else {
opts = append(opts, "has filename: "+e.arg1)
}
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return attachment: %s", e.ret0.Filename)}
}
return dbStringer("GetAttachmentMeta", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.GetAttachmentMeta() call.
func (e *ExpectedGetAttachmentMeta) WithDocID(docID string) *ExpectedGetAttachmentMeta {
e.arg0 = docID
return e
}
// WithFilename sets the expectation for the doc passed to the DB.GetAttachmentMeta() call.
func (e *ExpectedGetAttachmentMeta) WithFilename(filename string) *ExpectedGetAttachmentMeta {
e.arg1 = filename
return e
}
func (e *ExpectedLocalDocs) String() string {
var rets []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("LocalDocs", &e.commonExpectation, withOptions, nil, rets)
}
func (e *ExpectedPurge) String() string {
var opts, rets []string
if e.arg0 == nil {
opts = []string{"has any docRevMap"}
} else {
opts = []string{fmt.Sprintf("has docRevMap: %v", e.arg0)}
}
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return result: %v", e.ret0)}
}
return dbStringer("Purge", &e.commonExpectation, 0, opts, rets)
}
// WithDocRevMap sets the expected docRevMap for the call to DB.Purge().
func (e *ExpectedPurge) WithDocRevMap(docRevMap map[string][]string) *ExpectedPurge {
e.arg0 = docRevMap
return e
}
func (e *ExpectedPutAttachment) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any docID")
} else {
opts = append(opts, fmt.Sprintf("has docID: %s", e.arg0))
}
if e.arg1 == nil {
opts = append(opts, "has any attachment")
} else {
opts = append(opts, fmt.Sprintf("has attachment: %s", e.arg1.Filename))
}
if e.ret0 != "" {
rets = append(rets, "should return rev: "+e.ret0)
}
return dbStringer("PutAttachment", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.PutAttachment() call.
func (e *ExpectedPutAttachment) WithDocID(docID string) *ExpectedPutAttachment {
e.arg0 = docID
return e
}
// WithAttachment sets the expectation for the rev passed to the DB.PutAttachment() call.
func (e *ExpectedPutAttachment) WithAttachment(att *driver.Attachment) *ExpectedPutAttachment {
e.arg1 = att
return e
}
func (e *ExpectedQuery) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any ddocID")
} else {
opts = append(opts, "has ddocID: "+e.arg0)
}
if e.arg1 == "" {
opts = append(opts, "has any view")
} else {
opts = append(opts, "has view: "+e.arg1)
}
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("Query", &e.commonExpectation, withOptions, opts, rets)
}
// WithDDocID sets the expected ddocID value for the DB.Query() call.
func (e *ExpectedQuery) WithDDocID(ddocID string) *ExpectedQuery {
e.arg0 = ddocID
return e
}
// WithView sets the expected view value for the DB.Query() call.
func (e *ExpectedQuery) WithView(view string) *ExpectedQuery {
e.arg1 = view
return e
}
func (e *ExpectedSecurity) String() string {
var rets []string
if e.ret0 != nil {
rets = append(rets, fmt.Sprintf("should return: %s", jsonDoc(e.ret0)))
}
return dbStringer("Security", &e.commonExpectation, 0, nil, rets)
}
func (e *ExpectedSetSecurity) String() string {
var opts []string
if e.arg0 == nil {
opts = append(opts, "has any security object")
} else {
opts = append(opts, fmt.Sprintf("has security object: %v", e.arg0))
}
return dbStringer("SetSecurity", &e.commonExpectation, 0, opts, nil)
}
// WithSecurity sets the expected security object for the DB.SetSecurity() call.
func (e *ExpectedSetSecurity) WithSecurity(sec *driver.Security) *ExpectedSetSecurity {
e.arg0 = sec
return e
}
func (e *ExpectedStats) String() string {
var rets []string
if e.ret0 != nil {
rets = append(rets, fmt.Sprintf("should return stats: %v", e.ret0))
}
return dbStringer("Stats", &e.commonExpectation, 0, nil, rets)
}
func (e *ExpectedBulkDocs) String() string {
var opts, rets []string
if e.arg0 == nil {
opts = append(opts, "has any docs")
} else {
opts = append(opts, fmt.Sprintf("has: %d docs", len(e.arg0)))
}
if e.ret0 != nil {
rets = append(rets, fmt.Sprintf("should return: %d results", len(e.ret0)))
}
return dbStringer("BulkDocs", &e.commonExpectation, withOptions, opts, rets)
}
func (e *ExpectedGetAttachment) String() string {
var opts, rets []string
if e.arg0 == "" {
opts = append(opts, "has any docID")
} else {
opts = append(opts, "has docID: "+e.arg0)
}
if e.arg1 == "" {
opts = append(opts, "has any filename")
} else {
opts = append(opts, "has filename: "+e.arg1)
}
if e.ret0 != nil {
rets = append(rets, "should return attachment: "+e.ret0.Filename)
}
return dbStringer("GetAttachment", &e.commonExpectation, withOptions, opts, rets)
}
// WithDocID sets the expectation for the docID passed to the DB.GetAttachment() call.
func (e *ExpectedGetAttachment) WithDocID(docID string) *ExpectedGetAttachment {
e.arg0 = docID
return e
}
// WithFilename sets the expectation for the filename passed to the DB.GetAttachment() call.
func (e *ExpectedGetAttachment) WithFilename(docID string) *ExpectedGetAttachment {
e.arg1 = docID
return e
}
func (e *ExpectedDesignDocs) String() string {
var rets []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("DesignDocs", &e.commonExpectation, withOptions, nil, rets)
}
func (e *ExpectedChanges) String() string {
var rets []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
return dbStringer("Changes", &e.commonExpectation, withOptions, nil, rets)
}
func (e *ExpectedRevsDiff) String() string {
var rets, opts []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
}
if e.arg0 != nil {
opts = []string{fmt.Sprintf("with revMap: %v", e.arg0)}
} else {
opts = []string{"has any revMap"}
}
return dbStringer("RevsDiff", &e.commonExpectation, 0, opts, rets)
}
// WithRevLookup sets the expectation for the rev lookup passed to the
// DB.RevsDiff() call.
func (e *ExpectedRevsDiff) WithRevLookup(revLookup interface{}) *ExpectedRevsDiff {
e.arg0 = revLookup
return e
}
func (e *ExpectedPartitionStats) String() string {
var rets, opts []string
if e.ret0 != nil {
rets = []string{fmt.Sprintf("should return: %s", jsonDoc(e.ret0))}
}
if e.arg0 != "" {
opts = []string{fmt.Sprintf("with name: %v", e.arg0)}
} else {
opts = []string{"has any name"}
}
return dbStringer("PartitionStats", &e.commonExpectation, 0, opts, rets)
}
// WithName sets the expectation for the partition name passed to the
// DB.PartitionStats() call.
func (e *ExpectedPartitionStats) WithName(name string) *ExpectedPartitionStats {
e.arg0 = name
return e
}