forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomparator_test.go
54 lines (44 loc) · 1.38 KB
/
comparator_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
package gorocksdb
import (
"bytes"
. "github.com/smartystreets/goconvey/convey"
"os"
"testing"
)
type testComparatorHandler struct {
numCompared int
initiated bool
}
func (self *testComparatorHandler) Compare(a, b []byte) int {
self.numCompared++
return bytes.Compare(a, b)
}
func (self *testComparatorHandler) Name() string {
self.initiated = true
return "gorocksdb.test"
}
func TestNewComparator(t *testing.T) {
dbName := os.TempDir() + "/TestNewComparator"
Convey("Subject: Custom comparator", t, func() {
Convey("When create a custom comparator then it should not panic", func() {
handler := &testComparatorHandler{}
cmp := NewComparator(handler)
Convey("When passed to the db as comperator then it should not panic", func() {
options := NewDefaultOptions()
DestroyDb(dbName, options)
options.SetCreateIfMissing(true)
options.SetComparator(cmp)
db, err := OpenDb(options, dbName)
So(err, ShouldBeNil)
So(handler.initiated, ShouldBeTrue)
Convey("When put 3 values into the db then the comperator should be called two times", func() {
wo := NewDefaultWriteOptions()
So(db.Put(wo, []byte("key1"), []byte("value1")), ShouldBeNil)
So(db.Put(wo, []byte("key2"), []byte("value2")), ShouldBeNil)
So(db.Put(wo, []byte("key3"), []byte("value3")), ShouldBeNil)
So(handler.numCompared, ShouldEqual, 2)
})
})
})
})
}