mock_redis 0.5.4 → 0.31.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (201) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/lint.yml +31 -0
  3. data/.github/workflows/tests.yml +63 -0
  4. data/.gitignore +1 -1
  5. data/.overcommit.yml +21 -0
  6. data/.rspec +1 -1
  7. data/.rubocop.yml +148 -0
  8. data/.rubocop_todo.yml +35 -0
  9. data/.simplecov +4 -0
  10. data/CHANGELOG.md +278 -0
  11. data/Gemfile +9 -5
  12. data/LICENSE.md +21 -0
  13. data/README.md +52 -16
  14. data/Rakefile +0 -8
  15. data/lib/mock_redis/assertions.rb +0 -1
  16. data/lib/mock_redis/connection_method.rb +13 -0
  17. data/lib/mock_redis/database.rb +193 -257
  18. data/lib/mock_redis/expire_wrapper.rb +2 -2
  19. data/lib/mock_redis/future.rb +23 -0
  20. data/lib/mock_redis/geospatial_methods.rb +240 -0
  21. data/lib/mock_redis/hash_methods.rb +83 -24
  22. data/lib/mock_redis/indifferent_hash.rb +11 -0
  23. data/lib/mock_redis/info_method.rb +160 -0
  24. data/lib/mock_redis/list_methods.rb +34 -19
  25. data/lib/mock_redis/multi_db_wrapper.rb +8 -7
  26. data/lib/mock_redis/pipelined_wrapper.rb +42 -16
  27. data/lib/mock_redis/set_methods.rb +62 -19
  28. data/lib/mock_redis/sort_method.rb +81 -0
  29. data/lib/mock_redis/stream/id.rb +58 -0
  30. data/lib/mock_redis/stream.rb +88 -0
  31. data/lib/mock_redis/stream_methods.rb +102 -0
  32. data/lib/mock_redis/string_methods.rb +235 -42
  33. data/lib/mock_redis/transaction_wrapper.rb +62 -28
  34. data/lib/mock_redis/utility_methods.rb +62 -11
  35. data/lib/mock_redis/version.rb +4 -1
  36. data/lib/mock_redis/zset.rb +24 -29
  37. data/lib/mock_redis/zset_methods.rb +187 -59
  38. data/lib/mock_redis.rb +77 -27
  39. data/mock_redis.gemspec +23 -15
  40. data/spec/client_spec.rb +29 -0
  41. data/spec/cloning_spec.rb +17 -18
  42. data/spec/commands/append_spec.rb +4 -4
  43. data/spec/commands/auth_spec.rb +1 -1
  44. data/spec/commands/bgrewriteaof_spec.rb +2 -2
  45. data/spec/commands/bgsave_spec.rb +2 -2
  46. data/spec/commands/bitcount_spec.rb +25 -0
  47. data/spec/commands/bitfield_spec.rb +169 -0
  48. data/spec/commands/blpop_spec.rb +19 -21
  49. data/spec/commands/brpop_spec.rb +25 -20
  50. data/spec/commands/brpoplpush_spec.rb +16 -17
  51. data/spec/commands/connected_spec.rb +7 -0
  52. data/spec/commands/connection_spec.rb +15 -0
  53. data/spec/commands/dbsize_spec.rb +3 -3
  54. data/spec/commands/decr_spec.rb +8 -8
  55. data/spec/commands/decrby_spec.rb +8 -8
  56. data/spec/commands/del_spec.rb +35 -3
  57. data/spec/commands/disconnect_spec.rb +7 -0
  58. data/spec/commands/dump_spec.rb +19 -0
  59. data/spec/commands/echo_spec.rb +4 -4
  60. data/spec/commands/eval_spec.rb +7 -0
  61. data/spec/commands/evalsha_spec.rb +10 -0
  62. data/spec/commands/exists_spec.rb +36 -7
  63. data/spec/commands/expire_spec.rb +48 -20
  64. data/spec/commands/expireat_spec.rb +12 -13
  65. data/spec/commands/flushall_spec.rb +5 -5
  66. data/spec/commands/flushdb_spec.rb +5 -5
  67. data/spec/commands/future_spec.rb +30 -0
  68. data/spec/commands/geoadd_spec.rb +58 -0
  69. data/spec/commands/geodist_spec.rb +118 -0
  70. data/spec/commands/geohash_spec.rb +52 -0
  71. data/spec/commands/geopos_spec.rb +55 -0
  72. data/spec/commands/get_spec.rb +14 -6
  73. data/spec/commands/getbit_spec.rb +7 -7
  74. data/spec/commands/getrange_spec.rb +9 -9
  75. data/spec/commands/getset_spec.rb +7 -7
  76. data/spec/commands/hdel_spec.rb +41 -11
  77. data/spec/commands/hexists_spec.rb +11 -11
  78. data/spec/commands/hget_spec.rb +7 -7
  79. data/spec/commands/hgetall_spec.rb +15 -5
  80. data/spec/commands/hincrby_spec.rb +16 -16
  81. data/spec/commands/hincrbyfloat_spec.rb +58 -0
  82. data/spec/commands/hkeys_spec.rb +5 -5
  83. data/spec/commands/hlen_spec.rb +5 -5
  84. data/spec/commands/hmget_spec.rb +19 -9
  85. data/spec/commands/hmset_spec.rb +38 -12
  86. data/spec/commands/hscan_each_spec.rb +48 -0
  87. data/spec/commands/hscan_spec.rb +27 -0
  88. data/spec/commands/hset_spec.rb +26 -12
  89. data/spec/commands/hsetnx_spec.rb +16 -16
  90. data/spec/commands/hvals_spec.rb +5 -5
  91. data/spec/commands/incr_spec.rb +8 -8
  92. data/spec/commands/incrby_spec.rb +13 -13
  93. data/spec/commands/incrbyfloat_spec.rb +13 -13
  94. data/spec/commands/info_spec.rb +54 -5
  95. data/spec/commands/keys_spec.rb +83 -31
  96. data/spec/commands/lastsave_spec.rb +2 -2
  97. data/spec/commands/lindex_spec.rb +20 -10
  98. data/spec/commands/linsert_spec.rb +14 -14
  99. data/spec/commands/llen_spec.rb +4 -4
  100. data/spec/commands/lpop_spec.rb +6 -6
  101. data/spec/commands/lpush_spec.rb +21 -15
  102. data/spec/commands/lpushx_spec.rb +24 -11
  103. data/spec/commands/lrange_spec.rb +24 -8
  104. data/spec/commands/lrem_spec.rb +16 -16
  105. data/spec/commands/lset_spec.rb +17 -12
  106. data/spec/commands/ltrim_spec.rb +17 -7
  107. data/spec/commands/mapped_hmget_spec.rb +13 -9
  108. data/spec/commands/mapped_hmset_spec.rb +12 -12
  109. data/spec/commands/mapped_mget_spec.rb +22 -0
  110. data/spec/commands/mapped_mset_spec.rb +19 -0
  111. data/spec/commands/mapped_msetnx_spec.rb +26 -0
  112. data/spec/commands/mget_spec.rb +48 -17
  113. data/spec/commands/move_spec.rb +37 -37
  114. data/spec/commands/mset_spec.rb +20 -6
  115. data/spec/commands/msetnx_spec.rb +14 -14
  116. data/spec/commands/persist_spec.rb +15 -16
  117. data/spec/commands/pexpire_spec.rb +86 -0
  118. data/spec/commands/pexpireat_spec.rb +48 -0
  119. data/spec/commands/ping_spec.rb +6 -2
  120. data/spec/commands/pipelined_spec.rb +98 -7
  121. data/spec/commands/pttl_spec.rb +41 -0
  122. data/spec/commands/randomkey_spec.rb +3 -3
  123. data/spec/commands/rename_spec.rb +16 -12
  124. data/spec/commands/renamenx_spec.rb +13 -15
  125. data/spec/commands/restore_spec.rb +47 -0
  126. data/spec/commands/rpop_spec.rb +6 -6
  127. data/spec/commands/rpoplpush_spec.rb +13 -8
  128. data/spec/commands/rpush_spec.rb +21 -15
  129. data/spec/commands/rpushx_spec.rb +24 -11
  130. data/spec/commands/sadd_spec.rb +14 -10
  131. data/spec/commands/scan_each_spec.rb +39 -0
  132. data/spec/commands/scan_spec.rb +64 -0
  133. data/spec/commands/scard_spec.rb +3 -3
  134. data/spec/commands/script_spec.rb +9 -0
  135. data/spec/commands/sdiff_spec.rb +13 -13
  136. data/spec/commands/sdiffstore_spec.rb +13 -13
  137. data/spec/commands/select_spec.rb +13 -5
  138. data/spec/commands/set_spec.rb +112 -0
  139. data/spec/commands/setbit_spec.rb +25 -16
  140. data/spec/commands/setex_spec.rb +20 -4
  141. data/spec/commands/setnx_spec.rb +6 -6
  142. data/spec/commands/setrange_spec.rb +12 -12
  143. data/spec/commands/sinter_spec.rb +11 -13
  144. data/spec/commands/sinterstore_spec.rb +12 -12
  145. data/spec/commands/sismember_spec.rb +10 -10
  146. data/spec/commands/smembers_spec.rb +15 -5
  147. data/spec/commands/smove_spec.rb +13 -13
  148. data/spec/commands/sort_list_spec.rb +21 -0
  149. data/spec/commands/sort_set_spec.rb +21 -0
  150. data/spec/commands/sort_zset_spec.rb +21 -0
  151. data/spec/commands/spop_spec.rb +19 -4
  152. data/spec/commands/srandmember_spec.rb +28 -4
  153. data/spec/commands/srem_spec.rb +17 -12
  154. data/spec/commands/sscan_each_spec.rb +48 -0
  155. data/spec/commands/sscan_spec.rb +39 -0
  156. data/spec/commands/strlen_spec.rb +4 -5
  157. data/spec/commands/sunion_spec.rb +13 -11
  158. data/spec/commands/sunionstore_spec.rb +12 -12
  159. data/spec/commands/ttl_spec.rb +11 -6
  160. data/spec/commands/type_spec.rb +1 -1
  161. data/spec/commands/watch_spec.rb +9 -4
  162. data/spec/commands/xadd_spec.rb +122 -0
  163. data/spec/commands/xlen_spec.rb +22 -0
  164. data/spec/commands/xrange_spec.rb +164 -0
  165. data/spec/commands/xread_spec.rb +66 -0
  166. data/spec/commands/xrevrange_spec.rb +130 -0
  167. data/spec/commands/xtrim_spec.rb +36 -0
  168. data/spec/commands/zadd_spec.rb +100 -11
  169. data/spec/commands/zcard_spec.rb +4 -4
  170. data/spec/commands/zcount_spec.rb +18 -10
  171. data/spec/commands/zincrby_spec.rb +6 -6
  172. data/spec/commands/zinterstore_spec.rb +54 -20
  173. data/spec/commands/zpopmax_spec.rb +60 -0
  174. data/spec/commands/zpopmin_spec.rb +60 -0
  175. data/spec/commands/zrange_spec.rb +54 -13
  176. data/spec/commands/zrangebyscore_spec.rb +42 -27
  177. data/spec/commands/zrank_spec.rb +4 -4
  178. data/spec/commands/zrem_spec.rb +18 -12
  179. data/spec/commands/zremrangebyrank_spec.rb +5 -5
  180. data/spec/commands/zremrangebyscore_spec.rb +12 -5
  181. data/spec/commands/zrevrange_spec.rb +35 -10
  182. data/spec/commands/zrevrangebyscore_spec.rb +26 -15
  183. data/spec/commands/zrevrank_spec.rb +4 -4
  184. data/spec/commands/zscan_each_spec.rb +48 -0
  185. data/spec/commands/zscan_spec.rb +26 -0
  186. data/spec/commands/zscore_spec.rb +7 -7
  187. data/spec/commands/zunionstore_spec.rb +54 -21
  188. data/spec/mock_redis_spec.rb +61 -0
  189. data/spec/spec_helper.rb +35 -8
  190. data/spec/support/redis_multiplexer.rb +62 -37
  191. data/spec/support/shared_examples/does_not_cleanup_empty_strings.rb +14 -0
  192. data/spec/support/shared_examples/only_operates_on_hashes.rb +5 -3
  193. data/spec/support/shared_examples/only_operates_on_lists.rb +5 -3
  194. data/spec/support/shared_examples/only_operates_on_sets.rb +5 -3
  195. data/spec/support/shared_examples/only_operates_on_strings.rb +4 -4
  196. data/spec/support/shared_examples/only_operates_on_zsets.rb +18 -16
  197. data/spec/support/shared_examples/sorts_enumerables.rb +56 -0
  198. data/spec/transactions_spec.rb +79 -29
  199. metadata +162 -42
  200. data/LICENSE +0 -19
  201. data/spec/commands/hash_operator_spec.rb +0 -21
