-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
458 lines (391 loc) · 11.7 KB
/
store_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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package store
import (
"testing"
"time"
"com.github.andrelcunha.go-redis-clone/internal/utils/slice"
)
func TestStore(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
s.Set(0, "Key1", "Value1")
value, ok := s.Get(0, "Key1")
if !ok {
t.Fatalf("Failed to get key")
}
if value != "Value1" {
t.Fatalf("Expected Value1, got %s", value)
}
s.Del(0, "Key1")
_, ok = s.Get(0, "Key1")
if ok {
t.Fatalf("Expected key1 to be deleted")
}
}
func TestExists(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
s.Set(0, "Key1", "Value1")
if !s.Exists(0, "Key1") {
t.Fatalf("Expected Key1 to exist")
}
if s.Exists(0, "Key2") {
t.Fatalf("Expected Key2 to not exist")
}
}
func TestSetNX(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
if !s.SetNX(0, "Key1", "Value1") {
t.Fatalf("Expected SETNX to succeed for Key1")
}
if s.SetNX(0, "Key1", "Value2") {
t.Fatalf("Expected SETNX to fail for Key1")
}
value, ok := s.Get(0, "Key1")
if !ok || value != "Value1" {
t.Fatalf("Expected Value1, got %s", value)
}
}
func TestExpire(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
s.Set(0, "Key1", "Value1")
if !s.Expire(0, "Key1", 1*time.Second) {
t.Fatalf("Expected Expire to succeed for Key1")
}
time.Sleep(2 * time.Second)
if s.Exists(0, "Key1") {
t.Fatalf("Expected Key1 to be expired")
}
}
func TestIncr(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
newValue, err := s.Incr(0, "counter")
if err != nil {
t.Fatalf("INCR failed: %v", err)
}
// test if value is created and set as '0'++
if newValue != 1 {
t.Fatalf("expected 1, got %d", newValue)
}
// test if value is incremented
newValue, err = s.Incr(0, "counter")
if err != nil {
t.Fatalf("INCR failed: %v", err)
}
if newValue != 2 {
t.Fatalf("expected 2, got %d", newValue)
}
}
func TesDecr(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
newValue, err := s.Decr(0, "counter")
if err != nil {
t.Fatalf("DECR failed: %v", err)
}
// test if value is created and set as '0'--
if newValue != -1 {
t.Fatalf("expected -1, got %d", newValue)
}
// test if value is incremented
newValue, err = s.Incr(0, "counter")
if err != nil {
t.Fatalf("DECR failed: %v", err)
}
if newValue != -2 {
t.Fatalf("expected -2, got %d", newValue)
}
}
// test Ttl
func TestTtl(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
s.Set(0, "Key1", "Value1")
if !s.Expire(0, "Key1", 4*time.Second) {
t.Fatalf("Expected Expire to succeed for Key1")
}
time.Sleep(1 * time.Second)
// Test that TTL returns the correct remaining time
ttl, err := s.TTL(0, "Key1")
if err != nil {
t.Fatalf("Expected TTL to succeed for Key1")
}
if ttl != 2 {
t.Fatalf("Expected TTL to be 2 seconds, got %v", ttl)
}
time.Sleep(3 * time.Second)
// Test that TTL returns -2 for expired key
ttl, err = s.TTL(0, "Key1")
if err != nil {
t.Fatalf("Expected TTL to succeed for Key1")
}
if ttl != 0 {
t.Fatalf("Expected TTL to be -2, got %v", ttl)
}
s.Set(0, "Key2", "Value2")
ttl, err = s.TTL(0, "Key2")
if err != nil {
t.Fatalf("Expected TTL to succeed for Key2")
}
if ttl != -1 {
t.Fatalf("Expected TTL to be -1, got %v", ttl)
}
s.Del(0, "Key2")
ttl, err = s.TTL(0, "Key2")
if err != nil {
t.Fatalf("Expected TTL to succeed for Key2")
}
if ttl != -2 {
t.Fatalf("Expected TTL to be -2, got %v", ttl)
}
}
// test LPush
func TestLPush(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
//test if the response is correct
listLen := s.LPush(0, "list", "value1", "value2")
if listLen != 2 {
t.Fatalf("Expected response to be 2, got %d", listLen)
}
//test if the list length is correct
s.LPush(0, "list", "value3")
list, _ := s.Data[0]["list"].([]string)
if len(list) != 3 {
t.Fatalf("Expected list length to be 3, got %d", len(list))
}
//test if the list contents are correct
expected := []string{"value3", "value2", "value1"}
if !slice.Equal(list, expected) {
t.Fatalf("Expected list to be [value3 value2 value1], got %v", list)
}
}
// test RPush
func TestRPush(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
//test if the response is correct
listLen := s.RPush(0, "list", "value1", "value2")
if listLen != 2 {
t.Fatalf("Expected response to be 2, got %d", listLen)
}
//test if the list length is correct
s.RPush(0, "list", "value3")
list, _ := s.Data[0]["list"].([]string)
if len(list) != 3 {
t.Fatalf("Expected list length to be 3, got %d", len(list))
}
//test if the list contents are correct
if list[0] != "value1" || list[1] != "value2" || list[2] != "value3" {
t.Fatalf("Expected list to be [value3 value1 value2], got %v", list)
}
}
// test LPop
func TestLPop(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
//test if LPop returns nil when key does not exist
t.Log("test if LPOP returns nil when key does not exist")
value, _ := s.LPop(0, "list", nil)
if value != nil {
t.Fatalf("Expected got nil, got %s", value)
}
s.LPush(0, "list", "value1", "value2", "value3")
//test if LPOP returns error when called with count argument smaller than 0
t.Log("test if LPOP returns error when called with count argument smaller than 0")
count := -1
value, err := s.LPop(0, "list", &count)
if err == nil {
t.Fatalf("Expected error when calling LPOP with count smaller than 0")
}
//test if LPOP returns empty list when called with count = 0
t.Log("test if LPOP returns empty list when called with count = 0")
count = 0
value, err = s.LPop(0, "list", &count)
if (err != nil) || len(value.([]string)) != 0 {
t.Fatalf("Expected [] (empty list), got %s, value length is %d ", value, len(value.([]string)))
}
// test if LPOP returns the first element as string when called with count = nil
t.Log("test if LPOP returns the first element as string when called with count = nil")
value, err = s.LPop(0, "list", nil)
if (err != nil) || value.(string) != "value3" {
t.Fatalf("Expected value1, got %s", value)
}
//test if LPop returns the list when called with count argument greater than list length
t.Log("test if LPOP returns the list when called with count argument greater than list length")
count = 3
value, err = s.LPop(0, "list", &count)
expected := []string{"value2", "value1"}
if (err != nil) || !slice.Equal(value.([]string), expected) {
t.Fatalf("Expected [value2 value1], got %v", value)
}
//test if LPOP returns nil when the list is empty
t.Log("test if LPOP returns nil when the list is empty")
value, err = s.LPop(0, "list", &count)
if value != nil {
t.Fatalf("Expected nil, got %v", value)
}
}
// test RPop
func TestRPop(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
//test if RPop returns nil when key does not exist
t.Log("test if RPop returns nil when key does not exist")
value, _ := s.RPop(0, "list", nil)
if value != nil {
t.Fatalf("Expected got nil, got %s", value)
}
s.LPush(0, "list", "value1", "value2", "value3")
//test if RPop returns error when called with count argument smaller than 0
t.Log("test if RPop returns error when called with count argument smaller than 0")
count := -1
value, err := s.RPop(0, "list", &count)
if err == nil {
t.Fatalf("Expected error when calling RPop with count smaller than 0")
}
//test if RPop returns empty list when called with count = 0
t.Log("test if RPop returns empty list when called with count = 0")
count = 0
value, err = s.RPop(0, "list", &count)
if (err != nil) || len(value.([]string)) != 0 {
t.Fatalf("Expected [] (empty list), got %s, value length is %d ", value, len(value.([]string)))
}
s.Del(0, "list")
// test if RPop returns the first element as string when called with count = nil
s.LPush(0, "list", "value1", "value2", "value3")
t.Log("test if RPop returns the last element as string when called with count = nil")
value, err = s.RPop(0, "list", nil)
if (err != nil) || value == nil || value.(string) != "value1" {
t.Fatalf("Expected value1, got %s", value)
}
//test if RPop returns the list when called with count argument greater than list length
t.Log("test if RPop returns the list when called with count argument greater than list length")
count = 3
value, err = s.RPop(0, "list", &count)
expected := []string{"value3", "value2"}
if (err != nil) || !slice.Equal(value.([]string), expected) {
t.Fatalf("Expected [value3 value2], got %v", value)
}
//test if RPop returns nil when the list is empty
t.Log("test if RPop returns nil when the list is empty")
value, err = s.RPop(0, "list", &count)
if value != nil {
t.Fatalf("Expected nil, got %v", value)
}
}
// test LRange
func TestLRange(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
s.LPush(0, "list", "value1", "value2", "value3", "value4")
// Test full range
t.Log("test if LRange returns the full range")
list, err := s.LRange(0, "list", 0, -1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expected := []string{"value4", "value3", "value2", "value1"}
if !slice.Equal(list, expected) {
t.Fatalf("Expected %v, got %v", expected, list)
}
// Test partial range
t.Log("test if LRange returns the partial range")
list, err = s.LRange(0, "list", 1, 2)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expected = []string{"value3", "value2"}
if !slice.Equal(list, expected) {
t.Fatalf("Expected %v, got %v", expected, list)
}
}
// Test Rename
func TestRename(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
// test if Rename returns error when key does not exist
t.Log("test if Rename returns error when key does not exist")
s.Rename(0, "key1", "key2")
value, ok := s.Get(0, "key2")
if ok {
t.Fatalf("Expected ok equal false, got %v", ok)
}
// test if Rename does not rename when key exists
t.Log("test if Rename does not rename when key exists")
s.Set(0, "key1", "value1")
s.Rename(0, "key1", "key2")
value, ok = s.Get(0, "key2")
if value != "value1" {
t.Fatalf("Expected value1, got %s", value)
}
s.Del(0, "key1")
s.Del(0, "key2")
// test if Rename does not rename when key exists and new key already exists
t.Log("test if Rename does not rename when key exists and new key already exists")
s.Set(0, "key1", "value1")
s.Set(0, "key2", "value2")
s.Rename(0, "key1", "key2")
value, ok = s.Get(0, "key2")
if value != "value1" {
t.Fatalf("Expected value1, got %s", value)
}
}
// Test Type
func TestType(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
dbIndex := 0
// Arrange
s.Set(dbIndex, "myString", "value1")
myList := []string{"one", "two", "three"}
s.RPush(dbIndex, "myList", myList...)
// int
s.Lock()
s.Data[dbIndex]["myInt"] = 123
s.mu.Unlock()
// test if myString is a string
stype := s.Type(dbIndex, "myString")
if stype != "string" {
t.Logf("expected 'string', got %s", stype)
t.Fail()
}
// test if myList is a list
ltype := s.Type(dbIndex, "myList")
if ltype != "list" {
t.Logf("expected 'list', got '%s'", ltype)
t.Fail()
}
// test if a non-existing key is type 'none'
ntype := s.Type(dbIndex, "other")
if ntype != "none" {
t.Logf("expected 'none', got '%s'", ntype)
t.Fail()
}
// test if an integer is none
itype := s.Type(dbIndex, "myInt")
if itype != "none" {
t.Logf("expected 'none', got '%s'", itype)
t.Fail()
}
}
// Test Keys
func TestKeys(t *testing.T) {
aofChan := make(chan string, 100)
s := NewStore(aofChan)
indexDb := 0
s.Set(indexDb, "key1", "value1")
s.Set(indexDb, "key2", "value2")
list1 := []string{"one", "two", "tree"}
s.RPush(indexDb, "list1", list1...)
keys, err := s.Keys(indexDb, "*")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
expeted := []string{"key1", "key2", "list1"}
if !slice.Equal(keys, expeted) {
t.Logf("expected %v, got %v", expeted, keys)
}
}