mock_redis 0.5.4 → 0.31.0

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 (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
@@ -11,44 +11,42 @@ describe '#blpop(key [, key, ...,], timeout)' do
11
11
  @redises.rpush(@list2, 'eleven')
12
12
  end
13
13
 
14
- it "returns [first-nonempty-list, popped-value]" do
15
- @redises.blpop(@list1, @list2, 1).should == [@list1, 'one']
14
+ it 'returns [first-nonempty-list, popped-value]' do
15
+ @redises.blpop(@list1, @list2).should == [@list1, 'one']
16
16
  end
17
17
 
18
- it "pops that value off the list" do
19
- @redises.blpop(@list1, @list2, 1)
20
- @redises.blpop(@list1, @list2, 1)
18
+ it 'pops that value off the list' do
19
+ @redises.blpop(@list1, @list2)
20
+ @redises.blpop(@list1, @list2)
21
21
 
22
- @redises.blpop(@list1, @list2, 1).should == [@list2, 'ten']
22
+ @redises.blpop(@list1, @list2).should == [@list2, 'ten']
23
23
  end
24
24
 
25
- it "ignores empty keys" do
26
- @redises.blpop('mock-redis-test:not-here', @list1, 1).should ==
25
+ it 'ignores empty keys' do
26
+ @redises.blpop('mock-redis-test:not-here', @list1).should ==
27
27
  [@list1, 'one']
28
28
  end
29
29
 
30
- it "allows subsecond timeouts" do
30
+ it 'raises an error on negative timeout' do
31
31
  lambda do
32
- @redises.blpop(@list1, @list2, 0.5)
33
- end.should_not raise_error(Redis::CommandError)
34
- end
35
-
36
- it "raises an error on negative timeout" do
37
- lambda do
38
- @redises.blpop(@list1, @list2, -1)
32
+ @redises.blpop(@list1, @list2, :timeout => -1)
39
33
  end.should raise_error(Redis::CommandError)
40
34
  end
41
35
 
42
- it_should_behave_like "a list-only command"
36
+ it_should_behave_like 'a list-only command'
37
+
38
+ context '[mock only]' do
39
+ it 'ignores positive timeouts and returns nil' do
40
+ @redises.mock.blpop('mock-redis-test:not-here', :timeout => 1).should be_nil
41
+ end
43
42
 
44
- context "[mock only]" do
45
- it "ignores positive timeouts and returns nil" do
43
+ it 'ignores positive legacy timeouts and returns nil' do
46
44
  @redises.mock.blpop('mock-redis-test:not-here', 1).should be_nil
47
45
  end
48
46
 
49
- it "raises WouldBlock on zero timeout (no blocking in the mock)" do
47
+ it 'raises WouldBlock on zero timeout (no blocking in the mock)' do
50
48
  lambda do
51
- @redises.mock.blpop('mock-redis-test:not-here', 0)
49
+ @redises.mock.blpop('mock-redis-test:not-here', :timeout => 0)
52
50
  end.should raise_error(MockRedis::WouldBlock)
53
51
  end
54
52
  end
@@ -11,43 +11,48 @@ describe '#brpop(key [, key, ...,], timeout)' do
11
11
  @redises.rpush(@list2, 'ten')
12
12
  end
13
13
 
14
- it "returns [first-nonempty-list, popped-value]" do
15
- @redises.brpop(@list1, @list2, 1).should == [@list1, 'two']
14
+ it 'returns [first-nonempty-list, popped-value]' do
15
+ @redises.brpop(@list1, @list2).should == [@list1, 'two']
16
16
  end
17
17
 
18
- it "pops that value off the list" do
19
- @redises.brpop(@list1, @list2, 1)
20
- @redises.brpop(@list1, @list2, 1)
21
- @redises.brpop(@list1, @list2, 1).should == [@list2, 'ten']
18
+ it 'pops that value off the list' do
19
+ @redises.brpop(@list1, @list2)
20
+ @redises.brpop(@list1, @list2)
21
+ @redises.brpop(@list1, @list2).should == [@list2, 'ten']
22
22
  end
23
23
 
24
- it "ignores empty keys" do
25
- @redises.brpop('mock-redis-test:not-here', @list1, 1).should ==
24
+ it 'ignores empty keys' do
25
+ @redises.brpop('mock-redis-test:not-here', @list1).should ==
26
26
  [@list1, 'two']
27
27
  end
28
28
 
29
- it "allows subsecond timeouts" do
30
- lambda do
31
- @redises.brpop(@list1, @list2, 0.5)
32
- end.should_not raise_error(Redis::CommandError)
33
- end
29
+ # TODO: Not sure how redis-rb is handling this but they're not raising an error
30
+ # it 'raises an error on subsecond timeouts' do
31
+ # lambda do
32
+ # @redises.brpop(@list1, @list2, :timeout => 0.5)
33
+ # end.should raise_error(Redis::CommandError)
34
+ # end
34
35
 
35
- it "raises an error on negative timeout" do
36
+ it 'raises an error on negative timeout' do
36
37
  lambda do
37
- @redises.brpop(@list1, @list2, -1)
38
+ @redises.brpop(@list1, @list2, :timeout => -1)
38
39
  end.should raise_error(Redis::CommandError)
39
40
  end
40
41
 
41
- it_should_behave_like "a list-only command"
42
+ it_should_behave_like 'a list-only command'
43
+
44
+ context '[mock only]' do
45
+ it 'ignores positive timeouts and returns nil' do
46
+ @redises.mock.brpop('mock-redis-test:not-here', :timeout => 1).should be_nil
47
+ end
42
48
 
43
- context "[mock only]" do
44
- it "ignores positive timeouts and returns nil" do
49
+ it 'ignores positive legacy timeouts and returns nil' do
45
50
  @redises.mock.brpop('mock-redis-test:not-here', 1).should be_nil
46
51
  end
47
52
 
48
- it "raises WouldBlock on zero timeout (no blocking in the mock)" do
53
+ it 'raises WouldBlock on zero timeout (no blocking in the mock)' do
49
54
  lambda do
50
- @redises.mock.brpop('mock-redis-test:not-here', 0)
55
+ @redises.mock.brpop('mock-redis-test:not-here', :timeout => 0)
51
56
  end.should raise_error(MockRedis::WouldBlock)
52
57
  end
53
58
  end
@@ -14,39 +14,38 @@ describe '#brpoplpush(source, destination, timeout)' do
14
14
  @redises.rpush(@list2, 'beta')
15
15
  end
16
16
 
17
- it "takes the last element of source and prepends it to destination" do
18
- @redises.brpoplpush(@list1, @list2, 0)
17
+ it 'takes the last element of source and prepends it to destination' do
18
+ @redises.brpoplpush(@list1, @list2)
19
19
  @redises.lrange(@list1, 0, -1).should == %w[A]
20
20
  @redises.lrange(@list2, 0, -1).should == %w[B alpha beta]
21
21
  end
22
22
 
23
- it "returns the moved element" do
24
- @redises.brpoplpush(@list1, @list2, 0).should == "B"
23
+ it 'returns the moved element' do
24
+ @redises.brpoplpush(@list1, @list2).should == 'B'
25
25
  end
26
26
 
27
- it "allows subsecond timeout" do
27
+ it 'raises an error on negative timeout' do
28
28
  lambda do
29
- @redises.brpoplpush(@list1, @list2, 0.5)
30
- end.should_not raise_error(Redis::CommandError)
31
- end
32
-
33
- it "raises an error on negative timeout" do
34
- lambda do
35
- @redises.brpoplpush(@list1, @list2, -1)
29
+ @redises.brpoplpush(@list1, @list2, :timeout => -1)
36
30
  end.should raise_error(Redis::CommandError)
37
31
  end
38
32
 
39
- it_should_behave_like "a list-only command"
33
+ it_should_behave_like 'a list-only command'
34
+
35
+ context '[mock only]' do
36
+ it 'ignores positive timeouts and returns nil' do
37
+ @redises.mock.brpoplpush('mock-redis-test:not-here', @list1, :timeout => 1).
38
+ should be_nil
39
+ end
40
40
 
41
- context "[mock only]" do
42
- it "ignores positive timeouts and returns nil" do
41
+ it 'ignores positive legacy timeouts and returns nil' do
43
42
  @redises.mock.brpoplpush('mock-redis-test:not-here', @list1, 1).
44
43
  should be_nil
45
44
  end
46
45
 
47
- it "raises WouldBlock on zero timeout (no blocking in the mock)" do
46
+ it 'raises WouldBlock on zero timeout (no blocking in the mock)' do
48
47
  lambda do
49
- @redises.mock.brpoplpush('mock-redis-test:not-here', @list1, 0)
48
+ @redises.mock.brpoplpush('mock-redis-test:not-here', @list1, :timeout => 0)
50
49
  end.should raise_error(MockRedis::WouldBlock)
51
50
  end
52
51
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#connected? [mock only]' do
4
+ it 'returns true' do
5
+ @redises.mock.connected?.should == true
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#connection' do
4
+ let(:redis) { @redises.mock }
5
+
6
+ it 'returns the correct values' do
7
+ redis.connection.should == {
8
+ :host => '127.0.0.1',
9
+ :port => 6379,
10
+ :db => 0,
11
+ :id => 'redis://127.0.0.1:6379/0',
12
+ :location => '127.0.0.1:6379'
13
+ }
14
+ end
15
+ end
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#dbsize [mock only]" do
3
+ describe '#dbsize [mock only]' do
4
4
  # mock only since we can't guarantee that the real Redis is empty
5
5
  before { @mock = @redises.mock }
6
6
 
7
- it "returns 0 for an empty DB" do
7
+ it 'returns 0 for an empty DB' do
8
8
  @mock.dbsize.should == 0
9
9
  end
10
10
 
11
- it "returns the number of keys in the DB" do
11
+ it 'returns the number of keys in the DB' do
12
12
  @mock.set('foo', 1)
13
13
  @mock.lpush('bar', 2)
14
14
  @mock.hset('baz', 3, 4)
@@ -3,32 +3,32 @@ require 'spec_helper'
3
3
  describe '#decr(key)' do
4
4
  before { @key = 'mock-redis-test:46895' }
5
5
 
6
- it "returns the value after the decrement" do
6
+ it 'returns the value after the decrement' do
7
7
  @redises.set(@key, 2)
8
8
  @redises.decr(@key).should == 1
9
9
  end
10
10
 
11
- it "treats a missing key like 0" do
11
+ it 'treats a missing key like 0' do
12
12
  @redises.decr(@key).should == -1
13
13
  end
14
14
 
15
- it "decrements negative numbers" do
15
+ it 'decrements negative numbers' do
16
16
  @redises.set(@key, -10)
17
17
  @redises.decr(@key).should == -11
18
18
  end
19
19
 
20
- it "works multiple times" do
20
+ it 'works multiple times' do
21
21
  @redises.decr(@key).should == -1
22
22
  @redises.decr(@key).should == -2
23
23
  @redises.decr(@key).should == -3
24
24
  end
25
25
 
26
- it "raises an error if the value does not look like an integer" do
27
- @redises.set(@key, "minus one")
26
+ it 'raises an error if the value does not look like an integer' do
27
+ @redises.set(@key, 'minus one')
28
28
  lambda do
29
29
  @redises.decr(@key)
30
- end.should raise_error(RuntimeError)
30
+ end.should raise_error(Redis::CommandError)
31
31
  end
32
32
 
33
- it_should_behave_like "a string-only command"
33
+ it_should_behave_like 'a string-only command'
34
34
  end
@@ -3,32 +3,32 @@ require 'spec_helper'
3
3
  describe '#decrby(key, decrement)' do
4
4
  before { @key = 'mock-redis-test:43650' }
5
5
 
6
- it "returns the value after the decrement" do
6
+ it 'returns the value after the decrement' do
7
7
  @redises.set(@key, 4)
8
8
  @redises.decrby(@key, 2).should == 2
9
9
  end
10
10
 
11
- it "treats a missing key like 0" do
11
+ it 'treats a missing key like 0' do
12
12
  @redises.decrby(@key, 2).should == -2
13
13
  end
14
14
 
15
- it "decrements negative numbers" do
15
+ it 'decrements negative numbers' do
16
16
  @redises.set(@key, -10)
17
17
  @redises.decrby(@key, 2).should == -12
18
18
  end
19
19
 
20
- it "works multiple times" do
20
+ it 'works multiple times' do
21
21
  @redises.decrby(@key, 2).should == -2
22
22
  @redises.decrby(@key, 2).should == -4
23
23
  @redises.decrby(@key, 2).should == -6
24
24
  end
25
25
 
26
- it "raises an error if the value does not look like an integer" do
27
- @redises.set(@key, "one")
26
+ it 'raises an error if the value does not look like an integer' do
27
+ @redises.set(@key, 'one')
28
28
  lambda do
29
29
  @redises.decrby(@key, 1)
30
- end.should raise_error(RuntimeError)
30
+ end.should raise_error(Redis::CommandError)
31
31
  end
32
32
 
33
- it_should_behave_like "a string-only command"
33
+ it_should_behave_like 'a string-only command'
34
34
  end
@@ -1,20 +1,52 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe '#del(key [, key, ...])' do
4
- it "returns the number of keys deleted" do
4
+ before :all do
5
+ sleep 1 - (Time.now.to_f % 1)
6
+ end
7
+
8
+ before :each do
9
+ # TODO: Redis appears to be returning a timestamp a few seconds in the future
10
+ # so we're ignoring the last 5 digits (time in milliseconds)
11
+ @redises._gsub(/\d{5}-\d/, '...-.')
12
+ end
13
+
14
+ it 'returns the number of keys deleted' do
5
15
  @redises.set('mock-redis-test:1', 1)
6
16
  @redises.set('mock-redis-test:2', 1)
7
17
 
8
18
  @redises.del(
9
19
  'mock-redis-test:1',
10
20
  'mock-redis-test:2',
11
- 'mock-redis-test:other').should == 2
21
+ 'mock-redis-test:other'
22
+ ).should == 2
12
23
  end