@@ -5,34 +5,38 @@ describe '#rename(key, newkey)' do
5
5
  @key = 'mock-redis-test:rename:key'
6
6
  @newkey = 'mock-redis-test:rename:newkey'
7
7
 
8
- @redises.set(@key, "oof")
8
+ @redises.set(@key, 'oof')
9
9
  end
10
10
 
11
- it "responds with 'OK'" do
11
+ it 'responds with "OK"' do
12
12
  @redises.rename(@key, @newkey).should == 'OK'
13
13
  end
14
14
 
15
- it "moves the data" do
15
+ it 'moves the data' do
16
16
  @redises.rename(@key, @newkey)
17
- @redises.get(@newkey).should == "oof"
17
+ @redises.get(@newkey).should == 'oof'
18
18
  end
19
19
 
20
- it "raises an error when the source key is nonexistant" do
20
+ it 'raises an error when the source key is nonexistant' do
21
21
  @redises.del(@key)
22
22
  lambda do
23
23
  @redises.rename(@key, @newkey)
24
24
  end.should raise_error(Redis::CommandError)
25
25
  end
26
26
 
27
- it "raises an error when key == newkey" do
28
- lambda do
29
- @redises.rename(@key, @key)
30
- end.should raise_error(RuntimeError)
27
+ it 'responds with "OK" when key == newkey' do
28
+ @redises.rename(@key, @key).should == 'OK'
29
+ end
30
+
31
+ it 'overwrites any existing value at newkey' do
32
+ @redises.set(@newkey, 'rab')
33
+ @redises.rename(@key, @newkey)
34
+ @redises.get(@newkey).should == 'oof'
31
35
  end
