mock_redis 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE +19 -0
  5. data/README.md +88 -0
  6. data/Rakefile +10 -0
  7. data/lib/mock_redis/assertions.rb +13 -0
  8. data/lib/mock_redis/database.rb +432 -0
  9. data/lib/mock_redis/exceptions.rb +3 -0
  10. data/lib/mock_redis/expire_wrapper.rb +25 -0
  11. data/lib/mock_redis/hash_methods.rb +102 -0
  12. data/lib/mock_redis/list_methods.rb +187 -0
  13. data/lib/mock_redis/multi_db_wrapper.rb +48 -0
  14. data/lib/mock_redis/set_methods.rb +125 -0
  15. data/lib/mock_redis/string_methods.rb +195 -0
  16. data/lib/mock_redis/transaction_wrapper.rb +75 -0
  17. data/lib/mock_redis/undef_redis_methods.rb +11 -0
  18. data/lib/mock_redis/utility_methods.rb +22 -0
  19. data/lib/mock_redis/version.rb +3 -0
  20. data/lib/mock_redis/zset.rb +80 -0
  21. data/lib/mock_redis/zset_methods.rb +210 -0
  22. data/lib/mock_redis.rb +32 -0
  23. data/mock_redis.gemspec +24 -0
  24. data/spec/cloning_spec.rb +96 -0
  25. data/spec/commands/append_spec.rb +24 -0
  26. data/spec/commands/auth_spec.rb +7 -0
  27. data/spec/commands/bgrewriteaof_spec.rb +7 -0
  28. data/spec/commands/bgsave_spec.rb +7 -0
  29. data/spec/commands/blpop_spec.rb +55 -0
  30. data/spec/commands/brpop_spec.rb +54 -0
  31. data/spec/commands/brpoplpush_spec.rb +53 -0
  32. data/spec/commands/dbsize_spec.rb +18 -0
  33. data/spec/commands/decr_spec.rb +34 -0
  34. data/spec/commands/decrby_spec.rb +34 -0
  35. data/spec/commands/del_spec.rb +20 -0
  36. data/spec/commands/echo_spec.rb +11 -0
  37. data/spec/commands/exists_spec.rb +14 -0
  38. data/spec/commands/expire_spec.rb +83 -0
  39. data/spec/commands/expireat_spec.rb +48 -0
  40. data/spec/commands/flushall_spec.rb +38 -0
  41. data/spec/commands/flushdb_spec.rb +38 -0
  42. data/spec/commands/get_spec.rb +23 -0
  43. data/spec/commands/getbit_spec.rb +34 -0
  44. data/spec/commands/getrange_spec.rb +22 -0
  45. data/spec/commands/getset_spec.rb +23 -0
  46. data/spec/commands/hdel_spec.rb +35 -0
  47. data/spec/commands/hexists_spec.rb +22 -0
  48. data/spec/commands/hget_spec.rb +23 -0
  49. data/spec/commands/hgetall_spec.rb +22 -0
  50. data/spec/commands/hincrby_spec.rb +52 -0
  51. data/spec/commands/hkeys_spec.rb +19 -0
  52. data/spec/commands/hlen_spec.rb +19 -0
  53. data/spec/commands/hmget_spec.rb +30 -0
  54. data/spec/commands/hmset_spec.rb +43 -0
  55. data/spec/commands/hset_spec.rb +23 -0
  56. data/spec/commands/hsetnx_spec.rb +39 -0
  57. data/spec/commands/hvals_spec.rb +19 -0
  58. data/spec/commands/incr_spec.rb +34 -0
  59. data/spec/commands/incrby_spec.rb +44 -0
  60. data/spec/commands/info_spec.rb +13 -0
  61. data/spec/commands/keys_spec.rb +87 -0
  62. data/spec/commands/lastsave_spec.rb +8 -0
  63. data/spec/commands/lindex_spec.rb +39 -0
  64. data/spec/commands/linsert_spec.rb +68 -0
  65. data/spec/commands/llen_spec.rb +16 -0
  66. data/spec/commands/lpop_spec.rb +34 -0
  67. data/spec/commands/lpush_spec.rb +30 -0
  68. data/spec/commands/lpushx_spec.rb +33 -0
  69. data/spec/commands/lrange_spec.rb +35 -0
  70. data/spec/commands/lrem_spec.rb +79 -0
  71. data/spec/commands/lset_spec.rb +38 -0
  72. data/spec/commands/ltrim_spec.rb +35 -0
  73. data/spec/commands/mget_spec.rb +34 -0
  74. data/spec/commands/mset_spec.rb +29 -0
  75. data/spec/commands/msetnx_spec.rb +40 -0
  76. data/spec/commands/persist_spec.rb +49 -0
  77. data/spec/commands/ping_spec.rb +7 -0
  78. data/spec/commands/quit_spec.rb +7 -0
  79. data/spec/commands/randomkey_spec.rb +20 -0
  80. data/spec/commands/rename_spec.rb +31 -0
  81. data/spec/commands/renamenx_spec.rb +36 -0
  82. data/spec/commands/rpop_spec.rb +34 -0
  83. data/spec/commands/rpoplpush_spec.rb +45 -0
  84. data/spec/commands/rpush_spec.rb +30 -0
  85. data/spec/commands/rpushx_spec.rb +33 -0
  86. data/spec/commands/sadd_spec.rb +22 -0
  87. data/spec/commands/save_spec.rb +7 -0
  88. data/spec/commands/scard_spec.rb +18 -0
  89. data/spec/commands/sdiff_spec.rb +47 -0
  90. data/spec/commands/sdiffstore_spec.rb +51 -0
  91. data/spec/commands/select_spec.rb +53 -0
  92. data/spec/commands/set_spec.rb +7 -0
  93. data/spec/commands/setbit_spec.rb +46 -0
  94. data/spec/commands/setex_spec.rb +22 -0
  95. data/spec/commands/setnx_spec.rb +25 -0
  96. data/spec/commands/setrange_spec.rb +30 -0
  97. data/spec/commands/sinter_spec.rb +41 -0
  98. data/spec/commands/sinterstore_spec.rb +46 -0
  99. data/spec/commands/sismember_spec.rb +29 -0
  100. data/spec/commands/smembers_spec.rb +18 -0
  101. data/spec/commands/smove_spec.rb +41 -0
  102. data/spec/commands/spop_spec.rb +25 -0
  103. data/spec/commands/srandmember_spec.rb +25 -0
  104. data/spec/commands/srem_spec.rb +35 -0
  105. data/spec/commands/strlen_spec.rb +19 -0
  106. data/spec/commands/sunion_spec.rb +40 -0
  107. data/spec/commands/sunionstore_spec.rb +46 -0
  108. data/spec/commands/ttl_spec.rb +36 -0
  109. data/spec/commands/type_spec.rb +36 -0
  110. data/spec/commands/unwatch_spec.rb +7 -0
  111. data/spec/commands/watch_spec.rb +7 -0
  112. data/spec/commands/zadd_spec.rb +29 -0
  113. data/spec/commands/zcard_spec.rb +19 -0
  114. data/spec/commands/zcount_spec.rb +23 -0
  115. data/spec/commands/zincrby_spec.rb +24 -0
  116. data/spec/commands/zinterstore_spec.rb +88 -0
  117. data/spec/commands/zrange_spec.rb +31 -0
  118. data/spec/commands/zrangebyscore_spec.rb +42 -0
  119. data/spec/commands/zrank_spec.rb +23 -0
  120. data/spec/commands/zrem_spec.rb +25 -0
  121. data/spec/commands/zremrangebyrank_spec.rb +22 -0
  122. data/spec/commands/zremrangebyscore_spec.rb +21 -0
  123. data/spec/commands/zrevrange_spec.rb +31 -0
  124. data/spec/commands/zrevrangebyscore_spec.rb +42 -0
  125. data/spec/commands/zrevrank_spec.rb +23 -0
  126. data/spec/commands/zscore_spec.rb +16 -0
  127. data/spec/commands/zunionstore_spec.rb +96 -0
  128. data/spec/spec_helper.rb +44 -0
  129. data/spec/support/redis_multiplexer.rb +91 -0
  130. data/spec/support/shared_examples/only_operates_on_hashes.rb +13 -0
  131. data/spec/support/shared_examples/only_operates_on_lists.rb +13 -0
  132. data/spec/support/shared_examples/only_operates_on_sets.rb +13 -0
  133. data/spec/support/shared_examples/only_operates_on_strings.rb +13 -0
  134. data/spec/support/shared_examples/only_operates_on_zsets.rb +57 -0
  135. data/spec/transactions_spec.rb +73 -0
  136. metadata +358 -0
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#ttl(key)" do
4
+ before do
5
+ @key = 'mock-redis-test:persist'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns -1 for a key with no expiration" do
10
+ @redises.ttl(@key).should == -1
11
+ end
12
+
13
+ it "returns -1 for a key that does not exist" do
14
+ @redises.ttl('mock-redis-test:nonesuch').should == -1
15
+ end
16
+
17
+ context "[mock only]" do
18
+ # These are mock-only since we can't actually manipulate time in
19
+ # the real Redis.
20
+
21
+ before(:all) do
22
+ @mock = @redises.mock
23
+ end
24
+
25
+ before do
26
+ @now = Time.now
27
+ Time.stub!(:now).and_return(@now)
28
+ end
29
+
30
+ it "gives you the key's remaining lifespan in seconds" do
31
+ @mock.expire(@key, 5)
32
+ @mock.ttl(@key).should == 4
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#type(key)" do
4
+ before do
5
+ @key = 'mock-redis-test:type'
6
+ end
7
+
8
+ it "returns 'none' for no key" do
9
+ @redises.type(@key).should == 'none'
10
+ end
11
+
12
+ it "returns 'string' for a string" do
13
+ @redises.set(@key, 'stringlish')
14
+ @redises.type(@key).should == 'string'
15
+ end
16
+
17
+ it "returns 'list' for a list" do
18
+ @redises.lpush(@key, 100)
19
+ @redises.type(@key).should == 'list'
20
+ end
21
+
22
+ it "returns 'hash' for a hash" do
23
+ @redises.hset(@key, 100, 200)
24
+ @redises.type(@key).should == 'hash'
25
+ end
26
+
27
+ it "returns 'set' for a set" do
28
+ @redises.sadd(@key, 100)
29
+ @redises.type(@key).should == 'set'
30
+ end
31
+
32
+ it "returns 'zset' for a zset" do
33
+ @redises.zadd(@key, 1, 2)
34
+ @redises.type(@key).should == 'zset'
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#unwatch' do
4
+ it "responds with 'OK'" do
5
+ @redises.unwatch.should == 'OK'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#watch(key)' do
4
+ it "responds with 'OK'" do
5
+ @redises.watch('mock-redis-test').should == 'OK'
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zadd(key, score, member)" do
4
+ before { @key = 'mock-redis-test:zadd' }
5
+
6
+ it "returns true if member wasn't present in the set" do
7
+ @redises.zadd(@key, 1, 'foo').should be_true
8
+ end
9
+
10
+ it "returns false if member was present in the set" do
11
+ @redises.zadd(@key, 1, 'foo')
12
+ @redises.zadd(@key, 1, 'foo').should be_false
13
+ end
14
+
15
+ it "adds member to the set" do
16
+ @redises.zadd(@key, 1, 'foo')
17
+ @redises.zrange(@key, 0, -1).should == ['foo']
18
+ end
19
+
20
+ it "updates the score" do
21
+ @redises.zadd(@key, 1, 'foo')
22
+ @redises.zadd(@key, 2, 'foo')
23
+
24
+ @redises.zscore(@key, 'foo').should == "2"
25
+ end
26
+
27
+ it_should_behave_like "arg 1 is a score"
28
+ it_should_behave_like "a zset-only command"
29
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zcard(key)" do
4
+ before do
5
+ @key = 'mock-redis-test:zcard'
6
+ end
7
+
8
+ it "returns the number of elements in the zset" do
9
+ @redises.zadd(@key, 1, 'Washington')
10
+ @redises.zadd(@key, 2, 'Adams')
11
+ @redises.zcard(@key).should == 2
12
+ end
13
+
14
+ it "returns 0 for nonexistent sets" do
15
+ @redises.zcard(@key).should == 0
16
+ end
17
+
18
+ it_should_behave_like "a zset-only command"
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zcount(key, min, max)" do
4
+ before do
5
+ @key = 'mock-redis-test:zcount'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the number of members in the zset with scores in (min..max)" do
13
+ @redises.zcount(@key, 3, 10).should == 2
14
+ end
15
+
16
+ it "returns 0 if there are no such members" do
17
+ @redises.zcount(@key, 100, 200).should == 0
18
+ end
19
+
20
+ it_should_behave_like "arg 1 is a score"
21
+ it_should_behave_like "arg 2 is a score"
22
+ it_should_behave_like "a zset-only command"
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zincrby(key, increment, member)" do
4
+ before do
5
+ @key = 'mock-redis-test:zincrby'
6
+ @redises.zadd(@key, 1, 'bert')
7
+ end
8
+
9
+ it "returns the new score as a string" do
10
+ @redises.zincrby(@key, 10, 'bert').should == "11"
11
+ end
12
+
13
+ it "updates the item's score" do
14
+ @redises.zincrby(@key, 10, 'bert')
15
+ @redises.zscore(@key, 'bert').should == "11"
16
+ end
17
+
18
+ it "adds missing members with score increment" do
19
+ @redises.zincrby(@key, 5.5, 'bigbird').should == "5.5"
20
+ end
21
+
22
+ it_should_behave_like "arg 1 is a score"
23
+ it_should_behave_like "a zset-only command"
24
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zinterstore(destination, keys, [:weights => [w,w,], [:aggregate => :sum|:min|:max])" do
4
+ before do
5
+ @odds = 'mock-redis-test:zinterstore:odds'
6
+ @primes = 'mock-redis-test:zinterstore:primes'
7
+ @dest = 'mock-redis-test:zinterstore:dest'
8
+
9
+ @redises.zadd(@odds, 1, 'one')
10
+ @redises.zadd(@odds, 3, 'three')
11
+ @redises.zadd(@odds, 5, 'five')
12
+ @redises.zadd(@odds, 7, 'seven')
13
+ @redises.zadd(@odds, 9, 'nine')
14
+
15
+ @redises.zadd(@primes, 2, 'two')
16
+ @redises.zadd(@primes, 3, 'three')
17
+ @redises.zadd(@primes, 5, 'five')
18
+ @redises.zadd(@primes, 7, 'seven')
19
+ end
20
+
21
+ it "returns the number of elements in the new set" do
22
+ @redises.zinterstore(@dest, [@odds, @primes]).should == 3
23
+ end
24
+
25
+ it "sums the members' scores by default" do
26
+ @redises.zinterstore(@dest, [@odds, @primes])
27
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
28
+ %w[three 6 five 10 seven 14]
29
+ end
30
+
31
+ it "raises an error if keys is empty" do
32
+ lambda do
33
+ @redises.zinterstore(@dest, [])
34
+ end.should raise_error(RuntimeError)
35
+ end
36
+
37
+ context "the :weights argument" do
38
+ it "multiplies the scores by the weights while aggregating" do
39
+ @redises.zinterstore(@dest, [@odds, @primes], :weights => [2, 3])
40
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
41
+ %w[three 15 five 25 seven 35]
42
+ end
43
+
44
+ it "raises an error if the number of weights != the number of keys" do
45
+ lambda do
46
+ @redises.zinterstore(@dest, [@odds, @primes], :weights => [1,2,3])
47
+ end.should raise_error(RuntimeError)
48
+ end
49
+ end
50
+
51
+ context "the :aggregate argument" do
52
+ before do
53
+ @smalls = 'mock-redis-test:zinterstore:smalls'
54
+ @bigs = 'mock-redis-test:zinterstore:bigs'
55
+
56
+ @redises.zadd(@smalls, 1, 'bert')
57
+ @redises.zadd(@smalls, 2, 'ernie')
58
+ @redises.zadd(@bigs, 100, 'bert')
59
+ @redises.zadd(@bigs, 200, 'ernie')
60
+ end
61
+
62
+ it "aggregates scores with min when :aggregate => :min is specified" do
63
+ @redises.zinterstore(@dest, [@bigs, @smalls], :aggregate => :min)
64
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
65
+ %w[bert 1 ernie 2]
66
+ end
67
+
68
+ it "aggregates scores with max when :aggregate => :max is specified" do
69
+ @redises.zinterstore(@dest, [@bigs, @smalls], :aggregate => :max)
70
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
71
+ %w[bert 100 ernie 200]
72
+ end
73
+
74
+ it "allows 'min', 'MIN', etc. as aliases for :min" do
75
+ @redises.zinterstore(@dest, [@bigs, @smalls], :aggregate => 'min')
76
+ @redises.zscore(@dest, 'bert').should == '1'
77
+
78
+ @redises.zinterstore(@dest, [@bigs, @smalls], :aggregate => 'MIN')
79
+ @redises.zscore(@dest, 'bert').should == '1'
80
+ end
81
+
82
+ it "raises an error for unknown aggregation function" do
83
+ lambda do
84
+ @redises.zinterstore(@dest, [@bigs, @smalls], :aggregate => :mix)
85
+ end.should raise_error(RuntimeError)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrange(key, start, stop [, :with_scores => true])" do
4
+ before do
5
+ @key = 'mock-redis-test:zrange'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the elements in order by score" do
13
+ @redises.zrange(@key, 0, 1).should == ['Washington', 'Adams']
14
+ end
15
+
16
+ it "returns the elements in order by score (negative indices)" do
17
+ @redises.zrange(@key, -2, -1).should == ['Jefferson', 'Madison']
18
+ end
19
+
20
+ it "returns the scores when :with_scores is specified" do
21
+ @redises.zrange(@key, 0, 1, :with_scores => true).
22
+ should == ["Washington", "1", "Adams", "2"]
23
+ end
24
+
25
+ it "returns the scores when :withscores is specified" do
26
+ @redises.zrange(@key, 0, 1, :withscores => true).
27
+ should == ["Washington", "1", "Adams", "2"]
28
+ end
29
+
30
+ it_should_behave_like "a zset-only command"
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrangebyscore(key, start, stop [:with_scores => true] [:limit => [offset count]])" do
4
+ before do
5
+ @key = 'mock-redis-test:zrangebyscore'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the elements in order by score" do
13
+ @redises.zrangebyscore(@key, 1, 2).should == ['Washington', 'Adams']
14
+ end
15
+
16
+ it "returns the scores when :with_scores is specified" do
17
+ @redises.zrangebyscore(@key, 1, 2, :with_scores => true).
18
+ should == ["Washington", "1", "Adams", "2"]
19
+ end
20
+
21
+ it "returns the scores when :withscores is specified" do
22
+ @redises.zrangebyscore(@key, 1, 2, :withscores => true).
23
+ should == ["Washington", "1", "Adams", "2"]
24
+ end
25
+
26
+ it "honors the :limit => [offset count] argument" do
27
+ @redises.zrangebyscore(@key, -100, 100, :limit => [1, 2]).
28
+ should == ["Adams", "Jefferson"]
29
+ end
30
+
31
+ it "raises an error if :limit isn't a 2-tuple" do
32
+ lambda do
33
+ @redises.zrangebyscore(@key, -100, 100, :limit => [1, 2, 3])
34
+ end.should raise_error(RuntimeError)
35
+
36
+ lambda do
37
+ @redises.zrangebyscore(@key, -100, 100, :limit => "1, 2")
38
+ end.should raise_error(RuntimeError)
39
+ end
40
+
41
+ it_should_behave_like "a zset-only command"
42
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrank(key, member)" do
4
+ before do
5
+ @key = 'mock-redis-test:zrank'
6
+
7
+ @redises.zadd(@key, 1, 'one')
8
+ @redises.zadd(@key, 2, 'two')
9
+ @redises.zadd(@key, 3, 'three')
10
+ end
11
+
12
+ it "returns nil if member wasn't present in the set" do
13
+ @redises.zrank(@key, 'foo').should be_nil
14
+ end
15
+
16
+ it "returns the index of the member in the set" do
17
+ @redises.zrank(@key, 'one').should == 0
18
+ @redises.zrank(@key, 'two').should == 1
19
+ @redises.zrank(@key, 'three').should == 2
20
+ end
21
+
22
+ it_should_behave_like "a zset-only command"
23
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrem(key, member)" do
4
+ before do
5
+ @key = 'mock-redis-test:zrem'
6
+
7
+ @redises.zadd(@key, 1, 'one')
8
+ @redises.zadd(@key, 2, 'two')
9
+ end
10
+
11
+ it "returns true if member is present in the set" do
12
+ @redises.zrem(@key, 'one').should be_true
13
+ end
14
+
15
+ it "returns false if member is not present in the set" do
16
+ @redises.zrem(@key, 'nobody home').should be_false
17
+ end
18
+
19
+ it "removes member from the set" do
20
+ @redises.zrem(@key, 'one')
21
+ @redises.zrange(@key, 0, -1).should == ['two']
22
+ end
23
+
24
+ it_should_behave_like "a zset-only command"
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zremrangebyrank(key, start, stop)" do
4
+ before do
5
+ @key = 'mock-redis-test:zremrangebyrank'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the number of elements in range" do
13
+ @redises.zremrangebyrank(@key, 2, 3).should == 2
14
+ end
15
+
16
+ it "removes the elements" do
17
+ @redises.zremrangebyrank(@key, 2, 3)
18
+ @redises.zrange(@key, 0, -1).should == %w[Washington Adams]
19
+ end
20
+
21
+ it_should_behave_like "a zset-only command"
22
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zremrangebyscore(key, min, max)" do
4
+ before do
5
+ @key = 'mock-redis-test:zremrangebyscore'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the number of elements in range" do
13
+ @redises.zremrangebyscore(@key, 2, 3).should == 2
14
+ end
15
+
16
+ it "removes the elements" do
17
+ @redises.zremrangebyscore(@key, 2, 3)
18
+ @redises.zrange(@key, 0, -1).should == %w[Washington Madison]
19
+ end
20
+ it_should_behave_like "a zset-only command"
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrevrange(key, start, stop [, :with_scores => true])" do
4
+ before do
5
+ @key = 'mock-redis-test:zrevrange'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the elements in order by score" do
13
+ @redises.zrevrange(@key, 0, 1).should == ['Madison', 'Jefferson']
14
+ end
15
+
16
+ it "returns the elements in order by score (negative indices)" do
17
+ @redises.zrevrange(@key, -2, -1).should == ['Adams', 'Washington']
18
+ end
19
+
20
+ it "returns the scores when :with_scores is specified" do
21
+ @redises.zrevrange(@key, 2, 3, :with_scores => true).
22
+ should == ["Adams", "2", "Washington", "1"]
23
+ end
24
+
25
+ it "returns the scores when :withscores is specified" do
26
+ @redises.zrevrange(@key, 2, 3, :withscores => true).
27
+ should == ["Adams", "2", "Washington", "1"]
28
+ end
29
+
30
+ it_should_behave_like "a zset-only command"
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrevrangebyscore(key, start, stop [:with_scores => true] [:limit => [offset count]])" do
4
+ before do
5
+ @key = 'mock-redis-test:zrevrangebyscore'
6
+ @redises.zadd(@key, 1, 'Washington')
7
+ @redises.zadd(@key, 2, 'Adams')
8
+ @redises.zadd(@key, 3, 'Jefferson')
9
+ @redises.zadd(@key, 4, 'Madison')
10
+ end
11
+
12
+ it "returns the elements in order by score" do
13
+ @redises.zrevrangebyscore(@key, 4, 3).should == ['Madison', 'Jefferson']
14
+ end
15
+
16
+ it "returns the scores when :with_scores is specified" do
17
+ @redises.zrevrangebyscore(@key, 4, 3, :with_scores => true).
18
+ should == %w[Madison 4 Jefferson 3]
19
+ end
20
+
21
+ it "returns the scores when :withscores is specified" do
22
+ @redises.zrevrangebyscore(@key, 4, 3, :withscores => true).
23
+ should == %w[Madison 4 Jefferson 3]
24
+ end
25
+
26
+ it "honors the :limit => [offset count] argument" do
27
+ @redises.zrevrangebyscore(@key, 100, -100, :limit => [1, 2]).
28
+ should == ["Jefferson", "Adams"]
29
+ end
30
+
31
+ it "raises an error if :limit isn't a 2-tuple" do
32
+ lambda do
33
+ @redises.zrevrangebyscore(@key, 100, -100, :limit => [1, 2, 3])
34
+ end.should raise_error(RuntimeError)
35
+
36
+ lambda do
37
+ @redises.zrevrangebyscore(@key, 100, -100, :limit => "1, 2")
38
+ end.should raise_error(RuntimeError)
39
+ end
40
+
41
+ it_should_behave_like "a zset-only command"
42
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zrevrank(key, member)" do
4
+ before do
5
+ @key = 'mock-redis-test:zrevrank'
6
+
7
+ @redises.zadd(@key, 1, 'one')
8
+ @redises.zadd(@key, 2, 'two')
9
+ @redises.zadd(@key, 3, 'three')
10
+ end
11
+
12
+ it "returns nil if member wasn't present in the set" do
13
+ @redises.zrevrank(@key, 'foo').should be_nil
14
+ end
15
+
16
+ it "returns the index of the member in the set (ordered by -score)" do
17
+ @redises.zrevrank(@key, 'one').should == 2
18
+ @redises.zrevrank(@key, 'two').should == 1
19
+ @redises.zrevrank(@key, 'three').should == 0
20
+ end
21
+
22
+ it_should_behave_like "a zset-only command"
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zscore(key, member)" do
4
+ before { @key = 'mock-redis-test:zscore' }
5
+
6
+ it "returns the score as a string" do
7
+ @redises.zadd(@key, 0.25, 'foo').should be_true
8
+ @redises.zscore(@key, 'foo').should == "0.25"
9
+ end
10
+
11
+ it "returns nil if member is not present in the set" do
12
+ @redises.zscore(@key, 'foo').should be_nil
13
+ end
14
+
15
+ it_should_behave_like "a zset-only command"
16
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#zunionstore(destination, keys, [:weights => [w,w,], [:aggregate => :sum|:min|:max])" do
4
+ before do
5
+ @set1 = 'mock-redis-test:zunionstore1'
6
+ @set2 = 'mock-redis-test:zunionstore2'
7
+ @set3 = 'mock-redis-test:zunionstore3'
8
+ @dest = 'mock-redis-test:zunionstoredest'
9
+
10
+ @redises.zadd(@set1, 1, 'one')
11
+
12
+ @redises.zadd(@set2, 1, 'one')
13
+ @redises.zadd(@set2, 2, 'two')
14
+
15
+ @redises.zadd(@set3, 1, 'one')
16
+ @redises.zadd(@set3, 2, 'two')
17
+ @redises.zadd(@set3, 3, 'three')
18
+ end
19
+
20
+ it "returns the number of elements in the new set" do
21
+ @redises.zunionstore(@dest, [@set1, @set2, @set3]).should == 3
22
+ end
23
+
24
+ it "sums the members' scores by default" do
25
+ @redises.zunionstore(@dest, [@set1, @set2, @set3])
26
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
27
+ %w[one 3 three 3 two 4]
28
+ end
29
+
30
+ it "raises an error if keys is empty" do
31
+ lambda do
32
+ @redises.zunionstore(@dest, [])
33
+ end.should raise_error(RuntimeError)
34
+ end
35
+
36
+ context "the :weights argument" do
37
+ it "multiplies the scores by the weights while aggregating" do
38
+ @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [2, 3, 5])
39
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
40
+ %w[one 10 three 15 two 16]
41
+ end
42
+
43
+ it "raises an error if the number of weights != the number of keys" do
44
+ lambda do
45
+ @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [1,2])
46
+ end.should raise_error(RuntimeError)
47
+ end
48
+ end
49
+
50
+ context "the :aggregate argument" do
51
+ before do
52
+ @smalls = 'mock-redis-test:zunionstore:smalls'
53
+ @bigs = 'mock-redis-test:zunionstore:bigs'
54
+
55
+ @redises.zadd(@smalls, 1, 'bert')
56
+ @redises.zadd(@smalls, 2, 'ernie')
57
+ @redises.zadd(@bigs, 100, 'bert')
58
+ @redises.zadd(@bigs, 200, 'ernie')
59
+ end
60
+
61
+ it "aggregates scores with min when :aggregate => :min is specified" do
62
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
63
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
64
+ %w[bert 1 ernie 2]
65
+ end
66
+
67
+ it "aggregates scores with max when :aggregate => :max is specified" do
68
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
69
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
70
+ %w[bert 100 ernie 200]
71
+ end
72
+
73
+ it "ignores scores for missing members" do
74
+ @redises.zadd(@smalls, 3, 'grover')
75
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
76
+ @redises.zscore(@dest, 'grover').should == '3'
77
+
78
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
79
+ @redises.zscore(@dest, 'grover').should == '3'
80
+ end
81
+
82
+ it "allows 'min', 'MIN', etc. as aliases for :min" do
83
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'min')
84
+ @redises.zscore(@dest, 'bert').should == '1'
85
+
86
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'MIN')
87
+ @redises.zscore(@dest, 'bert').should == '1'
88
+ end
89
+
90
+ it "raises an error for unknown aggregation function" do
91
+ lambda do
92
+ @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :mix)
93
+ end.should raise_error(RuntimeError)
94
+ end
95
+ end
96
+ end