1
2
3
4
5
6
7
8
9
10
11
12
13 package mockdb
14
15 import (
16 "encoding/json"
17 "fmt"
18
19 "github.com/go-kivik/kivik/v4/driver"
20 )
21
22
23
24 type ExpectedDBClose struct {
25 commonExpectation
26 callback func() error
27 }
28
29 func (e *ExpectedDBClose) method(v bool) string {
30 if v {
31 return "DB.Close(ctx)"
32 }
33 return "DB.Close()"
34 }
35
36 func (e *ExpectedDBClose) met(expectation) bool {
37 return true
38 }
39
40
41 func (e *ExpectedDBClose) WillReturnError(err error) *ExpectedDBClose {
42 e.err = err
43 return e
44 }
45
46
47
48
49 func (e *ExpectedDBClose) WillExecute(cb func() error) *ExpectedDBClose {
50 e.callback = cb
51 return e
52 }
53
54 func (e *ExpectedDBClose) String() string {
55 msg := fmt.Sprintf("call to DB(%s#%d).Close()", e.dbo().name, e.dbo().id)
56 extra := delayString(e.delay)
57 extra += errorString(e.err)
58 if extra != "" {
59 msg += " which:" + extra
60 }
61 return msg
62 }
63
64 func (e *ExpectedAllDocs) String() string {
65 var rets []string
66 if e.ret0 != nil {
67 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
68 }
69 return dbStringer("AllDocs", &e.commonExpectation, withOptions, nil, rets)
70 }
71
72 func jsonDoc(i interface{}) string {
73 jsonText, err := json.Marshal(i)
74 if err != nil {
75 return fmt.Sprintf("<invalid json:%s>", err)
76 }
77 return string(jsonText)
78 }
79
80 func (e *ExpectedBulkGet) String() string {
81 msg := fmt.Sprintf("call to DB(%s#%d).BulkGet() which:", e.dbo().name, e.dbo().id)
82 if e.arg0 == nil {
83 msg += "\n\t- has any doc references"
84 } else {
85 msg += fmt.Sprintf("\n\t- has doc references: %v", jsonDoc(e.arg0))
86 }
87 msg += optionsString(e.options)
88 if e.ret0 != nil {
89 msg += fmt.Sprintf("\n\t- should return: %d results", e.ret0.count())
90 }
91 msg += delayString(e.delay)
92 msg += errorString(e.err)
93 return msg
94 }
95
96 func (e *ExpectedFind) String() string {
97 var opts, rets []string
98 if e.arg0 == nil {
99 opts = append(opts, "has any query")
100 } else {
101 opts = append(opts, fmt.Sprintf("has query: %v", e.arg0))
102 }
103 if e.ret0 != nil {
104 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
105 }
106 return dbStringer("Find", &e.commonExpectation, withOptions, opts, rets)
107 }
108
109
110 func (e *ExpectedFind) WithQuery(query interface{}) *ExpectedFind {
111 e.arg0 = query
112 return e
113 }
114
115 func (e *ExpectedCreateIndex) String() string {
116 msg := fmt.Sprintf("call to DB(%s#%d).CreateIndex() which:", e.dbo().name, e.dbo().id)
117 if e.arg0 == "" {
118 msg += "\n\t- has any ddoc"
119 } else {
120 msg += "\n\t- has ddoc: " + e.arg0
121 }
122 if e.arg1 == "" {
123 msg += "\n\t- has any name"
124 } else {
125 msg += "\n\t- has name: " + e.arg1
126 }
127 if e.arg2 == nil {
128 msg += "\n\t- has any index"
129 } else {
130 msg += fmt.Sprintf("\n\t- has index: %v", e.arg2)
131 }
132 msg += delayString(e.delay)
133 msg += errorString(e.err)
134 return msg
135 }
136
137
138 func (e *ExpectedCreateIndex) WithDDocID(ddocID string) *ExpectedCreateIndex {
139 e.arg0 = ddocID
140 return e
141 }
142
143
144 func (e *ExpectedCreateIndex) WithName(name string) *ExpectedCreateIndex {
145 e.arg1 = name
146 return e
147 }
148
149
150 func (e *ExpectedCreateIndex) WithIndex(index interface{}) *ExpectedCreateIndex {
151 e.arg2 = index
152 return e
153 }
154
155 func (e *ExpectedGetIndexes) String() string {
156 msg := fmt.Sprintf("call to DB(%s#%d).GetIndexes()", e.dbo().name, e.dbo().id)
157 var extra string
158 if e.ret0 != nil {
159 extra += fmt.Sprintf("\n\t- should return indexes: %v", e.ret0)
160 }
161 extra += delayString(e.delay)
162 extra += errorString(e.err)
163 if extra != "" {
164 msg += " which:" + extra
165 }
166 return msg
167 }
168
169 func (e *ExpectedDeleteIndex) String() string {
170 msg := fmt.Sprintf("call to DB(%s#%d).DeleteIndex() which:", e.dbo().name, e.dbo().id)
171 msg += fieldString("ddoc", e.arg0)
172 msg += fieldString("name", e.arg1)
173 msg += delayString(e.delay)
174 msg += errorString(e.err)
175 return msg
176 }
177
178
179 func (e *ExpectedDeleteIndex) WithDDoc(ddoc string) *ExpectedDeleteIndex {
180 e.arg0 = ddoc
181 return e
182 }
183
184
185 func (e *ExpectedDeleteIndex) WithName(name string) *ExpectedDeleteIndex {
186 e.arg1 = name
187 return e
188 }
189
190 func (e *ExpectedExplain) String() string {
191 msg := fmt.Sprintf("call to DB(%s#%d).Explain() which:", e.dbo().name, e.dbo().id)
192 if e.arg0 == nil {
193 msg += "\n\t- has any query"
194 } else {
195 msg += fmt.Sprintf("\n\t- has query: %v", e.arg0)
196 }
197 if e.ret0 != nil {
198 msg += fmt.Sprintf("\n\t- should return query plan: %v", e.ret0)
199 }
200 msg += delayString(e.delay)
201 msg += errorString(e.err)
202 return msg
203 }
204
205
206 func (e *ExpectedExplain) WithQuery(query interface{}) *ExpectedExplain {
207 e.arg0 = query
208 return e
209 }
210
211 func (e *ExpectedCreateDoc) String() string {
212 msg := fmt.Sprintf("call to DB(%s#%d).CreateDoc() which:", e.dbo().name, e.dbo().id)
213 if e.arg0 == nil {
214 msg += "\n\t- has any doc"
215 } else {
216 msg += fmt.Sprintf("\n\t- has doc: %s", jsonDoc(e.arg0))
217 }
218 msg += optionsString(e.options)
219 if e.ret0 != "" {
220 msg += "\n\t- should return docID: " + e.ret0
221 }
222 if e.ret1 != "" {
223 msg += "\n\t- should return rev: " + e.ret1
224 }
225 msg += delayString(e.delay)
226 msg += errorString(e.err)
227 return msg
228 }
229
230
231 func (e *ExpectedCreateDoc) WithDoc(doc interface{}) *ExpectedCreateDoc {
232 e.arg0 = doc
233 return e
234 }
235
236 const (
237 withOptions = 1 << iota
238 )
239
240 func dbStringer(methodName string, e *commonExpectation, flags int, opts []string, rets []string) string {
241 msg := fmt.Sprintf("call to DB(%s#%d).%s()", e.db.name, e.db.id, methodName)
242 var extra string
243 for _, c := range opts {
244 extra += "\n\t- " + c
245 }
246 if flags&withOptions > 0 {
247 extra += optionsString(e.options)
248 }
249 for _, c := range rets {
250 extra += "\n\t- " + c
251 }
252 extra += delayString(e.delay)
253 extra += errorString(e.err)
254 if extra != "" {
255 msg += " which:" + extra
256 }
257 return msg
258 }
259
260 func (e *ExpectedCompact) String() string {
261 return dbStringer("Compact", &e.commonExpectation, 0, nil, nil)
262 }
263
264 func (e *ExpectedViewCleanup) String() string {
265 return dbStringer("ViewCleanup", &e.commonExpectation, 0, nil, nil)
266 }
267
268 func (e *ExpectedPut) String() string {
269 custom := []string{}
270 if e.arg0 == "" {
271 custom = append(custom, "has any docID")
272 } else {
273 custom = append(custom, fmt.Sprintf("has docID: %s", e.arg0))
274 }
275 if e.arg1 == nil {
276 custom = append(custom, "has any doc")
277 } else {
278 custom = append(custom, fmt.Sprintf("has doc: %s", jsonDoc(e.arg1)))
279 }
280 return dbStringer("Put", &e.commonExpectation, withOptions, custom, nil)
281 }
282
283
284 func (e *ExpectedPut) WithDocID(docID string) *ExpectedPut {
285 e.arg0 = docID
286 return e
287 }
288
289
290 func (e *ExpectedPut) WithDoc(doc interface{}) *ExpectedPut {
291 e.arg1 = doc
292 return e
293 }
294
295 func (e *ExpectedGetRev) String() string {
296 var opts []string
297 if e.arg0 == "" {
298 opts = append(opts, "has any docID")
299 } else {
300 opts = append(opts, fmt.Sprintf("has docID: %s", e.arg0))
301 }
302 var rets []string
303 if e.ret0 != "" {
304 rets = append(rets, "should return rev: "+e.ret0)
305 }
306 return dbStringer("GetRev", &e.commonExpectation, withOptions, opts, rets)
307 }
308
309
310 func (e *ExpectedGetRev) WithDocID(docID string) *ExpectedGetRev {
311 e.arg0 = docID
312 return e
313 }
314
315 func (e *ExpectedFlush) String() string {
316 return dbStringer("Flush", &e.commonExpectation, 0, nil, nil)
317 }
318
319 func (e *ExpectedDeleteAttachment) String() string {
320 var opts, rets []string
321 if e.arg0 == "" {
322 opts = append(opts, "has any docID")
323 } else {
324 opts = append(opts, "has docID: "+e.arg0)
325 }
326 if e.arg1 == "" {
327 opts = append(opts, "has any filename")
328 } else {
329 opts = append(opts, "has filename: "+e.arg1)
330 }
331 if e.ret0 != "" {
332 rets = append(rets, "should return rev: "+e.ret0)
333 }
334 return dbStringer("DeleteAttachment", &e.commonExpectation, withOptions, opts, rets)
335 }
336
337
338 func (e *ExpectedDeleteAttachment) WithDocID(docID string) *ExpectedDeleteAttachment {
339 e.arg0 = docID
340 return e
341 }
342
343
344 func (e *ExpectedDeleteAttachment) WithFilename(filename string) *ExpectedDeleteAttachment {
345 e.arg1 = filename
346 return e
347 }
348
349 func (e *ExpectedDelete) String() string {
350 var opts, rets []string
351 if e.arg0 == "" {
352 opts = append(opts, "has any docID")
353 } else {
354 opts = append(opts, "has docID: "+e.arg0)
355 }
356 if e.ret0 != "" {
357 rets = append(rets, "should return rev: "+e.ret0)
358 }
359 return dbStringer("Delete", &e.commonExpectation, withOptions, opts, rets)
360 }
361
362
363 func (e *ExpectedDelete) WithDocID(docID string) *ExpectedDelete {
364 e.arg0 = docID
365 return e
366 }
367
368 func (e *ExpectedCopy) String() string {
369 var opts, rets []string
370 if e.arg0 == "" {
371 opts = append(opts, "has any targetID")
372 } else {
373 opts = append(opts, "has targetID: "+e.arg0)
374 }
375 if e.arg1 == "" {
376 opts = append(opts, "has any sourceID")
377 } else {
378 opts = append(opts, "has sourceID: "+e.arg1)
379 }
380 if e.ret0 != "" {
381 rets = append(rets, "should return rev: "+e.ret0)
382 }
383 return dbStringer("Copy", &e.commonExpectation, withOptions, opts, rets)
384 }
385
386
387 func (e *ExpectedCopy) WithTargetID(docID string) *ExpectedCopy {
388 e.arg0 = docID
389 return e
390 }
391
392
393 func (e *ExpectedCopy) WithSourceID(docID string) *ExpectedCopy {
394 e.arg1 = docID
395 return e
396 }
397
398 func (e *ExpectedCompactView) String() string {
399 var opts []string
400 if e.arg0 == "" {
401 opts = []string{"has any ddocID"}
402 } else {
403 opts = []string{"has ddocID: " + e.arg0}
404 }
405 return dbStringer("CompactView", &e.commonExpectation, 0, opts, nil)
406 }
407
408
409 func (e *ExpectedCompactView) WithDDoc(ddocID string) *ExpectedCompactView {
410 e.arg0 = ddocID
411 return e
412 }
413
414 func (e *ExpectedGet) String() string {
415 var opts, rets []string
416 if e.arg0 == "" {
417 opts = []string{"has any docID"}
418 } else {
419 opts = []string{"has docID: " + e.arg0}
420 }
421 if e.ret0 != nil {
422 rets = []string{fmt.Sprintf("should return document with rev: %s", e.ret0.Rev)}
423 }
424 return dbStringer("Get", &e.commonExpectation, withOptions, opts, rets)
425 }
426
427
428 func (e *ExpectedGet) WithDocID(docID string) *ExpectedGet {
429 e.arg0 = docID
430 return e
431 }
432
433 func (e *ExpectedOpenRevs) String() string {
434 var opts, rets []string
435 if e.arg0 == "" {
436 opts = []string{"has any docID"}
437 } else {
438 opts = []string{"has docID: " + e.arg0}
439 }
440 if len(e.arg1) == 0 {
441 opts = append(opts, "has any revs")
442 } else {
443 opts = append(opts, fmt.Sprintf("with revs: %s", e.arg1))
444 }
445 if e.ret0 != nil {
446 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
447 }
448 return dbStringer("OpenRevs", &e.commonExpectation, withOptions, opts, rets)
449 }
450
451
452 func (e *ExpectedOpenRevs) WithDocID(docID string) *ExpectedOpenRevs {
453 e.arg0 = docID
454 return e
455 }
456
457
458 func (e *ExpectedOpenRevs) WithRevs(revs []string) *ExpectedOpenRevs {
459 e.arg1 = revs
460 return e
461 }
462
463 func (e *ExpectedGetAttachmentMeta) String() string {
464 var opts, rets []string
465 if e.arg0 == "" {
466 opts = []string{"has any docID"}
467 } else {
468 opts = []string{"has docID: " + e.arg0}
469 }
470 if e.arg1 == "" {
471 opts = append(opts, "has any filename")
472 } else {
473 opts = append(opts, "has filename: "+e.arg1)
474 }
475 if e.ret0 != nil {
476 rets = []string{fmt.Sprintf("should return attachment: %s", e.ret0.Filename)}
477 }
478 return dbStringer("GetAttachmentMeta", &e.commonExpectation, withOptions, opts, rets)
479 }
480
481
482 func (e *ExpectedGetAttachmentMeta) WithDocID(docID string) *ExpectedGetAttachmentMeta {
483 e.arg0 = docID
484 return e
485 }
486
487
488 func (e *ExpectedGetAttachmentMeta) WithFilename(filename string) *ExpectedGetAttachmentMeta {
489 e.arg1 = filename
490 return e
491 }
492
493 func (e *ExpectedLocalDocs) String() string {
494 var rets []string
495 if e.ret0 != nil {
496 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
497 }
498 return dbStringer("LocalDocs", &e.commonExpectation, withOptions, nil, rets)
499 }
500
501 func (e *ExpectedPurge) String() string {
502 var opts, rets []string
503 if e.arg0 == nil {
504 opts = []string{"has any docRevMap"}
505 } else {
506 opts = []string{fmt.Sprintf("has docRevMap: %v", e.arg0)}
507 }
508 if e.ret0 != nil {
509 rets = []string{fmt.Sprintf("should return result: %v", e.ret0)}
510 }
511 return dbStringer("Purge", &e.commonExpectation, 0, opts, rets)
512 }
513
514
515 func (e *ExpectedPurge) WithDocRevMap(docRevMap map[string][]string) *ExpectedPurge {
516 e.arg0 = docRevMap
517 return e
518 }
519
520 func (e *ExpectedPutAttachment) String() string {
521 var opts, rets []string
522 if e.arg0 == "" {
523 opts = append(opts, "has any docID")
524 } else {
525 opts = append(opts, fmt.Sprintf("has docID: %s", e.arg0))
526 }
527 if e.arg1 == nil {
528 opts = append(opts, "has any attachment")
529 } else {
530 opts = append(opts, fmt.Sprintf("has attachment: %s", e.arg1.Filename))
531 }
532 if e.ret0 != "" {
533 rets = append(rets, "should return rev: "+e.ret0)
534 }
535 return dbStringer("PutAttachment", &e.commonExpectation, withOptions, opts, rets)
536 }
537
538
539 func (e *ExpectedPutAttachment) WithDocID(docID string) *ExpectedPutAttachment {
540 e.arg0 = docID
541 return e
542 }
543
544
545 func (e *ExpectedPutAttachment) WithAttachment(att *driver.Attachment) *ExpectedPutAttachment {
546 e.arg1 = att
547 return e
548 }
549
550 func (e *ExpectedQuery) String() string {
551 var opts, rets []string
552 if e.arg0 == "" {
553 opts = append(opts, "has any ddocID")
554 } else {
555 opts = append(opts, "has ddocID: "+e.arg0)
556 }
557 if e.arg1 == "" {
558 opts = append(opts, "has any view")
559 } else {
560 opts = append(opts, "has view: "+e.arg1)
561 }
562 if e.ret0 != nil {
563 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
564 }
565 return dbStringer("Query", &e.commonExpectation, withOptions, opts, rets)
566 }
567
568
569 func (e *ExpectedQuery) WithDDocID(ddocID string) *ExpectedQuery {
570 e.arg0 = ddocID
571 return e
572 }
573
574
575 func (e *ExpectedQuery) WithView(view string) *ExpectedQuery {
576 e.arg1 = view
577 return e
578 }
579
580 func (e *ExpectedSecurity) String() string {
581 var rets []string
582 if e.ret0 != nil {
583 rets = append(rets, fmt.Sprintf("should return: %s", jsonDoc(e.ret0)))
584 }
585 return dbStringer("Security", &e.commonExpectation, 0, nil, rets)
586 }
587
588 func (e *ExpectedSetSecurity) String() string {
589 var opts []string
590 if e.arg0 == nil {
591 opts = append(opts, "has any security object")
592 } else {
593 opts = append(opts, fmt.Sprintf("has security object: %v", e.arg0))
594 }
595 return dbStringer("SetSecurity", &e.commonExpectation, 0, opts, nil)
596 }
597
598
599 func (e *ExpectedSetSecurity) WithSecurity(sec *driver.Security) *ExpectedSetSecurity {
600 e.arg0 = sec
601 return e
602 }
603
604 func (e *ExpectedStats) String() string {
605 var rets []string
606 if e.ret0 != nil {
607 rets = append(rets, fmt.Sprintf("should return stats: %v", e.ret0))
608 }
609 return dbStringer("Stats", &e.commonExpectation, 0, nil, rets)
610 }
611
612 func (e *ExpectedBulkDocs) String() string {
613 var opts, rets []string
614 if e.arg0 == nil {
615 opts = append(opts, "has any docs")
616 } else {
617 opts = append(opts, fmt.Sprintf("has: %d docs", len(e.arg0)))
618 }
619 if e.ret0 != nil {
620 rets = append(rets, fmt.Sprintf("should return: %d results", len(e.ret0)))
621 }
622 return dbStringer("BulkDocs", &e.commonExpectation, withOptions, opts, rets)
623 }
624
625 func (e *ExpectedGetAttachment) String() string {
626 var opts, rets []string
627 if e.arg0 == "" {
628 opts = append(opts, "has any docID")
629 } else {
630 opts = append(opts, "has docID: "+e.arg0)
631 }
632 if e.arg1 == "" {
633 opts = append(opts, "has any filename")
634 } else {
635 opts = append(opts, "has filename: "+e.arg1)
636 }
637 if e.ret0 != nil {
638 rets = append(rets, "should return attachment: "+e.ret0.Filename)
639 }
640 return dbStringer("GetAttachment", &e.commonExpectation, withOptions, opts, rets)
641 }
642
643
644 func (e *ExpectedGetAttachment) WithDocID(docID string) *ExpectedGetAttachment {
645 e.arg0 = docID
646 return e
647 }
648
649
650 func (e *ExpectedGetAttachment) WithFilename(docID string) *ExpectedGetAttachment {
651 e.arg1 = docID
652 return e
653 }
654
655 func (e *ExpectedDesignDocs) String() string {
656 var rets []string
657 if e.ret0 != nil {
658 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
659 }
660 return dbStringer("DesignDocs", &e.commonExpectation, withOptions, nil, rets)
661 }
662
663 func (e *ExpectedChanges) String() string {
664 var rets []string
665 if e.ret0 != nil {
666 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
667 }
668 return dbStringer("Changes", &e.commonExpectation, withOptions, nil, rets)
669 }
670
671 func (e *ExpectedRevsDiff) String() string {
672 var rets, opts []string
673 if e.ret0 != nil {
674 rets = []string{fmt.Sprintf("should return: %d results", e.ret0.count())}
675 }
676 if e.arg0 != nil {
677 opts = []string{fmt.Sprintf("with revMap: %v", e.arg0)}
678 } else {
679 opts = []string{"has any revMap"}
680 }
681 return dbStringer("RevsDiff", &e.commonExpectation, 0, opts, rets)
682 }
683
684
685
686 func (e *ExpectedRevsDiff) WithRevLookup(revLookup interface{}) *ExpectedRevsDiff {
687 e.arg0 = revLookup
688 return e
689 }
690
691 func (e *ExpectedPartitionStats) String() string {
692 var rets, opts []string
693 if e.ret0 != nil {
694 rets = []string{fmt.Sprintf("should return: %s", jsonDoc(e.ret0))}
695 }
696 if e.arg0 != "" {
697 opts = []string{fmt.Sprintf("with name: %v", e.arg0)}
698 } else {
699 opts = []string{"has any name"}
700 }
701 return dbStringer("PartitionStats", &e.commonExpectation, 0, opts, rets)
702 }
703
704
705
706 func (e *ExpectedPartitionStats) WithName(name string) *ExpectedPartitionStats {
707 e.arg0 = name
708 return e
709 }
710
View as plain text