32
36
 
33
- it "overwrites any existing value at newkey" do
34
- @redises.set(@newkey, "rab")
37
+ it 'keeps expiration' do
38
+ @redises.expire(@key, 1000)
35
39
  @redises.rename(@key, @newkey)
36
- @redises.get(@newkey).should == "oof"
40
+ @redises.ttl(@newkey).should be > 0
37
41
  end
38
42
  end
@@ -5,39 +5,37 @@ describe '#renamenx(key, newkey)' do
5
5
  @key = 'mock-redis-test:renamenx:key'
6
6
  @newkey = 'mock-redis-test:renamenx:newkey'
7
7
 
8
- @redises.set(@key, "oof")
8
+ @redises.set(@key, 'oof')
9
9
  end
10
10
 
11
- it "responds with true when newkey does not exist" do
12
- @redises.renamenx(@key, @newkey).should be_true
11
+ it 'responds with true when newkey does not exist' do
12
+ @redises.renamenx(@key, @newkey).should == true
13
13
  end
14
14
 
15
- it "responds with false when newkey exists" do
15
+ it 'responds with false when newkey exists' do
16
16
  @redises.set(@newkey, 'monkey')
17
- @redises.renamenx(@key, @newkey).should be_false
17
+ @redises.renamenx(@key, @newkey).should == false
18
18
  end
