Ensure that a Tx can iterate over all elements in a bucket.
(t *testing.T)
| 680 | |
| 681 | // Ensure that a Tx can iterate over all elements in a bucket. |
| 682 | func TestCursor_QuickCheck(t *testing.T) { |
| 683 | f := func(items testdata) bool { |
| 684 | db := btesting.MustCreateDB(t) |
| 685 | defer db.MustClose() |
| 686 | |
| 687 | // Bulk insert all values. |
| 688 | tx, err := db.Begin(true) |
| 689 | if err != nil { |
| 690 | t.Fatal(err) |
| 691 | } |
| 692 | b, err := tx.CreateBucket([]byte("widgets")) |
| 693 | if err != nil { |
| 694 | t.Fatal(err) |
| 695 | } |
| 696 | for _, item := range items { |
| 697 | if err := b.Put(item.Key, item.Value); err != nil { |
| 698 | t.Fatal(err) |
| 699 | } |
| 700 | } |
| 701 | if err := tx.Commit(); err != nil { |
| 702 | t.Fatal(err) |
| 703 | } |
| 704 | |
| 705 | // Sort test data. |
| 706 | sort.Sort(items) |
| 707 | |
| 708 | // Iterate over all items and check consistency. |
| 709 | var index = 0 |
| 710 | tx, err = db.Begin(false) |
| 711 | if err != nil { |
| 712 | t.Fatal(err) |
| 713 | } |
| 714 | |
| 715 | c := tx.Bucket([]byte("widgets")).Cursor() |
| 716 | for k, v := c.First(); k != nil && index < len(items); k, v = c.Next() { |
| 717 | if !bytes.Equal(k, items[index].Key) { |
| 718 | t.Fatalf("unexpected key: %v", k) |
| 719 | } else if !bytes.Equal(v, items[index].Value) { |
| 720 | t.Fatalf("unexpected value: %v", v) |
| 721 | } |
| 722 | index++ |
| 723 | } |
| 724 | if len(items) != index { |
| 725 | t.Fatalf("unexpected item count: %v, expected %v", len(items), index) |
| 726 | } |
| 727 | |
| 728 | if err := tx.Rollback(); err != nil { |
| 729 | t.Fatal(err) |
| 730 | } |
| 731 | |
| 732 | return true |
| 733 | } |
| 734 | if err := quick.Check(f, qconfig()); err != nil { |
| 735 | t.Error(err) |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // Ensure that a transaction can iterate over all elements in a bucket in reverse. |
nothing calls this directly
no test coverage detected