aboutsummaryrefslogtreecommitdiff
path: root/transaction_test.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-02-20 13:53:40 -0700
committerBen Johnson <benbjohnson@yahoo.com>2014-02-20 13:53:40 -0700
commit15e0eae829433f645c0d1f93333292f8b996c9c4 (patch)
treee146ad3febdb31337327f64bfee272de54fb27d6 /transaction_test.go
parentMerge pull request #45 from benbjohnson/seek (diff)
downloaddedo-15e0eae829433f645c0d1f93333292f8b996c9c4.tar.gz
dedo-15e0eae829433f645c0d1f93333292f8b996c9c4.tar.xz
Bidirectional cursors.
Diffstat (limited to 'transaction_test.go')
-rw-r--r--transaction_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/transaction_test.go b/transaction_test.go
index 30274d5..4a7170c 100644
--- a/transaction_test.go
+++ b/transaction_test.go
@@ -109,6 +109,41 @@ func TestTransactionCursorLeafRoot(t *testing.T) {
})
}
+// Ensure that a Transaction cursor can iterate in reverse over a single root with a couple elements.
+func TestTransactionCursorLeafRootReverse(t *testing.T) {
+ withOpenDB(func(db *DB, path string) {
+ db.CreateBucket("widgets")
+ db.Put("widgets", []byte("baz"), []byte{})
+ db.Put("widgets", []byte("foo"), []byte{0})
+ db.Put("widgets", []byte("bar"), []byte{1})
+ txn, _ := db.Transaction()
+ c, err := txn.Cursor("widgets")
+ assert.NoError(t, err)
+
+ k, v := c.Last()
+ assert.Equal(t, string(k), "foo")
+ assert.Equal(t, v, []byte{0})
+
+ k, v = c.Prev()
+ assert.Equal(t, string(k), "baz")
+ assert.Equal(t, v, []byte{})
+
+ k, v = c.Prev()
+ assert.Equal(t, string(k), "bar")
+ assert.Equal(t, v, []byte{1})
+
+ k, v = c.Prev()
+ assert.Nil(t, k)
+ assert.Nil(t, v)
+
+ k, v = c.Prev()
+ assert.Nil(t, k)
+ assert.Nil(t, v)
+
+ txn.Close()
+ })
+}
+
// Ensure that a Transaction cursor can restart from the beginning.
func TestTransactionCursorRestart(t *testing.T) {
withOpenDB(func(db *DB, path string) {
@@ -172,3 +207,40 @@ func TestTransactionCursorIterate(t *testing.T) {
}
fmt.Fprint(os.Stderr, "\n")
}
+
+// Ensure that a transaction can iterate over all elements in a bucket in reverse.
+func TestTransactionCursorIterateReverse(t *testing.T) {
+ f := func(items testdata) bool {
+ withOpenDB(func(db *DB, path string) {
+ // Bulk insert all values.
+ db.CreateBucket("widgets")
+ rwtxn, _ := db.RWTransaction()
+ for _, item := range items {
+ assert.NoError(t, rwtxn.Put("widgets", item.Key, item.Value))
+ }
+ assert.NoError(t, rwtxn.Commit())
+
+ // Sort test data.
+ sort.Sort(revtestdata(items))
+
+ // Iterate over all items and check consistency.
+ var index = 0
+ txn, _ := db.Transaction()
+ c, err := txn.Cursor("widgets")
+ assert.NoError(t, err)
+ for k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() {
+ assert.Equal(t, k, items[index].Key)
+ assert.Equal(t, v, items[index].Value)
+ index++
+ }
+ assert.Equal(t, len(items), index)
+ txn.Close()
+ })
+ fmt.Fprint(os.Stderr, ".")
+ return true
+ }
+ if err := quick.Check(f, qconfig()); err != nil {
+ t.Error(err)
+ }
+ fmt.Fprint(os.Stderr, "\n")
+}