19
19
 
20
- it "moves the data" do
20
+ it 'moves the data' do
21
21
  @redises.renamenx(@key, @newkey)
22
- @redises.get(@newkey).should == "oof"
22
+ @redises.get(@newkey).should == 'oof'
23
23
  end
24
24
 
25
- it "raises an error when the source key is nonexistant" do
25
+ it 'raises an error when the source key is nonexistant' do
26
26
  @redises.del(@key)
27
27
  lambda do
28
28
  @redises.rename(@key, @newkey)
29
29
  end.should raise_error(Redis::CommandError)
30
30
  end
31
31
 
32
- it "raises an error when key == newkey" do
33
- lambda do
34
- @redises.renamenx(@key, @key)
35
- end.should raise_error(RuntimeError)
32
+ it 'returns false when key == newkey' do
33
+ @redises.renamenx(@key, @key).should == false
36
34
  end
37
35
 
38
- it "leaves any existing value at newkey alone" do
39
- @redises.set(@newkey, "rab")
36
+ it 'leaves any existing value at newkey alone' do
37
+ @redises.set(@newkey, 'rab')
40
38
  @redises.renamenx(@key, @newkey)
41
- @redises.get(@newkey).should == "rab"
39
+ @redises.get(@newkey).should == 'rab'
42
40
  end
43
41
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#restore(key, ttl, value)' do
4
+ before do
5
+ @key = 'mock-redis-test:45794'
6
+ @src = MockRedis.new
7
+ @src.set(@key, '123')
8
+ @dumped_value = @src.dump(@key)
9
+ @dst = MockRedis.new
10
+ @now = Time.now.round
11
+ Time.stub(:now).and_return(@now)
12
+ end
13
+
14
+ it 'allows dump/restoring values between two redis instances' do
15
+ expect(@dst.restore(@key, 0, @dumped_value)).to eq('OK')
16
+ expect(@dst.get(@key)).to eq('123')
17
+ expect(@dst.pttl(@key)).to eq(-1)
18
+ end
19
+
20
+ context 'when the key being restored to already exists' do
21
+ before do
22
+ @dst.set(@key, '456')
23
+ end
24
+
25
+ it 'raises an error by default' do
26
+ expect { @dst.restore(@key, 0, @dumped_value) }.to raise_error(Redis::CommandError)
27
+ expect(@dst.get(@key)).to eq('456')
28
+ end
29
+
30
+ it 'allows replacing the key if replace==true' do
31
+ expect(@dst.restore(@key, 0, @dumped_value, replace: true)).to eq('OK')
32
+ expect(@dst.get(@key)).to eq('123')
33
+ end
34
+ end
35
+
36
+ it 'sets ttl in ms' do
37
+ @dst.restore(@key, 500, @dumped_value)
38
+ expect(@dst.pttl(@key)).to eq(500)
39
+ end
40
+
41
+ it 'can dump/restore more complex data types' do
42
+ key = 'a_hash'
43
+ @src.mapped_hmset(key, foo: 'bar')
44
+ @dst.restore(key, 0, @src.dump(key))
45
+ expect(@dst.hgetall(key)).to eq('foo' => 'bar')
46
+ end
47
+ end
@@ -3,32 +3,32 @@ require 'spec_helper'
3
3
  describe '#rpop(key)' do
