-
Notifications
You must be signed in to change notification settings - Fork 8
/
simplebolt_test.go
359 lines (319 loc) · 7.43 KB
/
simplebolt_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package simplebolt
import (
"github.com/xyproto/pinterface"
"os"
"path"
"testing"
)
func TestList(t *testing.T) {
const (
listname = "abc123_test_test_test_123abc"
testdata = "123abc"
)
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
list, err := NewList(db, listname)
if err != nil {
t.Error(err)
}
if err := list.Add(testdata); err != nil {
t.Errorf("Error, could not add item to list! %s", err.Error())
}
items, err := list.All()
if err != nil {
t.Error(err)
}
if len(items) != 1 {
t.Errorf("Error, wrong list length! %v", len(items))
}
if (len(items) > 0) && (items[0] != testdata) {
t.Errorf("Error, wrong list contents! %v", items)
}
err = list.Clear()
if err != nil {
t.Errorf("Error, could not clear list! %s", err.Error())
}
err = list.Remove()
if err != nil {
t.Errorf("Error, could not remove list! %s", err.Error())
}
}
func TestRemove(t *testing.T) {
const (
kvname = "abc123_test_test_test_123abc"
testkey = "sdsdf234234"
testvalue = "asdfasdf1234"
)
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
kv, err := NewKeyValue(db, kvname)
if err != nil {
t.Error(err)
}
if err := kv.Set(testkey, testvalue); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue {
t.Errorf("Error, wrong value! %s != %s", val, testvalue)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
}
func TestInc(t *testing.T) {
const (
kvname = "kv_234_test_test_test"
testkey = "key_234_test_test_test"
testvalue0 = "9"
testvalue1 = "10"
testvalue2 = "1"
)
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
kv, err := NewKeyValue(db, kvname)
if err != nil {
t.Error(err)
}
if err := kv.Set(testkey, testvalue0); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue0 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue0)
}
incval, err := kv.Inc(testkey)
if err != nil {
t.Errorf("Error, could not INCR key! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue1 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue1)
} else if incval != testvalue1 {
t.Errorf("Error, wrong inc value! %s != %s", incval, testvalue1)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
// Creates "0" and increases the value with 1
kv.Inc(testkey)
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue2 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue2)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
}
func TestVarious(t *testing.T) {
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
kv, err := NewKeyValue(db, "fruit")
if err != nil {
t.Error(err)
}
if err := kv.Set("banana", "yes"); err != nil {
t.Error("Could not set a key+value:", err)
}
_, err = kv.Get("banana")
if err != nil {
t.Error("Could not get value:", err)
}
kv.Set("banana", "2")
kv.Inc("banana")
_, err = kv.Get("banana")
if err != nil {
t.Error(err)
}
kv.Inc("fnu")
_, err = kv.Get("fnu")
if err != nil {
t.Error(err)
}
_, _ = kv.Get("doesnotexist")
//fmt.Println("does not exist", val, err)
kv.Remove()
l, err := NewList(db, "fruit")
if err != nil {
t.Error(err)
}
l.Add("kiwi")
l.Add("banana")
l.Add("pear")
l.Add("apple")
if _, err := l.All(); err != nil {
t.Error(err)
}
last, err := l.Last()
if err != nil {
t.Error(err)
}
if last != "apple" {
t.Error("last one should be apple")
}
lastN, err := l.LastN(3)
if err != nil {
t.Error(err)
}
if lastN[0] != "banana" {
t.Error("banana is wrong")
}
l.Remove()
// Check that the list qualifies for the IList interface
var _ pinterface.IList = l
s, err := NewSet(db, "numbers")
if err != nil {
t.Error(err)
}
s.Add("9")
s.Add("7")
s.Add("2")
s.Add("2")
s.Add("2")
s.Add("7")
s.Add("8")
_, err = s.All()
if err != nil {
t.Error(err)
}
s.Clear()
v, err := s.All()
if err != nil {
t.Error(err)
}
if len(v) != 0 {
t.Error("Could not clear set")
}
s.Remove()
// Check that the set qualifies for the ISet interface
var _ pinterface.ISet = s
val, err := kv.Inc("counter")
if (val != "1") || (err != nil) {
t.Error("counter should be 1 but is", val)
}
kv.Remove()
// Check that the key value qualifies for the IKeyValue interface
var _ pinterface.IKeyValue = kv
h, err := NewHashMap(db, "counter")
if err != nil {
t.Error(err)
}
h.Set("bob", "password", "hunter1")
h.Set("bob", "email", "bob@zombo.com")
h.All()
_, err = h.Has("bob", "password")
if err != nil {
t.Error(err)
}
_, err = h.Exists("bob")
if err != nil {
t.Error(err)
}
h.Set("john", "password", "hunter2")
h.Set("john", "email", "john@zombo.com")
h.Del("john")
found, err := h.Exists("john")
if err != nil {
t.Error(err)
}
if found {
t.Error("not supposed to exist")
}
h.Remove()
_, err = h.Has("bob", "password")
if err == nil {
t.Error("not supposed to exist")
}
_, err = h.Exists("bob")
if err == nil {
t.Error("not supposed to exist")
}
// Check that the hash map qualifies for the IHashMap interface
var _ pinterface.IHashMap = h
}
func TestInterface(t *testing.T) {
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
// Check that the database qualifies for the IHost interface
var _ pinterface.IHost = db
// Check if the struct conforms to ICreator
var _ pinterface.ICreator = NewCreator(db)
}
func TestHashMap(t *testing.T) {
const (
hashname = "abc123_test_test_test_123abc_123"
testid = "bob"
testidInv = "b:ob"
testkey = "password"
testvalue = "hunter1"
)
db, err := New(path.Join(os.TempDir(), "bolt.db"))
if err != nil {
t.Error(err)
}
defer db.Close()
hash, err := NewHashMap(db, hashname)
if err != nil {
t.Error(err)
}
if err := hash.Set(testidInv, testkey, testvalue); err == nil {
t.Error("Should not be allowed to use an element id with \":\"")
}
if err := hash.Set(testid, testkey, testvalue); err != nil {
t.Errorf("Error, could not add item to hash map! %s", err.Error())
}
value2, err := hash.Get(testid, testkey)
if err != nil {
t.Error(err)
}
if value2 != testvalue {
t.Errorf("Got a different value in return! %s != %s", value2, testvalue)
}
items, err := hash.All()
if err != nil {
t.Error(err)
}
if len(items) != 1 {
t.Errorf("Error, wrong hash map length! %v", len(items))
}
if (len(items) > 0) && (items[0] != testid) {
t.Errorf("Error, wrong hash map id! %v", items)
}
keys, err := hash.Keys(testid)
if err != nil {
t.Error(err)
}
// only "password"
if len(keys) != 1 {
t.Errorf("Error, wrong keys: %v\n", keys)
}
if keys[0] != "password" {
t.Errorf("Error, wrong keys: %v\n", keys)
}
err = hash.Remove()
if err != nil {
t.Errorf("Error, could not remove hash map! %s", err.Error())
}
}