func TestBucket_NextSequence_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			i, err := b.NextSequence()
			assert.Equal(t, i, 0)
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
Ensure that retrieving the next sequence on a read-only bucket returns an error.
package bolt
import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"
	"testing"
	"testing/quick"
	"github.com/stretchr/testify/assert"
)
func TestBucket_Get_NonExistent(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Nil(t, value)
			return nil
		})
	})
}
func TestBucket_Get_FromNode(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			b.Put([]byte("foo"), []byte("bar"))
			value := b.Get([]byte("foo"))
			assert.Equal(t, value, []byte("bar"))
			return nil
		})
	})
}
func TestBucket_Get_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Nil(t, tx.Bucket([]byte("widgets")).Get([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_Put(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			err := tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
			assert.NoError(t, err)
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Equal(t, value, []byte("bar"))
			return nil
		})
	})
}
func TestBucket_Put_Repeat(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Put([]byte("foo"), []byte("bar")))
			assert.NoError(t, b.Put([]byte("foo"), []byte("baz")))
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Equal(t, value, []byte("baz"))
			return nil
		})
	})
}
func TestBucket_Put_Large(t *testing.T) {
	var count = 100
	var factor = 200
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			for i := 1; i < count; i++ {
				assert.NoError(t, b.Put([]byte(strings.Repeat("0", i*factor)), []byte(strings.Repeat("X", (count-i)*factor))))
			}
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			for i := 1; i < count; i++ {
				value := b.Get([]byte(strings.Repeat("0", i*factor)))
				assert.Equal(t, []byte(strings.Repeat("X", (count-i)*factor)), value)
			}
			return nil
		})
	})
}
func TestBucket_Put_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Equal(t, ErrIncompatibleValue, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			return nil
		})
	})
}
func TestBucket_Put_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		assert.Equal(t, ErrTxClosed, b.Put([]byte("foo"), []byte("bar")))
	})
}
func TestBucket_Put_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			err := b.Put([]byte("foo"), []byte("bar"))
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
func TestBucket_Delete(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
			err := tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
			assert.NoError(t, err)
			value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
			assert.Nil(t, value)
			return nil
		})
	})
}
func TestBucket_Delete_Large(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			var b, _ = tx.CreateBucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.NoError(t, b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat("*", 1024))))
			}
			return nil
		})
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.NoError(t, b.Delete([]byte(strconv.Itoa(i))))
			}
			return nil
		})
		db.View(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 100; i++ {
				assert.Nil(t, b.Get([]byte(strconv.Itoa(i))))
			}
			return nil
		})
	})
}
func TestBucket_Nested(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			b, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			_, err = b.CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.NoError(t, b.Put([]byte("bar"), []byte("0000")))
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Put([]byte("bar"), []byte("xxxx")))
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			for i := 0; i < 10000; i++ {
				assert.NoError(t, b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))))
			}
			return nil
		})
		mustCheck(db)
		db.Update(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.NoError(t, b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")))
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			var b = tx.Bucket([]byte("widgets"))
			assert.Equal(t, []byte("yyyy"), b.Bucket([]byte("foo")).Get([]byte("baz")))
			assert.Equal(t, []byte("xxxx"), b.Get([]byte("bar")))
			for i := 0; i < 10000; i++ {
				assert.Equal(t, []byte(strconv.Itoa(i)), b.Get([]byte(strconv.Itoa(i))))
			}
			return nil
		})
	})
}
func TestBucket_Delete_Bucket(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			b := tx.Bucket([]byte("widgets"))
			_, err := b.CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			assert.Equal(t, ErrIncompatibleValue, b.Delete([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_Delete_ReadOnly(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			return nil
		})
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("widgets"))
			err := b.Delete([]byte("foo"))
			assert.Equal(t, err, ErrTxNotWritable)
			return nil
		})
	})
}
func TestBucket_Delete_Closed(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		tx, _ := db.Begin(true)
		tx.CreateBucket([]byte("widgets"))
		b := tx.Bucket([]byte("widgets"))
		tx.Rollback()
		assert.Equal(t, ErrTxClosed, b.Delete([]byte("foo")))
	})
}
func TestBucket_DeleteBucket_Nested(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).CreateBucket([]byte("bar"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Put([]byte("baz"), []byte("bat")))
			assert.NoError(t, tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_DeleteBucket_Nested2(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			_, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).CreateBucket([]byte("bar"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Put([]byte("baz"), []byte("bat")))
			return nil
		})
		db.Update(func(tx *Tx) error {
			assert.NotNil(t, tx.Bucket([]byte("widgets")))
			assert.NotNil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")))
			assert.NotNil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")))
			assert.Equal(t, []byte("bat"), tx.Bucket([]byte("widgets")).Bucket([]byte("foo")).Bucket([]byte("bar")).Get([]byte("baz")))
			assert.NoError(t, tx.DeleteBucket([]byte("widgets")))
			return nil
		})
		db.View(func(tx *Tx) error {
			assert.Nil(t, tx.Bucket([]byte("widgets")))
			return nil
		})
	})
}
func TestBucket_DeleteBucket_Large(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			_, err = tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.NoError(t, err)
			b := tx.Bucket([]byte("widgets")).Bucket([]byte("foo"))
			for i := 0; i < 1000; i++ {
				assert.NoError(t, b.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%0100d", i))))
			}
			return nil
		})
		db.Update(func(tx *Tx) error {
			assert.NoError(t, tx.DeleteBucket([]byte("widgets")))
			return nil
		})
	})
}
func TestBucket_Bucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			assert.Nil(t, tx.Bucket([]byte("widgets")).Bucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			_, err = tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo"))
			assert.Equal(t, ErrIncompatibleValue, err)
			return nil
		})
	})
}
func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			_, err := tx.CreateBucket([]byte("widgets"))
			assert.NoError(t, err)
			assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
			assert.Equal(t, ErrIncompatibleValue, tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")))
			return nil
		})
	})
}
func TestBucket_NextSequence(t *testing.T) {
	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			tx.CreateBucket([]byte("widgets"))
			tx.CreateBucket([]byte("woojits"))
			seq, err := tx.Bucket([]byte("widgets")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 1)
			seq, err = tx.Bucket([]byte("widgets")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 2)
			seq, err = tx.Bucket([]byte("woojits")).NextSequence()
			assert.NoError(t, err)
			assert.Equal(t, seq, 1)
			return nil
		})
	})
}