4
4
  before { @key = 'mock-redis-test:43093' }
5
5
 
6
- it "returns and removes the first element of a list" do
6
+ it 'returns and removes the first element of a list' do
7
7
  @redises.lpush(@key, 1)
8
8
  @redises.lpush(@key, 2)
9
9
 
10
- @redises.rpop(@key).should == "1"
10
+ @redises.rpop(@key).should == '1'
11
11
 
12
12
  @redises.llen(@key).should == 1
13
13
  end
14
14
 
15
- it "returns nil if the list is empty" do
15
+ it 'returns nil if the list is empty' do
16
16
  @redises.lpush(@key, 'foo')
17
17
  @redises.rpop(@key)
18
18
 
19
19
  @redises.rpop(@key).should be_nil
20
20
  end
21
21
 
22
- it "returns nil for nonexistent values" do
22
+ it 'returns nil for nonexistent values' do
23
23
  @redises.rpop(@key).should be_nil
24
24
  end
25
25
 
26
- it "removes empty lists" do
26
+ it 'removes empty lists' do
27
27
  @redises.lpush(@key, 'foo')
28
28
  @redises.rpop(@key)
29
29
 
30
30
  @redises.get(@key).should be_nil
31
31
  end
32
32
 
33
- it_should_behave_like "a list-only command"
33
+ it_should_behave_like 'a list-only command'
34
34
  end
@@ -12,34 +12,39 @@ describe '#rpoplpush(source, destination)' do
12
12
  @redises.lpush(@list2, 'x')
13
13
  end
14
14
 
15
- it "returns the value moved" do
16
- @redises.rpoplpush(@list1, @list2).should == "b"
15
+ it 'returns the value moved' do
16
+ @redises.rpoplpush(@list1, @list2).should == 'b'
17
17
  end
18
18
 
19
- it "takes the last element of destination and prepends it to source" do
19
+ it "returns false and doesn't append if source empty" do
20
+ @redises.rpoplpush('empty', @list1).should be_nil
21
+ @redises.lrange(@list1, 0, -1).should == %w[a b]
22
+ end
23
+
24
+ it 'takes the last element of destination and prepends it to source' do
20
25
  @redises.rpoplpush(@list1, @list2)
21
26
 
22
27
  @redises.lrange(@list1, 0, -1).should == %w[a]
23
28
  @redises.lrange(@list2, 0, -1).should == %w[b x y]
24
29
  end
25
30
 
26
- it "rotates a list when source and destination are the same" do
31
+ it 'rotates a list when source and destination are the same' do
27
32
  @redises.rpoplpush(@list1, @list1)
28
33
  @redises.lrange(@list1, 0, -1).should == %w[b a]
29
34
  end
30
35
 
31
- it "removes empty lists" do
36
+ it 'removes empty lists' do
32
37
  @redises.llen(@list1).times { @redises.rpoplpush(@list1, @list2) }
33
38
  @redises.get(@list1).should be_nil
34
39
  end
35
40
 
36
- it "raises an error for non-list source value" do
41
+ it 'raises an error for non-list source value' do
37
42
  @redises.set(@list1, 'string value')
38
43
 
39
44
  lambda do
40
45
  @redises.rpoplpush(@list1, @list2)
41
- end.should raise_error(RuntimeError)
46
+ end.should raise_error(Redis::CommandError)
42
47
  end
43
48
 
44
- it_should_behave_like "a list-only command"
49
+ it_should_behave_like 'a list-only command'
45
50
  end
@@ -1,37 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#rpush(key)" do
3
+ describe '#rpush(key)' do
4
4
  before { @key = "mock-redis-test:#{__FILE__}" }
5
5
 
6
- it "returns the new size of the list" do
6
+ it 'returns the new size of the list' do
7
7
  @redises.rpush(@key, 'X').should == 1
8
8
  @redises.rpush(@key, 'X').should == 2
9
9
  end
10
10
 
