github.com/go-kivik/kivik/v4@v4.3.2/mockdb/expectations.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package mockdb 14 15 import ( 16 "context" 17 "fmt" 18 "time" 19 20 kivik "github.com/go-kivik/kivik/v4" 21 "github.com/go-kivik/kivik/v4/driver" 22 ) 23 24 func (e *ExpectedClose) String() string { 25 extra := delayString(e.delay) + errorString(e.err) 26 msg := "call to Close()" 27 if extra != "" { 28 msg += " which:" + extra 29 } 30 return msg 31 } 32 33 // String satisfies the fmt.Stringer interface. 34 func (e *ExpectedAllDBs) String() string { 35 return "call to AllDBs() which:" + 36 optionsString(e.options) + 37 delayString(e.delay) + 38 errorString(e.err) 39 } 40 41 func (e *ExpectedClusterSetup) String() string { 42 msg := "call to ClusterSetup() which:" 43 if e.arg0 == nil { 44 msg += "\n\t- has any action" 45 } else { 46 msg += fmt.Sprintf("\n\t- has the action: %v", e.arg0) 47 } 48 msg += delayString(e.delay) 49 msg += errorString(e.err) 50 return msg 51 } 52 53 // String satisfies the fmt.Stringer interface 54 func (e *ExpectedClusterStatus) String() string { 55 return "call to ClusterStatus() which:" + 56 optionsString(e.options) + 57 delayString(e.delay) + 58 errorString(e.err) 59 } 60 61 func (e *ExpectedMembership) String() string { 62 extra := delayString(e.delay) + errorString(e.err) 63 msg := "call to Membership()" 64 if extra != "" { 65 msg += " which:" + extra 66 } 67 return msg 68 } 69 70 // WithAction specifies the action to be matched. Note that this expectation 71 // is compared with the actual action's marshaled JSON output, so it is not 72 // essential that the data types match exactly, in a Go sense. 73 func (e *ExpectedClusterSetup) WithAction(action interface{}) *ExpectedClusterSetup { 74 e.arg0 = action 75 return e 76 } 77 78 func (e *ExpectedDBExists) String() string { 79 msg := "call to DBExists() which:" + 80 fieldString("name", e.arg0) + 81 optionsString(e.options) + 82 delayString(e.delay) 83 if e.err == nil { 84 msg += fmt.Sprintf("\n\t- should return: %t", e.ret0) 85 } else { 86 msg += fmt.Sprintf("\n\t- should return error: %s", e.err) 87 } 88 return msg 89 } 90 91 // WithName sets the expectation that DBExists will be called with the provided 92 // name. 93 func (e *ExpectedDBExists) WithName(name string) *ExpectedDBExists { 94 e.arg0 = name 95 return e 96 } 97 98 func (e *ExpectedDestroyDB) String() string { 99 return "call to DestroyDB() which:" + 100 fieldString("name", e.arg0) + 101 optionsString(e.options) + 102 delayString(e.delay) + 103 errorString(e.err) 104 } 105 106 // WithName sets the expectation that DestroyDB will be called with this name. 107 func (e *ExpectedDestroyDB) WithName(name string) *ExpectedDestroyDB { 108 e.arg0 = name 109 return e 110 } 111 112 func (e *ExpectedDBsStats) String() string { 113 msg := "call to DBsStats() which:" 114 if e.arg0 == nil { 115 msg += "\n\t- has any names" 116 } else { 117 msg += fmt.Sprintf("\n\t- has names: %s", e.arg0) 118 } 119 return msg + delayString(e.delay) + errorString(e.err) 120 } 121 122 // WithNames sets the expectation that DBsStats will be called with these names. 123 func (e *ExpectedDBsStats) WithNames(names []string) *ExpectedDBsStats { 124 e.arg0 = names 125 return e 126 } 127 128 func (e *ExpectedAllDBsStats) String() string { 129 return "call to AllDBsStats() which:" + 130 optionsString(e.options) + 131 delayString(e.delay) + 132 errorString(e.err) 133 } 134 135 func (e *ExpectedPing) String() string { 136 msg := "call to Ping()" 137 extra := delayString(e.delay) + errorString(e.err) 138 if extra != "" { 139 msg += " which:" + extra 140 } 141 return msg 142 } 143 144 func (e *ExpectedSession) String() string { 145 msg := "call to Session()" 146 extra := "" 147 if e.ret0 != nil { 148 extra += fmt.Sprintf("\n\t- should return: %v", e.ret0) 149 } 150 extra += delayString(e.delay) + errorString(e.err) 151 if extra != "" { 152 msg += " which:" + extra 153 } 154 return msg 155 } 156 157 func (e *ExpectedVersion) String() string { 158 msg := "call to Version()" 159 extra := "" 160 if e.ret0 != nil { 161 extra += fmt.Sprintf("\n\t- should return: %v", e.ret0) 162 } 163 extra += delayString(e.delay) + errorString(e.err) 164 if extra != "" { 165 msg += " which:" + extra 166 } 167 return msg 168 } 169 170 func (e *ExpectedDB) String() string { 171 msg := "call to DB() which:" + 172 fieldString("name", e.arg0) + 173 optionsString(e.options) 174 if e.db != nil { 175 msg += fmt.Sprintf("\n\t- should return database with %d expectations", e.db.expectations()) 176 } 177 msg += delayString(e.delay) 178 return msg 179 } 180 181 // WithName sets the expectation that DB() will be called with this name. 182 func (e *ExpectedDB) WithName(name string) *ExpectedDB { 183 e.arg0 = name 184 return e 185 } 186 187 // ExpectedCreateDB represents an expectation to call the CreateDB() method. 188 // 189 // Implementation note: Because kivik always calls DB() after a 190 // successful CreateDB() is executed, ExpectCreateDB() creates two 191 // expectations under the covers, one for the backend CreateDB() call, 192 // and one for the DB() call. If WillReturnError() is called, the DB() call 193 // expectation is removed. 194 type ExpectedCreateDB struct { 195 commonExpectation 196 arg0 string 197 callback func(ctx context.Context, arg0 string, options driver.Options) error 198 } 199 200 func (e *ExpectedCreateDB) String() string { 201 msg := "call to CreateDB() which:" + 202 fieldString("name", e.arg0) + 203 optionsString(e.options) 204 if e.db != nil { 205 msg += fmt.Sprintf("\n\t- should return database with %d expectations", e.db.expectations()) 206 } 207 msg += delayString(e.delay) 208 msg += errorString(e.err) 209 return msg 210 } 211 212 func (e *ExpectedCreateDB) method(v bool) string { 213 if !v { 214 return "CreateDB()" 215 } 216 var name, options string 217 if e.arg0 == "" { 218 name = "?" 219 } else { 220 name = fmt.Sprintf("%q", e.arg0) 221 } 222 if e.options != nil { 223 options = fmt.Sprintf(", %v", e.options) 224 } 225 return fmt.Sprintf("CreateDB(ctx, %s%s)", name, options) 226 } 227 228 func (e *ExpectedCreateDB) met(ex expectation) bool { 229 exp := ex.(*ExpectedCreateDB) 230 return exp.arg0 == "" || exp.arg0 == e.arg0 231 } 232 233 // WithOptions set the expectation that DB() will be called with these options. 234 func (e *ExpectedCreateDB) WithOptions(options ...kivik.Option) *ExpectedCreateDB { 235 e.options = multiOptions{e.options, multiOptions(options)} 236 return e 237 } 238 239 // WithName sets the expectation that DB() will be called with this name. 240 func (e *ExpectedCreateDB) WithName(name string) *ExpectedCreateDB { 241 e.arg0 = name 242 return e 243 } 244 245 // WillExecute sets a callback function to be called with any inputs to the 246 // original function. Any values returned by the callback will be returned as 247 // if generated by the driver. 248 func (e *ExpectedCreateDB) WillExecute(cb func(ctx context.Context, arg0 string, options driver.Options) error) *ExpectedCreateDB { 249 e.callback = cb 250 return e 251 } 252 253 // WillReturnError sets the return value for the DB() call. 254 func (e *ExpectedCreateDB) WillReturnError(err error) *ExpectedCreateDB { 255 e.err = err 256 return e 257 } 258 259 // WillDelay will cause execution of DB() to delay by duration d. 260 func (e *ExpectedCreateDB) WillDelay(delay time.Duration) *ExpectedCreateDB { 261 e.delay = delay 262 return e 263 } 264 265 func (e *ExpectedDBUpdates) String() string { 266 msg := "call to DBUpdates()" 267 var extra string 268 if e.ret0 != nil { 269 extra += fmt.Sprintf("\n\t- should return: %d results", e.ret0.count()) 270 } 271 extra += delayString(e.delay) 272 extra += errorString(e.err) 273 if extra != "" { 274 msg += " which:" + extra 275 } 276 return msg 277 } 278 279 func (e *ExpectedConfig) String() string { 280 msg := "call to Config() which:" 281 msg += fieldString("node", e.arg0) 282 if e.ret0 != nil { 283 msg += fmt.Sprintf("\n\t- should return: %v", e.ret0) 284 } 285 msg += delayString(e.delay) 286 msg += errorString(e.err) 287 return msg 288 } 289 290 // WithNode sets the expected node. 291 func (e *ExpectedConfig) WithNode(node string) *ExpectedConfig { 292 e.arg0 = node 293 return e 294 } 295 296 func (e *ExpectedConfigSection) String() string { 297 msg := "call to ConfigSection() which:" + 298 fieldString("node", e.arg0) + 299 fieldString("section", e.arg1) 300 if e.ret0 != nil { 301 msg += fmt.Sprintf("\n\t- should return: %v", e.ret0) 302 } 303 msg += delayString(e.delay) 304 msg += errorString(e.err) 305 return msg 306 } 307 308 // WithNode sets the expected node. 309 func (e *ExpectedConfigSection) WithNode(node string) *ExpectedConfigSection { 310 e.arg0 = node 311 return e 312 } 313 314 // WithSection sets the expected section. 315 func (e *ExpectedConfigSection) WithSection(section string) *ExpectedConfigSection { 316 e.arg1 = section 317 return e 318 } 319 320 func (e *ExpectedConfigValue) String() string { 321 msg := "call to ConfigValue() which:" + 322 fieldString("node", e.arg0) + 323 fieldString("section", e.arg1) + 324 fieldString("key", e.arg2) 325 if e.ret0 != "" { 326 msg += fmt.Sprintf("\n\t- should return: %v", e.ret0) 327 } 328 msg += delayString(e.delay) 329 msg += errorString(e.err) 330 return msg 331 } 332 333 // WithNode sets the expected node. 334 func (e *ExpectedConfigValue) WithNode(node string) *ExpectedConfigValue { 335 e.arg0 = node 336 return e 337 } 338 339 // WithSection sets the expected section. 340 func (e *ExpectedConfigValue) WithSection(section string) *ExpectedConfigValue { 341 e.arg1 = section 342 return e 343 } 344 345 // WithKey sets the expected key. 346 func (e *ExpectedConfigValue) WithKey(key string) *ExpectedConfigValue { 347 e.arg2 = key 348 return e 349 } 350 351 func (e *ExpectedSetConfigValue) String() string { 352 msg := "call to SetConfigValue() which:" + 353 fieldString("node", e.arg0) + 354 fieldString("section", e.arg1) + 355 fieldString("key", e.arg2) + 356 fieldString("value", e.arg3) 357 if e.ret0 != "" { 358 msg += fmt.Sprintf("\n\t- should return: %v", e.ret0) 359 } 360 msg += delayString(e.delay) 361 msg += errorString(e.err) 362 return msg 363 } 364 365 // WithNode sets the expected node. 366 func (e *ExpectedSetConfigValue) WithNode(node string) *ExpectedSetConfigValue { 367 e.arg0 = node 368 return e 369 } 370 371 // WithSection sets the expected section. 372 func (e *ExpectedSetConfigValue) WithSection(section string) *ExpectedSetConfigValue { 373 e.arg1 = section 374 return e 375 } 376 377 // WithKey sets the expected key. 378 func (e *ExpectedSetConfigValue) WithKey(key string) *ExpectedSetConfigValue { 379 e.arg2 = key 380 return e 381 } 382 383 // WithValue sets the expected value. 384 func (e *ExpectedSetConfigValue) WithValue(value string) *ExpectedSetConfigValue { 385 e.arg3 = value 386 return e 387 } 388 389 func (e *ExpectedDeleteConfigKey) String() string { 390 msg := "call to DeleteConfigKey() which:" + 391 fieldString("node", e.arg0) + 392 fieldString("section", e.arg1) + 393 fieldString("key", e.arg2) 394 if e.ret0 != "" { 395 msg += fmt.Sprintf("\n\t- should return: %v", e.ret0) 396 } 397 msg += delayString(e.delay) 398 msg += errorString(e.err) 399 return msg 400 } 401 402 // WithNode sets the expected node. 403 func (e *ExpectedDeleteConfigKey) WithNode(node string) *ExpectedDeleteConfigKey { 404 e.arg0 = node 405 return e 406 } 407 408 // WithSection sets the expected section. 409 func (e *ExpectedDeleteConfigKey) WithSection(section string) *ExpectedDeleteConfigKey { 410 e.arg1 = section 411 return e 412 } 413 414 // WithKey sets the expected key. 415 func (e *ExpectedDeleteConfigKey) WithKey(key string) *ExpectedDeleteConfigKey { 416 e.arg2 = key 417 return e 418 } 419 420 func (e *ExpectedReplicate) String() string { 421 msg := "call to Replicate() which:" + 422 fieldString("target", e.arg0) + 423 fieldString("source", e.arg1) + 424 optionsString(e.options) 425 if e.ret0 != nil { 426 msg += fmt.Sprintf("\n\t- should return: %v", jsonDoc(e.ret0)) 427 } 428 return msg + 429 delayString(e.delay) + 430 errorString(e.err) 431 } 432 433 // WithSource sets the expected source. 434 func (e *ExpectedReplicate) WithSource(source string) *ExpectedReplicate { 435 e.arg1 = source 436 return e 437 } 438 439 // WithTarget sets the expected target. 440 func (e *ExpectedReplicate) WithTarget(target string) *ExpectedReplicate { 441 e.arg0 = target 442 return e 443 } 444 445 func (e *ExpectedGetReplications) String() string { 446 msg := "call to GetReplications() which:" + 447 optionsString(e.options) 448 if l := len(e.ret0); l > 0 { 449 msg += fmt.Sprintf("\n\t- should return: %d results", l) 450 } 451 return msg + 452 delayString(e.delay) + 453 errorString(e.err) 454 }