forked from jamesls/fakeredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fakeredis.py
1649 lines (1388 loc) · 64.2 KB
/
test_fakeredis.py
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
from time import sleep, time
from redis.exceptions import ResponseError
import unittest2 as unittest
import inspect
from functools import wraps
from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr
import redis
import redis.client
import fakeredis
from datetime import datetime, timedelta
def redis_must_be_running(cls):
# This can probably be improved. This will determines
# at import time if the tests should be run, but we probably
# want it to be when the tests are actually run.
try:
r = redis.StrictRedis('localhost', port=6379)
r.ping()
except redis.ConnectionError:
redis_running = False
else:
redis_running = True
if not redis_running:
for name, attr in inspect.getmembers(cls):
if name.startswith('test_'):
@wraps(attr)
def skip_test(*args, **kwargs):
raise SkipTest("Redis is not running.")
setattr(cls, name, skip_test)
cls.setUp = lambda x: None
cls.tearDown = lambda x: None
return cls
class TestFakeStrictRedis(unittest.TestCase):
def setUp(self):
self.redis = self.create_redis()
def tearDown(self):
self.redis.flushall()
del self.redis
def create_redis(self, db=0):
return fakeredis.FakeStrictRedis(db=db)
def test_flushdb(self):
self.redis.set('foo', 'bar')
self.assertEqual(self.redis.keys(), ['foo'])
self.assertEqual(self.redis.flushdb(), True)
self.assertEqual(self.redis.keys(), [])
def test_set_then_get(self):
self.assertEqual(self.redis.set('foo', 'bar'), True)
self.assertEqual(self.redis.get('foo'), 'bar')
def test_get_does_not_exist(self):
self.assertEqual(self.redis.get('foo'), None)
def test_get_with_non_str_keys(self):
self.assertEqual(self.redis.set('2', 'bar'), True)
self.assertEqual(self.redis.get(2), 'bar')
def test_set_non_str_keys(self):
self.assertEqual(self.redis.set(2, 'bar'), True)
self.assertEqual(self.redis.get(2), 'bar')
self.assertEqual(self.redis.get('2'), 'bar')
def test_getbit(self):
self.redis.setbit('foo', 3, 1)
self.assertEqual(self.redis.getbit('foo', 0), 0)
self.assertEqual(self.redis.getbit('foo', 1), 0)
self.assertEqual(self.redis.getbit('foo', 2), 0)
self.assertEqual(self.redis.getbit('foo', 3), 1)
self.assertEqual(self.redis.getbit('foo', 4), 0)
self.assertEqual(self.redis.getbit('foo', 100), 0)
def test_multiple_bits_set(self):
self.redis.setbit('foo', 1, 1)
self.redis.setbit('foo', 3, 1)
self.redis.setbit('foo', 5, 1)
self.assertEqual(self.redis.getbit('foo', 0), 0)
self.assertEqual(self.redis.getbit('foo', 1), 1)
self.assertEqual(self.redis.getbit('foo', 2), 0)
self.assertEqual(self.redis.getbit('foo', 3), 1)
self.assertEqual(self.redis.getbit('foo', 4), 0)
self.assertEqual(self.redis.getbit('foo', 5), 1)
self.assertEqual(self.redis.getbit('foo', 6), 0)
def test_unset_bits(self):
self.redis.setbit('foo', 1, 1)
self.redis.setbit('foo', 2, 0)
self.redis.setbit('foo', 3, 1)
self.assertEqual(self.redis.getbit('foo', 1), 1)
self.redis.setbit('foo', 1, 0)
self.assertEqual(self.redis.getbit('foo', 1), 0)
self.redis.setbit('foo', 3, 0)
self.assertEqual(self.redis.getbit('foo', 3), 0)
def test_setbits_and_getkeys(self):
# The bit operations and the get commands
# should play nicely with each other.
self.redis.setbit('foo', 1, 1)
self.assertEqual(self.redis.get('foo'), '@')
self.redis.setbit('foo', 2, 1)
self.assertEqual(self.redis.get('foo'), '`')
self.redis.setbit('foo', 3, 1)
self.assertEqual(self.redis.get('foo'), 'p')
self.redis.setbit('foo', 9, 1)
self.assertEqual(self.redis.get('foo'), 'p@')
self.redis.setbit('foo', 54, 1)
self.assertEqual(self.redis.get('foo'), 'p@\x00\x00\x00\x00\x02')
def test_bitcount(self):
self.redis.delete('foo')
self.assertEqual(self.redis.bitcount('foo'), 0)
self.redis.setbit('foo', 1, 1)
self.assertEqual(self.redis.bitcount('foo'), 1)
self.redis.setbit('foo', 8, 1)
self.assertEqual(self.redis.bitcount('foo'), 2)
self.assertEqual(self.redis.bitcount('foo', 1, 1), 1)
self.redis.setbit('foo', 57, 1)
self.assertEqual(self.redis.bitcount('foo'), 3)
self.redis.set('foo', ' ')
self.assertEqual(self.redis.bitcount('foo'), 1)
def test_getset_not_exist(self):
val = self.redis.getset('foo', 'bar')
self.assertEqual(val, None)
self.assertEqual(self.redis.get('foo'), 'bar')
def test_getset_exists(self):
self.redis.set('foo', 'bar')
val = self.redis.getset('foo', 'baz')
self.assertEqual(val, 'bar')
def test_setitem_getitem(self):
self.assertEqual(self.redis.keys(), [])
self.redis['foo'] = 'bar'
self.assertEqual(self.redis['foo'], 'bar')
def test_strlen(self):
self.redis['foo'] = 'bar'
self.assertEqual(self.redis.strlen('foo'), 3)
self.assertEqual(self.redis.strlen('noexists'), 0)
def test_substr(self):
self.redis['foo'] = 'one_two_three'
self.assertEqual(self.redis.substr('foo', 0), 'one_two_three')
self.assertEqual(self.redis.substr('foo', 0, 2), 'one')
self.assertEqual(self.redis.substr('foo', 4, 6), 'two')
self.assertEqual(self.redis.substr('foo', -5), 'three')
def test_substr_noexist_key(self):
self.assertEqual(self.redis.substr('foo', 0), '')
self.assertEqual(self.redis.substr('foo', 10), '')
self.assertEqual(self.redis.substr('foo', -5, -1), '')
def test_append(self):
self.assertTrue(self.redis.set('foo', 'bar'))
self.assertEqual(self.redis.append('foo', 'baz'), 6)
self.assertEqual(self.redis.get('foo'), 'barbaz')
def test_incr_with_no_preexisting_key(self):
self.assertEqual(self.redis.incr('foo'), 1)
self.assertEqual(self.redis.incr('bar', 2), 2)
def test_incr_preexisting_key(self):
self.redis.set('foo', 15)
self.assertEqual(self.redis.incr('foo', 5), 20)
self.assertEqual(self.redis.get('foo'), '20')
def test_incr_bad_type(self):
self.redis.set('foo', 'bar')
with self.assertRaises(redis.ResponseError):
self.redis.incr('foo', 15)
def test_decr(self):
self.redis.set('foo', 10)
self.assertEqual(self.redis.decr('foo'), 9)
self.assertEqual(self.redis.get('foo'), '9')
def test_decr_newkey(self):
self.redis.decr('foo')
self.assertEqual(self.redis.get('foo'), '-1')
def test_decr_badtype(self):
self.redis.set('foo', 'bar')
with self.assertRaises(redis.ResponseError):
self.redis.decr('foo', 15)
def test_exists(self):
self.assertFalse('foo' in self.redis)
self.redis.set('foo', 'bar')
self.assertTrue('foo' in self.redis)
def test_contains(self):
self.assertFalse(self.redis.exists('foo'))
self.redis.set('foo', 'bar')
self.assertTrue(self.redis.exists('foo'))
def test_rename(self):
self.redis.set('foo', 'unique value')
self.assertTrue(self.redis.rename('foo', 'bar'))
self.assertEqual(self.redis.get('foo'), None)
self.assertEqual(self.redis.get('bar'), 'unique value')
def test_rename_nonexistent_key(self):
with self.assertRaises(redis.ResponseError):
self.redis.rename('foo', 'bar')
def test_renamenx_doesnt_exist(self):
self.redis.set('foo', 'unique value')
self.assertTrue(self.redis.renamenx('foo', 'bar'))
self.assertEqual(self.redis.get('foo'), None)
self.assertEqual(self.redis.get('bar'), 'unique value')
def test_rename_does_exist(self):
self.redis.set('foo', 'unique value')
self.redis.set('bar', 'unique value2')
self.assertFalse(self.redis.renamenx('foo', 'bar'))
self.assertEqual(self.redis.get('foo'), 'unique value')
self.assertEqual(self.redis.get('bar'), 'unique value2')
def test_mget(self):
self.redis.set('foo', 'one')
self.redis.set('bar', 'two')
self.assertEqual(self.redis.mget(['foo', 'bar']), ['one', 'two'])
self.assertEqual(self.redis.mget(['foo', 'bar', 'baz']),
['one', 'two', None])
self.assertEqual(self.redis.mget('foo', 'bar'), ['one', 'two'])
self.assertEqual(self.redis.mget('foo', 'bar', None),
['one', 'two', None])
def test_mset(self):
self.assertEqual(self.redis.mset({'foo': 'one', 'bar': 'two'}), True)
self.assertEqual(self.redis.mset({'foo': 'one', 'bar': 'two'}), True)
self.assertEqual(self.redis.mget('foo', 'bar'), ['one', 'two'])
def test_msetnx(self):
self.assertEqual(self.redis.msetnx({'foo': 'one', 'bar': 'two'}),
True)
self.assertEqual(self.redis.msetnx({'bar': 'two', 'baz': 'three'}),
False)
self.assertEqual(self.redis.mget('foo', 'bar', 'baz'),
['one', 'two', None])
def test_setex(self):
self.assertEqual(self.redis.setex('foo', 100, 'bar'), True)
self.assertEqual(self.redis.get('foo'), 'bar')
def test_setex_using_timedelta(self):
self.assertEqual(self.redis.setex('foo', timedelta(seconds=100), 'bar'), True)
self.assertEqual(self.redis.get('foo'), 'bar')
def test_setnx(self):
self.assertEqual(self.redis.setnx('foo', 'bar'), True)
self.assertEqual(self.redis.get('foo'), 'bar')
self.assertEqual(self.redis.setnx('foo', 'baz'), False)
self.assertEqual(self.redis.get('foo'), 'bar')
def test_delete(self):
self.redis['foo'] = 'bar'
self.assertEqual(self.redis.delete('foo'), True)
self.assertEqual(self.redis.get('foo'), None)
def test_delete_multiple(self):
self.redis['one'] = 'one'
self.redis['two'] = 'two'
self.redis['three'] = 'three'
# Since redis>=2.7.6 returns number of deleted items.
self.assertEqual(self.redis.delete('one', 'two'), 2)
self.assertEqual(self.redis.get('one'), None)
self.assertEqual(self.redis.get('two'), None)
self.assertEqual(self.redis.get('three'), 'three')
self.assertEqual(self.redis.delete('one', 'two'), False)
# If any keys are deleted, True is returned.
self.assertEqual(self.redis.delete('two', 'three'), True)
self.assertEqual(self.redis.get('three'), None)
def test_delete_nonexistent_key(self):
self.assertEqual(self.redis.delete('foo'), False)
## Tests for the list type.
def test_rpush_then_lrange_with_nested_list1(self):
self.assertEqual(self.redis.rpush('foo', [12345L, 6789L]), 1)
self.assertEqual(self.redis.rpush('foo', [54321L, 9876L]), 2)
self.assertEqual(self.redis.lrange(
'foo', 0, -1), ['[12345L, 6789L]', '[54321L, 9876L]'])
self.redis.flushall()
def test_rpush_then_lrange_with_nested_list2(self):
self.assertEqual(self.redis.rpush('foo', [12345L, 'banana']), 1)
self.assertEqual(self.redis.rpush('foo', [54321L, 'elephant']), 2)
self.assertEqual(self.redis.lrange(
'foo', 0, -1), ['[12345L, \'banana\']', '[54321L, \'elephant\']'])
self.redis.flushall()
def test_rpush_then_lrange_with_nested_list3(self):
self.assertEqual(self.redis.rpush('foo', [12345L, []]), 1)
self.assertEqual(self.redis.rpush('foo', [54321L, []]), 2)
self.assertEqual(self.redis.lrange(
'foo', 0, -1), ['[12345L, []]', '[54321L, []]'])
self.redis.flushall()
def test_lpush_then_lrange_all(self):
self.assertEqual(self.redis.lpush('foo', 'bar'), 1)
self.assertEqual(self.redis.lpush('foo', 'baz'), 2)
self.assertEqual(self.redis.lpush('foo', 'bam', 'buzz'), 4)
self.assertEqual(self.redis.lrange('foo', 0, -1),
['buzz', 'bam', 'baz', 'bar'])
def test_lpush_then_lrange_portion(self):
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'two')
self.redis.lpush('foo', 'three')
self.redis.lpush('foo', 'four')
self.assertEqual(self.redis.lrange('foo', 0, 2),
['four', 'three', 'two'])
self.assertEqual(self.redis.lrange('foo', 0, 3),
['four', 'three', 'two', 'one'])
def test_lpush_key_does_not_exist(self):
self.assertEqual(self.redis.lrange('foo', 0, -1), [])
def test_lpush_with_nonstr_key(self):
self.redis.lpush(1, 'one')
self.redis.lpush(1, 'two')
self.redis.lpush(1, 'three')
self.assertEqual(self.redis.lrange(1, 0, 2),
['three', 'two', 'one'])
self.assertEqual(self.redis.lrange('1', 0, 2),
['three', 'two', 'one'])
def test_llen(self):
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'two')
self.redis.lpush('foo', 'three')
self.assertEqual(self.redis.llen('foo'), 3)
def test_llen_no_exist(self):
self.assertEqual(self.redis.llen('foo'), 0)
def test_lrem_postitive_count(self):
self.redis.lpush('foo', 'same')
self.redis.lpush('foo', 'same')
self.redis.lpush('foo', 'different')
self.redis.lrem('foo', 2, 'same')
self.assertEqual(self.redis.lrange('foo', 0, -1), ['different'])
def test_lrem_negative_count(self):
self.redis.lpush('foo', 'removeme')
self.redis.lpush('foo', 'three')
self.redis.lpush('foo', 'two')
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'removeme')
self.redis.lrem('foo', -1, 'removeme')
# Should remove it from the end of the list,
# leaving the 'removeme' from the front of the list alone.
self.assertEqual(self.redis.lrange('foo', 0, -1),
['removeme', 'one', 'two', 'three'])
def test_lrem_zero_count(self):
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'one')
self.redis.lrem('foo', 0, 'one')
self.assertEqual(self.redis.lrange('foo', 0, -1), [])
def test_lrem_default_value(self):
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'one')
self.redis.lpush('foo', 'one')
self.redis.lrem('foo', 0, 'one')
self.assertEqual(self.redis.lrange('foo', 0, -1), [])
def test_lrem_does_not_exist(self):
self.redis.lpush('foo', 'one')
self.redis.lrem('foo', 0, 'one')
# These should be noops.
self.redis.lrem('foo', -2, 'one')
self.redis.lrem('foo', 2, 'one')
def test_lrem_return_value(self):
self.redis.lpush('foo', 'one')
count = self.redis.lrem('foo', 0, 'one')
self.assertEqual(count, 1)
self.assertEqual(self.redis.lrem('foo', 0, 'one'), 0)
def test_rpush(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('foo', 'three')
self.redis.rpush('foo', 'four', 'five')
self.assertEqual(self.redis.lrange('foo', 0, -1),
['one', 'two', 'three', 'four', 'five'])
def test_lpop(self):
self.assertEqual(self.redis.rpush('foo', 'one'), 1)
self.assertEqual(self.redis.rpush('foo', 'two'), 2)
self.assertEqual(self.redis.rpush('foo', 'three'), 3)
self.assertEqual(self.redis.lpop('foo'), 'one')
self.assertEqual(self.redis.lpop('foo'), 'two')
self.assertEqual(self.redis.lpop('foo'), 'three')
def test_lpop_empty_list(self):
self.redis.rpush('foo', 'one')
self.redis.lpop('foo')
self.assertEqual(self.redis.lpop('foo'), None)
# Verify what happens if we try to pop from a key
# we've never seen before.
self.assertEqual(self.redis.lpop('noexists'), None)
def test_lset(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('foo', 'three')
self.redis.lset('foo', 0, 'four')
self.redis.lset('foo', -2, 'five')
self.assertEqual(self.redis.lrange('foo', 0, -1),
['four', 'five', 'three'])
def test_lset_index_out_of_range(self):
self.redis.rpush('foo', 'one')
with self.assertRaises(redis.ResponseError):
self.redis.lset('foo', 3, 'three')
def test_rpushx(self):
self.redis.rpush('foo', 'one')
self.redis.rpushx('foo', 'two')
self.redis.rpushx('bar', 'three')
self.assertEqual(self.redis.lrange('foo', 0, -1), ['one', 'two'])
self.assertEqual(self.redis.lrange('bar', 0, -1), [])
def test_ltrim(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('foo', 'three')
self.redis.rpush('foo', 'four')
self.assertTrue(self.redis.ltrim('foo', 1, 3))
self.assertEqual(self.redis.lrange('foo', 0, -1), ['two', 'three',
'four'])
self.assertTrue(self.redis.ltrim('foo', 1, -1))
self.assertEqual(self.redis.lrange('foo', 0, -1), ['three', 'four'])
def test_ltrim_with_non_existent_key(self):
self.assertTrue(self.redis.ltrim('foo', 0, -1))
def test_lindex(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.assertEqual(self.redis.lindex('foo', 0), 'one')
self.assertEqual(self.redis.lindex('foo', 4), None)
self.assertEqual(self.redis.lindex('bar', 4), None)
def test_lpushx(self):
self.redis.lpush('foo', 'two')
self.redis.lpushx('foo', 'one')
self.redis.lpushx('bar', 'one')
self.assertEqual(self.redis.lrange('foo', 0, -1), ['one', 'two'])
self.assertEqual(self.redis.lrange('bar', 0, -1), [])
def test_rpop(self):
self.assertEqual(self.redis.rpop('foo'), None)
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.assertEqual(self.redis.rpop('foo'), 'two')
self.assertEqual(self.redis.rpop('foo'), 'one')
self.assertEqual(self.redis.rpop('foo'), None)
def test_linsert(self):
self.redis.rpush('foo', 'hello')
self.redis.rpush('foo', 'world')
self.redis.linsert('foo', 'before', 'world', 'there')
self.assertEqual(self.redis.lrange('foo', 0, -1),
['hello', 'there', 'world'])
def test_rpoplpush(self):
self.assertEqual(self.redis.rpoplpush('foo', 'bar'), None)
self.assertEqual(self.redis.lpop('bar'), None)
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('bar', 'one')
self.assertEqual(self.redis.rpoplpush('foo', 'bar'), 'two')
self.assertEqual(self.redis.lrange('foo', 0, -1), ['one'])
self.assertEqual(self.redis.lrange('bar', 0, -1), ['two', 'one'])
def test_rpoplpush_to_nonexistent_destination(self):
self.redis.rpush('foo', 'one')
self.assertEqual(self.redis.rpoplpush('foo', 'bar'), 'one')
self.assertEqual(self.redis.rpop('bar'), 'one')
def test_blpop_single_list(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('foo', 'three')
self.assertEqual(self.redis.blpop(['foo'], timeout=1),
('foo', 'one'))
def test_blpop_test_multiple_lists(self):
self.redis.rpush('baz', 'zero')
self.assertEqual(self.redis.blpop(['foo', 'baz'], timeout=1),
('baz', 'zero'))
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
# bar has nothing, so the returned value should come
# from foo.
self.assertEqual(self.redis.blpop(['bar', 'foo'], timeout=1),
('foo', 'one'))
self.redis.rpush('bar', 'three')
# bar now has something, so the returned value should come
# from bar.
self.assertEqual(self.redis.blpop(['bar', 'foo'], timeout=1),
('bar', 'three'))
self.assertEqual(self.redis.blpop(['bar', 'foo'], timeout=1),
('foo', 'two'))
def test_blpop_allow_single_key(self):
# blpop converts single key arguments to a one element list.
self.redis.rpush('foo', 'one')
self.assertEqual(self.redis.blpop('foo', timeout=1), ('foo', 'one'))
def test_brpop_test_multiple_lists(self):
self.redis.rpush('baz', 'zero')
self.assertEqual(self.redis.brpop(['foo', 'baz'], timeout=1),
('baz', 'zero'))
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.assertEqual(self.redis.brpop(['bar', 'foo'], timeout=1),
('foo', 'two'))
def test_brpop_single_key(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.assertEqual(self.redis.brpop('foo', timeout=1),
('foo', 'two'))
def test_brpoplpush_multi_keys(self):
self.assertEqual(self.redis.lpop('bar'), None)
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.assertEqual(self.redis.brpoplpush('foo', 'bar', timeout=1),
'two')
self.assertEqual(self.redis.lrange('bar', 0, -1), ['two'])
@attr('slow')
def test_blocking_operations_when_empty(self):
self.assertEqual(self.redis.blpop(['foo'], timeout=1),
None)
self.assertEqual(self.redis.blpop(['bar', 'foo'], timeout=1),
None)
self.assertEqual(self.redis.brpop('foo', timeout=1),
None)
self.assertEqual(self.redis.brpoplpush('foo', 'bar', timeout=1),
None)
## Tests for the hash type.
def test_hset_then_hget(self):
self.assertEqual(self.redis.hset('foo', 'key', 'value'), 1)
self.assertEqual(self.redis.hget('foo', 'key'), 'value')
def test_hset_update(self):
self.assertEqual(self.redis.hset('foo', 'key', 'value'), 1)
self.assertEqual(self.redis.hset('foo', 'key', 'value'), 0)
def test_hgetall(self):
self.assertEqual(self.redis.hset('foo', 'k1', 'v1'), 1)
self.assertEqual(self.redis.hset('foo', 'k2', 'v2'), 1)
self.assertEqual(self.redis.hset('foo', 'k3', 'v3'), 1)
self.assertEqual(self.redis.hgetall('foo'), {'k1': 'v1', 'k2': 'v2',
'k3': 'v3'})
def test_hgetall_with_tuples(self):
self.assertEqual(self.redis.hset('foo', (1, 2), (1, 2, 3)), 1)
self.assertEqual(self.redis.hgetall('foo'), {'(1, 2)': '(1, 2, 3)'})
def test_hgetall_empty_key(self):
self.assertEqual(self.redis.hgetall('foo'), {})
def test_hexists(self):
self.redis.hset('foo', 'bar', 'v1')
self.assertEqual(self.redis.hexists('foo', 'bar'), 1)
self.assertEqual(self.redis.hexists('foo', 'baz'), 0)
self.assertEqual(self.redis.hexists('bar', 'bar'), 0)
def test_hkeys(self):
self.redis.hset('foo', 'k1', 'v1')
self.redis.hset('foo', 'k2', 'v2')
self.assertEqual(set(self.redis.hkeys('foo')), set(['k1', 'k2']))
self.assertEqual(set(self.redis.hkeys('bar')), set([]))
def test_hlen(self):
self.redis.hset('foo', 'k1', 'v1')
self.redis.hset('foo', 'k2', 'v2')
self.assertEqual(self.redis.hlen('foo'), 2)
def test_hvals(self):
self.redis.hset('foo', 'k1', 'v1')
self.redis.hset('foo', 'k2', 'v2')
self.assertEqual(set(self.redis.hvals('foo')), set(['v1', 'v2']))
self.assertEqual(set(self.redis.hvals('bar')), set([]))
def test_hmget(self):
self.redis.hset('foo', 'k1', 'v1')
self.redis.hset('foo', 'k2', 'v2')
self.redis.hset('foo', 'k3', 'v3')
# Normal case.
self.assertEqual(self.redis.hmget('foo', ['k1', 'k3']), ['v1', 'v3'])
self.assertEqual(self.redis.hmget('foo', 'k1', 'k3'), ['v1', 'v3'])
# Key does not exist.
self.assertEqual(self.redis.hmget('bar', ['k1', 'k3']), [None, None])
self.assertEqual(self.redis.hmget('bar', 'k1', 'k3'), [None, None])
# Some keys in the hash do not exist.
self.assertEqual(self.redis.hmget('foo', ['k1', 'k500']),
['v1', None])
self.assertEqual(self.redis.hmget('foo', 'k1', 'k500'),
['v1', None])
def test_hdel(self):
self.redis.hset('foo', 'k1', 'v1')
self.redis.hset('foo', 'k2', 'v2')
self.redis.hset('foo', 'k3', 'v3')
self.assertEqual(self.redis.hget('foo', 'k1'), 'v1')
self.assertEqual(self.redis.hdel('foo', 'k1'), True)
self.assertEqual(self.redis.hget('foo', 'k1'), None)
self.assertEqual(self.redis.hdel('foo', 'k1'), False)
# Since redis>=2.7.6 returns number of deleted items.
self.assertEqual(self.redis.hdel('foo', 'k2', 'k3'), 2)
self.assertEqual(self.redis.hget('foo', 'k2'), None)
self.assertEqual(self.redis.hget('foo', 'k3'), None)
self.assertEqual(self.redis.hdel('foo', 'k2', 'k3'), False)
def test_hincrby(self):
self.redis.hset('foo', 'counter', 0)
self.assertEqual(self.redis.hincrby('foo', 'counter'), 1)
self.assertEqual(self.redis.hincrby('foo', 'counter'), 2)
self.assertEqual(self.redis.hincrby('foo', 'counter'), 3)
def test_hincrby_with_no_starting_value(self):
self.assertEqual(self.redis.hincrby('foo', 'counter'), 1)
self.assertEqual(self.redis.hincrby('foo', 'counter'), 2)
self.assertEqual(self.redis.hincrby('foo', 'counter'), 3)
def test_hincrby_with_range_param(self):
self.assertEqual(self.redis.hincrby('foo', 'counter', 2), 2)
self.assertEqual(self.redis.hincrby('foo', 'counter', 2), 4)
self.assertEqual(self.redis.hincrby('foo', 'counter', 2), 6)
def test_hsetnx(self):
self.assertEqual(self.redis.hsetnx('foo', 'newkey', 'v1'), True)
self.assertEqual(self.redis.hsetnx('foo', 'newkey', 'v1'), False)
self.assertEqual(self.redis.hget('foo', 'newkey'), 'v1')
def test_hmsetset_empty_raises_error(self):
with self.assertRaises(redis.DataError):
self.redis.hmset('foo', {})
def test_hmsetset(self):
self.redis.hset('foo', 'k1', 'v1')
self.assertEqual(self.redis.hmset('foo', {'k2': 'v2', 'k3': 'v3'}),
True)
def test_sadd(self):
self.assertEqual(self.redis.sadd('foo', 'member1'), 1)
self.assertEqual(self.redis.sadd('foo', 'member1'), 0)
self.assertEqual(self.redis.smembers('foo'), set(['member1']))
self.assertEqual(self.redis.sadd('foo', 'member2', 'member3'), 2)
self.assertEqual(self.redis.smembers('foo'),
set(['member1', 'member2', 'member3']))
self.assertEqual(self.redis.sadd('foo', 'member3', 'member4'), 1)
self.assertEqual(self.redis.smembers('foo'),
set(['member1', 'member2', 'member3', 'member4']))
def test_scard(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('foo', 'member2')
self.assertEqual(self.redis.scard('foo'), 2)
def test_sdiff(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sdiff('foo', 'bar'), set(['member1']))
# Original sets shouldn't be modified.
self.assertEqual(self.redis.smembers('foo'),
set(['member1', 'member2']))
self.assertEqual(self.redis.smembers('bar'),
set(['member2', 'member3']))
def test_sdiff_one_key(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.assertEqual(self.redis.sdiff('foo'), set(['member1', 'member2']))
def test_sdiff_empty(self):
self.assertEqual(self.redis.sdiff('foo'), set())
def test_sdiffstore(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sdiffstore('baz', 'foo', 'bar'), 1)
def test_sinter(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sinter('foo', 'bar'), set(['member2']))
self.assertEqual(self.redis.sinter('foo'), set(['member1', 'member2']))
def test_sinterstore(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sinterstore('baz', 'foo', 'bar'), 1)
def test_sismember(self):
self.assertEqual(self.redis.sismember('foo', 'member1'), False)
self.redis.sadd('foo', 'member1')
self.assertEqual(self.redis.sismember('foo', 'member1'), True)
def test_smembers(self):
self.assertEqual(self.redis.smembers('foo'), set())
def test_smove(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.assertEqual(self.redis.smove('foo', 'bar', 'member1'), True)
self.assertEqual(self.redis.smembers('bar'), set(['member1']))
def test_smove_non_existent_key(self):
self.assertEqual(self.redis.smove('foo', 'bar', 'member1'), False)
def test_spop(self):
# This is tricky because it pops a random element.
self.redis.sadd('foo', 'member1')
self.assertEqual(self.redis.spop('foo'), 'member1')
self.assertEqual(self.redis.spop('foo'), None)
def test_srandmember(self):
self.redis.sadd('foo', 'member1')
self.assertEqual(self.redis.srandmember('foo'), 'member1')
# Shouldn't be removed from the set.
self.assertEqual(self.redis.srandmember('foo'), 'member1')
def test_srem(self):
self.redis.sadd('foo', 'member1', 'member2', 'member3', 'member4')
self.assertEqual(self.redis.smembers('foo'),
set(['member1', 'member2', 'member3', 'member4']))
self.assertEqual(self.redis.srem('foo', 'member1'), True)
self.assertEqual(self.redis.smembers('foo'),
set(['member2', 'member3', 'member4']))
self.assertEqual(self.redis.srem('foo', 'member1'), False)
# Since redis>=2.7.6 returns number of deleted items.
self.assertEqual(self.redis.srem('foo', 'member2', 'member3'), 2)
self.assertEqual(self.redis.smembers('foo'), set(['member4']))
self.assertEqual(self.redis.srem('foo', 'member3', 'member4'), True)
self.assertEqual(self.redis.smembers('foo'), set([]))
self.assertEqual(self.redis.srem('foo', 'member3', 'member4'), False)
def test_sunion(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sunion('foo', 'bar'),
set(['member1', 'member2', 'member3']))
def test_sunionstore(self):
self.redis.sadd('foo', 'member1')
self.redis.sadd('foo', 'member2')
self.redis.sadd('bar', 'member2')
self.redis.sadd('bar', 'member3')
self.assertEqual(self.redis.sunionstore('baz', 'foo', 'bar'), 3)
self.assertEqual(self.redis.smembers('baz'),
set(['member1', 'member2', 'member3']))
def test_zadd(self):
self.redis.zadd('foo', four=4)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zadd('foo', 2, 'two', 1, 'one', zero=0), 3)
self.assertEqual(self.redis.zrange('foo', 0, -1),
['zero', 'one', 'two', 'three', 'four'])
self.assertEqual(self.redis.zadd('foo', 7, 'zero', one=1, five=5), 1)
self.assertEqual(self.redis.zrange('foo', 0, -1),
['one', 'two', 'three', 'four', 'five', 'zero'])
def test_zadd_uses_str(self):
self.redis.zadd('foo', 12345, (1, 2, 3))
self.assertEqual(self.redis.zrange('foo', 0, 0), ['(1, 2, 3)'])
def test_zadd_errors(self):
# The args are backwards, it should be 2, "two", so we
# expect an exception to be raised.
with self.assertRaises(redis.ResponseError):
self.redis.zadd('foo', 'two', 2)
with self.assertRaises(redis.ResponseError):
self.redis.zadd('foo', two='two')
def test_zadd_multiple(self):
self.redis.zadd('foo', 1, 'one', 2, 'two')
self.assertEqual(self.redis.zrange('foo', 0, 0),
['one'])
self.assertEqual(self.redis.zrange('foo', 1, 1),
['two'])
def test_zrange_same_score(self):
self.redis.zadd('foo', two_a=2)
self.redis.zadd('foo', two_b=2)
self.redis.zadd('foo', two_c=2)
self.redis.zadd('foo', two_d=2)
self.redis.zadd('foo', two_e=2)
self.assertEqual(self.redis.zrange('foo', 2, 3),
['two_c', 'two_d'])
def test_zcard(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.assertEqual(self.redis.zcard('foo'), 2)
def test_zcard_non_existent_key(self):
self.assertEqual(self.redis.zcard('foo'), 0)
def test_zcount(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', three=2)
self.redis.zadd('foo', five=5)
self.assertEqual(self.redis.zcount('foo', 2, 4), 1)
self.assertEqual(self.redis.zcount('foo', 1, 4), 2)
self.assertEqual(self.redis.zcount('foo', 0, 5), 3)
def test_zincrby(self):
self.redis.zadd('foo', one=1)
self.assertEqual(self.redis.zincrby('foo', 'one', 10), 11)
self.assertEqual(self.redis.zrange('foo', 0, -1, withscores=True),
[('one', 11)])
def test_zrange_descending(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrange('foo', 0, -1, desc=True),
['three', 'two', 'one'])
def test_zrange_descending_with_scores(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrange('foo', 0, -1, desc=True,
withscores=True),
[('three', 3), ('two', 2), ('one', 1)])
def test_zrange_with_positive_indices(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrange('foo', 0, 1), ['one', 'two'])
def test_zrank(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrank('foo', 'one'), 0)
self.assertEqual(self.redis.zrank('foo', 'two'), 1)
self.assertEqual(self.redis.zrank('foo', 'three'), 2)
def test_zrank_non_existent_member(self):
self.assertEqual(self.redis.zrank('foo', 'one'), None)
def test_zrem(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.redis.zadd('foo', four=4)
self.assertEqual(self.redis.zrem('foo', 'one'), True)
self.assertEqual(self.redis.zrange('foo', 0, -1),
['two', 'three', 'four'])
# Since redis>=2.7.6 returns number of deleted items.
self.assertEqual(self.redis.zrem('foo', 'two', 'three'), 2)
self.assertEqual(self.redis.zrange('foo', 0, -1), ['four'])
self.assertEqual(self.redis.zrem('foo', 'three', 'four'), True)
self.assertEqual(self.redis.zrange('foo', 0, -1), [])
self.assertEqual(self.redis.zrem('foo', 'three', 'four'), False)
def test_zrem_non_existent_member(self):
self.assertFalse(self.redis.zrem('foo', 'one'))
def test_zscore(self):
self.redis.zadd('foo', one=54)
self.assertEqual(self.redis.zscore('foo', 'one'), 54)
def test_zscore_non_existent_member(self):
self.assertIsNone(self.redis.zscore('foo', 'one'))
def test_zrevrank(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrevrank('foo', 'one'), 2)
self.assertEqual(self.redis.zrevrank('foo', 'two'), 1)
self.assertEqual(self.redis.zrevrank('foo', 'three'), 0)
def test_zrevrank_non_existent_member(self):
self.assertEqual(self.redis.zrevrank('foo', 'one'), None)
def test_zrevrange(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrevrange('foo', 0, 1), ['three', 'two'])
self.assertEqual(self.redis.zrevrange('foo', 0, -1),
['three', 'two', 'one'])
def test_zrangebyscore(self):
self.redis.zadd('foo', zero=0)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', two_a_also=2)
self.redis.zadd('foo', two_b_also=2)
self.redis.zadd('foo', four=4)
self.assertEqual(self.redis.zrangebyscore('foo', 1, 3),
['two', 'two_a_also', 'two_b_also'])
self.assertEqual(self.redis.zrangebyscore('foo', 2, 3),
['two', 'two_a_also', 'two_b_also'])
self.assertEqual(self.redis.zrangebyscore('foo', 0, 4),
['zero', 'two', 'two_a_also', 'two_b_also', 'four'])
def test_zrangebyscore_slice(self):
self.redis.zadd('foo', two_a=2)
self.redis.zadd('foo', two_b=2)
self.redis.zadd('foo', two_c=2)
self.redis.zadd('foo', two_d=2)
self.assertEqual(self.redis.zrangebyscore('foo', 0, 4, 0, 2),
['two_a', 'two_b'])
self.assertEqual(self.redis.zrangebyscore('foo', 0, 4, 1, 3),
['two_b', 'two_c', 'two_d'])
def test_zrangebyscore_withscores(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrangebyscore('foo', 1, 3, 0, 2, True),
[('one', 1), ('two', 2)])
def test_zrevrangebyscore(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrevrangebyscore('foo', 3, 1),
['three', 'two', 'one'])
self.assertEqual(self.redis.zrevrangebyscore('foo', 3, 2),
['three', 'two'])
self.assertEqual(self.redis.zrevrangebyscore('foo', 3, 1, 0, 1),
['three'])
self.assertEqual(self.redis.zrevrangebyscore('foo', 3, 1, 1, 2),
['two', 'one'])
def test_zremrangebyrank(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zremrangebyrank('foo', 0, 1), 2)
self.assertEqual(self.redis.zrange('foo', 0, -1), ['three'])
def test_zremrangebyrank_negative_indices(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zremrangebyrank('foo', -2, -1), 2)
self.assertEqual(self.redis.zrange('foo', 0, -1), ['one'])
def test_zremrangebyrank_out_of_bounds(self):
self.redis.zadd('foo', one=1)
self.assertEqual(self.redis.zremrangebyrank('foo', 1, 3), 0)
def test_zremrangebyscore(self):
self.redis.zadd('foo', zero=0)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', four=4)
# Outside of range.
self.assertEqual(self.redis.zremrangebyscore('foo', 5, 10), 0)
self.assertEqual(self.redis.zrange('foo', 0, -1),
['zero', 'two', 'four'])
# Middle of range.