11
- it "creates a new list when run against a nonexistent key" do
11
+ it 'creates a new list when run against a nonexistent key' do
12
12
  @redises.rpush(@key, 'value')
13
13
  @redises.llen(@key).should == 1
14
14
  end
15
15
 
16
- it "appends items to the list" do
17
- @redises.rpush(@key, "bert")
18
- @redises.rpush(@key, "ernie")
16
+ it 'appends items to the list' do
17
+ @redises.rpush(@key, 'bert')
18
+ @redises.rpush(@key, 'ernie')
19
19
 
20
- @redises.lindex(@key, 0).should == "bert"
21
- @redises.lindex(@key, 1).should == "ernie"
20
+ @redises.lindex(@key, 0).should == 'bert'
21
+ @redises.lindex(@key, 1).should == 'ernie'
22
22
  end
23
23
 
24
- it "stores values as strings" do
24
+ it 'stores values as strings' do
25
25
  @redises.rpush(@key, 1)
26
- @redises.lindex(@key, 0).should == "1"
26
+ @redises.lindex(@key, 0).should == '1'
27
27
  end
28
28
 
29
- it "supports a variable number of arguments" do
29
+ it 'supports a variable number of arguments' do
30
30
  @redises.rpush(@key, [1, 2, 3]).should == 3
31
- @redises.lindex(@key, 0).should == "1"
32
- @redises.lindex(@key, 1).should == "2"
33
- @redises.lindex(@key, 2).should == "3"
31
+ @redises.lindex(@key, 0).should == '1'
32
+ @redises.lindex(@key, 1).should == '2'
33
+ @redises.lindex(@key, 2).should == '3'
34
34
  end
35
35
 
36
- it_should_behave_like "a list-only command"
36
+ it 'raises an error if an empty array is given' do
37
+ lambda do
38
+ @redises.rpush(@key, [])
39
+ end.should raise_error(Redis::CommandError)
40
+ end
41
+
42
+ it_should_behave_like 'a list-only command'
37
43
  end
@@ -1,33 +1,46 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#rpushx(key, value)" do
3
+ describe '#rpushx(key, value)' do
4
4
  before { @key = 'mock-redis-test:92925' }
5
5
 
6
- it "returns the new size of the list" do
6
+ it 'returns the new size of the list' do
7
7
  @redises.lpush(@key, 'X')
8
8
 
9
9
  @redises.rpushx(@key, 'X').should == 2
10
10
  @redises.rpushx(@key, 'X').should == 3
11
11
  end
12
12
 
13
- it "does nothing when run against a nonexistent key" do
13
+ it 'does nothing when run against a nonexistent key' do
14
14
  @redises.rpushx(@key, 'value')
15
15
  @redises.get(@key).should be_nil
16
16
  end
17
17
 
18
- it "appends items to the list" do
19
- @redises.lpush(@key, "bert")
20
- @redises.rpushx(@key, "ernie")
18
+ it 'appends items to the list' do
19
+ @redises.lpush(@key, 'bert')
20
+ @redises.rpushx(@key, 'ernie')
21
21
 
22
- @redises.lindex(@key, 0).should == "bert"
23
- @redises.lindex(@key, 1).should == "ernie"
22
+ @redises.lindex(@key, 0).should == 'bert'
23
+ @redises.lindex(@key, 1).should == 'ernie'
24
24
  end
25
25
 
26
- it "stores values as strings" do
26
+ it 'stores values as strings' do
27
27
  @redises.rpush(@key, 1)
28
28
  @redises.rpushx(@key, 2)
29
- @redises.lindex(@key, 1).should == "2"
29
+ @redises.lindex(@key, 1).should == '2'
30
30
  end
31
31
 
32
- it_should_behave_like "a list-only command"
32
+ it 'raises an error if an empty array is given' do
33
+ @redises.lpush(@key, 'X')
34
+ lambda do
35
+ @redises.rpushx(@key, [])
36
+ end.should raise_error(Redis::CommandError)
37
+ end
38
+
39
+ it 'stores multiple items if an array of more than one item is given' do
40
+ @redises.lpush(@key, 'X')
41
+ @redises.rpushx(@key, [1, 2]).should == 3
42
+ @redises.lrange(@key, 0, -1).should == %w[X 1 2]
43
+ end
44
+
45
+ it_should_behave_like 'a list-only command'
33
46
  end
@@ -3,39 +3,43 @@ require 'spec_helper'
3
3
  describe '#sadd(key, member)' do
4
4
  before { @key = 'mock-redis-test:sadd' }
5
5
 
6
- it "returns true if the set did not already contain member" do
7
- @redises.sadd(@key, 1).should be_true
6
+ it 'returns true if the set did not already contain member' do
7
+ @redises.sadd(@key, 1).should == true
8
8
  end
