github.com/go-kivik/kivik/v4@v4.3.2/mockdb/db_test.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 "errors" 18 "strings" 19 "testing" 20 "time" 21 22 "gitlab.com/flimzy/testy" 23 24 kivik "github.com/go-kivik/kivik/v4" 25 "github.com/go-kivik/kivik/v4/driver" 26 ) 27 28 func TestCloseDB(t *testing.T) { 29 tests := testy.NewTable() 30 tests.Add("error", mockTest{ 31 setup: func(m *Client) { 32 db := m.NewDB() 33 m.ExpectDB().WillReturn(db) 34 db.ExpectClose().WillReturnError(errors.New("foo err")) 35 }, 36 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 37 err := c.DB("foo").Close() 38 if !testy.ErrorMatches("foo err", err) { 39 t.Errorf("Unexpected error: %s", err) 40 } 41 }, 42 err: "", 43 }) 44 tests.Add("unexpected", mockTest{ 45 setup: func(m *Client) { 46 db := m.NewDB() 47 m.ExpectDB().WillReturn(db) 48 }, 49 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 50 err := c.DB("foo").Close() 51 if !testy.ErrorMatches("call to DB.Close() was not expected, all expectations already fulfilled", err) { 52 t.Errorf("Unexpected error: %s", err) 53 } 54 }, 55 }) 56 tests.Add("wrong db", mockTest{ 57 setup: func(m *Client) { 58 foo := m.NewDB() 59 bar := m.NewDB() 60 m.ExpectDB().WithName("foo").WillReturn(foo) 61 m.ExpectDB().WithName("bar").WillReturn(bar) 62 bar.ExpectClose() 63 foo.ExpectClose() 64 }, 65 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 66 foo := c.DB("foo") 67 _ = c.DB("bar") 68 err := foo.Close() 69 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 70 t.Errorf("Unexpected error: %s", err) 71 } 72 }, 73 err: "there is a remaining unmet expectation", 74 }) 75 tests.Add("callback", mockTest{ 76 setup: func(m *Client) { 77 db := m.NewDB() 78 m.ExpectDB().WillReturn(db) 79 db.ExpectClose().WillExecute(func() error { 80 return errors.New("custom error") 81 }) 82 }, 83 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 84 err := c.DB("foo").Close() 85 if !testy.ErrorMatches("custom error", err) { 86 t.Errorf("Unexpected error: %s", err) 87 } 88 }, 89 }) 90 tests.Run(t, testMock) 91 } 92 93 func TestAllDocs(t *testing.T) { 94 t.Parallel() 95 tests := testy.NewTable() 96 tests.Add("error", mockTest{ 97 setup: func(m *Client) { 98 db := m.NewDB() 99 m.ExpectDB().WillReturn(db) 100 db.ExpectAllDocs().WillReturnError(errors.New("foo err")) 101 }, 102 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 103 db := c.DB("foo") 104 rows := db.AllDocs(context.TODO()) 105 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 106 t.Errorf("Unexpected error: %s", err) 107 } 108 }, 109 }) 110 tests.Add("unexpected", mockTest{ 111 setup: func(m *Client) { 112 db := m.NewDB() 113 m.ExpectDB().WillReturn(db) 114 }, 115 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 116 db := c.DB("foo") 117 rows := db.AllDocs(context.TODO()) 118 if err := rows.Err(); !testy.ErrorMatches("call to DB.AllDocs() was not expected, all expectations already fulfilled", err) { 119 t.Errorf("Unexpected error: %s", err) 120 } 121 }, 122 }) 123 tests.Add("rows close error", mockTest{ 124 setup: func(m *Client) { 125 db := m.NewDB() 126 m.ExpectDB().WillReturn(db) 127 db.ExpectAllDocs().WillReturn(NewRows().CloseError(errors.New("bar err"))) 128 }, 129 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 130 db := c.DB("foo") 131 rows := db.AllDocs(context.TODO()) 132 if err := rows.Err(); !testy.ErrorMatches("", err) { 133 t.Errorf("Unexpected error: %s", err) 134 } 135 if err := rows.Close(); !testy.ErrorMatches("bar err", err) { 136 t.Errorf("Unexpected error: %s", err) 137 } 138 }, 139 }) 140 tests.Add("rows offset", mockTest{ 141 setup: func(m *Client) { 142 db := m.NewDB() 143 m.ExpectDB().WillReturn(db) 144 db.ExpectAllDocs().WillReturn(NewRows().Offset(123)) 145 }, 146 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 147 db := c.DB("foo") 148 rows := db.AllDocs(context.TODO()) 149 for rows.Next() { 150 // skip all rows 151 } 152 metadata, err := rows.Metadata() 153 if !testy.ErrorMatches("", err) { 154 t.Errorf("Unexpected error: %s", err) 155 } 156 if metadata.Offset != 123 { 157 t.Errorf("Unexpected offset: %d", metadata.Offset) 158 } 159 }, 160 }) 161 tests.Add("rows totalrows", mockTest{ 162 setup: func(m *Client) { 163 db := m.NewDB() 164 m.ExpectDB().WillReturn(db) 165 db.ExpectAllDocs().WillReturn(NewRows().TotalRows(123)) 166 }, 167 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 168 db := c.DB("foo") 169 rows := db.AllDocs(context.TODO()) 170 for rows.Next() { 171 // skip all rows 172 } 173 metadata, err := rows.Metadata() 174 if !testy.ErrorMatches("", err) { 175 t.Errorf("Unexpected error: %s", err) 176 } 177 if metadata.TotalRows != 123 { 178 t.Errorf("Unexpected total rows: %d", metadata.TotalRows) 179 } 180 }, 181 }) 182 tests.Add("rows update seq", mockTest{ 183 setup: func(m *Client) { 184 db := m.NewDB() 185 m.ExpectDB().WillReturn(db) 186 db.ExpectAllDocs().WillReturn(NewRows().UpdateSeq("1-xxx")) 187 }, 188 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 189 db := c.DB("foo") 190 rows := db.AllDocs(context.TODO()) 191 for rows.Next() { 192 // skip all rows 193 } 194 metadata, err := rows.Metadata() 195 if !testy.ErrorMatches("", err) { 196 t.Errorf("Unexpected error: %s", err) 197 } 198 if o := metadata.UpdateSeq; o != "1-xxx" { 199 t.Errorf("Unexpected update seq: %s", metadata.UpdateSeq) 200 } 201 }, 202 }) 203 tests.Add("rows warning", mockTest{ 204 setup: func(m *Client) { 205 db := m.NewDB() 206 m.ExpectDB().WillReturn(db) 207 db.ExpectAllDocs().WillReturn(NewRows().Warning("Caution!")) 208 }, 209 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 210 db := c.DB("foo") 211 rows := db.AllDocs(context.TODO()) 212 for rows.Next() { 213 // skip all rows 214 } 215 metadata, err := rows.Metadata() 216 if !testy.ErrorMatches("", err) { 217 t.Errorf("Unexpected error: %s", err) 218 } 219 if o := metadata.Warning; o != "Caution!" { 220 t.Errorf("Unexpected warning seq: %s", metadata.Warning) 221 } 222 }, 223 }) 224 tests.Add("rows", mockTest{ 225 setup: func(m *Client) { 226 db := m.NewDB() 227 m.ExpectDB().WillReturn(db) 228 db.ExpectAllDocs().WillReturn(NewRows(). 229 AddRow(&driver.Row{ID: "foo"}). 230 AddRow(&driver.Row{ID: "bar"}). 231 AddRow(&driver.Row{ID: "baz"})) 232 }, 233 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 234 db := c.DB("foo") 235 rows := db.AllDocs(context.TODO()) 236 if err := rows.Err(); !testy.ErrorMatches("", err) { 237 t.Errorf("Unexpected error: %s", err) 238 } 239 ids := []string{} 240 for rows.Next() { 241 id, _ := rows.ID() 242 ids = append(ids, id) 243 } 244 expected := []string{"foo", "bar", "baz"} 245 if d := testy.DiffInterface(expected, ids); d != nil { 246 t.Error(d) 247 } 248 }, 249 }) 250 tests.Add("row error", mockTest{ 251 setup: func(m *Client) { 252 db := m.NewDB() 253 m.ExpectDB().WillReturn(db) 254 db.ExpectAllDocs().WillReturn(NewRows(). 255 AddRow(&driver.Row{ID: "foo"}). 256 AddRowError(errors.New("foo err"))) 257 }, 258 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 259 db := c.DB("foo") 260 rows := db.AllDocs(context.TODO()) 261 if err := rows.Err(); !testy.ErrorMatches("", err) { 262 t.Errorf("Unexpected error: %s", err) 263 } 264 ids := []string{} 265 for rows.Next() { 266 id, _ := rows.ID() 267 ids = append(ids, id) 268 } 269 expected := []string{"foo"} 270 if d := testy.DiffInterface(expected, ids); d != nil { 271 t.Error(d) 272 } 273 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 274 t.Errorf("Unexpected error: %s", err) 275 } 276 }, 277 }) 278 tests.Add("options", mockTest{ 279 setup: func(m *Client) { 280 db := m.NewDB() 281 m.ExpectDB().WillReturn(db) 282 db.ExpectAllDocs().WithOptions(kivik.Param("foo", 123)) 283 }, 284 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 285 db := c.DB("foo") 286 rows := db.AllDocs(context.TODO()) 287 if err := rows.Err(); !testy.ErrorMatchesRE(`map\[foo:123]`, err) { 288 t.Errorf("Unexpected error: %s", err) 289 } 290 }, 291 err: "there is a remaining unmet expectation", 292 }) 293 tests.Add("delay", mockTest{ 294 setup: func(m *Client) { 295 db := m.NewDB() 296 m.ExpectDB().WillReturn(db) 297 db.ExpectAllDocs().WillDelay(time.Second) 298 }, 299 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 300 db := c.DB("foo") 301 rows := db.AllDocs(newCanceledContext()) 302 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 303 t.Errorf("Unexpected error: %s", err) 304 } 305 }, 306 }) 307 tests.Add("row delay", mockTest{ 308 setup: func(m *Client) { 309 db := m.NewDB() 310 m.ExpectDB().WillReturn(db) 311 db.ExpectAllDocs().WillReturn(NewRows(). 312 AddDelay(time.Millisecond). 313 AddRow(&driver.Row{ID: "foo"}). 314 AddDelay(time.Second). 315 AddRow(&driver.Row{ID: "bar"})) 316 }, 317 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 318 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 319 defer cancel() 320 rows := c.DB("foo").AllDocs(ctx) 321 if err := rows.Err(); !testy.ErrorMatches("", err) { 322 t.Errorf("Unexpected error: %s", err) 323 } 324 ids := []string{} 325 for rows.Next() { 326 id, _ := rows.ID() 327 ids = append(ids, id) 328 } 329 expected := []string{"foo"} 330 if d := testy.DiffInterface(expected, ids); d != nil { 331 t.Error(d) 332 } 333 if err := rows.Err(); !testy.ErrorMatches("context deadline exceeded", err) { 334 t.Errorf("Unexpected error: %s", err) 335 } 336 }, 337 }) 338 tests.Add("wrong db", mockTest{ 339 setup: func(m *Client) { 340 foo := m.NewDB() 341 bar := m.NewDB() 342 m.ExpectDB().WithName("foo").WillReturn(foo) 343 m.ExpectDB().WithName("bar").WillReturn(bar) 344 bar.ExpectAllDocs() 345 foo.ExpectAllDocs() 346 }, 347 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 348 foo := c.DB("foo") 349 _ = c.DB("bar") 350 rows := foo.AllDocs(context.TODO()) 351 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 352 t.Errorf("Unexpected error: %s", err) 353 } 354 }, 355 err: "there is a remaining unmet expectation", 356 }) 357 tests.Run(t, testMock) 358 } 359 360 func TestBulkGet(t *testing.T) { // nolint: gocyclo 361 tests := testy.NewTable() 362 tests.Add("error", mockTest{ 363 setup: func(m *Client) { 364 db := m.NewDB() 365 m.ExpectDB().WillReturn(db) 366 db.ExpectBulkGet().WillReturnError(errors.New("foo err")) 367 }, 368 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 369 db := c.DB("foo") 370 rows := db.BulkGet(context.TODO(), []kivik.BulkGetReference{}) 371 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 372 t.Errorf("Unexpected error: %s", err) 373 } 374 }, 375 }) 376 tests.Add("unexpected", mockTest{ 377 setup: func(m *Client) { 378 db := m.NewDB() 379 m.ExpectDB().WillReturn(db) 380 }, 381 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 382 db := c.DB("foo") 383 rows := db.BulkGet(context.TODO(), []kivik.BulkGetReference{}) 384 if err := rows.Err(); !testy.ErrorMatches("call to DB.BulkGet() was not expected, all expectations already fulfilled", err) { 385 t.Errorf("Unexpected error: %s", err) 386 } 387 }, 388 }) 389 tests.Add("rows", mockTest{ 390 setup: func(m *Client) { 391 db := m.NewDB() 392 m.ExpectDB().WillReturn(db) 393 db.ExpectBulkGet().WillReturn(NewRows(). 394 AddRow(&driver.Row{ID: "foo"}). 395 AddRow(&driver.Row{ID: "bar"}). 396 AddRow(&driver.Row{ID: "baz"})) 397 }, 398 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 399 db := c.DB("foo") 400 rows := db.BulkGet(context.TODO(), []kivik.BulkGetReference{}) 401 if err := rows.Err(); !testy.ErrorMatches("", err) { 402 t.Errorf("Unexpected error: %s", err) 403 } 404 ids := []string{} 405 for rows.Next() { 406 id, _ := rows.ID() 407 ids = append(ids, id) 408 } 409 expected := []string{"foo", "bar", "baz"} 410 if d := testy.DiffInterface(expected, ids); d != nil { 411 t.Error(d) 412 } 413 }, 414 }) 415 tests.Add("options", mockTest{ 416 setup: func(m *Client) { 417 db := m.NewDB() 418 m.ExpectDB().WillReturn(db) 419 db.ExpectBulkGet().WithOptions(kivik.Param("foo", 123)) 420 }, 421 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 422 db := c.DB("foo") 423 rows := db.BulkGet(context.TODO(), []kivik.BulkGetReference{}) 424 if err := rows.Err(); !testy.ErrorMatchesRE(`map\[foo:123]`, err) { 425 t.Errorf("Unexpected error: %s", err) 426 } 427 }, 428 err: "there is a remaining unmet expectation", 429 }) 430 tests.Add("delay", mockTest{ 431 setup: func(m *Client) { 432 db := m.NewDB() 433 m.ExpectDB().WillReturn(db) 434 db.ExpectBulkGet().WillDelay(time.Second) 435 }, 436 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 437 db := c.DB("foo") 438 rows := db.BulkGet(newCanceledContext(), []kivik.BulkGetReference{}) 439 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 440 t.Errorf("Unexpected error: %s", err) 441 } 442 }, 443 }) 444 tests.Add("wrong db", mockTest{ 445 setup: func(m *Client) { 446 foo := m.NewDB() 447 bar := m.NewDB() 448 m.ExpectDB().WithName("foo").WillReturn(foo) 449 m.ExpectDB().WithName("bar").WillReturn(bar) 450 bar.ExpectBulkGet() 451 foo.ExpectBulkGet() 452 }, 453 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 454 foo := c.DB("foo") 455 _ = c.DB("bar") 456 rows := foo.BulkGet(context.TODO(), []kivik.BulkGetReference{}) 457 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 458 t.Errorf("Unexpected error: %s", err) 459 } 460 }, 461 err: "there is a remaining unmet expectation", 462 }) 463 tests.Run(t, testMock) 464 } 465 466 func TestFind(t *testing.T) { 467 tests := testy.NewTable() 468 tests.Add("error", mockTest{ 469 setup: func(m *Client) { 470 db := m.NewDB() 471 m.ExpectDB().WillReturn(db) 472 db.ExpectFind().WillReturnError(errors.New("foo err")) 473 }, 474 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 475 db := c.DB("foo") 476 rows := db.Find(context.TODO(), nil) 477 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 478 t.Errorf("Unexpected error: %s", err) 479 } 480 }, 481 }) 482 tests.Add("unmatched query", mockTest{ 483 setup: func(m *Client) { 484 db := m.NewDB() 485 m.ExpectDB().WillReturn(db) 486 db.ExpectFind().WithQuery(123) 487 }, 488 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 489 db := c.DB("foo") 490 rows := db.Find(context.TODO(), map[string]interface{}{"selector": map[string]interface{}{"foo": "123"}}) 491 if err := rows.Err(); !testy.ErrorMatchesRE("has query: 123", err) { 492 t.Errorf("Unexpected error: %s", err) 493 } 494 }, 495 err: "there is a remaining unmet expectation", 496 }) 497 tests.Add("rows", mockTest{ 498 setup: func(m *Client) { 499 db := m.NewDB() 500 m.ExpectDB().WillReturn(db) 501 db.ExpectFind().WillReturn(NewRows(). 502 AddRow(&driver.Row{ID: "foo"}). 503 AddRow(&driver.Row{ID: "bar"}). 504 AddRow(&driver.Row{ID: "baz"})) 505 }, 506 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 507 db := c.DB("foo") 508 rows := db.Find(context.TODO(), map[string]interface{}{}) 509 if err := rows.Err(); !testy.ErrorMatches("", err) { 510 t.Errorf("Unexpected error: %s", err) 511 } 512 ids := []string{} 513 for rows.Next() { 514 id, _ := rows.ID() 515 ids = append(ids, id) 516 } 517 expected := []string{"foo", "bar", "baz"} 518 if d := testy.DiffInterface(expected, ids); d != nil { 519 t.Error(d) 520 } 521 }, 522 }) 523 tests.Add("query", mockTest{ 524 setup: func(m *Client) { 525 db := m.NewDB() 526 m.ExpectDB().WillReturn(db) 527 db.ExpectFind().WithQuery(map[string]interface{}{"foo": "123"}) 528 }, 529 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 530 db := c.DB("foo") 531 rows := db.Find(context.TODO(), map[string]string{"foo": "123"}) 532 if err := rows.Err(); !testy.ErrorMatchesRE("", err) { 533 t.Errorf("Unexpected error: %s", err) 534 } 535 _ = rows.Close() 536 }, 537 }) 538 tests.Add("delay", mockTest{ 539 setup: func(m *Client) { 540 db := m.NewDB() 541 m.ExpectDB().WillReturn(db) 542 db.ExpectFind().WillDelay(time.Second) 543 }, 544 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 545 db := c.DB("foo") 546 rows := db.Find(newCanceledContext(), map[string]interface{}{}) 547 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 548 t.Errorf("Unexpected error: %s", err) 549 } 550 }, 551 }) 552 tests.Add("wrong db", mockTest{ 553 setup: func(m *Client) { 554 foo := m.NewDB() 555 bar := m.NewDB() 556 m.ExpectDB().WithName("foo").WillReturn(foo) 557 m.ExpectDB().WithName("bar").WillReturn(bar) 558 bar.ExpectFind() 559 foo.ExpectFind() 560 }, 561 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 562 foo := c.DB("foo") 563 _ = c.DB("bar") 564 rows := foo.Find(context.TODO(), map[string]interface{}{}) 565 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 566 t.Errorf("Unexpected error: %s", err) 567 } 568 }, 569 err: "there is a remaining unmet expectation", 570 }) 571 tests.Run(t, testMock) 572 } 573 574 func TestCreateIndex(t *testing.T) { 575 tests := testy.NewTable() 576 tests.Add("error", mockTest{ 577 setup: func(m *Client) { 578 db := m.NewDB() 579 m.ExpectDB().WillReturn(db) 580 db.ExpectCreateIndex().WillReturnError(errors.New("foo err")) 581 }, 582 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 583 err := c.DB("foo").CreateIndex(context.TODO(), "foo", "bar", 123) 584 if !testy.ErrorMatches("foo err", err) { 585 t.Errorf("Unexpected error: %s", err) 586 } 587 }, 588 }) 589 tests.Add("unmatched index", mockTest{ 590 setup: func(m *Client) { 591 db := m.NewDB() 592 m.ExpectDB().WillReturn(db) 593 db.ExpectCreateIndex().WithIndex(321) 594 }, 595 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 596 err := c.DB("foo").CreateIndex(context.TODO(), "foo", "bar", 123) 597 if !testy.ErrorMatchesRE("has index: 321", err) { 598 t.Errorf("Unexpected error: %s", err) 599 } 600 }, 601 err: "there is a remaining unmet expectation", 602 }) 603 tests.Add("ddoc", mockTest{ 604 setup: func(m *Client) { 605 db := m.NewDB() 606 m.ExpectDB().WillReturn(db) 607 db.ExpectCreateIndex().WithDDocID("moo") 608 }, 609 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 610 err := c.DB("foo").CreateIndex(context.TODO(), "foo", "bar", 123) 611 if !testy.ErrorMatchesRE("has ddoc: moo", err) { 612 t.Errorf("Unexpected error: %s", err) 613 } 614 }, 615 err: "there is a remaining unmet expectation", 616 }) 617 tests.Add("name", mockTest{ 618 setup: func(m *Client) { 619 db := m.NewDB() 620 m.ExpectDB().WillReturn(db) 621 db.ExpectCreateIndex().WithName("moo") 622 }, 623 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 624 err := c.DB("foo").CreateIndex(context.TODO(), "foo", "bar", 123) 625 if !testy.ErrorMatchesRE("has name: moo", err) { 626 t.Errorf("Unexpected error: %s", err) 627 } 628 }, 629 err: "there is a remaining unmet expectation", 630 }) 631 tests.Add("index", mockTest{ 632 setup: func(m *Client) { 633 db := m.NewDB() 634 m.ExpectDB().WillReturn(db) 635 db.ExpectCreateIndex().WithIndex("moo") 636 }, 637 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 638 err := c.DB("foo").CreateIndex(context.TODO(), "foo", "bar", "moo") 639 if !testy.ErrorMatches("", err) { 640 t.Errorf("Unexpected error: %s", err) 641 } 642 }, 643 }) 644 tests.Add("delay", mockTest{ 645 setup: func(m *Client) { 646 db := m.NewDB() 647 m.ExpectDB().WillReturn(db) 648 db.ExpectCreateIndex().WillDelay(time.Second) 649 }, 650 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 651 err := c.DB("foo").CreateIndex(newCanceledContext(), "foo", "bar", "moo") 652 if !testy.ErrorMatches("context canceled", err) { 653 t.Errorf("Unexpected error: %s", err) 654 } 655 }, 656 }) 657 tests.Add("wrong db", mockTest{ 658 setup: func(m *Client) { 659 foo := m.NewDB() 660 bar := m.NewDB() 661 m.ExpectDB().WithName("foo").WillReturn(foo) 662 m.ExpectDB().WithName("bar").WillReturn(bar) 663 bar.ExpectCreateIndex() 664 foo.ExpectCreateIndex() 665 }, 666 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 667 foo := c.DB("foo") 668 _ = c.DB("bar") 669 err := foo.CreateIndex(context.TODO(), "foo", "bar", 123) 670 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 671 t.Errorf("Unexpected error: %s", err) 672 } 673 }, 674 err: "there is a remaining unmet expectation", 675 }) 676 tests.Run(t, testMock) 677 } 678 679 func TestGetIndexes(t *testing.T) { 680 tests := testy.NewTable() 681 tests.Add("error", mockTest{ 682 setup: func(m *Client) { 683 db := m.NewDB() 684 m.ExpectDB().WillReturn(db) 685 db.ExpectGetIndexes().WillReturnError(errors.New("foo err")) 686 }, 687 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 688 _, err := c.DB("foo").GetIndexes(context.TODO()) 689 if !testy.ErrorMatches("foo err", err) { 690 t.Errorf("Unexpected error: %s", err) 691 } 692 }, 693 }) 694 tests.Add("indexes", mockTest{ 695 setup: func(m *Client) { 696 db := m.NewDB() 697 m.ExpectDB().WillReturn(db) 698 db.ExpectGetIndexes().WillReturn([]driver.Index{ 699 {Name: "foo"}, 700 {Name: "bar"}, 701 }) 702 }, 703 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 704 indexes, err := c.DB("foo").GetIndexes(context.TODO()) 705 if !testy.ErrorMatches("", err) { 706 t.Errorf("Unexpected error: %s", err) 707 } 708 expected := []kivik.Index{ 709 {Name: "foo"}, 710 {Name: "bar"}, 711 } 712 if d := testy.DiffInterface(expected, indexes); d != nil { 713 t.Error(d) 714 } 715 }, 716 }) 717 tests.Add("unexpected", mockTest{ 718 setup: func(m *Client) { 719 db := m.NewDB() 720 m.ExpectDB().WillReturn(db) 721 }, 722 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 723 _, err := c.DB("foo").GetIndexes(context.TODO()) 724 if !testy.ErrorMatches("call to DB.GetIndexes() was not expected, all expectations already fulfilled", err) { 725 t.Errorf("Unexpected error: %s", err) 726 } 727 }, 728 }) 729 tests.Add("delay", mockTest{ 730 setup: func(m *Client) { 731 db := m.NewDB() 732 m.ExpectDB().WillReturn(db) 733 db.ExpectGetIndexes().WillDelay(time.Second) 734 }, 735 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 736 _, err := c.DB("foo").GetIndexes(newCanceledContext()) 737 if !testy.ErrorMatches("context canceled", err) { 738 t.Errorf("Unexpected error: %s", err) 739 } 740 }, 741 }) 742 tests.Add("wrong db", mockTest{ 743 setup: func(m *Client) { 744 foo := m.NewDB() 745 bar := m.NewDB() 746 m.ExpectDB().WithName("foo").WillReturn(foo) 747 m.ExpectDB().WithName("bar").WillReturn(bar) 748 bar.ExpectGetIndexes() 749 foo.ExpectGetIndexes() 750 }, 751 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 752 foo := c.DB("foo") 753 _ = c.DB("bar") 754 _, err := foo.GetIndexes(context.TODO()) 755 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 756 t.Errorf("Unexpected error: %s", err) 757 } 758 }, 759 err: "there is a remaining unmet expectation", 760 }) 761 tests.Run(t, testMock) 762 } 763 764 func TestDeleteIndex(t *testing.T) { 765 tests := testy.NewTable() 766 tests.Add("error", mockTest{ 767 setup: func(m *Client) { 768 db := m.NewDB() 769 m.ExpectDB().WillReturn(db) 770 db.ExpectDeleteIndex().WillReturnError(errors.New("foo err")) 771 }, 772 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 773 err := c.DB("foo").DeleteIndex(context.TODO(), "foo", "bar") 774 if !testy.ErrorMatches("foo err", err) { 775 t.Errorf("Unexpected error: %s", err) 776 } 777 }, 778 }) 779 tests.Add("ddoc", mockTest{ 780 setup: func(m *Client) { 781 db := m.NewDB() 782 m.ExpectDB().WillReturn(db) 783 db.ExpectDeleteIndex().WithDDoc("oink") 784 }, 785 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 786 err := c.DB("foo").DeleteIndex(context.TODO(), "foo", "bar") 787 if !testy.ErrorMatchesRE("has ddoc: oink", err) { 788 t.Errorf("Unexpected error: %s", err) 789 } 790 }, 791 err: "there is a remaining unmet expectation", 792 }) 793 tests.Add("name", mockTest{ 794 setup: func(m *Client) { 795 db := m.NewDB() 796 m.ExpectDB().WillReturn(db) 797 db.ExpectDeleteIndex().WithName("oink") 798 }, 799 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 800 err := c.DB("foo").DeleteIndex(context.TODO(), "foo", "bar") 801 if !testy.ErrorMatchesRE("has name: oink", err) { 802 t.Errorf("Unexpected error: %s", err) 803 } 804 }, 805 err: "there is a remaining unmet expectation", 806 }) 807 tests.Add("delay", mockTest{ 808 setup: func(m *Client) { 809 db := m.NewDB() 810 m.ExpectDB().WillReturn(db) 811 db.ExpectDeleteIndex().WillDelay(time.Second) 812 }, 813 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 814 err := c.DB("foo").DeleteIndex(newCanceledContext(), "foo", "bar") 815 if !testy.ErrorMatches("context canceled", err) { 816 t.Errorf("Unexpected error: %s", err) 817 } 818 }, 819 }) 820 tests.Add("wrong db", mockTest{ 821 setup: func(m *Client) { 822 foo := m.NewDB() 823 bar := m.NewDB() 824 m.ExpectDB().WithName("foo").WillReturn(foo) 825 m.ExpectDB().WithName("bar").WillReturn(bar) 826 bar.ExpectDeleteIndex() 827 foo.ExpectDeleteIndex() 828 }, 829 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 830 foo := c.DB("foo") 831 _ = c.DB("bar") 832 err := foo.DeleteIndex(context.TODO(), "foo", "bar") 833 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 834 t.Errorf("Unexpected error: %s", err) 835 } 836 }, 837 err: "there is a remaining unmet expectation", 838 }) 839 tests.Run(t, testMock) 840 } 841 842 func TestExplain(t *testing.T) { 843 tests := testy.NewTable() 844 tests.Add("error", mockTest{ 845 setup: func(m *Client) { 846 db := m.NewDB() 847 m.ExpectDB().WillReturn(db) 848 db.ExpectExplain().WillReturnError(errors.New("foo err")) 849 }, 850 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 851 _, err := c.DB("foo").Explain(context.TODO(), "foo") 852 if !testy.ErrorMatches("foo err", err) { 853 t.Errorf("Unexpected error: %s", err) 854 } 855 }, 856 }) 857 tests.Add("unexpected", mockTest{ 858 setup: func(m *Client) { 859 db := m.NewDB() 860 m.ExpectDB().WillReturn(db) 861 }, 862 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 863 _, err := c.DB("foo").Explain(context.TODO(), "foo") 864 if !testy.ErrorMatches("call to DB.Explain() was not expected, all expectations already fulfilled", err) { 865 t.Errorf("Unexpected error: %s", err) 866 } 867 }, 868 }) 869 tests.Add("query", mockTest{ 870 setup: func(m *Client) { 871 db := m.NewDB() 872 m.ExpectDB().WillReturn(db) 873 db.ExpectExplain().WithQuery(map[string]string{"foo": "bar"}) 874 }, 875 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 876 _, err := c.DB("foo").Explain(context.TODO(), map[string]interface{}{"foo": "bar"}) 877 if !testy.ErrorMatches("", err) { 878 t.Errorf("Unexpected error: %s", err) 879 } 880 }, 881 }) 882 tests.Add("plan", mockTest{ 883 setup: func(m *Client) { 884 db := m.NewDB() 885 m.ExpectDB().WillReturn(db) 886 db.ExpectExplain().WillReturn(&driver.QueryPlan{DBName: "foo"}) 887 }, 888 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 889 plan, err := c.DB("foo").Explain(context.TODO(), map[string]interface{}{"foo": "bar"}) 890 if !testy.ErrorMatches("", err) { 891 t.Errorf("Unexpected error: %s", err) 892 } 893 expected := &kivik.QueryPlan{DBName: "foo"} 894 if d := testy.DiffInterface(expected, plan); d != nil { 895 t.Error(d) 896 } 897 }, 898 }) 899 tests.Add("delay", mockTest{ 900 setup: func(m *Client) { 901 db := m.NewDB() 902 m.ExpectDB().WillReturn(db) 903 db.ExpectExplain().WillDelay(time.Second) 904 }, 905 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 906 _, err := c.DB("foo").Explain(newCanceledContext(), 123) 907 if !testy.ErrorMatches("context canceled", err) { 908 t.Errorf("Unexpected error: %s", err) 909 } 910 }, 911 }) 912 tests.Add("wrong db", mockTest{ 913 setup: func(m *Client) { 914 foo := m.NewDB() 915 bar := m.NewDB() 916 m.ExpectDB().WithName("foo").WillReturn(foo) 917 m.ExpectDB().WithName("bar").WillReturn(bar) 918 bar.ExpectExplain() 919 foo.ExpectExplain() 920 }, 921 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 922 foo := c.DB("foo") 923 _ = c.DB("bar") 924 _, err := foo.Explain(context.TODO(), 123) 925 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 926 t.Errorf("Unexpected error: %s", err) 927 } 928 }, 929 err: "there is a remaining unmet expectation", 930 }) 931 tests.Run(t, testMock) 932 } 933 934 func TestCreateDoc(t *testing.T) { 935 tests := testy.NewTable() 936 tests.Add("error", mockTest{ 937 setup: func(m *Client) { 938 db := m.NewDB() 939 m.ExpectDB().WillReturn(db) 940 db.ExpectCreateDoc().WillReturnError(errors.New("foo err")) 941 }, 942 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 943 _, _, err := c.DB("foo").CreateDoc(context.TODO(), "foo") 944 if !testy.ErrorMatches("foo err", err) { 945 t.Errorf("Unexpected error: %s", err) 946 } 947 }, 948 }) 949 tests.Add("return", func() interface{} { 950 docID, rev := "foo", "1-xxx" 951 return mockTest{ 952 setup: func(m *Client) { 953 db := m.NewDB() 954 m.ExpectDB().WillReturn(db) 955 db.ExpectCreateDoc().WillReturn(docID, rev) 956 }, 957 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 958 i, r, err := c.DB("foo").CreateDoc(context.TODO(), "foo") 959 if !testy.ErrorMatches("", err) { 960 t.Errorf("Unexpected error: %s", err) 961 } 962 if i != docID || r != rev { 963 t.Errorf("Unexpected docID/Rev: %s/%s", i, r) 964 } 965 }, 966 } 967 }) 968 tests.Add("mismatched doc", mockTest{ 969 setup: func(m *Client) { 970 db := m.NewDB() 971 m.ExpectDB().WillReturn(db) 972 db.ExpectCreateDoc().WithDoc("foo") 973 }, 974 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 975 _, _, err := c.DB("foo").CreateDoc(context.TODO(), "bar") 976 if !testy.ErrorMatchesRE(`has doc: "foo"`, err) { 977 t.Errorf("Unexpected error: %s", err) 978 } 979 }, 980 err: "there is a remaining unmet expectation", 981 }) 982 tests.Add("options", mockTest{ 983 setup: func(m *Client) { 984 db := m.NewDB() 985 m.ExpectDB().WillReturn(db) 986 db.ExpectCreateDoc().WithOptions(kivik.Param("foo", "bar")) 987 }, 988 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 989 _, _, err := c.DB("foo").CreateDoc(context.TODO(), "bar", kivik.Params(nil)) 990 if !testy.ErrorMatchesRE(`has options: map\[foo:bar]`, err) { 991 t.Errorf("Unexpected error: %s", err) 992 } 993 }, 994 err: "there is a remaining unmet expectation", 995 }) 996 tests.Add("delay", mockTest{ 997 setup: func(m *Client) { 998 db := m.NewDB() 999 m.ExpectDB().WillReturn(db) 1000 db.ExpectCreateDoc().WillDelay(time.Second) 1001 }, 1002 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1003 _, _, err := c.DB("foo").CreateDoc(newCanceledContext(), 123) 1004 if !testy.ErrorMatches("context canceled", err) { 1005 t.Errorf("Unexpected error: %s", err) 1006 } 1007 }, 1008 }) 1009 tests.Add("wrong db", mockTest{ 1010 setup: func(m *Client) { 1011 foo := m.NewDB() 1012 bar := m.NewDB() 1013 m.ExpectDB().WithName("foo").WillReturn(foo) 1014 m.ExpectDB().WithName("bar").WillReturn(bar) 1015 bar.ExpectCreateDoc() 1016 foo.ExpectCreateDoc() 1017 }, 1018 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1019 foo := c.DB("foo") 1020 _ = c.DB("bar") 1021 _, _, err := foo.CreateDoc(context.TODO(), 123) 1022 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1023 t.Errorf("Unexpected error: %s", err) 1024 } 1025 }, 1026 err: "there is a remaining unmet expectation", 1027 }) 1028 tests.Run(t, testMock) 1029 } 1030 1031 func TestCompact(t *testing.T) { 1032 tests := testy.NewTable() 1033 tests.Add("error", mockTest{ 1034 setup: func(m *Client) { 1035 db := m.NewDB() 1036 m.ExpectDB().WillReturn(db) 1037 db.ExpectCompact().WillReturnError(errors.New("foo err")) 1038 }, 1039 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1040 err := c.DB("foo").Compact(context.TODO()) 1041 if !testy.ErrorMatches("foo err", err) { 1042 t.Errorf("Unexpected error: %s", err) 1043 } 1044 }, 1045 }) 1046 tests.Add("delay", mockTest{ 1047 setup: func(m *Client) { 1048 db := m.NewDB() 1049 m.ExpectDB().WillReturn(db) 1050 db.ExpectCompact().WillDelay(time.Second) 1051 }, 1052 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1053 err := c.DB("foo").Compact(newCanceledContext()) 1054 if !testy.ErrorMatches("context canceled", err) { 1055 t.Errorf("Unexpected error: %s", err) 1056 } 1057 }, 1058 }) 1059 tests.Add("unexpected", mockTest{ 1060 setup: func(m *Client) { 1061 db := m.NewDB() 1062 m.ExpectDB().WillReturn(db) 1063 }, 1064 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1065 err := c.DB("foo").Compact(context.TODO()) 1066 if !testy.ErrorMatches("call to DB.Compact() was not expected, all expectations already fulfilled", err) { 1067 t.Errorf("Unexpected error: %s", err) 1068 } 1069 }, 1070 }) 1071 tests.Run(t, testMock) 1072 } 1073 1074 func TestCompactView(t *testing.T) { 1075 tests := testy.NewTable() 1076 tests.Add("error", mockTest{ 1077 setup: func(m *Client) { 1078 db := m.NewDB() 1079 m.ExpectDB().WillReturn(db) 1080 db.ExpectCompactView().WillReturnError(errors.New("foo err")) 1081 }, 1082 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1083 err := c.DB("foo").CompactView(context.TODO(), "foo") 1084 if !testy.ErrorMatches("foo err", err) { 1085 t.Errorf("Unexpected error: %s", err) 1086 } 1087 }, 1088 }) 1089 tests.Add("ddocID", mockTest{ 1090 setup: func(m *Client) { 1091 db := m.NewDB() 1092 m.ExpectDB().WillReturn(db) 1093 db.ExpectCompactView().WithDDoc("foo") 1094 }, 1095 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1096 err := c.DB("foo").CompactView(context.TODO(), "foo") 1097 if !testy.ErrorMatches("", err) { 1098 t.Errorf("Unexpected error: %s", err) 1099 } 1100 }, 1101 }) 1102 tests.Add("unexpected ddoc", mockTest{ 1103 setup: func(m *Client) { 1104 db := m.NewDB() 1105 m.ExpectDB().WillReturn(db) 1106 db.ExpectCompactView().WithDDoc("foo") 1107 }, 1108 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1109 err := c.DB("foo").CompactView(context.TODO(), "bar") 1110 if !testy.ErrorMatchesRE("has ddocID: foo", err) { 1111 t.Errorf("Unexpected error: %s", err) 1112 } 1113 }, 1114 err: "there is a remaining unmet expectation", 1115 }) 1116 tests.Add("delay", mockTest{ 1117 setup: func(m *Client) { 1118 db := m.NewDB() 1119 m.ExpectDB().WillReturn(db) 1120 db.ExpectCompactView().WillDelay(time.Second) 1121 }, 1122 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1123 err := c.DB("foo").CompactView(newCanceledContext(), "foo") 1124 if !testy.ErrorMatches("context canceled", err) { 1125 t.Errorf("Unexpected error: %s", err) 1126 } 1127 }, 1128 }) 1129 tests.Add("wrong db", mockTest{ 1130 setup: func(m *Client) { 1131 foo := m.NewDB() 1132 bar := m.NewDB() 1133 m.ExpectDB().WithName("foo").WillReturn(foo) 1134 m.ExpectDB().WithName("bar").WillReturn(bar) 1135 bar.ExpectCompactView() 1136 foo.ExpectCompactView() 1137 }, 1138 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1139 foo := c.DB("foo") 1140 _ = c.DB("bar") 1141 err := foo.CompactView(context.TODO(), "foo") 1142 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1143 t.Errorf("Unexpected error: %s", err) 1144 } 1145 }, 1146 err: "there is a remaining unmet expectation", 1147 }) 1148 tests.Run(t, testMock) 1149 } 1150 1151 func TestViewCleanup(t *testing.T) { 1152 tests := testy.NewTable() 1153 tests.Add("error", mockTest{ 1154 setup: func(m *Client) { 1155 db := m.NewDB() 1156 m.ExpectDB().WillReturn(db) 1157 db.ExpectViewCleanup().WillReturnError(errors.New("foo err")) 1158 }, 1159 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1160 err := c.DB("foo").ViewCleanup(context.TODO()) 1161 if !testy.ErrorMatches("foo err", err) { 1162 t.Errorf("Unexpected error: %s", err) 1163 } 1164 }, 1165 }) 1166 tests.Add("delay", mockTest{ 1167 setup: func(m *Client) { 1168 db := m.NewDB() 1169 m.ExpectDB().WillReturn(db) 1170 db.ExpectViewCleanup().WillDelay(time.Second) 1171 }, 1172 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1173 err := c.DB("foo").ViewCleanup(newCanceledContext()) 1174 if !testy.ErrorMatches("context canceled", err) { 1175 t.Errorf("Unexpected error: %s", err) 1176 } 1177 }, 1178 }) 1179 tests.Add("unexpected", mockTest{ 1180 setup: func(m *Client) { 1181 db := m.NewDB() 1182 m.ExpectDB().WillReturn(db) 1183 }, 1184 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1185 err := c.DB("foo").ViewCleanup(context.TODO()) 1186 if !testy.ErrorMatches("call to DB.ViewCleanup() was not expected, all expectations already fulfilled", err) { 1187 t.Errorf("Unexpected error: %s", err) 1188 } 1189 }, 1190 }) 1191 tests.Run(t, testMock) 1192 } 1193 1194 func TestPut(t *testing.T) { 1195 tests := testy.NewTable() 1196 tests.Add("error", mockTest{ 1197 setup: func(m *Client) { 1198 db := m.NewDB() 1199 m.ExpectDB().WillReturn(db) 1200 db.ExpectPut().WillReturnError(errors.New("foo err")) 1201 }, 1202 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1203 _, err := c.DB("foo").Put(context.TODO(), "foo", 123) 1204 if !testy.ErrorMatches("foo err", err) { 1205 t.Errorf("Unexpected error: %s", err) 1206 } 1207 }, 1208 }) 1209 tests.Add("delay", mockTest{ 1210 setup: func(m *Client) { 1211 db := m.NewDB() 1212 m.ExpectDB().WillReturn(db) 1213 db.ExpectPut().WillDelay(time.Second) 1214 }, 1215 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1216 _, err := c.DB("foo").Put(newCanceledContext(), "foo", 123) 1217 if !testy.ErrorMatches("context canceled", err) { 1218 t.Errorf("Unexpected error: %s", err) 1219 } 1220 }, 1221 }) 1222 tests.Add("unexpected", mockTest{ 1223 setup: func(m *Client) { 1224 db := m.NewDB() 1225 m.ExpectDB().WillReturn(db) 1226 }, 1227 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1228 _, err := c.DB("foo").Put(context.TODO(), "foo", 123) 1229 if !testy.ErrorMatches("call to DB.Put() was not expected, all expectations already fulfilled", err) { 1230 t.Errorf("Unexpected error: %s", err) 1231 } 1232 }, 1233 }) 1234 tests.Add("wrong db", mockTest{ 1235 setup: func(m *Client) { 1236 foo := m.NewDB() 1237 bar := m.NewDB() 1238 m.ExpectDB().WithName("foo").WillReturn(foo) 1239 m.ExpectDB().WithName("bar").WillReturn(bar) 1240 bar.ExpectPut() 1241 foo.ExpectPut() 1242 }, 1243 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1244 foo := c.DB("foo") 1245 _ = c.DB("bar") 1246 _, err := foo.Put(context.TODO(), "foo", 123) 1247 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1248 t.Errorf("Unexpected error: %s", err) 1249 } 1250 }, 1251 err: "there is a remaining unmet expectation", 1252 }) 1253 tests.Add("wrong id", mockTest{ 1254 setup: func(m *Client) { 1255 db := m.NewDB() 1256 m.ExpectDB().WillReturn(db) 1257 db.ExpectPut().WithDocID("foo") 1258 }, 1259 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1260 _, err := c.DB("foo").Put(context.TODO(), "bar", 123) 1261 if !testy.ErrorMatchesRE("has docID: foo", err) { 1262 t.Errorf("Unexpected error: %s", err) 1263 } 1264 }, 1265 err: "there is a remaining unmet expectation", 1266 }) 1267 tests.Add("wrong doc", mockTest{ 1268 setup: func(m *Client) { 1269 db := m.NewDB() 1270 m.ExpectDB().WillReturn(db) 1271 db.ExpectPut().WithDoc(map[string]string{"foo": "bar"}) 1272 }, 1273 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1274 _, err := c.DB("foo").Put(context.TODO(), "foo", 123) 1275 if !testy.ErrorMatchesRE("has docID: foo", err) { 1276 t.Errorf("Unexpected error: %s", err) 1277 } 1278 }, 1279 err: "there is a remaining unmet expectation", 1280 }) 1281 tests.Add("wrong options", mockTest{ 1282 setup: func(m *Client) { 1283 db := m.NewDB() 1284 m.ExpectDB().WillReturn(db) 1285 db.ExpectPut().WithOptions(kivik.Param("foo", "bar")) 1286 }, 1287 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1288 _, err := c.DB("foo").Put(context.TODO(), "foo", 123, kivik.Param("foo", 123)) 1289 if !testy.ErrorMatchesRE("has docID: foo", err) { 1290 t.Errorf("Unexpected error: %s", err) 1291 } 1292 }, 1293 err: "there is a remaining unmet expectation", 1294 }) 1295 tests.Add("success", mockTest{ 1296 setup: func(m *Client) { 1297 db := m.NewDB() 1298 m.ExpectDB().WillReturn(db) 1299 db.ExpectPut().WillReturn("oink") 1300 }, 1301 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1302 result, err := c.DB("foo").Put(context.TODO(), "foo", 123) 1303 if !testy.ErrorMatchesRE("", err) { 1304 t.Errorf("Unexpected error: %s", err) 1305 } 1306 if result != "oink" { 1307 t.Errorf("Unexpected result: %s", result) 1308 } 1309 }, 1310 }) 1311 tests.Run(t, testMock) 1312 } 1313 1314 func TestGetRev(t *testing.T) { 1315 tests := testy.NewTable() 1316 tests.Add("error", mockTest{ 1317 setup: func(m *Client) { 1318 db := m.NewDB() 1319 m.ExpectDB().WillReturn(db) 1320 db.ExpectGetRev().WillReturnError(errors.New("foo err")) 1321 }, 1322 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1323 _, err := c.DB("foo").GetRev(context.TODO(), "foo") 1324 if !testy.ErrorMatches("foo err", err) { 1325 t.Errorf("Unexpected error: %s", err) 1326 } 1327 }, 1328 }) 1329 tests.Add("delay", mockTest{ 1330 setup: func(m *Client) { 1331 db := m.NewDB() 1332 m.ExpectDB().WillReturn(db) 1333 db.ExpectGetRev().WillDelay(time.Second) 1334 }, 1335 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1336 _, err := c.DB("foo").GetRev(newCanceledContext(), "foo") 1337 if !testy.ErrorMatches("context canceled", err) { 1338 t.Errorf("Unexpected error: %s", err) 1339 } 1340 }, 1341 }) 1342 tests.Add("unexpected", mockTest{ 1343 setup: func(m *Client) { 1344 db := m.NewDB() 1345 m.ExpectDB().WillReturn(db) 1346 }, 1347 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1348 _, err := c.DB("foo").GetRev(context.TODO(), "foo") 1349 if !testy.ErrorMatches("call to DB.GetRev() was not expected, all expectations already fulfilled", err) { 1350 t.Errorf("Unexpected error: %s", err) 1351 } 1352 }, 1353 }) 1354 tests.Add("wrong db", mockTest{ 1355 setup: func(m *Client) { 1356 foo := m.NewDB() 1357 bar := m.NewDB() 1358 m.ExpectDB().WithName("foo").WillReturn(foo) 1359 m.ExpectDB().WithName("bar").WillReturn(bar) 1360 bar.ExpectGetRev() 1361 foo.ExpectGetRev() 1362 }, 1363 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1364 foo := c.DB("foo") 1365 _ = c.DB("bar") 1366 _, err := foo.GetRev(context.TODO(), "foo") 1367 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1368 t.Errorf("Unexpected error: %s", err) 1369 } 1370 }, 1371 err: "there is a remaining unmet expectation", 1372 }) 1373 tests.Add("wrong id", mockTest{ 1374 setup: func(m *Client) { 1375 db := m.NewDB() 1376 m.ExpectDB().WillReturn(db) 1377 db.ExpectGetRev().WithDocID("foo") 1378 }, 1379 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1380 _, err := c.DB("foo").GetRev(context.TODO(), "bar") 1381 if !testy.ErrorMatchesRE("has docID: foo", err) { 1382 t.Errorf("Unexpected error: %s", err) 1383 } 1384 }, 1385 err: "there is a remaining unmet expectation", 1386 }) 1387 tests.Add("wrong options", mockTest{ 1388 setup: func(m *Client) { 1389 db := m.NewDB() 1390 m.ExpectDB().WillReturn(db) 1391 db.ExpectGetRev().WithOptions(kivik.Param("foo", "bar")) 1392 }, 1393 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1394 _, err := c.DB("foo").GetRev(context.TODO(), "foo", kivik.Param("foo", 123)) 1395 if !testy.ErrorMatchesRE("has docID: foo", err) { 1396 t.Errorf("Unexpected error: %s", err) 1397 } 1398 }, 1399 err: "there is a remaining unmet expectation", 1400 }) 1401 tests.Add("success", mockTest{ 1402 setup: func(m *Client) { 1403 db := m.NewDB() 1404 m.ExpectDB().WillReturn(db) 1405 db.ExpectGetRev().WillReturn("1-oink") 1406 }, 1407 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1408 rev, err := c.DB("foo").GetRev(context.TODO(), "foo") 1409 if !testy.ErrorMatchesRE("", err) { 1410 t.Errorf("Unexpected error: %s", err) 1411 } 1412 if rev != "1-oink" { 1413 t.Errorf("Unexpected rev: %s", rev) 1414 } 1415 }, 1416 }) 1417 tests.Run(t, testMock) 1418 } 1419 1420 func TestFlush(t *testing.T) { 1421 tests := testy.NewTable() 1422 tests.Add("error", mockTest{ 1423 setup: func(m *Client) { 1424 db := m.NewDB() 1425 m.ExpectDB().WillReturn(db) 1426 db.ExpectFlush().WillReturnError(errors.New("foo err")) 1427 }, 1428 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1429 err := c.DB("foo").Flush(context.TODO()) 1430 if !testy.ErrorMatches("foo err", err) { 1431 t.Errorf("Unexpected error: %s", err) 1432 } 1433 }, 1434 }) 1435 tests.Add("delay", mockTest{ 1436 setup: func(m *Client) { 1437 db := m.NewDB() 1438 m.ExpectDB().WillReturn(db) 1439 db.ExpectFlush().WillDelay(time.Second) 1440 }, 1441 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1442 err := c.DB("foo").Flush(newCanceledContext()) 1443 if !testy.ErrorMatches("context canceled", err) { 1444 t.Errorf("Unexpected error: %s", err) 1445 } 1446 }, 1447 }) 1448 tests.Add("wrong db", mockTest{ 1449 setup: func(m *Client) { 1450 foo := m.NewDB() 1451 bar := m.NewDB() 1452 m.ExpectDB().WithName("foo").WillReturn(foo) 1453 m.ExpectDB().WithName("bar").WillReturn(bar) 1454 bar.ExpectFlush() 1455 foo.ExpectFlush() 1456 }, 1457 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1458 foo := c.DB("foo") 1459 _ = c.DB("bar") 1460 err := foo.Flush(context.TODO()) 1461 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1462 t.Errorf("Unexpected error: %s", err) 1463 } 1464 }, 1465 err: "there is a remaining unmet expectation", 1466 }) 1467 tests.Run(t, testMock) 1468 } 1469 1470 func TestDeleteAttachment(t *testing.T) { 1471 tests := testy.NewTable() 1472 tests.Add("error", mockTest{ 1473 setup: func(m *Client) { 1474 db := m.NewDB() 1475 m.ExpectDB().WillReturn(db) 1476 db.ExpectDeleteAttachment().WillReturnError(errors.New("foo err")) 1477 }, 1478 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1479 _, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1480 if !testy.ErrorMatches("foo err", err) { 1481 t.Errorf("Unexpected error: %s", err) 1482 } 1483 }, 1484 }) 1485 tests.Add("delay", mockTest{ 1486 setup: func(m *Client) { 1487 db := m.NewDB() 1488 m.ExpectDB().WillReturn(db) 1489 db.ExpectDeleteAttachment().WillDelay(time.Second) 1490 }, 1491 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1492 _, err := c.DB("foo").DeleteAttachment(newCanceledContext(), "foo", "1-foo", "foo.txt") 1493 if !testy.ErrorMatches("context canceled", err) { 1494 t.Errorf("Unexpected error: %s", err) 1495 } 1496 }, 1497 }) 1498 tests.Add("wrong db", mockTest{ 1499 setup: func(m *Client) { 1500 foo := m.NewDB() 1501 bar := m.NewDB() 1502 m.ExpectDB().WithName("foo").WillReturn(foo) 1503 m.ExpectDB().WithName("bar").WillReturn(bar) 1504 bar.ExpectDeleteAttachment() 1505 foo.ExpectDeleteAttachment() 1506 }, 1507 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1508 foo := c.DB("foo") 1509 _ = c.DB("bar") 1510 _, err := foo.DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1511 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1512 t.Errorf("Unexpected error: %s", err) 1513 } 1514 }, 1515 err: "there is a remaining unmet expectation", 1516 }) 1517 tests.Add("wrong docID", mockTest{ 1518 setup: func(m *Client) { 1519 db := m.NewDB() 1520 m.ExpectDB().WillReturn(db) 1521 db.ExpectDeleteAttachment().WithDocID("bar") 1522 }, 1523 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1524 _, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1525 if !testy.ErrorMatchesRE("has docID: bar", err) { 1526 t.Errorf("Unexpected error: %s", err) 1527 } 1528 }, 1529 err: "there is a remaining unmet expectation", 1530 }) 1531 tests.Add("wrong rev", mockTest{ 1532 setup: func(m *Client) { 1533 db := m.NewDB() 1534 m.ExpectDB().WillReturn(db) 1535 db.ExpectDeleteAttachment().WithOptions(kivik.Rev("2-asd")) 1536 }, 1537 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1538 _, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1539 if !testy.ErrorMatchesRE(`has options: map\[rev:1-foo\]`, err) { 1540 t.Errorf("Unexpected error: %s", err) 1541 } 1542 }, 1543 err: "there is a remaining unmet expectation", 1544 }) 1545 tests.Add("wrong filename", mockTest{ 1546 setup: func(m *Client) { 1547 db := m.NewDB() 1548 m.ExpectDB().WillReturn(db) 1549 db.ExpectDeleteAttachment().WithFilename("bar.txt") 1550 }, 1551 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1552 _, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1553 if !testy.ErrorMatchesRE("has filename: bar.txt", err) { 1554 t.Errorf("Unexpected error: %s", err) 1555 } 1556 }, 1557 err: "there is a remaining unmet expectation", 1558 }) 1559 tests.Add("wrong options", mockTest{ 1560 setup: func(m *Client) { 1561 db := m.NewDB() 1562 m.ExpectDB().WillReturn(db) 1563 db.ExpectDeleteAttachment().WithOptions(kivik.Param("foo", "baz")) 1564 }, 1565 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1566 _, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1567 if !testy.ErrorMatchesRE(`has options: map\[foo:baz]`, err) { 1568 t.Errorf("Unexpected error: %s", err) 1569 } 1570 }, 1571 err: "there is a remaining unmet expectation", 1572 }) 1573 tests.Add("success", mockTest{ 1574 setup: func(m *Client) { 1575 db := m.NewDB() 1576 m.ExpectDB().WillReturn(db) 1577 db.ExpectDeleteAttachment().WillReturn("2-fds") 1578 }, 1579 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1580 rev, err := c.DB("foo").DeleteAttachment(context.TODO(), "foo", "1-foo", "foo.txt") 1581 if !testy.ErrorMatches("", err) { 1582 t.Errorf("Unexpected error: %s", err) 1583 } 1584 if rev != "2-fds" { 1585 t.Errorf("Unexpected rev: %s", rev) 1586 } 1587 }, 1588 }) 1589 tests.Run(t, testMock) 1590 } 1591 1592 func TestDelete(t *testing.T) { 1593 tests := testy.NewTable() 1594 tests.Add("error", mockTest{ 1595 setup: func(m *Client) { 1596 db := m.NewDB() 1597 m.ExpectDB().WillReturn(db) 1598 db.ExpectDelete().WillReturnError(errors.New("foo err")) 1599 }, 1600 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1601 _, err := c.DB("foo").Delete(context.TODO(), "foo", "1-foo") 1602 if !testy.ErrorMatches("foo err", err) { 1603 t.Errorf("Unexpected error: %s", err) 1604 } 1605 }, 1606 }) 1607 tests.Add("delay", mockTest{ 1608 setup: func(m *Client) { 1609 db := m.NewDB() 1610 m.ExpectDB().WillReturn(db) 1611 db.ExpectDelete().WillDelay(time.Second) 1612 }, 1613 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1614 _, err := c.DB("foo").Delete(newCanceledContext(), "foo", "1-foo") 1615 if !testy.ErrorMatches("context canceled", err) { 1616 t.Errorf("Unexpected error: %s", err) 1617 } 1618 }, 1619 }) 1620 tests.Add("wrong db", mockTest{ 1621 setup: func(m *Client) { 1622 foo := m.NewDB() 1623 bar := m.NewDB() 1624 m.ExpectDB().WithName("foo").WillReturn(foo) 1625 m.ExpectDB().WithName("bar").WillReturn(bar) 1626 bar.ExpectDelete() 1627 foo.ExpectDelete() 1628 }, 1629 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1630 foo := c.DB("foo") 1631 _ = c.DB("bar") 1632 _, err := foo.Delete(context.TODO(), "foo", "1-foo") 1633 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1634 t.Errorf("Unexpected error: %s", err) 1635 } 1636 }, 1637 err: "there is a remaining unmet expectation", 1638 }) 1639 tests.Add("wrong docID", mockTest{ 1640 setup: func(m *Client) { 1641 db := m.NewDB() 1642 m.ExpectDB().WillReturn(db) 1643 db.ExpectDelete().WithDocID("bar") 1644 }, 1645 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1646 _, err := c.DB("foo").Delete(context.TODO(), "foo", "1-foo") 1647 if !testy.ErrorMatchesRE("has docID: bar", err) { 1648 t.Errorf("Unexpected error: %s", err) 1649 } 1650 }, 1651 err: "there is a remaining unmet expectation", 1652 }) 1653 tests.Add("wrong rev", mockTest{ 1654 setup: func(m *Client) { 1655 db := m.NewDB() 1656 m.ExpectDB().WillReturn(db) 1657 db.ExpectDelete().WithOptions(kivik.Rev("2-lkj")) 1658 }, 1659 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1660 _, err := c.DB("foo").Delete(context.TODO(), "foo", "1-foo") 1661 if !testy.ErrorMatchesRE(`has options: map\[rev:2-lkj\]`, err) { 1662 t.Errorf("Unexpected error: %s", err) 1663 } 1664 }, 1665 err: "there is a remaining unmet expectation", 1666 }) 1667 tests.Add("wrong options", mockTest{ 1668 setup: func(m *Client) { 1669 db := m.NewDB() 1670 m.ExpectDB().WillReturn(db) 1671 db.ExpectDelete().WithOptions(kivik.Param("foo", "baz")) 1672 }, 1673 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1674 _, err := c.DB("foo").Delete(context.TODO(), "foo", "1-foo") 1675 if !testy.ErrorMatchesRE(`has options: map\[foo:baz]`, err) { 1676 t.Errorf("Unexpected error: %s", err) 1677 } 1678 }, 1679 err: "there is a remaining unmet expectation", 1680 }) 1681 tests.Add("success", mockTest{ 1682 setup: func(m *Client) { 1683 db := m.NewDB() 1684 m.ExpectDB().WillReturn(db) 1685 db.ExpectDelete().WillReturn("2-uio") 1686 }, 1687 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1688 rev, err := c.DB("foo").Delete(context.TODO(), "foo", "1-foo") 1689 if !testy.ErrorMatches("", err) { 1690 t.Errorf("Unexpected error: %s", err) 1691 } 1692 if rev != "2-uio" { 1693 t.Errorf("Unexpected rev: %s", rev) 1694 } 1695 }, 1696 }) 1697 tests.Run(t, testMock) 1698 } 1699 1700 func TestCopy(t *testing.T) { 1701 tests := testy.NewTable() 1702 tests.Add("error", mockTest{ 1703 setup: func(m *Client) { 1704 db := m.NewDB() 1705 m.ExpectDB().WillReturn(db) 1706 db.ExpectCopy().WillReturnError(errors.New("foo err")) 1707 }, 1708 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1709 _, err := c.DB("foo").Copy(context.TODO(), "foo", "bar") 1710 if !testy.ErrorMatches("foo err", err) { 1711 t.Errorf("Unexpected error: %s", err) 1712 } 1713 }, 1714 }) 1715 tests.Add("delay", mockTest{ 1716 setup: func(m *Client) { 1717 db := m.NewDB() 1718 m.ExpectDB().WillReturn(db) 1719 db.ExpectCopy().WillDelay(time.Second) 1720 }, 1721 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1722 _, err := c.DB("foo").Copy(newCanceledContext(), "foo", "bar") 1723 if !testy.ErrorMatches("context canceled", err) { 1724 t.Errorf("Unexpected error: %s", err) 1725 } 1726 }, 1727 }) 1728 tests.Add("wrong db", mockTest{ 1729 setup: func(m *Client) { 1730 foo := m.NewDB() 1731 bar := m.NewDB() 1732 m.ExpectDB().WithName("foo").WillReturn(foo) 1733 m.ExpectDB().WithName("bar").WillReturn(bar) 1734 bar.ExpectCopy() 1735 foo.ExpectCopy() 1736 }, 1737 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1738 foo := c.DB("foo") 1739 _ = c.DB("bar") 1740 _, err := foo.Copy(context.TODO(), "foo", "1-foo") 1741 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1742 t.Errorf("Unexpected error: %s", err) 1743 } 1744 }, 1745 err: "there is a remaining unmet expectation", 1746 }) 1747 tests.Add("wrong targetID", mockTest{ 1748 setup: func(m *Client) { 1749 db := m.NewDB() 1750 m.ExpectDB().WillReturn(db) 1751 db.ExpectCopy().WithTargetID("bar") 1752 }, 1753 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1754 _, err := c.DB("foo").Copy(context.TODO(), "foo", "bar") 1755 if !testy.ErrorMatchesRE("has targetID: bar", err) { 1756 t.Errorf("Unexpected error: %s", err) 1757 } 1758 }, 1759 err: "there is a remaining unmet expectation", 1760 }) 1761 tests.Add("wrong sourceID", mockTest{ 1762 setup: func(m *Client) { 1763 db := m.NewDB() 1764 m.ExpectDB().WillReturn(db) 1765 db.ExpectCopy().WithSourceID("baz") 1766 }, 1767 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1768 _, err := c.DB("foo").Copy(context.TODO(), "foo", "bar") 1769 if !testy.ErrorMatchesRE("has sourceID: baz", err) { 1770 t.Errorf("Unexpected error: %s", err) 1771 } 1772 }, 1773 err: "there is a remaining unmet expectation", 1774 }) 1775 tests.Add("wrong options", mockTest{ 1776 setup: func(m *Client) { 1777 db := m.NewDB() 1778 m.ExpectDB().WillReturn(db) 1779 db.ExpectCopy().WithOptions(kivik.Param("foo", "baz")) 1780 }, 1781 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1782 _, err := c.DB("foo").Copy(context.TODO(), "foo", "bar") 1783 if !testy.ErrorMatchesRE(`has options: map\[foo:baz]`, err) { 1784 t.Errorf("Unexpected error: %s", err) 1785 } 1786 }, 1787 err: "there is a remaining unmet expectation", 1788 }) 1789 tests.Add("success", mockTest{ 1790 setup: func(m *Client) { 1791 db := m.NewDB() 1792 m.ExpectDB().WillReturn(db) 1793 db.ExpectCopy().WillReturn("2-oiu") 1794 }, 1795 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1796 rev, err := c.DB("foo").Copy(context.TODO(), "foo", "bar") 1797 if !testy.ErrorMatches("", err) { 1798 t.Errorf("Unexpected error: %s", err) 1799 } 1800 if rev != "2-oiu" { 1801 t.Errorf("Unexpected rev: %s", rev) 1802 } 1803 }, 1804 }) 1805 tests.Run(t, testMock) 1806 } 1807 1808 func TestGet(t *testing.T) { 1809 tests := testy.NewTable() 1810 tests.Add("error", mockTest{ 1811 setup: func(m *Client) { 1812 db := m.NewDB() 1813 m.ExpectDB().WillReturn(db) 1814 db.ExpectGet().WillReturnError(errors.New("foo err")) 1815 }, 1816 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1817 rows := c.DB("foo").Get(context.TODO(), "foo") 1818 err := rows.Err() 1819 if !testy.ErrorMatches("foo err", err) { 1820 t.Errorf("Unexpected error: %s", err) 1821 } 1822 }, 1823 }) 1824 tests.Add("delay", mockTest{ 1825 setup: func(m *Client) { 1826 db := m.NewDB() 1827 m.ExpectDB().WillReturn(db) 1828 db.ExpectGet().WillDelay(time.Second) 1829 }, 1830 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1831 rows := c.DB("foo").Get(newCanceledContext(), "foo") 1832 err := rows.Err() 1833 if !testy.ErrorMatches("context canceled", err) { 1834 t.Errorf("Unexpected error: %s", err) 1835 } 1836 }, 1837 }) 1838 tests.Add("wrong db", mockTest{ 1839 setup: func(m *Client) { 1840 foo := m.NewDB() 1841 bar := m.NewDB() 1842 m.ExpectDB().WithName("foo").WillReturn(foo) 1843 m.ExpectDB().WithName("bar").WillReturn(bar) 1844 bar.ExpectGet() 1845 foo.ExpectGet() 1846 }, 1847 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1848 foo := c.DB("foo") 1849 _ = c.DB("bar") 1850 rows := foo.Get(context.TODO(), "foo") 1851 err := rows.Err() 1852 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1853 t.Errorf("Unexpected error: %s", err) 1854 } 1855 }, 1856 err: "there is a remaining unmet expectation", 1857 }) 1858 tests.Add("wrong docID", mockTest{ 1859 setup: func(m *Client) { 1860 db := m.NewDB() 1861 m.ExpectDB().WillReturn(db) 1862 db.ExpectGet().WithDocID("bar") 1863 }, 1864 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1865 rows := c.DB("foo").Get(context.TODO(), "foo") 1866 err := rows.Err() 1867 if !testy.ErrorMatchesRE("has docID: bar", err) { 1868 t.Errorf("Unexpected error: %s", err) 1869 } 1870 }, 1871 err: "there is a remaining unmet expectation", 1872 }) 1873 tests.Add("wrong options", mockTest{ 1874 setup: func(m *Client) { 1875 db := m.NewDB() 1876 m.ExpectDB().WillReturn(db) 1877 db.ExpectGet().WithOptions(kivik.Param("foo", "baz")) 1878 }, 1879 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1880 rows := c.DB("foo").Get(context.TODO(), "foo") 1881 err := rows.Err() 1882 if !testy.ErrorMatchesRE(`has options: map\[foo:baz]`, err) { 1883 t.Errorf("Unexpected error: %s", err) 1884 } 1885 }, 1886 err: "there is a remaining unmet expectation", 1887 }) 1888 tests.Add("success", mockTest{ 1889 setup: func(m *Client) { 1890 db := m.NewDB() 1891 m.ExpectDB().WillReturn(db) 1892 db.ExpectGet().WillReturn(&driver.Document{Rev: "2-bar"}) 1893 }, 1894 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1895 rows := c.DB("foo").Get(context.TODO(), "foo") 1896 rev, err := rows.Rev() 1897 if !testy.ErrorMatches("", err) { 1898 t.Errorf("Unexpected error: %s", err) 1899 } 1900 if rev != "2-bar" { 1901 t.Errorf("Unexpected rev: %s", rev) 1902 } 1903 }, 1904 }) 1905 tests.Run(t, testMock) 1906 } 1907 1908 func TestGetAttachmentMeta(t *testing.T) { 1909 tests := testy.NewTable() 1910 tests.Add("error", mockTest{ 1911 setup: func(m *Client) { 1912 db := m.NewDB() 1913 m.ExpectDB().WillReturn(db) 1914 db.ExpectGetAttachmentMeta().WillReturnError(errors.New("foo err")) 1915 }, 1916 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1917 _, err := c.DB("foo").GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 1918 if !testy.ErrorMatches("foo err", err) { 1919 t.Errorf("Unexpected error: %s", err) 1920 } 1921 }, 1922 }) 1923 tests.Add("delay", mockTest{ 1924 setup: func(m *Client) { 1925 db := m.NewDB() 1926 m.ExpectDB().WillReturn(db) 1927 db.ExpectGetAttachmentMeta().WillDelay(time.Second) 1928 }, 1929 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1930 _, err := c.DB("foo").GetAttachmentMeta(newCanceledContext(), "foo", "foo.txt") 1931 if !testy.ErrorMatches("context canceled", err) { 1932 t.Errorf("Unexpected error: %s", err) 1933 } 1934 }, 1935 }) 1936 tests.Add("wrong db", mockTest{ 1937 setup: func(m *Client) { 1938 foo := m.NewDB() 1939 bar := m.NewDB() 1940 m.ExpectDB().WithName("foo").WillReturn(foo) 1941 m.ExpectDB().WithName("bar").WillReturn(bar) 1942 bar.ExpectGetAttachmentMeta() 1943 foo.ExpectGetAttachmentMeta() 1944 }, 1945 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1946 foo := c.DB("foo") 1947 _ = c.DB("bar") 1948 _, err := foo.GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 1949 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 1950 t.Errorf("Unexpected error: %s", err) 1951 } 1952 }, 1953 err: "there is a remaining unmet expectation", 1954 }) 1955 tests.Add("wrong docID", mockTest{ 1956 setup: func(m *Client) { 1957 db := m.NewDB() 1958 m.ExpectDB().WillReturn(db) 1959 db.ExpectGetAttachmentMeta().WithDocID("bar") 1960 }, 1961 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1962 _, err := c.DB("foo").GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 1963 if !testy.ErrorMatchesRE("has docID: bar", err) { 1964 t.Errorf("Unexpected error: %s", err) 1965 } 1966 }, 1967 err: "there is a remaining unmet expectation", 1968 }) 1969 tests.Add("wrong filename", mockTest{ 1970 setup: func(m *Client) { 1971 db := m.NewDB() 1972 m.ExpectDB().WillReturn(db) 1973 db.ExpectGetAttachmentMeta().WithFilename("bar.jpg") 1974 }, 1975 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1976 _, err := c.DB("foo").GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 1977 if !testy.ErrorMatchesRE("has filename: bar.jpg", err) { 1978 t.Errorf("Unexpected error: %s", err) 1979 } 1980 }, 1981 err: "there is a remaining unmet expectation", 1982 }) 1983 tests.Add("wrong options", mockTest{ 1984 setup: func(m *Client) { 1985 db := m.NewDB() 1986 m.ExpectDB().WillReturn(db) 1987 db.ExpectGetAttachmentMeta().WithOptions(kivik.Param("foo", "baz")) 1988 }, 1989 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 1990 _, err := c.DB("foo").GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 1991 if !testy.ErrorMatchesRE(`has options: map\[foo:baz]`, err) { 1992 t.Errorf("Unexpected error: %s", err) 1993 } 1994 }, 1995 err: "there is a remaining unmet expectation", 1996 }) 1997 tests.Add("success", mockTest{ 1998 setup: func(m *Client) { 1999 db := m.NewDB() 2000 m.ExpectDB().WillReturn(db) 2001 db.ExpectGetAttachmentMeta().WillReturn(&driver.Attachment{Filename: "foo.txt"}) 2002 }, 2003 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2004 att, err := c.DB("foo").GetAttachmentMeta(context.TODO(), "foo", "foo.txt") 2005 if !testy.ErrorMatches("", err) { 2006 t.Errorf("Unexpected error: %s", err) 2007 } 2008 if filename := att.Filename; filename != "foo.txt" { 2009 t.Errorf("Unexpected filename: %s", filename) 2010 } 2011 }, 2012 }) 2013 tests.Run(t, testMock) 2014 } 2015 2016 func TestLocalDocs(t *testing.T) { 2017 tests := testy.NewTable() 2018 tests.Add("error", mockTest{ 2019 setup: func(m *Client) { 2020 db := m.NewDB() 2021 m.ExpectDB().WillReturn(db) 2022 db.ExpectLocalDocs().WillReturnError(errors.New("foo err")) 2023 }, 2024 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2025 db := c.DB("foo") 2026 rows := db.LocalDocs(context.TODO(), nil) 2027 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 2028 t.Errorf("Unexpected error: %s", err) 2029 } 2030 }, 2031 }) 2032 tests.Add("success", mockTest{ 2033 setup: func(m *Client) { 2034 db := m.NewDB() 2035 m.ExpectDB().WillReturn(db) 2036 db.ExpectLocalDocs().WillReturn(NewRows(). 2037 AddRow(&driver.Row{ID: "foo"}). 2038 AddRow(&driver.Row{ID: "bar"}). 2039 AddRow(&driver.Row{ID: "baz"})) 2040 }, 2041 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2042 db := c.DB("foo") 2043 rows := db.LocalDocs(context.TODO()) 2044 if err := rows.Err(); !testy.ErrorMatches("", err) { 2045 t.Errorf("Unexpected error: %s", err) 2046 } 2047 ids := []string{} 2048 for rows.Next() { 2049 id, _ := rows.ID() 2050 ids = append(ids, id) 2051 } 2052 expected := []string{"foo", "bar", "baz"} 2053 if d := testy.DiffInterface(expected, ids); d != nil { 2054 t.Error(d) 2055 } 2056 }, 2057 }) 2058 tests.Add("delay", mockTest{ 2059 setup: func(m *Client) { 2060 db := m.NewDB() 2061 m.ExpectDB().WillReturn(db) 2062 db.ExpectLocalDocs().WillDelay(time.Second) 2063 }, 2064 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2065 db := c.DB("foo") 2066 rows := db.LocalDocs(newCanceledContext()) 2067 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 2068 t.Errorf("Unexpected error: %s", err) 2069 } 2070 }, 2071 }) 2072 tests.Add("wrong db", mockTest{ 2073 setup: func(m *Client) { 2074 foo := m.NewDB() 2075 bar := m.NewDB() 2076 m.ExpectDB().WithName("foo").WillReturn(foo) 2077 m.ExpectDB().WithName("bar").WillReturn(bar) 2078 bar.ExpectLocalDocs() 2079 foo.ExpectLocalDocs() 2080 }, 2081 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2082 foo := c.DB("foo") 2083 _ = c.DB("bar") 2084 rows := foo.LocalDocs(context.TODO()) 2085 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2086 t.Errorf("Unexpected error: %s", err) 2087 } 2088 }, 2089 err: "there is a remaining unmet expectation", 2090 }) 2091 tests.Run(t, testMock) 2092 } 2093 2094 func TestPurge(t *testing.T) { 2095 tests := testy.NewTable() 2096 tests.Add("error", mockTest{ 2097 setup: func(m *Client) { 2098 db := m.NewDB() 2099 m.ExpectDB().WillReturn(db) 2100 db.ExpectPurge().WillReturnError(errors.New("foo err")) 2101 }, 2102 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2103 db := c.DB("foo") 2104 _, err := db.Purge(context.TODO(), nil) 2105 if !testy.ErrorMatches("foo err", err) { 2106 t.Errorf("Unexpected error: %s", err) 2107 } 2108 }, 2109 }) 2110 tests.Add("success", mockTest{ 2111 setup: func(m *Client) { 2112 db := m.NewDB() 2113 m.ExpectDB().WillReturn(db) 2114 db.ExpectPurge().WillReturn(&driver.PurgeResult{Seq: 123}) 2115 }, 2116 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2117 db := c.DB("foo") 2118 result, err := db.Purge(context.TODO(), nil) 2119 if !testy.ErrorMatches("", err) { 2120 t.Errorf("Unexpected error: %s", err) 2121 } 2122 if seq := result.Seq; seq != 123 { 2123 t.Errorf("Unexpected seq: %v", seq) 2124 } 2125 }, 2126 }) 2127 tests.Add("wrong map", mockTest{ 2128 setup: func(m *Client) { 2129 db := m.NewDB() 2130 m.ExpectDB().WillReturn(db) 2131 db.ExpectPurge().WithDocRevMap(map[string][]string{"foo": {"a", "b"}}) 2132 }, 2133 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2134 db := c.DB("foo") 2135 _, err := db.Purge(context.TODO(), nil) 2136 if !testy.ErrorMatchesRE("has docRevMap: map", err) { 2137 t.Errorf("Unexpected error: %s", err) 2138 } 2139 }, 2140 err: "there is a remaining unmet expectation", 2141 }) 2142 tests.Add("delay", mockTest{ 2143 setup: func(m *Client) { 2144 db := m.NewDB() 2145 m.ExpectDB().WillReturn(db) 2146 db.ExpectPurge().WillDelay(time.Second) 2147 }, 2148 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2149 db := c.DB("foo") 2150 _, err := db.Purge(newCanceledContext(), nil) 2151 if !testy.ErrorMatches("context canceled", err) { 2152 t.Errorf("Unexpected error: %s", err) 2153 } 2154 }, 2155 }) 2156 tests.Add("wrong db", mockTest{ 2157 setup: func(m *Client) { 2158 foo := m.NewDB() 2159 bar := m.NewDB() 2160 m.ExpectDB().WithName("foo").WillReturn(foo) 2161 m.ExpectDB().WithName("bar").WillReturn(bar) 2162 bar.ExpectPurge() 2163 foo.ExpectPurge() 2164 }, 2165 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2166 foo := c.DB("foo") 2167 _ = c.DB("bar") 2168 _, err := foo.Purge(context.TODO(), nil) 2169 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2170 t.Errorf("Unexpected error: %s", err) 2171 } 2172 }, 2173 err: "there is a remaining unmet expectation", 2174 }) 2175 tests.Run(t, testMock) 2176 } 2177 2178 func TestPutAttachment(t *testing.T) { 2179 tests := testy.NewTable() 2180 tests.Add("error", mockTest{ 2181 setup: func(m *Client) { 2182 db := m.NewDB() 2183 m.ExpectDB().WillReturn(db) 2184 db.ExpectPutAttachment().WillReturnError(errors.New("foo err")) 2185 }, 2186 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2187 _, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2188 if !testy.ErrorMatches("foo err", err) { 2189 t.Errorf("Unexpected error: %s", err) 2190 } 2191 }, 2192 }) 2193 tests.Add("delay", mockTest{ 2194 setup: func(m *Client) { 2195 db := m.NewDB() 2196 m.ExpectDB().WillReturn(db) 2197 db.ExpectPutAttachment().WillDelay(time.Second) 2198 }, 2199 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2200 _, err := c.DB("foo").PutAttachment(newCanceledContext(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2201 if !testy.ErrorMatches("context canceled", err) { 2202 t.Errorf("Unexpected error: %s", err) 2203 } 2204 }, 2205 }) 2206 tests.Add("wrong db", mockTest{ 2207 setup: func(m *Client) { 2208 foo := m.NewDB() 2209 bar := m.NewDB() 2210 m.ExpectDB().WithName("foo").WillReturn(foo) 2211 m.ExpectDB().WithName("bar").WillReturn(bar) 2212 bar.ExpectPutAttachment() 2213 foo.ExpectPutAttachment() 2214 }, 2215 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2216 foo := c.DB("foo") 2217 _ = c.DB("bar") 2218 _, err := foo.PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2219 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2220 t.Errorf("Unexpected error: %s", err) 2221 } 2222 }, 2223 err: "there is a remaining unmet expectation", 2224 }) 2225 tests.Add("wrong id", mockTest{ 2226 setup: func(m *Client) { 2227 db := m.NewDB() 2228 m.ExpectDB().WillReturn(db) 2229 db.ExpectPutAttachment().WithDocID("bar") 2230 }, 2231 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2232 _, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2233 if !testy.ErrorMatchesRE("has docID: bar", err) { 2234 t.Errorf("Unexpected error: %s", err) 2235 } 2236 }, 2237 err: "there is a remaining unmet expectation", 2238 }) 2239 tests.Add("wrong rev", mockTest{ 2240 setup: func(m *Client) { 2241 db := m.NewDB() 2242 m.ExpectDB().WillReturn(db) 2243 db.ExpectPutAttachment().WithOptions(kivik.Rev("2-bar")) 2244 }, 2245 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2246 _, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2247 if !testy.ErrorMatchesRE(`has options: map\[rev:2-bar\]`, err) { 2248 t.Errorf("Unexpected error: %s", err) 2249 } 2250 }, 2251 err: "there is a remaining unmet expectation", 2252 }) 2253 tests.Add("wrong attachment", mockTest{ 2254 setup: func(m *Client) { 2255 db := m.NewDB() 2256 m.ExpectDB().WillReturn(db) 2257 db.ExpectPutAttachment().WithAttachment(&driver.Attachment{Filename: "bar.jpg"}) 2258 }, 2259 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2260 _, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2261 if !testy.ErrorMatchesRE("has attachment: bar.jpg", err) { 2262 t.Errorf("Unexpected error: %s", err) 2263 } 2264 }, 2265 err: "there is a remaining unmet expectation", 2266 }) 2267 tests.Add("wrong options", mockTest{ 2268 setup: func(m *Client) { 2269 db := m.NewDB() 2270 m.ExpectDB().WillReturn(db) 2271 db.ExpectPutAttachment().WithOptions(kivik.Param("foo", "bar")) 2272 }, 2273 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2274 _, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}, kivik.Param("foo", 123)) 2275 if !testy.ErrorMatchesRE("has docID: foo", err) { 2276 t.Errorf("Unexpected error: %s", err) 2277 } 2278 }, 2279 err: "there is a remaining unmet expectation", 2280 }) 2281 tests.Add("success", mockTest{ 2282 setup: func(m *Client) { 2283 db := m.NewDB() 2284 m.ExpectDB().WillReturn(db) 2285 db.ExpectPutAttachment().WillReturn("2-boo") 2286 }, 2287 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2288 result, err := c.DB("foo").PutAttachment(context.TODO(), "foo", &kivik.Attachment{Filename: "foo.txt"}) 2289 if !testy.ErrorMatchesRE("", err) { 2290 t.Errorf("Unexpected error: %s", err) 2291 } 2292 if result != "2-boo" { 2293 t.Errorf("Unexpected result: %s", result) 2294 } 2295 }, 2296 }) 2297 tests.Run(t, testMock) 2298 } 2299 2300 func TestQuery(t *testing.T) { 2301 t.Parallel() 2302 tests := testy.NewTable() 2303 tests.Add("error", mockTest{ 2304 setup: func(m *Client) { 2305 db := m.NewDB() 2306 m.ExpectDB().WillReturn(db) 2307 db.ExpectQuery().WillReturnError(errors.New("foo err")) 2308 }, 2309 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2310 db := c.DB("foo") 2311 rows := db.Query(context.TODO(), "foo", "bar") 2312 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 2313 t.Errorf("Unexpected error: %s", err) 2314 } 2315 }, 2316 }) 2317 tests.Add("success", mockTest{ 2318 setup: func(m *Client) { 2319 db := m.NewDB() 2320 m.ExpectDB().WillReturn(db) 2321 db.ExpectQuery().WillReturn(NewRows(). 2322 AddRow(&driver.Row{ID: "foo"}). 2323 AddRow(&driver.Row{ID: "bar"}). 2324 AddRow(&driver.Row{ID: "baz"})) 2325 }, 2326 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2327 db := c.DB("foo") 2328 rows := db.Query(context.TODO(), "foo", "bar") 2329 if err := rows.Err(); !testy.ErrorMatches("", err) { 2330 t.Errorf("Unexpected error: %s", err) 2331 } 2332 ids := []string{} 2333 for rows.Next() { 2334 id, _ := rows.ID() 2335 ids = append(ids, id) 2336 } 2337 expected := []string{"foo", "bar", "baz"} 2338 if d := testy.DiffInterface(expected, ids); d != nil { 2339 t.Error(d) 2340 } 2341 }, 2342 }) 2343 tests.Add("delay", mockTest{ 2344 setup: func(m *Client) { 2345 db := m.NewDB() 2346 m.ExpectDB().WillReturn(db) 2347 db.ExpectQuery().WillDelay(time.Second) 2348 }, 2349 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2350 db := c.DB("foo") 2351 rows := db.Query(newCanceledContext(), "foo", "bar") 2352 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 2353 t.Errorf("Unexpected error: %s", err) 2354 } 2355 }, 2356 }) 2357 tests.Add("wrong db", mockTest{ 2358 setup: func(m *Client) { 2359 foo := m.NewDB() 2360 bar := m.NewDB() 2361 m.ExpectDB().WithName("foo").WillReturn(foo) 2362 m.ExpectDB().WithName("bar").WillReturn(bar) 2363 bar.ExpectQuery() 2364 foo.ExpectQuery() 2365 }, 2366 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2367 foo := c.DB("foo") 2368 _ = c.DB("bar") 2369 rows := foo.Query(context.TODO(), "foo", "bar") 2370 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2371 t.Errorf("Unexpected error: %s", err) 2372 } 2373 }, 2374 err: "there is a remaining unmet expectation", 2375 }) 2376 tests.Add("wrong ddocID", mockTest{ 2377 setup: func(m *Client) { 2378 db := m.NewDB() 2379 m.ExpectDB().WillReturn(db) 2380 db.ExpectQuery().WithDDocID("bar") 2381 }, 2382 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2383 db := c.DB("foo") 2384 rows := db.Query(context.TODO(), "foo", "bar") 2385 if err := rows.Err(); !testy.ErrorMatchesRE("has ddocID: bar", err) { 2386 t.Errorf("Unexpected error: %s", err) 2387 } 2388 }, 2389 err: "there is a remaining unmet expectation", 2390 }) 2391 tests.Add("wrong view", mockTest{ 2392 setup: func(m *Client) { 2393 db := m.NewDB() 2394 m.ExpectDB().WillReturn(db) 2395 db.ExpectQuery().WithView("baz") 2396 }, 2397 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2398 db := c.DB("foo") 2399 rows := db.Query(context.TODO(), "foo", "bar") 2400 if err := rows.Err(); !testy.ErrorMatchesRE("has view: baz", err) { 2401 t.Errorf("Unexpected error: %s", err) 2402 } 2403 }, 2404 err: "there is a remaining unmet expectation", 2405 }) 2406 tests.Run(t, testMock) 2407 } 2408 2409 var ( 2410 driverSec = &driver.Security{Admins: driver.Members{Names: []string{"bob"}}} 2411 clientSec = &kivik.Security{Admins: kivik.Members{Names: []string{"bob"}}} 2412 ) 2413 2414 func TestSecurity(t *testing.T) { 2415 tests := testy.NewTable() 2416 tests.Add("error", mockTest{ 2417 setup: func(m *Client) { 2418 db := m.NewDB() 2419 m.ExpectDB().WillReturn(db) 2420 db.ExpectSecurity().WillReturnError(errors.New("foo err")) 2421 }, 2422 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2423 db := c.DB("foo") 2424 _, err := db.Security(context.TODO()) 2425 if !testy.ErrorMatches("foo err", err) { 2426 t.Errorf("Unexpected error: %s", err) 2427 } 2428 }, 2429 }) 2430 tests.Add("success", mockTest{ 2431 setup: func(m *Client) { 2432 db := m.NewDB() 2433 m.ExpectDB().WillReturn(db) 2434 db.ExpectSecurity().WillReturn(driverSec) 2435 }, 2436 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2437 db := c.DB("foo") 2438 result, err := db.Security(context.TODO()) 2439 if !testy.ErrorMatches("", err) { 2440 t.Errorf("Unexpected error: %s", err) 2441 } 2442 if d := testy.DiffInterface(clientSec, result); d != nil { 2443 t.Error(d) 2444 } 2445 }, 2446 }) 2447 tests.Add("delay", mockTest{ 2448 setup: func(m *Client) { 2449 db := m.NewDB() 2450 m.ExpectDB().WillReturn(db) 2451 db.ExpectSecurity().WillDelay(time.Second) 2452 }, 2453 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2454 db := c.DB("foo") 2455 _, err := db.Security(newCanceledContext()) 2456 if !testy.ErrorMatches("context canceled", err) { 2457 t.Errorf("Unexpected error: %s", err) 2458 } 2459 }, 2460 }) 2461 tests.Add("wrong db", mockTest{ 2462 setup: func(m *Client) { 2463 foo := m.NewDB() 2464 bar := m.NewDB() 2465 m.ExpectDB().WithName("foo").WillReturn(foo) 2466 m.ExpectDB().WithName("bar").WillReturn(bar) 2467 bar.ExpectSecurity() 2468 foo.ExpectSecurity() 2469 }, 2470 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2471 foo := c.DB("foo") 2472 _ = c.DB("bar") 2473 _, err := foo.Security(context.TODO()) 2474 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2475 t.Errorf("Unexpected error: %s", err) 2476 } 2477 }, 2478 err: "there is a remaining unmet expectation", 2479 }) 2480 tests.Run(t, testMock) 2481 } 2482 2483 func TestSetSecurity(t *testing.T) { 2484 tests := testy.NewTable() 2485 tests.Add("error", mockTest{ 2486 setup: func(m *Client) { 2487 db := m.NewDB() 2488 m.ExpectDB().WillReturn(db) 2489 db.ExpectSetSecurity().WillReturnError(errors.New("foo err")) 2490 }, 2491 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2492 db := c.DB("foo") 2493 err := db.SetSecurity(context.TODO(), clientSec) 2494 if !testy.ErrorMatches("foo err", err) { 2495 t.Errorf("Unexpected error: %s", err) 2496 } 2497 }, 2498 }) 2499 tests.Add("success", mockTest{ 2500 setup: func(m *Client) { 2501 db := m.NewDB() 2502 m.ExpectDB().WillReturn(db) 2503 db.ExpectSetSecurity().WithSecurity(driverSec) 2504 }, 2505 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2506 db := c.DB("foo") 2507 err := db.SetSecurity(context.TODO(), clientSec) 2508 if !testy.ErrorMatches("", err) { 2509 t.Errorf("Unexpected error: %s", err) 2510 } 2511 }, 2512 }) 2513 tests.Add("delay", mockTest{ 2514 setup: func(m *Client) { 2515 db := m.NewDB() 2516 m.ExpectDB().WillReturn(db) 2517 db.ExpectSetSecurity().WillDelay(time.Second) 2518 }, 2519 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2520 db := c.DB("foo") 2521 err := db.SetSecurity(newCanceledContext(), clientSec) 2522 if !testy.ErrorMatches("context canceled", err) { 2523 t.Errorf("Unexpected error: %s", err) 2524 } 2525 }, 2526 }) 2527 tests.Add("wrong db", mockTest{ 2528 setup: func(m *Client) { 2529 foo := m.NewDB() 2530 bar := m.NewDB() 2531 m.ExpectDB().WithName("foo").WillReturn(foo) 2532 m.ExpectDB().WithName("bar").WillReturn(bar) 2533 bar.ExpectSetSecurity() 2534 foo.ExpectSetSecurity() 2535 }, 2536 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2537 foo := c.DB("foo") 2538 _ = c.DB("bar") 2539 err := foo.SetSecurity(context.TODO(), clientSec) 2540 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2541 t.Errorf("Unexpected error: %s", err) 2542 } 2543 }, 2544 err: "there is a remaining unmet expectation", 2545 }) 2546 tests.Run(t, testMock) 2547 } 2548 2549 func TestStats(t *testing.T) { 2550 tests := testy.NewTable() 2551 tests.Add("error", mockTest{ 2552 setup: func(m *Client) { 2553 db := m.NewDB() 2554 m.ExpectDB().WillReturn(db) 2555 db.ExpectStats().WillReturnError(errors.New("foo err")) 2556 }, 2557 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2558 db := c.DB("foo") 2559 _, err := db.Stats(context.TODO()) 2560 if !testy.ErrorMatches("foo err", err) { 2561 t.Errorf("Unexpected error: %s", err) 2562 } 2563 }, 2564 }) 2565 tests.Add("success", mockTest{ 2566 setup: func(m *Client) { 2567 db := m.NewDB() 2568 m.ExpectDB().WillReturn(db) 2569 db.ExpectStats().WillReturn(&driver.DBStats{Name: "foo"}) 2570 }, 2571 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2572 db := c.DB("foo") 2573 result, err := db.Stats(context.TODO()) 2574 if !testy.ErrorMatches("", err) { 2575 t.Errorf("Unexpected error: %s", err) 2576 } 2577 expected := &kivik.DBStats{Name: "foo"} 2578 if d := testy.DiffInterface(expected, result); d != nil { 2579 t.Error(d) 2580 } 2581 }, 2582 }) 2583 tests.Add("delay", mockTest{ 2584 setup: func(m *Client) { 2585 db := m.NewDB() 2586 m.ExpectDB().WillReturn(db) 2587 db.ExpectStats().WillDelay(time.Second) 2588 }, 2589 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2590 db := c.DB("foo") 2591 _, err := db.Stats(newCanceledContext()) 2592 if !testy.ErrorMatches("context canceled", err) { 2593 t.Errorf("Unexpected error: %s", err) 2594 } 2595 }, 2596 }) 2597 tests.Add("wrong db", mockTest{ 2598 setup: func(m *Client) { 2599 foo := m.NewDB() 2600 bar := m.NewDB() 2601 m.ExpectDB().WithName("foo").WillReturn(foo) 2602 m.ExpectDB().WithName("bar").WillReturn(bar) 2603 bar.ExpectStats() 2604 foo.ExpectStats() 2605 }, 2606 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2607 foo := c.DB("foo") 2608 _ = c.DB("bar") 2609 _, err := foo.Stats(context.TODO()) 2610 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2611 t.Errorf("Unexpected error: %s", err) 2612 } 2613 }, 2614 err: "there is a remaining unmet expectation", 2615 }) 2616 tests.Run(t, testMock) 2617 } 2618 2619 func TestBulkDocs(t *testing.T) { 2620 tests := testy.NewTable() 2621 tests.Add("error", mockTest{ 2622 setup: func(m *Client) { 2623 db := m.NewDB() 2624 m.ExpectDB().WillReturn(db) 2625 db.ExpectBulkDocs().WillReturnError(errors.New("foo err")) 2626 }, 2627 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2628 db := c.DB("foo") 2629 _, err := db.BulkDocs(context.TODO(), []interface{}{1}) 2630 if !testy.ErrorMatches("foo err", err) { 2631 t.Errorf("Unexpected error: %s", err) 2632 } 2633 }, 2634 }) 2635 tests.Add("unexpected", mockTest{ 2636 setup: func(m *Client) { 2637 db := m.NewDB() 2638 m.ExpectDB().WillReturn(db) 2639 }, 2640 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2641 db := c.DB("foo") 2642 _, err := db.BulkDocs(context.TODO(), []interface{}{1}) 2643 if !testy.ErrorMatches("call to DB.BulkDocs() was not expected, all expectations already fulfilled", err) { 2644 t.Errorf("Unexpected error: %s", err) 2645 } 2646 }, 2647 }) 2648 tests.Add("results", mockTest{ 2649 setup: func(m *Client) { 2650 db := m.NewDB() 2651 m.ExpectDB().WillReturn(db) 2652 db.ExpectBulkDocs().WillReturn([]driver.BulkResult{ 2653 {ID: "foo"}, 2654 {ID: "bar"}, 2655 {ID: "baz"}, 2656 }) 2657 }, 2658 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2659 db := c.DB("foo") 2660 rows, err := db.BulkDocs(context.TODO(), []interface{}{1}) 2661 if !testy.ErrorMatches("", err) { 2662 t.Errorf("Unexpected error: %s", err) 2663 } 2664 ids := []string{} 2665 for _, row := range rows { 2666 ids = append(ids, row.ID) 2667 } 2668 expected := []string{"foo", "bar", "baz"} 2669 if d := testy.DiffInterface(expected, ids); d != nil { 2670 t.Error(d) 2671 } 2672 }, 2673 }) 2674 tests.Add("result error", mockTest{ 2675 setup: func(m *Client) { 2676 db := m.NewDB() 2677 m.ExpectDB().WillReturn(db) 2678 db.ExpectBulkDocs().WillReturn([]driver.BulkResult{ 2679 {ID: "foo"}, 2680 {Error: errors.New("foo err")}, 2681 }) 2682 }, 2683 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2684 db := c.DB("foo") 2685 rows, err := db.BulkDocs(context.TODO(), []interface{}{1}) 2686 if !testy.ErrorMatches("", err) { 2687 t.Errorf("Unexpected error: %s", err) 2688 } 2689 ids := []string{} 2690 var rowErr error 2691 for _, row := range rows { 2692 if row.Error != nil { 2693 rowErr = row.Error 2694 continue 2695 } 2696 ids = append(ids, row.ID) 2697 } 2698 expected := []string{"foo"} 2699 if d := testy.DiffInterface(expected, ids); d != nil { 2700 t.Error(d) 2701 } 2702 if err := rowErr; !testy.ErrorMatches("foo err", err) { 2703 t.Errorf("Unexpected error: %s", err) 2704 } 2705 }, 2706 }) 2707 tests.Add("options", mockTest{ 2708 setup: func(m *Client) { 2709 db := m.NewDB() 2710 m.ExpectDB().WillReturn(db) 2711 db.ExpectBulkDocs().WithOptions(kivik.Param("foo", 123)) 2712 }, 2713 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2714 db := c.DB("foo") 2715 _, err := db.BulkDocs(context.TODO(), []interface{}{1}) 2716 if !testy.ErrorMatchesRE(`map\[foo:123]`, err) { 2717 t.Errorf("Unexpected error: %s", err) 2718 } 2719 }, 2720 err: "there is a remaining unmet expectation", 2721 }) 2722 tests.Add("delay", mockTest{ 2723 setup: func(m *Client) { 2724 db := m.NewDB() 2725 m.ExpectDB().WillReturn(db) 2726 db.ExpectBulkDocs().WillDelay(time.Second) 2727 }, 2728 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2729 db := c.DB("foo") 2730 _, err := db.BulkDocs(newCanceledContext(), []interface{}{1}) 2731 if !testy.ErrorMatches("context canceled", err) { 2732 t.Errorf("Unexpected error: %s", err) 2733 } 2734 }, 2735 }) 2736 tests.Add("wrong db", mockTest{ 2737 setup: func(m *Client) { 2738 foo := m.NewDB() 2739 bar := m.NewDB() 2740 m.ExpectDB().WithName("foo").WillReturn(foo) 2741 m.ExpectDB().WithName("bar").WillReturn(bar) 2742 bar.ExpectBulkDocs() 2743 foo.ExpectBulkDocs() 2744 }, 2745 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2746 foo := c.DB("foo") 2747 _ = c.DB("bar") 2748 _, err := foo.BulkDocs(context.TODO(), []interface{}{1}) 2749 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2750 t.Errorf("Unexpected error: %s", err) 2751 } 2752 }, 2753 err: "there is a remaining unmet expectation", 2754 }) 2755 tests.Run(t, testMock) 2756 } 2757 2758 func TestGetAttachment(t *testing.T) { 2759 tests := testy.NewTable() 2760 tests.Add("error", mockTest{ 2761 setup: func(m *Client) { 2762 db := m.NewDB() 2763 m.ExpectDB().WillReturn(db) 2764 db.ExpectGetAttachment().WillReturnError(errors.New("foo err")) 2765 }, 2766 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2767 _, err := c.DB("foo").GetAttachment(context.TODO(), "foo", "bar") 2768 if !testy.ErrorMatches("foo err", err) { 2769 t.Errorf("Unexpected error: %s", err) 2770 } 2771 }, 2772 }) 2773 tests.Add("delay", mockTest{ 2774 setup: func(m *Client) { 2775 db := m.NewDB() 2776 m.ExpectDB().WillReturn(db) 2777 db.ExpectGetAttachment().WillDelay(time.Second) 2778 }, 2779 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2780 _, err := c.DB("foo").GetAttachment(newCanceledContext(), "foo", "bar") 2781 if !testy.ErrorMatches("context canceled", err) { 2782 t.Errorf("Unexpected error: %s", err) 2783 } 2784 }, 2785 }) 2786 tests.Add("wrong db", mockTest{ 2787 setup: func(m *Client) { 2788 foo := m.NewDB() 2789 bar := m.NewDB() 2790 m.ExpectDB().WithName("foo").WillReturn(foo) 2791 m.ExpectDB().WithName("bar").WillReturn(bar) 2792 bar.ExpectGetAttachment() 2793 foo.ExpectGetAttachment() 2794 }, 2795 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2796 foo := c.DB("foo") 2797 _ = c.DB("bar") 2798 _, err := foo.GetAttachment(context.TODO(), "foo", "bar") 2799 if !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2800 t.Errorf("Unexpected error: %s", err) 2801 } 2802 }, 2803 err: "there is a remaining unmet expectation", 2804 }) 2805 tests.Add("wrong docID", mockTest{ 2806 setup: func(m *Client) { 2807 db := m.NewDB() 2808 m.ExpectDB().WillReturn(db) 2809 db.ExpectGetAttachment().WithDocID("bar") 2810 }, 2811 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2812 _, err := c.DB("foo").GetAttachment(context.TODO(), "foo", "bar") 2813 if !testy.ErrorMatchesRE("has docID: bar", err) { 2814 t.Errorf("Unexpected error: %s", err) 2815 } 2816 }, 2817 err: "there is a remaining unmet expectation", 2818 }) 2819 tests.Add("success", mockTest{ 2820 setup: func(m *Client) { 2821 db := m.NewDB() 2822 m.ExpectDB().WillReturn(db) 2823 db.ExpectGetAttachment().WillReturn(&driver.Attachment{Filename: "foo.txt"}) 2824 }, 2825 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2826 att, err := c.DB("foo").GetAttachment(context.TODO(), "foo", "bar") 2827 if !testy.ErrorMatches("", err) { 2828 t.Errorf("Unexpected error: %s", err) 2829 } 2830 if name := att.Filename; name != "foo.txt" { 2831 t.Errorf("Unexpected filename: %s", name) 2832 } 2833 }, 2834 }) 2835 tests.Run(t, testMock) 2836 } 2837 2838 func TestDesignDocs(t *testing.T) { 2839 tests := testy.NewTable() 2840 tests.Add("error", mockTest{ 2841 setup: func(m *Client) { 2842 db := m.NewDB() 2843 m.ExpectDB().WillReturn(db) 2844 db.ExpectDesignDocs().WillReturnError(errors.New("foo err")) 2845 }, 2846 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2847 db := c.DB("foo") 2848 rows := db.DesignDocs(context.TODO(), nil) 2849 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 2850 t.Errorf("Unexpected error: %s", err) 2851 } 2852 }, 2853 }) 2854 tests.Add("success", mockTest{ 2855 setup: func(m *Client) { 2856 db := m.NewDB() 2857 m.ExpectDB().WillReturn(db) 2858 db.ExpectDesignDocs().WillReturn(NewRows(). 2859 AddRow(&driver.Row{ID: "foo"}). 2860 AddRow(&driver.Row{ID: "bar"}). 2861 AddRow(&driver.Row{ID: "baz"})) 2862 }, 2863 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2864 db := c.DB("foo") 2865 rows := db.DesignDocs(context.TODO()) 2866 if err := rows.Err(); !testy.ErrorMatches("", err) { 2867 t.Errorf("Unexpected error: %s", err) 2868 } 2869 ids := []string{} 2870 for rows.Next() { 2871 id, _ := rows.ID() 2872 ids = append(ids, id) 2873 } 2874 expected := []string{"foo", "bar", "baz"} 2875 if d := testy.DiffInterface(expected, ids); d != nil { 2876 t.Error(d) 2877 } 2878 }, 2879 }) 2880 tests.Add("delay", mockTest{ 2881 setup: func(m *Client) { 2882 db := m.NewDB() 2883 m.ExpectDB().WillReturn(db) 2884 db.ExpectDesignDocs().WillDelay(time.Second) 2885 }, 2886 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2887 db := c.DB("foo") 2888 rows := db.DesignDocs(newCanceledContext()) 2889 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 2890 t.Errorf("Unexpected error: %s", err) 2891 } 2892 }, 2893 }) 2894 tests.Add("wrong db", mockTest{ 2895 setup: func(m *Client) { 2896 foo := m.NewDB() 2897 bar := m.NewDB() 2898 m.ExpectDB().WithName("foo").WillReturn(foo) 2899 m.ExpectDB().WithName("bar").WillReturn(bar) 2900 bar.ExpectDesignDocs() 2901 foo.ExpectDesignDocs() 2902 }, 2903 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2904 foo := c.DB("foo") 2905 _ = c.DB("bar") 2906 rows := foo.DesignDocs(context.TODO()) 2907 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 2908 t.Errorf("Unexpected error: %s", err) 2909 } 2910 }, 2911 err: "there is a remaining unmet expectation", 2912 }) 2913 tests.Run(t, testMock) 2914 } 2915 2916 func TestChanges(t *testing.T) { 2917 t.Parallel() 2918 tests := testy.NewTable() 2919 tests.Add("error", mockTest{ 2920 setup: func(m *Client) { 2921 db := m.NewDB() 2922 m.ExpectDB().WillReturn(db) 2923 db.ExpectChanges().WillReturnError(errors.New("foo err")) 2924 }, 2925 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2926 db := c.DB("foo") 2927 rows := db.Changes(context.TODO()) 2928 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 2929 t.Errorf("Unexpected error: %s", err) 2930 } 2931 }, 2932 }) 2933 tests.Add("unexpected", mockTest{ 2934 setup: func(m *Client) { 2935 db := m.NewDB() 2936 m.ExpectDB().WillReturn(db) 2937 }, 2938 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2939 db := c.DB("foo") 2940 rows := db.Changes(context.TODO()) 2941 if err := rows.Err(); !testy.ErrorMatches("call to DB.Changes() was not expected, all expectations already fulfilled", err) { 2942 t.Errorf("Unexpected error: %s", err) 2943 } 2944 }, 2945 }) 2946 tests.Add("close error", mockTest{ 2947 setup: func(m *Client) { 2948 db := m.NewDB() 2949 m.ExpectDB().WillReturn(db) 2950 db.ExpectChanges().WillReturn(NewChanges().CloseError(errors.New("bar err"))) 2951 }, 2952 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2953 db := c.DB("foo") 2954 rows := db.Changes(context.TODO()) 2955 if err := rows.Err(); !testy.ErrorMatches("", err) { 2956 t.Errorf("Unexpected error: %s", err) 2957 } 2958 if err := rows.Close(); !testy.ErrorMatches("bar err", err) { 2959 t.Errorf("Unexpected error: %s", err) 2960 } 2961 }, 2962 }) 2963 tests.Add("changes", mockTest{ 2964 setup: func(m *Client) { 2965 db := m.NewDB() 2966 m.ExpectDB().WillReturn(db) 2967 db.ExpectChanges().WillReturn(NewChanges(). 2968 AddChange(&driver.Change{ID: "foo"}). 2969 AddChange(&driver.Change{ID: "bar"}). 2970 AddChange(&driver.Change{ID: "baz"})) 2971 }, 2972 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2973 db := c.DB("foo") 2974 rows := db.Changes(context.TODO()) 2975 if err := rows.Err(); !testy.ErrorMatches("", err) { 2976 t.Errorf("Unexpected error: %s", err) 2977 } 2978 ids := []string{} 2979 for rows.Next() { 2980 ids = append(ids, rows.ID()) 2981 } 2982 expected := []string{"foo", "bar", "baz"} 2983 if d := testy.DiffInterface(expected, ids); d != nil { 2984 t.Error(d) 2985 } 2986 }, 2987 }) 2988 tests.Add("row error", mockTest{ 2989 setup: func(m *Client) { 2990 db := m.NewDB() 2991 m.ExpectDB().WillReturn(db) 2992 db.ExpectChanges().WillReturn(NewChanges(). 2993 AddChange(&driver.Change{ID: "foo"}). 2994 AddChangeError(errors.New("foo err"))) 2995 }, 2996 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 2997 db := c.DB("foo") 2998 rows := db.Changes(context.TODO()) 2999 if err := rows.Err(); !testy.ErrorMatches("", err) { 3000 t.Errorf("Unexpected error: %s", err) 3001 } 3002 ids := []string{} 3003 for rows.Next() { 3004 ids = append(ids, rows.ID()) 3005 } 3006 expected := []string{"foo"} 3007 if d := testy.DiffInterface(expected, ids); d != nil { 3008 t.Error(d) 3009 } 3010 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 3011 t.Errorf("Unexpected error: %s", err) 3012 } 3013 }, 3014 }) 3015 tests.Add("options", mockTest{ 3016 setup: func(m *Client) { 3017 db := m.NewDB() 3018 m.ExpectDB().WillReturn(db) 3019 db.ExpectChanges().WithOptions(kivik.Param("foo", 123)) 3020 }, 3021 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3022 db := c.DB("foo") 3023 rows := db.Changes(context.TODO()) 3024 if err := rows.Err(); !testy.ErrorMatchesRE(`map\[foo:123]`, err) { 3025 t.Errorf("Unexpected error: %s", err) 3026 } 3027 }, 3028 err: "there is a remaining unmet expectation", 3029 }) 3030 tests.Add("delay", mockTest{ 3031 setup: func(m *Client) { 3032 db := m.NewDB() 3033 m.ExpectDB().WillReturn(db) 3034 db.ExpectChanges().WillDelay(time.Second) 3035 }, 3036 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3037 db := c.DB("foo") 3038 rows := db.Changes(newCanceledContext()) 3039 if err := rows.Err(); !testy.ErrorMatches("context canceled", err) { 3040 t.Errorf("Unexpected error: %s", err) 3041 } 3042 }, 3043 }) 3044 tests.Add("change delay", mockTest{ 3045 setup: func(m *Client) { 3046 db := m.NewDB() 3047 m.ExpectDB().WillReturn(db) 3048 db.ExpectChanges().WillReturn(NewChanges(). 3049 AddDelay(time.Millisecond). 3050 AddChange(&driver.Change{ID: "foo"}). 3051 AddDelay(time.Second). 3052 AddChange(&driver.Change{ID: "bar"})) 3053 }, 3054 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3055 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 3056 defer cancel() 3057 rows := c.DB("foo").Changes(ctx) 3058 if err := rows.Err(); !testy.ErrorMatches("", err) { 3059 t.Errorf("Unexpected error: %s", err) 3060 } 3061 ids := []string{} 3062 for rows.Next() { 3063 ids = append(ids, rows.ID()) 3064 } 3065 expected := []string{"foo"} 3066 if d := testy.DiffInterface(expected, ids); d != nil { 3067 t.Error(d) 3068 } 3069 if err := rows.Err(); !testy.ErrorMatches("context deadline exceeded", err) { 3070 t.Errorf("Unexpected error: %s", err) 3071 } 3072 }, 3073 }) 3074 tests.Add("wrong db", mockTest{ 3075 setup: func(m *Client) { 3076 foo := m.NewDB() 3077 bar := m.NewDB() 3078 m.ExpectDB().WithName("foo").WillReturn(foo) 3079 m.ExpectDB().WithName("bar").WillReturn(bar) 3080 bar.ExpectChanges() 3081 foo.ExpectChanges() 3082 }, 3083 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3084 foo := c.DB("foo") 3085 _ = c.DB("bar") 3086 rows := foo.Changes(context.TODO()) 3087 if err := rows.Err(); !testy.ErrorMatchesRE(`Expected: call to DB\(bar`, err) { 3088 t.Errorf("Unexpected error: %s", err) 3089 } 3090 }, 3091 err: "there is a remaining unmet expectation", 3092 }) 3093 tests.Add("changes last_seq", mockTest{ 3094 setup: func(m *Client) { 3095 db := m.NewDB() 3096 m.ExpectDB().WillReturn(db) 3097 db.ExpectChanges().WillReturn(NewChanges().LastSeq("1-asdf")) 3098 }, 3099 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3100 db := c.DB("foo") 3101 rows := db.Changes(context.TODO()) 3102 if err := rows.Err(); !testy.ErrorMatches("", err) { 3103 t.Errorf("Unexpected error: %s", err) 3104 } 3105 _ = rows.Next() 3106 meta, err := rows.Metadata() 3107 if err != nil { 3108 t.Fatal(err) 3109 } 3110 if o := meta.LastSeq; o != "1-asdf" { 3111 t.Errorf("Unexpected last_seq: %s", o) 3112 } 3113 }, 3114 }) 3115 tests.Add("changes pending", mockTest{ 3116 setup: func(m *Client) { 3117 db := m.NewDB() 3118 m.ExpectDB().WillReturn(db) 3119 db.ExpectChanges().WillReturn(NewChanges().Pending(123)) 3120 }, 3121 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3122 db := c.DB("foo") 3123 rows := db.Changes(context.TODO()) 3124 if err := rows.Err(); !testy.ErrorMatches("", err) { 3125 t.Errorf("Unexpected error: %s", err) 3126 } 3127 _ = rows.Next() 3128 meta, err := rows.Metadata() 3129 if err != nil { 3130 t.Fatal(err) 3131 } 3132 if o := meta.Pending; o != 123 { 3133 t.Errorf("Unexpected pending: %d", o) 3134 } 3135 }, 3136 }) 3137 tests.Add("changes etag", mockTest{ 3138 setup: func(m *Client) { 3139 db := m.NewDB() 3140 m.ExpectDB().WillReturn(db) 3141 db.ExpectChanges().WillReturn(NewChanges().ETag("etag-foo")) 3142 }, 3143 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3144 db := c.DB("foo") 3145 rows := db.Changes(context.TODO()) 3146 if err := rows.Err(); !testy.ErrorMatches("", err) { 3147 t.Errorf("Unexpected error: %s", err) 3148 } 3149 if o := rows.ETag(); o != "etag-foo" { 3150 t.Errorf("Unexpected pending: %s", o) 3151 } 3152 _ = rows.Close() 3153 }, 3154 }) 3155 tests.Run(t, testMock) 3156 } 3157 3158 func TestRevsDiff(t *testing.T) { 3159 revMap := map[string]interface{}{"foo": []string{"1", "2"}} 3160 tests := testy.NewTable() 3161 tests.Add("error", mockTest{ 3162 setup: func(m *Client) { 3163 db := m.NewDB() 3164 m.ExpectDB().WillReturn(db) 3165 db.ExpectRevsDiff().WillReturnError(errors.New("foo err")) 3166 }, 3167 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3168 db := c.DB("foo") 3169 rows := db.RevsDiff(context.TODO(), revMap) 3170 if err := rows.Err(); !testy.ErrorMatches("foo err", err) { 3171 t.Errorf("Unexpected error: %s", err) 3172 } 3173 }, 3174 }) 3175 tests.Add("success", mockTest{ 3176 setup: func(m *Client) { 3177 db := m.NewDB() 3178 m.ExpectDB().WillReturn(db) 3179 db.ExpectRevsDiff(). 3180 WithRevLookup(revMap). 3181 WillReturn(NewRows(). 3182 AddRow(&driver.Row{ 3183 ID: "foo", 3184 Value: strings.NewReader(`{"missing":["1"],"possible_ancestors":["2"]}`), 3185 }). 3186 AddRow(&driver.Row{ 3187 ID: "bar", 3188 Value: strings.NewReader(`{"missing":["x"]}`), 3189 })) 3190 }, 3191 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3192 db := c.DB("foo") 3193 rows := db.RevsDiff(context.TODO(), revMap) 3194 if err := rows.Err(); !testy.ErrorMatches("", err) { 3195 t.Errorf("Unexpected error: %s", err) 3196 } 3197 results := map[string]interface{}{} 3198 for rows.Next() { 3199 var val map[string][]string 3200 if err := rows.ScanValue(&val); err != nil { 3201 t.Fatal(err) 3202 } 3203 id, _ := rows.ID() 3204 results[id] = val 3205 } 3206 if d := testy.DiffAsJSON(testy.Snapshot(t), results); d != nil { 3207 t.Error(d) 3208 } 3209 }, 3210 }) 3211 3212 tests.Run(t, testMock) 3213 } 3214 3215 func TestPartitionStats(t *testing.T) { 3216 tests := testy.NewTable() 3217 tests.Add("error", mockTest{ 3218 setup: func(m *Client) { 3219 db := m.NewDB() 3220 m.ExpectDB().WillReturn(db) 3221 db.ExpectPartitionStats().WillReturnError(errors.New("foo err")) 3222 }, 3223 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3224 db := c.DB("foo") 3225 _, err := db.PartitionStats(context.TODO(), "foo") 3226 if !testy.ErrorMatches("foo err", err) { 3227 t.Errorf("Unexpected error: %s", err) 3228 } 3229 }, 3230 }) 3231 tests.Add("success", mockTest{ 3232 setup: func(m *Client) { 3233 db := m.NewDB() 3234 m.ExpectDB().WillReturn(db) 3235 db.ExpectPartitionStats(). 3236 WithName("foo"). 3237 WillReturn(&driver.PartitionStats{ 3238 DBName: "foo", 3239 Partition: "foo", 3240 }) 3241 }, 3242 test: func(t *testing.T, c *kivik.Client) { //nolint:thelper // Not a helper 3243 db := c.DB("foo") 3244 stats, err := db.PartitionStats(context.TODO(), "foo") 3245 if !testy.ErrorMatches("", err) { 3246 t.Errorf("Unexpected error: %s", err) 3247 } 3248 if d := testy.DiffAsJSON(testy.Snapshot(t), stats); d != nil { 3249 t.Error(d) 3250 } 3251 }, 3252 }) 3253 3254 tests.Run(t, testMock) 3255 }