-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
65 lines (54 loc) · 1.22 KB
/
db_test.go
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
package rumble
import (
"os"
"testing"
)
func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
// TODO: error from here does not provide previous line number
t.Errorf("function expected to panic but did not")
}
}()
f()
}
func prepareTestDB(bt interface{}, name string) *DB {
db, err := New(name)
if err != nil {
switch t := bt.(type) {
case *testing.T:
t.Fatal(err)
case *testing.B:
t.Fatal(err)
default:
panic(err)
}
}
return db
}
func TestDB(t *testing.T) {
db := prepareTestDB(t, "TestDB.db")
defer func() {
db.Bolt.Close()
os.Remove("TestDB.db")
}()
b := db.Bucket("foo")
if c := b.Count(); c != 0 {
t.Fatalf("expected count to be 0, got %v", c)
}
if err := b.Put(map[string]interface{}{"fizz": "buzz"}); err != nil {
t.Fatalf("error inserting map into foo: %s", err)
}
if c := b.Count(); c != 1 {
t.Fatalf("expected count to be 1, got %v", c)
}
b2 := db.Bucket("foo")
if c := b2.Count(); c != 1 {
t.Fatalf("expected count to be 1, got %v", c)
}
if err := db.DeleteBucket("foo"); err != nil {
t.Fatalf("error deleting bucket: %s", err)
}
// ensure delete occured by checking for panic on count
assertPanic(t, func() { b2.Count() })
}