-
Notifications
You must be signed in to change notification settings - Fork 2
/
kvstore_test.go
128 lines (101 loc) · 2.49 KB
/
kvstore_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package kvstore
import (
"os"
"reflect"
"testing"
)
var (
key1 uint64 = 20150110
key2 uint64 = 20150120
key3 uint64 = 20150130
keylesser uint64 = 20150101
keymiss uint64 = 20150115
keymore uint64 = 20150131
buf0 []byte = []byte("hello world.")
buf1 []byte = []byte("hello world 1.")
buf2 []byte = []byte("hello world 2.")
buf3 []byte = []byte("hello world 3.")
)
func TestWrongConfig(t *testing.T) {
c := &Config{}
_, err := New(c)
if err != ErrWrongConfig {
t.Errorf("Config without filename, can't create kvstore!")
}
}
func TestCreateEmptyFileAndGet(t *testing.T) {
os.Remove("./data/empty.gzip.file")
c := &Config{
FileName: "./data/empty.gzip.file",
FileCapacity: 10,
CompressCodec: 1}
kv, err := New(c)
if err != nil {
t.Errorf("Failed to create kv, reason: %v\n", err)
}
_, err = kv.Get(key1)
if err != ErrDataNotFound {
t.Errorf("Get empty file, return value should be nil!")
}
kv.Close()
}
func TestAppendAndGet(t *testing.T) {
os.Remove("./data/gzip.file")
c := &Config{
FileName: "./data/gzip.file",
FileCapacity: 3,
CompressCodec: 1}
kv, err := New(c)
if err != nil {
t.Errorf("Failed to create kv, reason: %v\n", err)
}
err = kv.Append(key1, buf1)
if err != nil {
t.Errorf("Failed to create key1")
}
err = kv.Append(key1, buf1)
if err != ErrAppendFail {
t.Errorf("key1 equal key1, shouldn't been appended.")
}
err = kv.Append(keylesser, buf0)
if err != ErrAppendFail {
t.Errorf("keylesser less than key1, shouldn't been appended.")
}
err = kv.Append(key2, buf2)
if err != nil {
t.Errorf("Failed to create key2, reason: %v\n", err)
}
err = kv.Append(key3, buf3)
if err != nil {
t.Errorf("Failed to create key3, reason: %v\n", err)
}
err = kv.Append(keymore, buf0)
if err != ErrOutOfCapacity {
t.Errorf("Out out the capacity, should not be appended. Reason: %v\n", err)
}
buf, err := kv.Get(key1)
if !reflect.DeepEqual(buf, buf1) {
t.Errorf("buf should equal buf1")
}
buf, err = kv.Get(key2)
if !reflect.DeepEqual(buf, buf2) {
t.Errorf("buf should equal buf2")
}
buf, err = kv.Get(key3)
if !reflect.DeepEqual(buf, buf3) {
t.Errorf("buf should equal buf3")
}
buf, err = kv.Get(keylesser)
if err != ErrDataNotFound {
t.Errorf("key is lesser, should not be found!")
}
buf, err = kv.Get(keymiss)
if err != ErrDataNotFound {
t.Errorf("key is miss, should not be found!")
}
buf, err = kv.Get(keymore)
if err != ErrDataNotFound {
t.Errorf("key is bigger, should not be found!")
}
kv.Close()
}