13
24
 
14
- it "actually removes the key" do
25
+ it 'actually removes the key' do
15
26
  @redises.set('mock-redis-test:1', 1)
16
27
  @redises.del('mock-redis-test:1')
17
28
 
18
29
  @redises.get('mock-redis-test:1').should be_nil
19
30
  end
31
+
32
+ it 'accepts an array of keys' do
33
+ @redises.set('mock-redis-test:1', 1)
34
+ @redises.set('mock-redis-test:2', 2)
35
+
36
+ @redises.del(%w[mock-redis-test:1 mock-redis-test:2])
37
+
38
+ @redises.get('mock-redis-test:1').should be_nil
39
+ @redises.get('mock-redis-test:2').should be_nil
40
+ end
41
+
42
+ it 'raises an error if an empty array is given' do
43
+ expect { @redises.del [] }.to raise_error Redis::CommandError
44
+ end
45
+
46
+ it 'removes a stream key' do
47
+ @redises.xadd('mock-redis-stream', { key: 'value' }, maxlen: 0)
48
+ expect(@redises.exists?('mock-redis-stream')).to eq true
49
+ @redises.del('mock-redis-stream')
50
+ expect(@redises.exists?('mock-redis-stream')).to eq false
51
+ end
20
52
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#disconnect [mock only]' do
4
+ it 'returns nil' do
5
+ @redises.mock.disconnect.should be_nil
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#dump(key)' do
4
+ before do
5
+ @key = 'mock-redis-test:45794'
6
+ # These are mock-only, since our dump/restore implementations
7
+ # aren't compatible with real redis.
8
+ @mock = @redises.mock
9
+ end
10
+
11
+ it 'returns nil for keys that do not exist' do
12
+ @mock.dump(@key).should be_nil
13
+ end
14
+
15
+ it 'returns a serialized value for keys that do exist' do
16
+ @mock.set(@key, '2')
17
+ @mock.dump(@key).should == Marshal.dump('2')
18
+ end
19
+ end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#echo(str)" do
4
- it "returns its argument" do
3
+ describe '#echo(str)' do
4
+ it 'returns its argument' do
5
5
  @redises.echo('foo').should == 'foo'
