mirror of
https://github.com/etcd-io/bbolt.git
synced 2025-07-15 00:28:30 +00:00
I consolidated the DB.Tx() and DB.RWTx() calls into a single DB.Begin(writable bool) call. This is more consistent with the database/sql library. I also changed the DB.Do() and DB.With() call to DB.Update() and DB.View(), respectively. This is more intuitive and more inline with other database verbiage.
159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package main_test
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/boltdb/bolt"
|
|
. "github.com/boltdb/bolt/cmd/bolt"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Ensure that a value can be retrieved from the CLI.
|
|
func TestGet(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
tx.CreateBucket("widgets")
|
|
tx.Bucket("widgets").Put([]byte("foo"), []byte("bar"))
|
|
return nil
|
|
})
|
|
output := run("get", db.Path(), "widgets", "foo")
|
|
assert.Equal(t, "bar", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that an error is reported if the database is not found.
|
|
func TestGetDBNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
output := run("get", "no/such/db", "widgets", "foo")
|
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
}
|
|
|
|
// Ensure that an error is reported if the bucket is not found.
|
|
func TestGetBucketNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
output := run("get", db.Path(), "widgets", "foo")
|
|
assert.Equal(t, "bucket not found: widgets", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that an error is reported if the key is not found.
|
|
func TestGetKeyNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
return tx.CreateBucket("widgets")
|
|
})
|
|
output := run("get", db.Path(), "widgets", "foo")
|
|
assert.Equal(t, "key not found: foo", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that a value can be set from the CLI.
|
|
func TestSet(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
tx.CreateBucket("widgets")
|
|
return nil
|
|
})
|
|
assert.Equal(t, "", run("set", db.Path(), "widgets", "foo", "bar"))
|
|
assert.Equal(t, "bar", run("get", db.Path(), "widgets", "foo"))
|
|
})
|
|
}
|
|
|
|
// Ensure that an error is reported if the database is not found.
|
|
func TestSetDBNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
output := run("set", "no/such/db", "widgets", "foo", "bar")
|
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
}
|
|
|
|
// Ensure that an error is reported if the bucket is not found.
|
|
func TestSetBucketNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
output := run("set", db.Path(), "widgets", "foo", "bar")
|
|
assert.Equal(t, "bucket not found: widgets", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that a list of keys can be retrieved for a given bucket.
|
|
func TestKeys(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
tx.CreateBucket("widgets")
|
|
tx.Bucket("widgets").Put([]byte("0002"), []byte(""))
|
|
tx.Bucket("widgets").Put([]byte("0001"), []byte(""))
|
|
tx.Bucket("widgets").Put([]byte("0003"), []byte(""))
|
|
return nil
|
|
})
|
|
output := run("keys", db.Path(), "widgets")
|
|
assert.Equal(t, "0001\n0002\n0003", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that an error is reported if the database is not found.
|
|
func TestKeysDBNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
output := run("keys", "no/such/db", "widgets")
|
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
}
|
|
|
|
// Ensure that an error is reported if the bucket is not found.
|
|
func TestKeysBucketNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
output := run("keys", db.Path(), "widgets")
|
|
assert.Equal(t, "bucket not found: widgets", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that a list of buckets can be retrieved.
|
|
func TestBuckets(t *testing.T) {
|
|
SetTestMode(true)
|
|
open(func(db *bolt.DB) {
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
tx.CreateBucket("woojits")
|
|
tx.CreateBucket("widgets")
|
|
tx.CreateBucket("whatchits")
|
|
return nil
|
|
})
|
|
output := run("buckets", db.Path())
|
|
assert.Equal(t, "whatchits\nwidgets\nwoojits", output)
|
|
})
|
|
}
|
|
|
|
// Ensure that an error is reported if the database is not found.
|
|
func TestBucketsDBNotFound(t *testing.T) {
|
|
SetTestMode(true)
|
|
output := run("buckets", "no/such/db")
|
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
}
|
|
|
|
// open creates and opens a Bolt database in the temp directory.
|
|
func open(fn func(*bolt.DB)) {
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
f.Close()
|
|
os.Remove(f.Name())
|
|
defer os.RemoveAll(f.Name())
|
|
|
|
db, err := bolt.Open(f.Name(), 0600)
|
|
if err != nil {
|
|
panic("db open error: " + err.Error())
|
|
}
|
|
fn(db)
|
|
}
|
|
|
|
// run executes a command against the CLI and returns the output.
|
|
func run(args ...string) string {
|
|
args = append([]string{"bolt"}, args...)
|
|
NewApp().Run(args)
|
|
return strings.TrimSpace(LogBuffer())
|
|
}
|