-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrings_test.go
480 lines (441 loc) · 12.2 KB
/
strings_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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package godis
import (
"strconv"
"testing"
"time"
)
// Test setting key-values to the DB
func TestSET(t *testing.T) {
db := setUp()
for _, c := range cases {
got, err := db.SET(c.key, c.value)
if got != c.key || err != nil {
t.Errorf("SET(%q, %v) == %q, %v want %q, <nil>", c.key, c.value, got, err, c.key)
}
}
}
// Test getting key-values from the DB
func TestGET(t *testing.T) {
db := setUp()
for _, c := range cases {
db.SET(c.key, c.value)
got, err := db.GET(c.key)
if err != nil || got != c.value {
t.Errorf("GET(%q) == %q, %v want %q, <nil>", c.key, got, err, c.value)
}
}
}
// Test getting non-existent key from the DB
func TestGETNotEXISTS(t *testing.T) {
db := setUp()
key := "not exists"
got, err := db.GET(key)
if err.Error() != "keynotexists" {
// Means the key exists
t.Errorf("GET(%q) == %q, %v want %q, keynotexists", key, got, err, "")
}
}
// Test MGETting key-values from the DB
func TestMGET(t *testing.T) {
testKeys := []string{"key1", "key2", "key 3"}
want := []string{"new value 1", "value2", "value 3"}
db := setUp()
for _, c := range cases {
db.SET(c.key, c.value)
}
got, err := db.MGET(testKeys...)
for i, key := range testKeys {
if got[i] != want[i] || err != nil {
t.Errorf("MGET(%q) == %q, %v want %q, <nil>", key, got[i], err, want[i])
}
}
}
// Test MGETting non-existent key-values from the DB
func TestMGETNotExists(t *testing.T) {
testKeys := []string{"non-key1", "non-key2", "non-key3"}
db := setUp()
for _, c := range cases {
db.SET(c.key, c.value)
}
got, err := db.MGET(testKeys...)
for i, key := range testKeys {
if got[i] != nil || err != nil {
t.Errorf("MGET(%q) == %v, %v want %v, <nil>", key, got[i], err, nil)
}
}
}
// Test MGETting few non-existent key-values from the DB
func TestMGETFewNotExists(t *testing.T) {
testKeys := []string{"key1", "non-key2", "key 3"}
want := []string{"new value 1", "value2", "value 3"}
db := setUp()
for _, c := range cases {
db.SET(c.key, c.value)
}
got, err := db.MGET(testKeys...)
for i, key := range testKeys {
if i == 1 && got[i] != nil {
t.Errorf("MGET(%q) == %q, %v want %p, <nil>", key, got[i], err, nil)
}
if i != 1 && got[i] != want[i] {
t.Errorf("MGET(%q) == %q, %v want %q, <nil>", key, got[i], err, want[i])
}
}
}
// Test MSETing key-values pairs into the DB
func TestMSET(t *testing.T) {
tests := []string{"key1", "value1", "key2", "value2", "key3", "value3"}
db := setUp()
got, err := db.MSET(tests...)
if got != true || err != nil {
t.Errorf("MSET(%q) == %t, %v want %t, <nil>", tests, got, err, true)
}
}
// Test incrementing values for given key by 1
func TestINCR(t *testing.T) {
db := setUp()
for _, c := range integers {
db.SET(c.key, c.value)
got, err := db.INCR(c.key)
want, _ := strconv.Atoi(c.value)
want += 1
if got != want {
t.Errorf("INCR(%q) == %d, %v want %d, <nil>", c.key, got, err, want)
}
}
}
// Test incrementing non-existent keys
func TestINCRNonExists(t *testing.T) {
db := setUp()
key := "non-incr-key"
got, err := db.INCR(key)
if got != 1 {
t.Errorf("INCR(%q) == %d, %v want %d, <nil>", key, got, err, 1)
}
}
// Test decrementing values for given key by 1
func TestDECR(t *testing.T) {
db := setUp()
for _, c := range integers {
db.SET(c.key, c.value)
got, err := db.DECR(c.key)
want, _ := strconv.Atoi(c.value)
want -= 1
if got != want {
t.Errorf("DECR(%q) == %d, %v want %d, <nil>", c.key, got, err, want)
}
}
}
// Test decrementing non-existent keys
func TestDECRNonExists(t *testing.T) {
db := setUp()
key := "non-incr-key"
got, err := db.DECR(key)
if got != -1 {
t.Errorf("DECR(%q) == %d, %v want %d, <nil>", key, got, err, -1)
}
}
// Test incrementing values for given key by n
func TestINCRBY(t *testing.T) {
db := setUp()
n := 3
for _, c := range integers {
db.SET(c.key, c.value)
got, err := db.INCRBY(c.key, n)
want, _ := strconv.Atoi(c.value)
want += n
if got != want {
t.Errorf("INCRBY(%q, %d) == %d, %v want %d, <nil>", c.key, n, got, err, want)
}
}
}
// Test incrementing values for a string value
func TestINCRBYString(t *testing.T) {
db := setUp()
n := 3
key := "foo"
db.SET(key, "string value")
got, err := db.INCRBY(key, n)
if err.Error() != "typemismatch" {
t.Errorf("INCRBY(%q, %d) == %d, %v want 0, typemismatch", key, n, got, err)
}
}
// Test decrementing values for given key by n
func TestDECRBY(t *testing.T) {
db := setUp()
n := 3
for _, c := range integers {
db.SET(c.key, c.value)
got, err := db.DECRBY(c.key, n)
want, _ := strconv.Atoi(c.value)
want -= n
if got != want {
t.Errorf("DECRBY(%q, %d) == %d, %v want %d, <nil>", c.key, n, got, err, want)
}
}
}
// Test decrementing values for a string value
func TestDECRBYString(t *testing.T) {
db := setUp()
n := 3
key := "foo"
db.SET(key, "string value")
got, err := db.DECRBY(key, n)
if err.Error() != "typemismatch" {
t.Errorf("DECRBY(%q, %d) == %d, %v want 0, typemismatch", key, n, got, err)
}
}
/* Test incrementing values for given key by 1
func TestINCRmismatchs(t *testing.T) {
db := setUp()
n := 1
for _, c := range strings {
db.SET(c.key, c.value)
got := db.INCR(c.key)
_, ok := c.value.(int)
//if ok && got != value+n {
// t.Errorf("INCR(%q) == %d, want %d", c.key, got, value+n)
//}
if !ok {
t.Errorf("INCR(%q) got type %v, want type %v", c.key, reflect.TypeOf(got), reflect.TypeOf(n))
}
}
}*/
// Test incrementing values for given key by n
func TestINCRBYFLOAT(t *testing.T) {
db := setUp()
n := 3.40002154
for _, c := range floats {
db.SET(c.key, c.value)
got, err := db.INCRBYFLOAT(c.key, n)
want, _ := strconv.ParseFloat(c.value, 64)
want += n
if got != want {
t.Errorf("INCRBYFLOAT(%q, %f) == %f, %v want %f, <nil>", c.key, n,
got, err, want)
}
}
}
// Test incrementing values for a string value
func TestINCRBYFLOATString(t *testing.T) {
db := setUp()
n := 312.12345
key := "foo"
db.SET(key, "string value")
got, err := db.INCRBYFLOAT(key, n)
if err.Error() != "typemismatch" {
t.Errorf("INCRBYFLOAT(%q, %f) == %f, %v want 0, typemismatch",
key, n, got, err)
}
}
// Test incrementing values for given key by n for non existing key
func TestINCRBYFLOATNonExists(t *testing.T) {
db := setUp()
key := "mykey"
n := 3.40e43
got, err := db.INCRBYFLOAT(key, n)
if got != n {
t.Errorf("INCRBYFLOAT(%q, %e) == %e, %v want %e, <nil>", key, n, got,
err, n)
}
}
func TestSETEXWithinExp(t *testing.T) {
// One second before expiry time
key := "mykey"
val := "some value"
exp := 2
db := setUp()
db.SETEX(key, uint64(exp), val)
time.Sleep(time.Duration(exp-1) * time.Second)
got, err := db.GET(key)
if got != val || err != nil {
t.Errorf("GET(%q) == %q, %v want %q, <nil>", key, got, err, val)
}
}
func TestSETEXAfterExp(t *testing.T) {
// One second before expiry time
key := "mykey"
val := "some value"
exp := 1
db := setUp()
db.SETEX(key, uint64(exp), val)
time.Sleep(time.Duration(exp+1) * time.Second)
got, err := db.GET(key)
if got != "" || err.Error() != "keynotexists" {
t.Errorf("GET(%q) == %q, %v want \"\", keynotexists", key, got, err)
}
}
func TestSETEXWithZero(t *testing.T) {
// Zero as expiry time
key := "mykey"
val := "some value"
exp := 0
db := setUp()
got, err := db.SETEX(key, uint64(exp), val)
if err == nil {
t.Errorf("SETEX(%q, %d, %q) == %q, %v want \"\", badexpiry", key, exp, val, got, err)
}
}
func TestPSETEXWithinExp(t *testing.T) {
// One second before expiry time
key := "mykey"
val := "some value"
exp := 1000
db := setUp()
db.PSETEX(key, uint64(exp), val)
time.Sleep(time.Duration(exp-10) * time.Millisecond)
got, err := db.GET(key)
if got != val || err != nil {
t.Errorf("GET(%q) == %q, %v want %q, <nil>", key, got, err, val)
}
}
func TestPSETEXAfterExp(t *testing.T) {
// One second before expiry time
key := "mykey"
val := "some value"
exp := 1000
db := setUp()
db.PSETEX(key, uint64(exp), val)
time.Sleep(time.Duration(exp+10) * time.Millisecond)
got, err := db.GET(key)
if got != "" || err.Error() != "keynotexists" {
t.Errorf("GET(%q) == %q, %v want \"\", keynotexists", key, got, err)
}
}
func TestPSETEXWithZero(t *testing.T) {
// Zero as expiry time
key := "mykey"
val := "some value"
exp := 0
db := setUp()
got, err := db.PSETEX(key, uint64(exp), val)
if err == nil {
t.Errorf("PSETEX(%q, %d, %q) == %q, %v want \"\", badexpiry", key, exp, val, got, err)
}
}
// STRLEN should return length of a int
func TestSTRLENWithInt(t *testing.T) {
db := setUp()
key := "mykey"
val := "12345"
db.SET(key, val)
got, err := db.STRLEN(key)
if err != nil || got != 5 {
t.Errorf("STRLEN(%q) == %d, %v want %d, <nil>", key, got, err, 5)
}
}
// STRLEN should return length of a float
func TestSTRLENWithFloat(t *testing.T) {
db := setUp()
key := "mykey"
val := "12345.54321"
db.SET(key, val)
got, err := db.STRLEN(key)
if err != nil || got != 11 {
t.Errorf("STRLEN(%q) == %d, %v want %d, <nil>", key, got, err, 11)
}
}
// STRLEN should return length of a long
func TestSTRLENWithLong(t *testing.T) {
db := setUp()
key := "mykey"
val := "12345.5432123453453463423434344652123234235"
db.SET(key, val)
got, err := db.STRLEN(key)
if err != nil || got != 43 {
t.Errorf("STRLEN(%q) == %d, %v want %d, <nil>", key, got, err, 43)
}
}
// STRLEN should return length of a string
func TestSTRLENWithStr(t *testing.T) {
db := setUp()
key := "mykey"
val := "A quick brown fox jumped over a lazy dog and broke its leg"
db.SET(key, val)
got, err := db.STRLEN(key)
if err != nil || got != 58 {
t.Errorf("STRLEN(%q) == %d, %v want %d, <nil>", key, got, err, 58)
}
}
// STRLEN should return Zero if given key is empty.
func TestSTRLENWithoutVal(t *testing.T) {
db := setUp()
key := "mykey"
val := ""
db.SET(key, val)
got, err := db.STRLEN(key)
if err != nil || got != 0 {
t.Errorf("STRLEN(%q) == %d, %v want %d, <nil>", key, got, err, 0)
}
}
// STRLEN should return 0, keynotfound error if given key does not exist.
func TestSTRLENWithoutKey(t *testing.T) {
db := setUp()
key := "mykey"
got, err := db.STRLEN(key)
if err.Error() != "keynotexists" {
t.Errorf("STRLEN(%q) == %v,%v want 0,keynotexists", key, got, err)
}
}
// TODO : Write test cases for STRLEN in type mismatch after data structs are done.
// APPEND should return length of the string after concatenating strings if key
// exists.
func TestAPPENDKeyExists(t *testing.T) {
db := setUp()
key := "mykey"
val := "Hello "
toAppend := "World"
db.SET(key, val)
got, err := db.APPEND(key, toAppend)
if err != nil || got != 11 {
t.Errorf("APPEND(%q, %q) == %d, %v want %d, <nil>", key, toAppend, got,
err, 11)
}
}
// APPEND should SET the key, return length of its value if key
// does not yet exist.
func TestAPPENDKeyNotExists(t *testing.T) {
db := setUp()
key := "mykey"
toAppend := "Hello"
got, err := db.APPEND(key, toAppend)
if err != nil || got != 5 {
t.Errorf("APPEND(%q, %q) == %d, %v want %d, <nil>", key, toAppend, got,
err, 5)
}
}
// Test APPEND for a long string
func TestAPPENDLongValue(t *testing.T) {
db := setUp()
key := "mykey"
val := `1234567891234567891234567891234567891234567891234567891234567891234567
89123456789123456789123456789123456789123456789123456789123456789123456789
123456789123456789123456789123456789123456789123456789123456789123456789
123456789123456789`
toAppend := `abcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxz
abcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxz
abcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxz
abcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxz`
db.SET(key, val)
//Length returned by APPEND includes line breaks as its multi line string
got, err := db.APPEND(key, toAppend)
if err != nil || got != 521 {
t.Errorf("APPEND(%q, %q) == %d, %v want %d, <nil>", key, toAppend, got,
err, 521)
}
}
// Use GET to check the APPENDed values
func TestAPPENDGetValues(t *testing.T) {
db := setUp()
key := "mykey"
val := "Hello "
toAppend := "World"
db.SET(key, val)
db.APPEND(key, toAppend)
got, err := db.GET(key)
want := val + toAppend
if err != nil || got != want {
t.Errorf("GET(%q) == %q, %v want %q, <nil>", key, got, err, want)
}
}
// TODO : Write tests for APPEND to check typemismatch after data structs are done.