-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvstore_test.go
160 lines (142 loc) · 2.97 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package kvstore
import (
"os"
"testing"
)
var store *Store
func TestInit(t *testing.T) {
var err error
store, err = Init("test.store")
if err != nil {
t.Errorf("%v\n", err)
}
}
func TestCreate(t *testing.T) {
if store == nil {
t.Errorf("store = nil was provided")
return
}
keyValues := []*KeyValue{
&KeyValue{
Key: "abc",
Value: map[string]interface{}{
"a": "sasas",
"b": map[string]interface{}{
"abhinav": "singh",
},
},
Time: 1000, // valid for 1000 seconds
},
&KeyValue{
Key: "fgdfgsjewhdbdjsgdjhekfhdjkdshfhgfjkdldhdfkghghg",
Value: map[string]interface{}{
"asa": 100,
"vdd": map[string]interface{}{
"igdfg": "assaas",
},
},
Time: 2000, // valid for 2000 seconds
},
&KeyValue{
Key: "jds",
Value: map[string]interface{}{
"ghkgrf": 100000,
"fgihjf": map[string]interface{}{
"fsh": "feoapufe",
},
},
Time: 0, // valid forever
},
&KeyValue{
Key: "abc",
Value: map[string]interface{}{
"sasfa": "sasasassaas",
"b": map[string]interface{}{
"asdfaf": "gfhytrhjrhr",
},
},
Time: 5,
},
&KeyValue{
Key: "",
Value: map[string]interface{}{
"sasfa": "sasasassaas",
"b": map[string]interface{}{
"asdfaf": "gfhytrhjrhr",
},
},
Time: 5,
},
}
for i, keyValue := range keyValues {
err := Create(store, keyValue.Key, keyValue.Value, keyValue.Time)
if i == 0 || i == 2 {
if err != nil {
t.Errorf("key: %s, error: %v\n", keyValue.Key, err)
return
}
} else if i == 1 {
if err == nil || err.Error() != "key size cannot exceed 32 characters" {
t.Errorf("key: %s, error: %v\n", keyValue.Key, err)
return
}
} else if i == 3 {
if err == nil || err.Error() != "key already present" {
t.Errorf("key: %s, error: %v\n", keyValue.Key, err)
return
}
} else if i == 4 {
if err == nil || err.Error() != "key cannot be empty" {
t.Errorf("key: %s, error: %v\n", keyValue.Key, err)
return
}
}
}
}
func TestRead(t *testing.T) {
if store == nil {
t.Errorf("store = nil was provided")
return
}
var key string
var err error
key = "abc"
if _, err = Read(store, key); err != nil {
t.Errorf("key: %s, error: %v\n", key, err)
return
}
key = "zcege"
if _, err = Read(store, key); err == nil || err.Error() != "key not found" {
t.Errorf("key: %s, error: %v", key, err)
}
}
func TestDelete(t *testing.T) {
if store == nil {
t.Errorf("store = nil was provided")
return
}
var key string
var err error
key = "zfhegf"
if err = Delete(store, key); err == nil || err.Error() != "key not present" {
t.Errorf("key: %s, error: %v\n", key, err)
return
}
key = "abc"
if err = Delete(store, key); err != nil {
t.Errorf("key: %s, error: %v", key, err)
}
}
func TestClose(t *testing.T) {
if store == nil {
t.Errorf("store = nil was provided")
return
}
err := Close(store)
if err != nil {
t.Errorf("%v\n", err)
return
}
// remove temporary test store file
os.Remove("test.store")
}