6
6
  end
7
7
 
8
- it "stringifies its argument" do
9
- @redises.echo(1).should == "1"
8
+ it 'stringifies its argument' do
9
+ @redises.echo(1).should == '1'
10
10
  end
11
11
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#eval(*)' do
4
+ it 'returns nothing' do
5
+ @redises.eval('return nil').should be_nil
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#evalsha(*)' do
4
+ let(:script) { 'return nil' }
5
+ let(:script_digest) { Digest::SHA1.hexdigest(script) }
6
+
7
+ it 'returns nothing' do
8
+ @redises.evalsha(script_digest).should be_nil
9
+ end
10
+ end
@@ -1,14 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#exists(key)" do
4
- before { @key = 'mock-redis-test:45794' }
3
+ describe '#exists(*keys)' do
4
+ before { @key1 = 'mock-redis-test:exists1' }
5
+ before { @key2 = 'mock-redis-test:exists2' }
5
6
 
6
- it "returns false for keys that do not exist" do
7
- @redises.exists(@key).should be_false
7
+ it 'returns 0 for keys that do not exist' do
8
+ @redises.exists(@key1).should == 0
9
+ @redises.exists(@key1, @key2).should == 0
8
10
  end
9
11
 
10
- it "returns true for keys that do exist" do
11
- @redises.set(@key, 1)
12
- @redises.exists(@key).should be_true
12
+ it 'returns 1 for keys that do exist' do
13
+ @redises.set(@key1, 1)
14
+ @redises.exists(@key1).should == 1
15
+ end
16
+
17
+ it 'returns the count of all keys that exist' do
18
+ @redises.set(@key1, 1)
19
+ @redises.set(@key2, 1)
20
+ @redises.exists(@key1, @key2).should == 2
21
+ @redises.exists(@key1, @key2, 'does-not-exist').should == 2
22
+ end
23
+ end
24
+
25
+ describe '#exists?(*keys)' do
26
+ before { @key1 = 'mock-redis-test:exists1' }
27
+ before { @key2 = 'mock-redis-test:exists2' }
28
+
29
+ it 'returns false for keys that do not exist' do
30
+ @redises.exists?(@key1).should == false
31
+ @redises.exists?(@key1, @key2).should == false
32
+ end
33
+
34
+ it 'returns true for keys that do exist' do
35
+ @redises.set(@key1, 1)
36
+ @redises.exists?(@key1).should == true
37
+ end
38
+
39
+ it 'returns true if any keys exist' do
40
+ @redises.set(@key2, 1)
41
+ @redises.exists?(@key1, @key2).should == true
13
42
  end
