route/vendor/github.com/asdine/storm/bucket_test.go

69 lines
1.2 KiB
Go
Raw Normal View History

2017-10-06 15:29:20 +00:00
package storm
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestBucket(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
// Read tx
readTx, err := db.Bolt.Begin(false)
if err != nil {
t.Fatal(err)
}
2018-01-03 19:29:07 +00:00
require.Nil(t, db.GetBucket(readTx, "none"))
2017-10-06 15:29:20 +00:00
2018-01-03 19:29:07 +00:00
b, err := db.CreateBucketIfNotExists(readTx, "new")
2017-10-06 15:29:20 +00:00
// Cannot create buckets in a read transaction
require.Error(t, err)
require.Nil(t, b)
// Read transactions in Bolt needs a rollback and not a commit
readTx.Rollback()
// End read tx
// Write tx
writeTx, err := db.Bolt.Begin(true)
if err != nil {
t.Fatal(err)
}
2018-01-03 19:29:07 +00:00
require.Nil(t, db.GetBucket(writeTx, "none"))
2017-10-06 15:29:20 +00:00
2018-01-03 19:29:07 +00:00
b, err = db.CreateBucketIfNotExists(writeTx, "new")
2017-10-06 15:29:20 +00:00
require.NoError(t, err)
require.NotNil(t, b)
n2 := db.From("a", "b")
b, err = n2.CreateBucketIfNotExists(writeTx, "c")
require.NoError(t, err)
require.NotNil(t, b)
writeTx.Commit()
// End write tx
// Read tx
readTx, err = db.Bolt.Begin(false)
if err != nil {
t.Fatal(err)
}
2018-01-03 19:29:07 +00:00
require.NotNil(t, db.GetBucket(readTx, "new"))
require.Nil(t, db.GetBucket(readTx, "c"))
2017-10-06 15:29:20 +00:00
require.NotNil(t, n2.GetBucket(readTx, "c"))
readTx.Rollback()
// End read tx
}