aboutsummaryrefslogtreecommitdiff
path: root/c/cursor_test.go
blob: ff6d5473e83407c8527d6a3e75701a46e0c6eee6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package c

import (
	"github.com/boltdb/bolt"
	"github.com/stretchr/testify/assert"
	"testing"
	"testing/quick"
)

// Ensure that a cursor can iterate over all elements in a bucket.
func TestIterate(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	f := func(items testdata) bool {
		withOpenDB(func(db *DB, path string) {
			// Bulk insert all values.
			tx, _ := db.Begin(true)
			tx.CreateBucket("widgets")
			b := tx.Bucket("widgets")
			for _, item := range items {
				assert.NoError(t, b.Put(item.Key, item.Value))
			}
			assert.NoError(t, tx.Commit())

			// Sort test data.
			sort.Sort(items)

			// Iterate over all items and check consistency.
			var index = 0
			tx, _ = db.Begin(false)
			c := NewCursor(tx.Bucket("widgets"))
			for key, value := c.first(); key != nil && index < len(items); key, value = c.next() {
				assert.Equal(t, key, items[index].Key)
				assert.Equal(t, value, items[index].Value)
				index++
			}
			assert.Equal(t, len(items), index)
			assert.Equal(t, len(items), index)
			tx.Rollback()
		})
		return true
	}
	if err := quick.Check(f, qconfig()); err != nil {
		t.Error(err)
	}
	fmt.Fprint(os.Stderr, "\n")
}