diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-22 14:12:00 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-22 14:12:00 -0600 |
commit | 6d6303a0a2f4252f8fb9d8b610f1d49f76112b12 (patch) | |
tree | 804a006a06f8fd45a160adb83385b75fc9697248 /cmd/bolt/main_test.go | |
parent | Merge pull request #71 from benbjohnson/munmap-fix (diff) | |
parent | Fix print. (diff) | |
download | dedo-6d6303a0a2f4252f8fb9d8b610f1d49f76112b12.tar.gz dedo-6d6303a0a2f4252f8fb9d8b610f1d49f76112b12.tar.xz |
Merge pull request #74 from benbjohnson/cli
CLI
Diffstat (limited to 'cmd/bolt/main_test.go')
-rw-r--r-- | cmd/bolt/main_test.go | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/cmd/bolt/main_test.go b/cmd/bolt/main_test.go new file mode 100644 index 0000000..b755ccd --- /dev/null +++ b/cmd/bolt/main_test.go @@ -0,0 +1,158 @@ +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.Do(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.Do(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.Do(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.Do(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.Do(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()) +} |