9
9
 
10
- it "returns false if the set did already contain member" do
10
+ it 'returns false if the set did already contain member' do
11
11
  @redises.sadd(@key, 1)
12
- @redises.sadd(@key, 1).should be_false
12
+ @redises.sadd(@key, 1).should == false
13
13
  end
14
14
 
15
- it "adds member to the set" do
15
+ it 'adds member to the set' do
16
16
  @redises.sadd(@key, 1)
17
17
  @redises.sadd(@key, 2)
18
18
  @redises.smembers(@key).should == %w[2 1]
19
19
  end
20
20
 
21
21
  describe 'adding multiple members at once' do
22
-
23
- it "returns the amount of added members" do
22
+ it 'returns the amount of added members' do
24
23
  @redises.sadd(@key, [1, 2, 3]).should == 3
25
24
  @redises.sadd(@key, [1, 2, 3, 4]).should == 1
26
25
  end
27
26
 
28
- it "returns 0 if the set did already contain all members" do
27
+ it 'returns 0 if the set did already contain all members' do
29
28
  @redises.sadd(@key, [1, 2, 3])
30
29
  @redises.sadd(@key, [1, 2, 3]).should == 0
31
30
  end
32
31
 
33
- it "adds the members to the set" do
32
+ it 'adds the members to the set' do
34
33
  @redises.sadd(@key, [1, 2, 3])
35
34
  @redises.smembers(@key).should == %w[1 2 3]
36
35
  end
37
36
 
37
+ it 'raises an error if an empty array is given' do
38
+ lambda do
39
+ @redises.sadd(@key, [])
40
+ end.should raise_error(Redis::CommandError)
41
+ end
38
42
  end
39
43
 
40
- it_should_behave_like "a set-only command"
44
+ it_should_behave_like 'a set-only command'
41
45
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#scan_each' do
4
+ subject { MockRedis::Database.new(self) }
5
+
6
+ let(:match) { '*' }
7
+
8
+ before do
9
+ allow(subject).to receive_message_chain(:data, :keys).and_return(collection)
10
+ end
11
+
12
+ context 'when no keys are found' do
13
+ let(:collection) { [] }
14
+
15
+ it 'does not iterate over any elements' do
16
+ expect(subject.scan_each.to_a).to be_empty
17
+ end
18
+ end
19
+
20
+ context 'when keys are found' do
21
+ context 'when no match filter is supplied' do
22
+ let(:collection) { Array.new(20) { |i| "mock:key#{i}" } }
23
+
24
+ it 'iterates over each item in the collection' do
25
+ expect(subject.scan_each.to_a).to match_array(collection)
26
+ end
27
+ end
28
+
29
+ context 'when giving a custom match filter' do
30
+ let(:match) { 'mock:key*' }
31
+ let(:collection) { %w[mock:key mock:key2 mock:otherkey] }
32
+ let(:expected) { %w[mock:key mock:key2] }
33
+
34
+ it 'iterates over each item in the filtered collection' do
35
+ expect(subject.scan_each(match: match).to_a).to match_array(expected)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#scan' do
4
+ subject { MockRedis::Database.new(self) }
5
+
6
+ let(:count) { 10 }
7
+ let(:match) { '*' }
8
+
9
+ before do
10
+ allow(subject).to receive_message_chain(:data, :keys).and_return(collection)
11
+ end
12
+
13
+ context 'when no keys are found' do
14
+ let(:collection) { [] }
15
+
16
+ it 'returns a 0 cursor and an empty collection' do
17
+ expect(subject.scan(0, count: count, match: match)).to eq(['0', []])
18
+ end
19
+ end
20
+
21
+ context 'when keys are found' do
22
+ context 'when count is lower than collection size' do
23
+ let(:collection) { Array.new(count * 2) { |i| "mock:key#{i}" } }
24
+ let(:expected_first) { [count.to_s, collection[0...count]] }
25
+ let(:expected_second) { ['0', collection[count..-1]] }
26
+
27
+ it 'returns a the next cursor and the collection' do
28
+ expect(subject.scan(0, count: count, match: match)).to eq(expected_first)
29
+ end
30
+
31
+ it 'returns the correct results of the next cursor' do
32
+ expect(subject.scan(count, count: count, match: match)).to eq(expected_second)
33
+ end
34
+ end
35
+
36
+ context 'when count is greater or equal than collection size' do
37
+ let(:collection) { Array.new(count) { |i| "mock:key#{i}" } }
38
+ let(:expected) { ['0', collection] }
39
+
40
+ it 'returns a 0 cursor and the collection' do
41
+ expect(subject.scan(0, count: count, match: match)).to eq(expected)
42
+ end
43
+ end
44
+
45
+ context 'when cursor is greater than collection size' do
46
+ let(:collection) { Array.new(count) { |i| "mock:key#{i}" } }
47
+ let(:expected) { ['0', []] }
48
+
49
+ it 'returns a 0 cursor and empty collection' do
50
+ expect(subject.scan(20, count: count, match: match)).to eq(expected)
51
+ end
52
+ end
53
+
54
+ context 'when giving a custom match filter' do
55
+ let(:match) { 'mock:key*' }
56
+ let(:collection) { %w[mock:key mock:key2 mock:otherkey] }
57
+ let(:expected) { ['0', %w[mock:key mock:key2]] }
58
+
59
+ it 'returns a 0 cursor and the filtered collection' do
60
+ expect(subject.scan(0, count: count, match: match)).to eq(expected)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -3,16 +3,16 @@ require 'spec_helper'
3
3
  describe '#scard(key)' do
