1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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.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())
}
|