forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
63 lines (51 loc) · 1.24 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
package gorocksdb
import (
"io/ioutil"
"testing"
"github.com/facebookgo/ensure"
)
func TestOpenDb(t *testing.T) {
db := newTestDB(t, "TestOpenDb", nil)
defer db.Close()
}
func TestDBCRUD(t *testing.T) {
db := newTestDB(t, "TestDBGet", nil)
defer db.Close()
var (
givenKey = []byte("hello")
givenVal1 = []byte("world1")
givenVal2 = []byte("world2")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)
// create
ensure.Nil(t, db.Put(wo, givenKey, givenVal1))
// retrieve
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenVal1)
// update
ensure.Nil(t, db.Put(wo, givenKey, givenVal2))
v2, err := db.Get(ro, givenKey)
defer v2.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v2.Data(), givenVal2)
// delete
ensure.Nil(t, db.Delete(wo, givenKey))
v3, err := db.Get(ro, givenKey)
ensure.Nil(t, err)
ensure.True(t, v3.Data() == nil)
}
func newTestDB(t *testing.T, name string, applyOpts func(opts *Options)) *DB {
dir, err := ioutil.TempDir("", "gorocksdb-"+name)
ensure.Nil(t, err)
opts := NewDefaultOptions()
opts.SetCreateIfMissing(true)
if applyOpts != nil {
applyOpts(opts)
}
db, err := OpenDb(opts, dir)
ensure.Nil(t, err)
return db
}