aboutsummaryrefslogtreecommitdiff
path: root/cursor_test.go
diff options
context:
space:
mode:
authorMartin Kobetic <mkobetic@gmail.com>2014-06-11 21:46:19 +0000
committerMartin Kobetic <mkobetic@gmail.com>2014-06-11 21:46:19 +0000
commita00a88baef376804b9980a4854250d08da2908e5 (patch)
treed6cdfd42dd4dbed3d7ff15803a38343ddc42be9d /cursor_test.go
parentMerge pull request #189 from benbjohnson/increase-max-nodes-per-page (diff)
downloaddedo-a00a88baef376804b9980a4854250d08da2908e5.tar.gz
dedo-a00a88baef376804b9980a4854250d08da2908e5.tar.xz
add Cursor.Delete()
Diffstat (limited to 'cursor_test.go')
-rw-r--r--cursor_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/cursor_test.go b/cursor_test.go
index b44bf53..470860d 100644
--- a/cursor_test.go
+++ b/cursor_test.go
@@ -1,6 +1,7 @@
package bolt
import (
+ "bytes"
"encoding/binary"
"sort"
"testing"
@@ -67,6 +68,45 @@ func TestCursor_Seek(t *testing.T) {
})
}
+func TestCursor_Delete(t *testing.T) {
+ withOpenDB(func(db *DB, path string) {
+ var count = 1000
+
+ // Insert every other key between 0 and $count.
+ db.Update(func(tx *Tx) error {
+ b, _ := tx.CreateBucket([]byte("widgets"))
+ for i := 0; i < count; i += 1 {
+ k := make([]byte, 8)
+ binary.BigEndian.PutUint64(k, uint64(i))
+ b.Put(k, make([]byte, 100))
+ }
+ b.CreateBucket([]byte("sub"))
+ return nil
+ })
+
+ db.Update(func(tx *Tx) error {
+ c := tx.Bucket([]byte("widgets")).Cursor()
+ bound := make([]byte, 8)
+ binary.BigEndian.PutUint64(bound, uint64(count/2))
+ for key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() {
+ if err := c.Delete(); err != nil {
+ return err
+ }
+ }
+ c.Seek([]byte("sub"))
+ err := c.Delete()
+ assert.Equal(t, err, ErrIncompatibleValue)
+ return nil
+ })
+
+ db.View(func(tx *Tx) error {
+ b := tx.Bucket([]byte("widgets"))
+ assert.Equal(t, b.Stats().KeyN, count/2+1)
+ return nil
+ })
+ })
+}
+
// Ensure that a Tx cursor can seek to the appropriate keys when there are a
// large number of keys. This test also checks that seek will always move
// forward to the next key.