-
Notifications
You must be signed in to change notification settings - Fork 1
/
expectations.go
485 lines (427 loc) · 12.3 KB
/
expectations.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
package kivikmock
import (
"context"
"fmt"
"reflect"
"time"
)
func (e *ExpectedClose) String() string {
extra := delayString(e.delay) + errorString(e.err)
msg := "call to Close()"
if extra != "" {
msg += " which:" + extra
}
return msg
}
// String satisfies the fmt.Stringer interface.
func (e *ExpectedAllDBs) String() string {
return "call to AllDBs() which:" +
optionsString(e.options) +
delayString(e.delay) +
errorString(e.err)
}
// ExpectedAuthenticate is used to manage *kivik.Client.Authenticate
// expectation returned by Mock.ExpectAuthenticate.
type ExpectedAuthenticate struct {
commonExpectation
authType string
callback func(ctx context.Context, authenticator interface{}) error
}
// String satisfies the fmt.Stringer interface.
func (e *ExpectedAuthenticate) String() string {
msg := fmt.Sprintf("call to %s which:", e.method(false))
if e.authType == "" {
msg += "\n\t- has any authenticator"
} else {
msg += fmt.Sprint("\n\t- has an authenticator of type: " + e.authType)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedAuthenticate) method(v bool) string {
if !v {
return "Authenticate()"
}
if e.authType == "" {
return "Authenticate(ctx, ?)"
}
return fmt.Sprintf("Authenticate(ctx, <%s>)", e.authType)
}
func (e *ExpectedAuthenticate) met(ex expectation) bool {
exp := ex.(*ExpectedAuthenticate)
if exp.authType == "" {
return true
}
return e.authType == exp.authType
}
// WillReturnError allows setting an error for *kivik.Client.Authenticate action.
func (e *ExpectedAuthenticate) WillReturnError(err error) *ExpectedAuthenticate {
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 *ExpectedAuthenticate) WillExecute(cb func(ctx context.Context, authenticator interface{}) error) *ExpectedAuthenticate {
e.callback = cb
return e
}
// WithAuthenticator will match the the provide authenticator _type_ against
// that provided. There is no way to validate the authenticated credentials
// with this method.
func (e *ExpectedAuthenticate) WithAuthenticator(authenticator interface{}) *ExpectedAuthenticate {
e.authType = reflect.TypeOf(authenticator).Name()
return e
}
// WillDelay will cause execution of Authenticate() to delay by duration d.
func (e *ExpectedAuthenticate) WillDelay(d time.Duration) *ExpectedAuthenticate {
e.delay = d
return e
}
func (e *ExpectedClusterSetup) String() string {
msg := "call to ClusterSetup() which:"
if e.arg0 == nil {
msg += "\n\t- has any action"
} else {
msg += fmt.Sprintf("\n\t- has the action: %v", e.arg0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
// String satisfies the fmt.Stringer interface
func (e *ExpectedClusterStatus) String() string {
return "call to ClusterStatus() which:" +
optionsString(e.options) +
delayString(e.delay) +
errorString(e.err)
}
func (e *ExpectedMembership) String() string {
extra := delayString(e.delay) + errorString(e.err)
msg := "call to Membership()"
if extra != "" {
msg += " which:" + extra
}
return msg
}
// WithAction specifies the action to be matched. Note that this expectation
// is compared with the actual action's marshaled JSON output, so it is not
// essential that the data types match exactly, in a Go sense.
func (e *ExpectedClusterSetup) WithAction(action interface{}) *ExpectedClusterSetup {
e.arg0 = action
return e
}
func (e *ExpectedDBExists) String() string {
msg := "call to DBExists() which:" +
fieldString("name", e.arg0) +
optionsString(e.options) +
delayString(e.delay)
if e.err == nil {
msg += fmt.Sprintf("\n\t- should return: %t", e.ret0)
} else {
msg += fmt.Sprintf("\n\t- should return error: %s", e.err)
}
return msg
}
// WithName sets the expectation that DBExists will be called with the provided
// name.
func (e *ExpectedDBExists) WithName(name string) *ExpectedDBExists {
e.arg0 = name
return e
}
func (e *ExpectedDestroyDB) String() string {
return "call to DestroyDB() which:" +
fieldString("name", e.arg0) +
optionsString(e.options) +
delayString(e.delay) +
errorString(e.err)
}
// WithName sets the expectation that DestroyDB will be called with this name.
func (e *ExpectedDestroyDB) WithName(name string) *ExpectedDestroyDB {
e.arg0 = name
return e
}
func (e *ExpectedDBsStats) String() string {
msg := "call to DBsStats() which:"
if e.arg0 == nil {
msg += "\n\t- has any names"
} else {
msg += fmt.Sprintf("\n\t- has names: %s", e.arg0)
}
return msg + delayString(e.delay) + errorString(e.err)
}
// WithNames sets the expectation that DBsStats will be called with these names.
func (e *ExpectedDBsStats) WithNames(names []string) *ExpectedDBsStats {
e.arg0 = names
return e
}
func (e *ExpectedPing) String() string {
msg := "call to Ping()"
extra := delayString(e.delay) + errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedSession) String() string {
msg := "call to Session()"
extra := ""
if e.ret0 != nil {
extra += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
extra += delayString(e.delay) + errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedVersion) String() string {
msg := "call to Version()"
extra := ""
if e.ret0 != nil {
extra += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
extra += delayString(e.delay) + errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedDB) String() string {
msg := "call to DB() which:" +
fieldString("name", e.arg0) +
optionsString(e.options)
if e.db != nil {
msg += fmt.Sprintf("\n\t- should return database with %d expectations", e.db.expectations())
}
msg += delayString(e.delay)
return msg
}
// WithName sets the expectation that DB() will be called with this name.
func (e *ExpectedDB) WithName(name string) *ExpectedDB {
e.arg0 = name
return e
}
// ExpectedCreateDB represents an expectation to call the CreateDB() method.
//
// Implementation note: Because kivik always calls DB() after a
// successful CreateDB() is executed, ExpectCreateDB() creates two
// expectations under the covers, one for the backend CreateDB() call,
// and one for the DB() call. If WillReturnError() is called, the DB() call
// expectation is removed.
type ExpectedCreateDB struct {
commonExpectation
arg0 string
callback func(ctx context.Context, arg0 string, options map[string]interface{}) error
}
func (e *ExpectedCreateDB) String() string {
msg := "call to CreateDB() which:" +
fieldString("name", e.arg0) +
optionsString(e.options)
if e.db != nil {
msg += fmt.Sprintf("\n\t- should return database with %d expectations", e.db.expectations())
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedCreateDB) method(v bool) string {
if !v {
return "CreateDB()"
}
var name, options string
if e.arg0 == "" {
name = "?"
} else {
name = fmt.Sprintf("%q", e.arg0)
}
if e.options != nil {
options = fmt.Sprintf(", %v", e.options)
}
return fmt.Sprintf("CreateDB(ctx, %s%s)", name, options)
}
func (e *ExpectedCreateDB) met(ex expectation) bool {
exp := ex.(*ExpectedCreateDB)
return exp.arg0 == "" || exp.arg0 == e.arg0
}
// WithOptions set the expectation that DB() will be called with these options.
func (e *ExpectedCreateDB) WithOptions(options map[string]interface{}) *ExpectedCreateDB {
e.options = options
return e
}
// WithName sets the expectation that DB() will be called with this name.
func (e *ExpectedCreateDB) WithName(name string) *ExpectedCreateDB {
e.arg0 = name
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 *ExpectedCreateDB) WillExecute(cb func(ctx context.Context, arg0 string, options map[string]interface{}) error) *ExpectedCreateDB {
e.callback = cb
return e
}
// WillReturnError sets the return value for the DB() call.
func (e *ExpectedCreateDB) WillReturnError(err error) *ExpectedCreateDB {
e.err = err
return e
}
// WillDelay will cause execution of DB() to delay by duration d.
func (e *ExpectedCreateDB) WillDelay(delay time.Duration) *ExpectedCreateDB {
e.delay = delay
return e
}
func (e *ExpectedDBUpdates) String() string {
msg := "call to DBUpdates()"
var extra string
if e.ret0 != nil {
extra += fmt.Sprintf("\n\t- should return: %d results", e.ret0.count())
}
extra += delayString(e.delay)
extra += errorString(e.err)
if extra != "" {
msg += " which:" + extra
}
return msg
}
func (e *ExpectedConfig) String() string {
msg := "call to Config() which:"
msg += fieldString("node", e.arg0)
if e.ret0 != nil {
msg += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedConfig) WithNode(node string) *ExpectedConfig {
e.arg0 = node
return e
}
func (e *ExpectedConfigSection) String() string {
msg := "call to ConfigSection() which:" +
fieldString("node", e.arg0) +
fieldString("section", e.arg1)
if e.ret0 != nil {
msg += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedConfigSection) WithNode(node string) *ExpectedConfigSection {
e.arg0 = node
return e
}
func (e *ExpectedConfigSection) WithSection(section string) *ExpectedConfigSection {
e.arg1 = section
return e
}
func (e *ExpectedConfigValue) String() string {
msg := "call to ConfigValue() which:" +
fieldString("node", e.arg0) +
fieldString("section", e.arg1) +
fieldString("key", e.arg2)
if e.ret0 != "" {
msg += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedConfigValue) WithNode(node string) *ExpectedConfigValue {
e.arg0 = node
return e
}
func (e *ExpectedConfigValue) WithSection(section string) *ExpectedConfigValue {
e.arg1 = section
return e
}
func (e *ExpectedConfigValue) WithKey(key string) *ExpectedConfigValue {
e.arg2 = key
return e
}
func (e *ExpectedSetConfigValue) String() string {
msg := "call to SetConfigValue() which:" +
fieldString("node", e.arg0) +
fieldString("section", e.arg1) +
fieldString("key", e.arg2) +
fieldString("value", e.arg3)
if e.ret0 != "" {
msg += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedSetConfigValue) WithNode(node string) *ExpectedSetConfigValue {
e.arg0 = node
return e
}
func (e *ExpectedSetConfigValue) WithSection(section string) *ExpectedSetConfigValue {
e.arg1 = section
return e
}
func (e *ExpectedSetConfigValue) WithKey(key string) *ExpectedSetConfigValue {
e.arg2 = key
return e
}
func (e *ExpectedSetConfigValue) WithValue(value string) *ExpectedSetConfigValue {
e.arg3 = value
return e
}
func (e *ExpectedDeleteConfigKey) String() string {
msg := "call to DeleteConfigKey() which:" +
fieldString("node", e.arg0) +
fieldString("section", e.arg1) +
fieldString("key", e.arg2)
if e.ret0 != "" {
msg += fmt.Sprintf("\n\t- should return: %v", e.ret0)
}
msg += delayString(e.delay)
msg += errorString(e.err)
return msg
}
func (e *ExpectedDeleteConfigKey) WithNode(node string) *ExpectedDeleteConfigKey {
e.arg0 = node
return e
}
func (e *ExpectedDeleteConfigKey) WithSection(section string) *ExpectedDeleteConfigKey {
e.arg1 = section
return e
}
func (e *ExpectedDeleteConfigKey) WithKey(key string) *ExpectedDeleteConfigKey {
e.arg2 = key
return e
}
func (e *ExpectedReplicate) String() string {
msg := "call to Replicate() which:" +
fieldString("target", e.arg0) +
fieldString("source", e.arg1) +
optionsString(e.options)
if e.ret0 != nil {
msg += fmt.Sprintf("\n\t- should return: %v", jsonDoc(e.ret0))
}
return msg +
delayString(e.delay) +
errorString(e.err)
}
func (e *ExpectedReplicate) WithSource(source string) *ExpectedReplicate {
e.arg1 = source
return e
}
func (e *ExpectedReplicate) WithTarget(target string) *ExpectedReplicate {
e.arg0 = target
return e
}
func (e *ExpectedGetReplications) String() string {
msg := "call to GetReplications() which:" +
optionsString(e.options)
if l := len(e.ret0); l > 0 {
msg += fmt.Sprintf("\n\t- should return: %d results", l)
}
return msg +
delayString(e.delay) +
errorString(e.err)
}