14
43
  end
@@ -1,31 +1,35 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#expire(key, seconds)" do
3
+ describe '#expire(key, seconds)' do
4
4
  before do
5
5
  @key = 'mock-redis-test:expire'
6
6
  @redises.set(@key, 'spork')
7
7
  end
8
8
 
9
- it "returns true for a key that exists" do
10
- @redises.expire(@key, 1).should be_true
9
+ it 'returns true for a key that exists' do
10
+ @redises.expire(@key, 1).should == true
11
11
  end
12
12
 
13
- it "returns false for a key that does not exist" do
14
- @redises.expire('mock-redis-test:nonesuch', 1).should be_false
13
+ it 'returns false for a key that does not exist' do
14
+ @redises.expire('mock-redis-test:nonesuch', 1).should == false
15
15
  end
16
16
 
17
- it "removes a key immediately when seconds==0" do
17
+ it 'removes a key immediately when seconds==0' do
18
18
  @redises.expire(@key, 0)
19
19
  @redises.get(@key).should be_nil
20
20
  end
21
21
 
22
- it "raises an error if seconds is bogus" do
22
+ it 'raises an error if seconds is bogus' do
23
23
  lambda do
24
- @redises.expireat(@key, 'a couple minutes or so')
25
- end.should raise_error(RuntimeError)
24
+ @redises.expire(@key, 'a couple minutes or so')
25
+ end.should raise_error(Redis::CommandError)
26
26
  end