4
4
  before { @key = 'mock-redis-test:scard' }
5
5
 
6
- it "returns 0 for an empty set" do
6
+ it 'returns 0 for an empty set' do
7
7
  @redises.scard(@key).should == 0
8
8
  end
9
9
 
10
- it "returns the number of elements in the set" do
10
+ it 'returns the number of elements in the set' do
11
11
  @redises.sadd(@key, 'one')
12
12
  @redises.sadd(@key, 'two')
13
13
  @redises.sadd(@key, 'three')
14
14
  @redises.scard(@key).should == 3
15
15
  end
16
16
 
17
- it_should_behave_like "a set-only command"
17
+ it_should_behave_like 'a set-only command'
18
18
  end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#script(subcommand, *args)' do
4
+ before { @key = 'mock-redis-test:script' }
5
+
6
+ it 'works with load subcommand' do
7
+ expect { @redises.send_without_checking(:script, :load, 'return 1') }.to_not raise_error
8
+ end
9
+ end
@@ -6,42 +6,42 @@ describe '#sdiff(key [, key, ...])' do
6
6
  @evens = 'mock-redis-test:sdiff:odds'
7
7
  @primes = 'mock-redis-test:sdiff:primes'
8
8
 
9
- (1..10).each {|i| @redises.sadd(@numbers, i) }
10
- [2, 4, 6, 8, 10].each {|i| @redises.sadd(@evens, i) }
11
- [2, 3, 5, 7].each {|i| @redises.sadd(@primes, i) }
9
+ (1..10).each { |i| @redises.sadd(@numbers, i) }
10
+ [2, 4, 6, 8, 10].each { |i| @redises.sadd(@evens, i) }
11
+ [2, 3, 5, 7].each { |i| @redises.sadd(@primes, i) }
12
12
  end
13
13
 
14
- it "returns the first set minus the second set" do
14
+ it 'returns the first set minus the second set' do
15
15
  @redises.sdiff(@numbers, @evens).should == %w[1 3 5 7 9]
16
16
  end
17
17
 
18
- it "returns the first set minus all the other sets" do
18
+ it 'returns the first set minus all the other sets' do
19
19
  @redises.sdiff(@numbers, @evens, @primes).should == %w[1 9]
20
20
  end
21
21
 
22
- it "treats missing keys as empty sets" do
22
+ it 'treats missing keys as empty sets' do
23
23
  @redises.sdiff(@evens, 'mock-redis-test:nonesuch').should == %w[2 4 6 8 10]
24
24
  end
25
25
 
26
- it "returns the first set when called with a single argument" do
26
+ it 'returns the first set when called with a single argument' do
27
27
  @redises.sdiff(@primes).should == %w[2 3 5 7]
28
28
  end
29
29
 
30
- it "raises an error if given 0 arguments" do
30
+ it 'raises an error if given 0 arguments' do
31
31
  lambda do
32
- @redises.sdiff()
33
- end.should raise_error(RuntimeError)
32
+ @redises.sdiff
33
+ end.should raise_error(Redis::CommandError)
34
34
  end
35
35
 
36
- it "raises an error if any argument is not a a set" do
36
+ it 'raises an error if any argument is not a a set' do
37
37
  @redises.set('foo', 1)
38
38
 
39
39
  lambda do
40
40
  @redises.sdiff(@numbers, 'foo')
41
- end.should raise_error(RuntimeError)
41
+ end.should raise_error(Redis::CommandError)
42
42
 
43
43
  lambda do
44
44
  @redises.sdiff('foo', @numbers)
45
- end.should raise_error(RuntimeError)
45
+ end.should raise_error(Redis::CommandError)
46
46
  end
47
47
  end