-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_test.go
94 lines (81 loc) · 1.83 KB
/
kv_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
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
package toykv
import (
"fmt"
"github.com/cockroachdb/pebble"
"os"
"testing"
"time"
)
import "github.com/stretchr/testify/assert"
func TestKeyValue_Set(t *testing.T) {
_ = os.RemoveAll("store.db")
kv := KeyValueStore{}
err := kv.Open("store.db")
assert.Nil(t, err)
assert.NotNil(t, kv.DB)
now := time.Now()
err = kv.Merge('T', now.String(), "now")
assert.Nil(t, err)
_, err = kv.Get('T', now.String())
assert.Equal(t, pebble.ErrNotFound, err)
err = kv.Commit()
assert.Nil(t, err)
str, err := kv.Get('T', now.String())
assert.Nil(t, err)
assert.Equal(t, "now", str)
err = kv.Merge('T', now.String(), " is ")
assert.Nil(t, err)
err = kv.Merge('T', now.String(), now.String())
assert.Nil(t, err)
err = kv.Commit()
assert.Nil(t, err)
str, err = kv.Get('T', now.String())
assert.Nil(t, err)
assert.Equal(t, "now is "+now.String(), str)
kv.Close()
assert.Nil(t, kv.DB)
_ = os.RemoveAll("store.db")
}
type Int struct {
i int
}
func (i Int) String() string {
return fmt.Sprintf("%8d", i.i)
}
func (i *Int) Inc() {
i.i++
}
func TestKeyValue_Range(t *testing.T) {
_ = os.RemoveAll("range.store")
kv := KeyValueStore{}
err := kv.Open("range.store")
assert.Nil(t, err)
assert.NotNil(t, kv.DB)
i := Int{0}
for i.i < 1<<20 {
err = kv.Set('N', i.String(), "set")
assert.Nil(t, err)
i.Inc()
}
err = kv.Commit()
assert.Nil(t, err)
fro := Int{1100}
to := Int{2233}
i = fro
rng := kv.Range('N', fro.String(), to.String())
for rng.Valid() {
assert.Equal(t, uint8('N'), rng.Liter())
assert.Equal(t, i.String(), rng.Key())
assert.Equal(t, "set", rng.Value())
i.Inc()
rng.Next()
}
assert.Equal(t, to.i, i.i)
assert.Equal(t, uint8(0), rng.Liter())
assert.Equal(t, "", rng.Value())
assert.Equal(t, "", rng.Key())
assert.False(t, rng.Next())
kv.Close()
assert.Nil(t, kv.DB)
_ = os.RemoveAll("range.store")
}