27
27
 
28
- context "[mock only]" do
28
+ it 'stringifies key' do
29
+ @redises.expire(@key.to_sym, 9).should == true
30
+ end
31
+
32
+ context '[mock only]' do
29
33
  # These are mock-only since we can't actually manipulate time in
30
34
  # the real Redis.
31
35
 
@@ -35,49 +39,73 @@ describe "#expire(key, seconds)" do
35
39
 
36
40
  before do
37
41
  @now = Time.now
38
- Time.stub!(:now).and_return(@now)
42
+ Time.stub(:now).and_return(@now)
39
43
  end
40
44
 
41
- it "removes keys after enough time has passed" do
45
+ it 'removes keys after enough time has passed' do
42
46
  @mock.expire(@key, 5)
43
- Time.stub!(:now).and_return(@now + 5)
47
+ Time.stub(:now).and_return(@now + 5)
44
48
  @mock.get(@key).should be_nil
45
49
  end
46
50
 
47
- it "updates an existing expire time" do
51
+ it 'updates an existing expire time' do
48
52
  @mock.expire(@key, 5)
49
53
  @mock.expire(@key, 6)
50
54
 
51
- Time.stub!(:now).and_return(@now + 5)
55
+ Time.stub(:now).and_return(@now + 5)
56
+ @mock.get(@key).should_not be_nil
57
+ end
58
+
59
+ it 'has millisecond precision' do
60
+ @now = Time.at(@now.to_i + 0.5)
61
+ Time.stub(:now).and_return(@now)
62
+ @mock.expire(@key, 5)
63
+ Time.stub(:now).and_return(@now + 4.9)
52
64
  @mock.get(@key).should_not be_nil
