diff options
Diffstat (limited to 'cursor_test.go')
-rw-r--r-- | cursor_test.go | 40 |
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. |