-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathredis_test.v
710 lines (665 loc) · 11.9 KB
/
redis_test.v
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
module redis
fn setup() Redis {
redis := connect(ConnOpts{}) or { panic(err) }
return redis
}
fn cleanup(mut redis Redis) {
redis.flushall()
redis.disconnect()
}
fn test_auth() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.auth('', '') == false
}
fn test_set() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 0', '123') == true
assert redis.set('test 0', '456') == true
}
fn test_set_opts() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set_opts('test 5', '123', SetOpts{
ex: 2
}) == true
assert redis.set_opts('test 5', '456', SetOpts{
px: 2000
xx: true
}) == true
assert redis.set_opts('test 5', '789', SetOpts{
px: 1000
nx: true
}) == false
// assert redis.set_opts('test 5', '012', SetOpts{ keep_ttl: true }) == true //disabled because I don't have redis >= 6 to test it
}
fn test_setex() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.setex('test 6', 2, '123') == true
}
fn test_psetex() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.psetex('test 7', 2000, '123') == true
}
fn test_setnx() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.setnx('test 8', '123') == 1
assert redis.setnx('test 8', '456') == 0
}
fn test_incrby() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 20', '100') == true
r1 := redis.incrby('test 20', 4) or {
assert false
return
}
assert r1 == 104
r2 := redis.incrby('test 21', 2) or {
assert false
return
}
assert r2 == 2
assert redis.set('test 23', 'nan') == true
redis.incrby('test 23', 1) or {
assert true
return
}
assert false
}
fn test_incr() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 24', '100') == true
r1 := redis.incr('test 24') or {
assert false
return
}
assert r1 == 101
r2 := redis.incr('test 25') or {
assert false
return
}
assert r2 == 1
assert redis.set('test 26', 'nan') == true
redis.incr('test 26') or {
assert true
return
}
assert false
}
fn test_decr() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 27', '100') == true
r1 := redis.decr('test 27') or {
assert false
return
}
assert r1 == 99
r2 := redis.decr('test 28') or {
assert false
return
}
assert r2 == -1
assert redis.set('test 29', 'nan') == true
redis.decr('test 29') or {
assert true
return
}
assert false
}
fn test_decrby() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 30', '100') == true
r1 := redis.decrby('test 30', 4) or {
assert false
return
}
assert r1 == 96
r2 := redis.decrby('test 31', 2) or {
assert false
return
}
assert r2 == -2
assert redis.set('test 32', 'nan') == true
redis.decrby('test 32', 1) or {
assert true
return
}
assert false
}
fn test_incrbyfloat() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 33', '3.1415') == true
r1 := redis.incrbyfloat('test 33', 3.1415) or {
assert false
return
}
assert r1 == 6.283
r2 := redis.incrbyfloat('test 34', 3.14) or {
assert false
return
}
assert r2 == 3.14
r3 := redis.incrbyfloat('test 34', -3.14) or {
assert false
return
}
assert r3 == 0
assert redis.set('test 35', 'nan') == true
redis.incrbyfloat('test 35', 1.5) or {
assert true
return
}
assert false
}
fn test_append() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 48', 'bac') == true
r1 := redis.append('test 48', 'on') or {
assert false
return
}
assert r1 == 5
r2 := redis.get('test 48') or {
assert false
return
}
assert r2 == 'bacon'
}
fn test_lpush() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r := redis.lpush('test 53', 'item 1') or {
assert false
return
}
assert r == 1
}
fn test_rpush() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r := redis.rpush('test 59', 'item 1') or {
assert false
return
}
assert r == 1
}
fn test_setrange() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.setrange('test 52', 0, 'bac') or {
assert false
return
}
assert r1 == 3
r2 := redis.setrange('test 52', 3, 'on') or {
assert false
return
}
assert r2 == 5
}
fn test_expire() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.expire('test 10', 2) or {
assert false
return
}
assert r1 == 0
assert redis.set('test 10', '123') == true
r2 := redis.expire('test 10', 2) or {
assert false
return
}
assert r2 == 1
}
fn test_pexpire() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.pexpire('test 11', 200) or {
assert false
return
}
assert r1 == 0
assert redis.set('test 11', '123') == true
r2 := redis.pexpire('test 11', 200) or {
assert false
return
}
assert r2 == 1
}
fn test_expireat() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.expireat('test 12', 1293840000) or {
assert false
return
}
assert r1 == 0
assert redis.set('test 12', '123') == true
r2 := redis.expireat('test 12', 1293840000) or {
assert false
return
}
assert r2 == 1
}
fn test_pexpireat() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.pexpireat('test 13', 1555555555005) or {
assert false
return
}
assert r1 == 0
assert redis.set('test 13', '123') == true
r2 := redis.pexpireat('test 13', 1555555555005) or {
assert false
return
}
assert r2 == 1
}
fn test_persist() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.persist('test 46') or {
assert false
return
}
assert r1 == 0
assert redis.setex('test 46', 2, '123') == true
r2 := redis.persist('test 46') or {
assert false
return
}
assert r2 == 1
}
fn test_get() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 2', '123') == true
r := redis.get('test 2') or {
assert false
return
}
assert r == '123'
assert helper_get_key_not_found(mut redis, 'test 3') == true
}
fn test_getset() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.getset('test 36', '10') or {
assert false
return
}
assert r1 == ''
r2 := redis.getset('test 36', '15') or {
assert false
return
}
assert r2 == '10'
r3 := redis.get('test 36') or {
assert false
return
}
assert r3 == '15'
}
fn test_getrange() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 50', 'community') == true
r1 := redis.getrange('test 50', 4, -1) or {
assert false
return
}
assert r1 == 'unity'
r2 := redis.getrange('test 51', 0, -1) or {
assert false
return
}
assert r2 == ''
}
fn test_randomkey() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert helper_randomkey_database_empty(mut redis) == true
assert redis.set('test 47', '123') == true
r2 := redis.randomkey() or {
assert false
return
}
assert r2 == 'test 47'
assert helper_get_key_not_found(mut redis, 'test 3') == true
}
fn test_strlen() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 49', 'bacon') == true
r1 := redis.strlen('test 49') or {
assert false
return
}
assert r1 == 5
r2 := redis.strlen('test 50') or {
assert false
return
}
assert r2 == 0
}
fn test_lpop() {
mut redis := setup()
defer {
cleanup(mut redis)
}
redis.lpush('test 54', '123') or {
assert false
return
}
r1 := redis.lpop('test 54') or {
assert false
return
}
assert r1 == '123'
assert helper_lpop_key_not_found(mut redis, 'test 55') == true
}
fn test_rpop() {
mut redis := setup()
defer {
cleanup(mut redis)
}
redis.lpush('test 60', '123') or {
assert false
return
}
r1 := redis.rpop('test 60') or {
assert false
return
}
assert r1 == '123'
assert helper_rpop_key_not_found(mut redis, 'test 61') == true
}
fn test_llen() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.lpush('test 56', '123') or {
assert false
return
}
r2 := redis.llen('test 56') or {
assert false
return
}
assert r2 == r1
r3 := redis.llen('test 57') or {
assert false
return
}
assert r3 == 0
assert redis.set('test 58', 'not a list') == true
redis.llen('test 58') or {
assert true
return
}
assert false
}
fn test_ttl() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.setex('test 14', 15, '123') == true
r1 := redis.ttl('test 14') or {
assert false
return
}
assert r1 == 15
assert redis.set('test 15', '123') == true
r2 := redis.ttl('test 15') or {
assert false
return
}
assert r2 == -1
r3 := redis.ttl('test 16') or {
assert false
return
}
assert r3 == -2
}
fn test_pttl() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.psetex('test 17', 1500, '123') == true
r1 := redis.pttl('test 17') or {
assert false
return
}
assert r1 >= 1490 && r1 <= 1500
assert redis.set('test 18', '123') == true
r2 := redis.pttl('test 18') or {
assert false
return
}
assert r2 == -1
r3 := redis.pttl('test 19') or {
assert false
return
}
assert r3 == -2
}
fn test_exists() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.exists('test 37') or {
assert false
return
}
assert r1 == 0
assert redis.set('test 38', '123') == true
r2 := redis.exists('test 38') or {
assert false
return
}
assert r2 == 1
}
fn test_type_of() {
mut redis := setup()
defer {
cleanup(mut redis)
}
r1 := redis.type_of('test 39') or {
assert false
return
}
assert r1 == .t_none
assert redis.set('test 40', '123') == true
r2 := redis.type_of('test 40') or {
assert false
return
}
assert r2 == .t_string
}
fn test_del() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 4', '123') == true
c := redis.del('test 4') or {
assert false
return
}
assert c == 1
assert helper_get_key_not_found(mut redis, 'test 4') == true
}
fn test_rename() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.rename('test 41', 'test 42') == false
assert redis.set('test 41', 'will be 42') == true
assert redis.rename('test 41', 'test 42') == true
r := redis.get('test 42') or {
assert false
return
}
assert r == 'will be 42'
}
fn test_renamenx() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 45', '123') == true
assert helper_renamenx_err_helper(mut redis, 'test 43', 'test 44') == 'no such key'
assert redis.set('test 43', 'will be 44') == true
r1 := redis.renamenx('test 43', 'test 44') or {
assert false
return
}
assert r1 == 1
r2 := redis.get('test 44') or {
assert false
return
}
assert r2 == 'will be 44'
r3 := redis.renamenx('test 44', 'test 45') or {
assert false
return
}
assert r3 == 0
}
fn test_flushall() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.set('test 9', '123') == true
assert redis.flushall() == true
assert helper_get_key_not_found(mut redis, 'test 9') == true
}
fn test_ping() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.ping() == true
}
fn test_select() {
mut redis := setup()
defer {
cleanup(mut redis)
}
assert redis.select_db(0) == true
assert redis.select_db(15) == true
assert redis.select_db(-1) == false
assert redis.select_db(16) == false
}
fn helper_get_key_not_found(mut redis Redis, key string) bool {
redis.get(key) or {
if err.msg() == 'key not found' {
return true
} else {
return false
}
}
return false
}
fn helper_randomkey_database_empty(mut redis Redis) bool {
redis.randomkey() or {
if err.msg() == 'database is empty' {
return true
} else {
return false
}
}
return false
}
fn helper_renamenx_err_helper(mut redis Redis, key string, newkey string) string {
redis.renamenx(key, newkey) or { return err.msg() }
return ''
}
fn helper_lpop_key_not_found(mut redis Redis, key string) bool {
redis.lpop(key) or {
if err.msg() == 'key not found' {
return true
} else {
return false
}
}
return false
}
fn helper_rpop_key_not_found(mut redis Redis, key string) bool {
redis.rpop(key) or {
if err.msg() == 'key not found' {
return true
} else {
return false
}
}
return false
}