53
65
  end
54
66
 
55
- context "expirations on a deleted key" do
67
+ context 'expirations on a deleted key' do
56
68
  before { @mock.del(@key) }
57
69
 
58
- it "cleans up the expiration once the key is gone (string)" do
70
+ it 'cleans up the expiration once the key is gone (string)' do
59
71
  @mock.set(@key, 'string')
60
72
  @mock.expire(@key, 2)
61
73
  @mock.del(@key)
62
74
  @mock.set(@key, 'string')
63
75
 
64
- Time.stub!(:now).and_return(@now + 2)
76
+ Time.stub(:now).and_return(@now + 2)
65
77
 
66
78
  @mock.get(@key).should_not be_nil
67
79
  end
68
80
 
69
- it "cleans up the expiration once the key is gone (list)" do
81
+ it 'cleans up the expiration once the key is gone (list)' do
70
82
  @mock.rpush(@key, 'coconuts')
71
83
  @mock.expire(@key, 2)
72
84
  @mock.rpop(@key)
73
85
 
74
86
  @mock.rpush(@key, 'coconuts')
75
87
 
76
- Time.stub!(:now).and_return(@now + 2)
88
+ Time.stub(:now).and_return(@now + 2)
77
89
 
78
90
  @mock.lindex(@key, 0).should_not be_nil
79
91
  end
80
92
  end
81
93
 
94
+ context 'with two key expirations' do
95
+ let(:other_key) { 'mock-redis-test:expire-other' }
96
+
97
+ before { @redises.set(other_key, 'spork-other') }
98
+
99
+ it 'removes keys after enough time has passed' do
100
+ @mock.expire(@key, 5)
101
+ @mock.expire(other_key, 10)
102
+
103
+ Time.stub(:now).and_return(@now + 5)
104
+ @mock.get(@key).should be_nil
105
+
106
+ Time.stub(:now).and_return(@now + 10)
107
+ @mock.get(other_key).should be_nil
108
+ end
109
+ end
82
110
  end
83
111
  end