forked from asdine/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_test.go
68 lines (47 loc) · 1.16 KB
/
bucket_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
66
67
68
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)
}
require.Nil(t, db.GetBucket(readTx, "none"))
b, err := db.CreateBucketIfNotExists(readTx, "new")
// 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)
}
require.Nil(t, db.GetBucket(writeTx, "none"))
b, err = db.CreateBucketIfNotExists(writeTx, "new")
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)
}
require.NotNil(t, db.GetBucket(readTx, "new"))
require.Nil(t, db.GetBucket(readTx, "c"))
require.NotNil(t, n2.GetBucket(readTx, "c"))
readTx.Rollback()
// End read tx
}