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
@@ -7,52 +7,52 @@ describe '#sdiffstore(destination, key [, key, ...])' do
7
7
  @primes = 'mock-redis-test:sdiffstore:primes'
8
8
  @destination = 'mock-redis-test:sdiffstore:destination'
9
9
 
10
- (1..10).each {|i| @redises.sadd(@numbers, i) }
11
- [2, 4, 6, 8, 10].each {|i| @redises.sadd(@evens, i) }
12
- [2, 3, 5, 7].each {|i| @redises.sadd(@primes, i) }
10
+ (1..10).each { |i| @redises.sadd(@numbers, i) }
11
+ [2, 4, 6, 8, 10].each { |i| @redises.sadd(@evens, i) }
12
+ [2, 3, 5, 7].each { |i| @redises.sadd(@primes, i) }
13
13
  end
14
14
 
15
- it "returns the number of elements in the resulting set" do
15
+ it 'returns the number of elements in the resulting set' do
16
16
  @redises.sdiffstore(@destination, @numbers, @evens).should == 5
17
17
  end
18
18
 
19
- it "stores the resulting set" do
19
+ it 'stores the resulting set' do
20
20
  @redises.sdiffstore(@destination, @numbers, @evens)
21
21
  @redises.smembers(@destination).should == %w[9 7 5 3 1]
22
22
  end
23
23
 
24
- it "does not store empty sets" do
24
+ it 'does not store empty sets' do
25
25
  @redises.sdiffstore(@destination, @numbers, @numbers).should == 0
26
26
  @redises.get(@destination).should be_nil
27
27
  end
28
28
 
29
- it "treats missing keys as empty sets" do
29
+ it 'treats missing keys as empty sets' do
30
30
  @redises.sdiffstore(@destination, @evens, 'mock-redis-test:nonesuch')
31
31
  @redises.smembers(@destination).should == %w[10 8 6 4 2]
32
32
  end
33
33
 
34
- it "removes existing elements in destination" do
34
+ it 'removes existing elements in destination' do
35
35
  @redises.sadd(@destination, 42)
36
36
 
37
37
  @redises.sdiffstore(@destination, @primes)
38
38
  @redises.smembers(@destination).should == %w[7 5 3 2]
39
39
  end
40
40
 
41
- it "raises an error if given 0 sets" do
41
+ it 'raises an error if given 0 sets' do
42
42
  lambda do
43
43
  @redises.sdiffstore(@destination)
44
- end.should raise_error(RuntimeError)
44
+ end.should raise_error(Redis::CommandError)
45
45
  end
46
46
 
47
- it "raises an error if any argument is not a a set" do
47
+ it 'raises an error if any argument is not a a set' do
48
48
  @redises.set('mock-redis-test:notset', 1)
49
49
 
50
50
  lambda do
51
51
  @redises.sdiffstore(@destination, @numbers, 'mock-redis-test:notset')
52
- end.should raise_error(RuntimeError)
52
+ end.should raise_error(Redis::CommandError)
53
53
 
54
54
  lambda do
55
55
  @redises.sdiffstore(@destination, 'mock-redis-test:notset', @numbers)
56
- end.should raise_error(RuntimeError)
56
+ end.should raise_error(Redis::CommandError)
57
57
  end
58
58
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#select(db)" do
3
+ describe '#select(db)' do
4
4
  before { @key = 'mock-redis-test:select' }
5
5
 
6
6
  it "returns 'OK'" do
@@ -14,7 +14,7 @@ describe "#select(db)" do
14
14
  @redises.get(@key).should == 'foo'
15
15
  end
16
16
 
17
- it "switches databases" do
17
+ it 'switches databases' do
18
18
  @redises.select(0)
19
19
  @redises.set(@key, 'foo')
20
20
 
@@ -25,11 +25,11 @@ describe "#select(db)" do
25
25
  @redises.get(@key).should == 'foo'
26
26
  end
27
27
 
28
- context "[mock only]" do
28
+ context '[mock only]' do
29
29
  # Time dependence introduces a bit of nondeterminism here
30
30
  before do
31
31
  @now = Time.now
32
- Time.stub!(:now).and_return(@now)
32
+ Time.stub(:now).and_return(@now)
33
33
 
34
34
  @mock = @redises.mock
35
35
 
@@ -42,12 +42,20 @@ describe "#select(db)" do
42
42
  @mock.expire(@key, 200)
43
43
  end
44
44
 
45
- it "keeps expire times per-db" do
45
+ it 'keeps expire times per-db' do
46
46
  @mock.select(0)
47
47
  @mock.ttl(@key).should == 100
48
48
 
49
49
  @mock.select(1)
50
50
  @mock.ttl(@key).should == 200
51
51
  end
52
+
53
+ it 'keeps expire times in miliseconds per-db' do
54
+ @mock.select(0)
55
+ (100_000 - 1000..100_000 + 1000).should cover(@mock.pttl(@key))
56
+
57
+ @mock.select(1)
58
+ (200_000 - 1000..200_000 + 1000).should cover(@mock.pttl(@key))
59
+ end
52
60
  end
53
61
  end
@@ -1,7 +1,119 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe '#set(key, value)' do
4
+ let(:key) { 'mock-redis-test' }
5
+
4
6
  it "responds with 'OK'" do
5
7
  @redises.set('mock-redis-test', 1).should == 'OK'
6
8
  end
9
+
10
+ context 'options' do
11
+ it 'raises an error for EX seconds = 0' do
12
+ expect do
13
+ @redises.set('mock-redis-test', 1, ex: 0)
14
+ end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
15
+ end
16
+
17
+ it 'raises an error for PX milliseconds = 0' do
18
+ expect do
19
+ @redises.set('mock-redis-test', 1, px: 0)
20
+ end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
21
+ end
22
+
23
+ it 'accepts NX' do
24
+ @redises.del(key)
25
+ @redises.set(key, 1, nx: true).should == true
26
+ @redises.set(key, 1, nx: true).should == false
27
+ end
28
+
29
+ it 'accepts XX' do
30
+ @redises.del(key)
31
+ @redises.set(key, 1, xx: true).should == false
32
+ @redises.set(key, 1).should == 'OK'
33
+ @redises.set(key, 1, xx: true).should == true
34
+ end
35
+
36
+ it 'sets the ttl to -1' do
37
+ @redises.set(key, 1)
38
+ expect(@redises.ttl(key)).to eq(-1)
39
+ end
40
+
41
+ context 'with an expiry time' do
42
+ before :each do
43
+ Timecop.freeze
44
+ @redises.set(key, 1, ex: 90)
45
+ end
46
+
47
+ after :each do
48
+ @redises.del(key)
49
+ Timecop.return
50
+ end
51
+
52
+ it 'has the TTL set' do
53
+ expect(@redises.ttl(key)).to eq 90
54
+ end
55
+
56
+ it 'resets the TTL without keepttl' do
57
+ expect do
58
+ @redises.set(key, 2)
59
+ end.to change { @redises.ttl(key) }.from(90).to(-1)
60
+ end
61
+
62
+ it 'does not change the TTL with keepttl: true' do
63
+ expect do
64
+ @redises.set(key, 2, keepttl: true)
65
+ end.not_to change { @redises.ttl(key) }.from(90)
66
+ end
67
+ end
68
+
69
+ it 'accepts KEEPTTL' do
70
+ expect(@redises.set(key, 1, keepttl: true)).to eq 'OK'
71
+ end
72
+
73
+ it 'does not set TTL without ex' do
74
+ @redises.set(key, 1)
75
+ expect(@redises.ttl(key)).to eq(-1)
76
+ end
77
+
78
+ it 'sets the TTL' do
79
+ Timecop.freeze do
80
+ @redises.set(key, 1, ex: 90)
81
+ expect(@redises.ttl(key)).to eq 90
82
+ end
83
+ end
84
+
85
+ it 'raises on unknown options' do
86
+ @redises.del(key)
87
+ expect do
88
+ @redises.set(key, 1, logger: :something)
89
+ end.to raise_error(ArgumentError, /unknown keyword/)
90
+ end
91
+
92
+ context '[mock only]' do
93
+ before(:all) do
94
+ @mock = @redises.mock
95
+ end
96
+
97
+ before do
98
+ @now = Time.now
99
+ Time.stub(:now).and_return(@now)
100
+ end
101
+
102
+ it 'accepts EX seconds' do
103
+ @mock.set(key, 1, ex: 1).should == 'OK'
104
+ @mock.get(key).should_not be_nil
105
+ Time.stub(:now).and_return(@now + 2)
106
+ @mock.get(key).should be_nil
107
+ end
108
+
109
+ it 'accepts PX milliseconds' do
110
+ @mock.set(key, 1, px: 500).should == 'OK'
111
+ @mock.get(key).should_not be_nil
112
+ Time.stub(:now).and_return(@now + 300 / 1000.to_f)
113
+ @mock.get(key).should_not be_nil
114
+ Time.stub(:now).and_return(@now + 600 / 1000.to_f)
115
+ @mock.get(key).should be_nil
116
+ end
117
+ end
118
+ end
7
119
  end
@@ -1,10 +1,10 @@
1
- # -*- coding: utf-8 -*-
2
1
  require 'spec_helper'
3
2
 
4
- describe "#setbit(key, offset)" do
3
+ describe '#setbit(key, offset)' do
5
4
  before do
5
+ Encoding.default_external = 'UTF-8'
6
6
  @key = 'mock-redis-test:setbit'
7
- @redises.set(@key, 'h') # ASCII 0x68
7
+ @redises.set(@key, 'h') # ASCII 0x68
8
8
  end
9
9
 
10
10
  it "returns the original stored bit's value" do
@@ -12,35 +12,44 @@ describe "#setbit(key, offset)" do
12
12
  @redises.setbit(@key, 1, 1).should == 1
13
13
  end
14
14
 
15
- it "sets the bit within the string" do
16
- $debug = true
15
+ it 'sets the bit within the string' do
17
16
  @redises.setbit(@key, 7, 1)
18
- $debug = false
19
17
  @redises.get(@key).should == 'i' # ASCII 0x69
20
18
  end
21
19
 
22
- it "does the right thing with multibyte characters" do
23
- @redises.set(@key, "€99.94") # the euro sign is 3 bytes wide in UTF-8
20
+ it 'unsets the bit within the string' do
21
+ @redises.setbit(@key, 1, 0)
22
+ @redises.get(@key).should == '(' # ASCII 0x28
23
+ end
24
+
25
+ it 'does the right thing with multibyte characters' do
26
+ @redises.set(@key, '€99.94') # the euro sign is 3 bytes wide in UTF-8
24
27
  @redises.setbit(@key, 63, 1).should == 0
25
- @redises.get(@key).should == "€99.95"
28
+ @redises.get(@key).should == '€99.95'
26
29
  end
27
30
 
28
- it "expands the string if necessary" do
31
+ it 'expands the string if necessary' do
29
32
  @redises.setbit(@key, 9, 1)
30
- @redises.get(@key).should == "h@"
33
+ @redises.get(@key).should == 'h@'
31
34
  end
32
35
 
33
- it "sets added bits to 0" do
36
+ it 'sets added bits to 0' do
34
37
  @redises.setbit(@key, 17, 1)
35
38
  @redises.get(@key).should == "h\000@"
36
39
  end
37
40
 
38
- it "treats missing keys as empty strings" do
41
+ it 'treats missing keys as empty strings' do
39
42
  @redises.del(@key)
40
43
  @redises.setbit(@key, 1, 1)
41
- @redises.get(@key).should == "@"
44
+ @redises.get(@key).should == '@'
45
+ end
46
+
47
+ it 'sets and retrieves bits' do
48
+ @redises.setbit(@key, 22, 1)
49
+ @redises.getbit(@key, 22).should == 1
50
+ @redises.setbit(@key, 23, 0)
51
+ @redises.getbit(@key, 23).should == 0
42
52
  end
43
53
 
44
- it_should_behave_like "a string-only command"
54
+ it_should_behave_like 'a string-only command'
45
55
  end
46
-
@@ -7,16 +7,32 @@ describe '#setex(key, seconds, value)' do
7
7
  @redises.setex(@key, 10, 'value').should == 'OK'
8
8
  end
9
9
 
10
- it "sets the value" do
11
- @redises.setex(@key, 10000, 'value')
10
+ it 'sets the value' do
11
+ @redises.setex(@key, 10_000, 'value')
12
12
  @redises.get(@key).should == 'value'
13
13
  end
14
14
 
15
- it "sets the expiration time" do
16
- @redises.setex(@key, 10000, 'value')
15
+ it 'sets the expiration time' do
16
+ @redises.setex(@key, 10_000, 'value')
17
17
 
18
18
  # no guarantee these are the same
19
19
  @redises.real.ttl(@key).should > 0
20
20
  @redises.mock.ttl(@key).should > 0
21
21
  end
22
+
23
+ context 'when expiration time is zero' do
24
+ it 'raises Redis::CommandError' do
25
+ expect do
26
+ @redises.setex(@key, 0, 'value')
27
+ end.to raise_error(Redis::CommandError, 'ERR invalid expire time in setex')
28
+ end
29
+ end
30
+
31
+ context 'when expiration time is negative' do
32
+ it 'raises Redis::CommandError' do
33
+ expect do
34
+ @redises.setex(@key, -2, 'value')
35
+ end.to raise_error(Redis::CommandError, 'ERR invalid expire time in setex')
36
+ end
37
+ end
22
38
  end
@@ -3,21 +3,21 @@ require 'spec_helper'
3
3
  describe '#setnx(key, value)' do
4
4
  before { @key = 'mock-redis-test:setnx' }
5
5
 
6
- it "returns true if the key was absent" do
7
- @redises.setnx(@key, 1).should be_true
6
+ it 'returns true if the key was absent' do
7
+ @redises.setnx(@key, 1).should == true
8
8
  end
9
9
 
10
- it "returns false if the key was present" do
10
+ it 'returns false if the key was present' do
11
11
  @redises.set(@key, 2)
12
- @redises.setnx(@key, 1).should be_false
12
+ @redises.setnx(@key, 1).should == false
13
13
  end
14
14
 
15
- it "sets the value if missing" do
15
+ it 'sets the value if missing' do
16
16
  @redises.setnx(@key, 'value')
17
17
  @redises.get(@key).should == 'value'
18
18
  end
19
19
 
20
- it "does nothing if the value is present" do
20
+ it 'does nothing if the value is present' do
21
21
  @redises.set(@key, 'old')
22
22
  @redises.setnx(@key, 'new')
23
23
  @redises.get(@key).should == 'old'
@@ -1,30 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "#setrange(key, offset, value)" do
3
+ describe '#setrange(key, offset, value)' do
4
4
  before do
5
5
  @key = 'mock-redis-test:setrange'
6
- @redises.set(@key, "This is a string")
6
+ @redises.set(@key, 'This is a string')
7
7
  end
8
8
 
9
9
  it "returns the string's new length" do
10
- @redises.setrange(@key, 0, "That").should == 16
10
+ @redises.setrange(@key, 0, 'That').should == 16
11
11
  end
12
12
 
13
- it "updates part of the string" do
14
- @redises.setrange(@key, 0, "That")
15
- @redises.get(@key).should == "That is a string"
13
+ it 'updates part of the string' do
14
+ @redises.setrange(@key, 0, 'That')
15
+ @redises.get(@key).should == 'That is a string'
16
16
  end
17
17
 
18
- it "zero-pads the string if necessary" do
19
- @redises.setrange(@key, 20, "X")
18
+ it 'zero-pads the string if necessary' do
19
+ @redises.setrange(@key, 20, 'X')
20
20
  @redises.get(@key).should == "This is a string\000\000\000\000X"
21
21
  end
22
22
 
23
- it "stores things as strings" do
24
- other_key = "mock-redis-test:setrange-other-key"
23
+ it 'stores things as strings' do
24
+ other_key = 'mock-redis-test:setrange-other-key'
25
25
  @redises.setrange(other_key, 0, 1)
26
- @redises.get(other_key).should == "1"
26
+ @redises.get(other_key).should == '1'
27
27
  end
28
28
 
29
- it_should_behave_like "a string-only command"
29
+ it_should_behave_like 'a string-only command'
30
30
  end
@@ -6,36 +6,34 @@ describe '#sinter(key [, key, ...])' do
6
6
  @evens = 'mock-redis-test:sinter:evens'
7
7
  @primes = 'mock-redis-test:sinter: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 elements in the resulting set" do
15
- @redises.sinter(@evens, @primes).should == ["2"]
14
+ it 'returns the elements in the resulting set' do
15
+ @redises.sinter(@evens, @primes).should == ['2']
16
16
  end
17
17
 
18
- it "treats missing keys as empty sets" do
18
+ it 'treats missing keys as empty sets' do
19
19
  @redises.sinter(@destination, 'mock-redis-test:nonesuch').should == []
20
20
  end
21
21
 
22
- it "raises an error if given 0 sets" do
22
+ it 'raises an error if given 0 sets' do
23
23
  lambda do
24
24
  @redises.sinter
25
- end.should raise_error(RuntimeError)
25
+ end.should raise_error(Redis::CommandError)
26
26
  end
27
27
 
28
- it "raises an error if any argument is not a a set" do
28
+ it 'raises an error if any argument is not a a set' do
29
29
  @redises.set('mock-redis-test:notset', 1)
30
30
 
31
31
  lambda do
32
32
  @redises.sinter(@numbers, 'mock-redis-test:notset')
33
- end.should raise_error(RuntimeError)
33
+ end.should raise_error(Redis::CommandError)
34
34
 
35
35
  lambda do
36
36
  @redises.sinter('mock-redis-test:notset', @numbers)
37
- end.should raise_error(RuntimeError)
37
+ end.should raise_error(Redis::CommandError)
38
38
  end
39
39
  end
40
-
41
-
@@ -7,47 +7,47 @@ describe '#sinterstore(destination, key [, key, ...])' do
7
7
  @primes = 'mock-redis-test:sinterstore:primes'
8
8
  @destination = 'mock-redis-test:sinterstore:destination'
9
9
 
10
- (1..10).each {|i| @redises.sadd(@numbers, i) }
11
- [2, 4, 6, 8, 10].each {|i| @redises.sadd(@evens, i) }
12
- [2, 3, 5, 7].each {|i| @redises.sadd(@primes, i) }
10
+ (1..10).each { |i| @redises.sadd(@numbers, i) }
11
+ [2, 4, 6, 8, 10].each { |i| @redises.sadd(@evens, i) }
12
+ [2, 3, 5, 7].each { |i| @redises.sadd(@primes, i) }
13
13
  end
14
14
 
15
- it "returns the number of elements in the resulting set" do
15
+ it 'returns the number of elements in the resulting set' do
16
16
  @redises.sinterstore(@destination, @numbers, @evens).should == 5
17
17
  end
18
18
 
19
- it "stores the resulting set" do
19
+ it 'stores the resulting set' do
20
20
  @redises.sinterstore(@destination, @numbers, @evens)
21
21
  @redises.smembers(@destination).should == %w[10 8 6 4 2]
22
22
  end
23
23
 
24
- it "does not store empty sets" do
24
+ it 'does not store empty sets' do
25
25
  @redises.sinterstore(@destination, 'mock-redis-test:nonesuch', @numbers).should == 0
26
26
  @redises.get(@destination).should be_nil
27
27
  end
28
28
 
29
- it "removes existing elements in destination" do
29
+ it 'removes existing elements in destination' do
30
30
  @redises.sadd(@destination, 42)
31
31
 
32
32
  @redises.sinterstore(@destination, @primes)
33
33
  @redises.smembers(@destination).should == %w[7 5 3 2]
34
34
  end
35
35
 
36
- it "raises an error if given 0 sets" do
36
+ it 'raises an error if given 0 sets' do
37
37
  lambda do
38
38
  @redises.sinterstore(@destination)
39
- end.should raise_error(RuntimeError)
39
+ end.should raise_error(Redis::CommandError)
40
40
  end
41
41
 
42
- it "raises an error if any argument is not a a set" do
42
+ it 'raises an error if any argument is not a a set' do
43
43
  @redises.set('mock-redis-test:notset', 1)
44
44
 
45
45
  lambda do
46
46
  @redises.sinterstore(@destination, @numbers, 'mock-redis-test:notset')
47
- end.should raise_error(RuntimeError)
47
+ end.should raise_error(Redis::CommandError)
48
48
 
49
49
  lambda do
50
50
  @redises.sinterstore(@destination, 'mock-redis-test:notset', @numbers)
51
- end.should raise_error(RuntimeError)
51
+ end.should raise_error(Redis::CommandError)
52
52
  end
53
53
  end
@@ -7,23 +7,23 @@ describe '#sismember(key, member)' do
7
7
  @redises.sadd(@key, 'beer')
8
8
  end
9
9
 
10
- it "returns true if member is in set" do
11
- @redises.sismember(@key, 'whiskey').should be_true
12
- @redises.sismember(@key, 'beer').should be_true
10
+ it 'returns true if member is in set' do
11
+ @redises.sismember(@key, 'whiskey').should == true
12
+ @redises.sismember(@key, 'beer').should == true
13
13
  end
14
14
 
15
- it "returns false if member is not in set" do
16
- @redises.sismember(@key, 'cola').should be_false
15
+ it 'returns false if member is not in set' do
16
+ @redises.sismember(@key, 'cola').should == false
17
17
  end
18
18
 
19
- it "stringifies member" do
19
+ it 'stringifies member' do
20
20
  @redises.sadd(@key, '1')
21
- @redises.sismember(@key, 1).should be_true
21
+ @redises.sismember(@key, 1).should == true
22
22
  end
23
23
 
24
- it "treats a nonexistent value as an empty set" do
25
- @redises.sismember('mock-redis-test:nonesuch', 'beer').should be_false
24
+ it 'treats a nonexistent value as an empty set' do
25
+ @redises.sismember('mock-redis-test:nonesuch', 'beer').should == false
26
26
  end
27
27
 
28
- it_should_behave_like "a set-only command"
28
+ it_should_behave_like 'a set-only command'
29
29
  end
@@ -3,16 +3,26 @@ require 'spec_helper'
3
3
  describe '#smembers(key)' do
4
4
  before { @key = 'mock-redis-test:smembers' }
5
5
 
6
- it "returns [] for an empty set" do
6
+ it 'returns [] for an empty set' do
7
7
  @redises.smembers(@key).should == []
8
8
  end
9
9
 
10
10
  it "returns the set's members" do
11
- @redises.sadd(@key, "Hello")
12
- @redises.sadd(@key, "World")
13
- @redises.sadd(@key, "Test")
11
+ @redises.sadd(@key, 'Hello')
12
+ @redises.sadd(@key, 'World')
13
+ @redises.sadd(@key, 'Test')
14
14
  @redises.smembers(@key).should == %w[Test World Hello]
15
15
  end
16
16
 
17
- it_should_behave_like "a set-only command"
17
+ it 'returns unfrozen copies of the input' do
18
+ input = 'a string'
19
+ @redises.sadd(@key, input)
20
+ output = @redises.smembers(@key).first
21
+
22
+ expect(output).to eq input
23
+ expect(output).to_not equal input
24
+ expect(output).to_not be_frozen
25
+ end
26
+
27
+ it_should_behave_like 'a set-only command'
18
28
  end
@@ -9,33 +9,33 @@ describe '#smove(source, destination, member)' do
9
9
  @redises.sadd(@dest, 2)
10
10
  end
11
11
 
12
- it "returns true if the member exists in src" do
13
- @redises.smove(@src, @dest, 1).should be_true
12
+ it 'returns true if the member exists in src' do
13
+ @redises.smove(@src, @dest, 1).should == true
14
14
  end
15
15
 
16
- it "returns false if the member exists in src" do
17
- @redises.smove(@src, @dest, 'nope').should be_false
16
+ it 'returns false if the member exists in src' do
17
+ @redises.smove(@src, @dest, 'nope').should == false
18
18
  end
19
19
 
20
- it "returns true if the member exists in src and dest" do
20
+ it 'returns true if the member exists in src and dest' do
21
21
  @redises.sadd(@dest, 1)
22
- @redises.smove(@src, @dest, 1).should be_true
22
+ @redises.smove(@src, @dest, 1).should == true
23
23
  end
24
24
 
25
- it "moves member from source to destination" do
25
+ it 'moves member from source to destination' do
26
26
  @redises.smove(@src, @dest, 1)
27
- @redises.sismember(@dest, 1).should be_true
28
- @redises.sismember(@src, 1).should be_false
27
+ @redises.sismember(@dest, 1).should == true
28
+ @redises.sismember(@src, 1).should == false
29
29
  end
30
30
 
31
- it "cleans up empty sets" do
31
+ it 'cleans up empty sets' do
32
32
  @redises.smove(@src, @dest, 1)
33
33
  @redises.get(@src).should be_nil
34
34
  end
35
35
 
36
- it "treats a nonexistent value as an empty set" do
37
- @redises.smove('mock-redis-test:nonesuch', @dest, 1).should be_false
36
+ it 'treats a nonexistent value as an empty set' do
37
+ @redises.smove('mock-redis-test:nonesuch', @dest, 1).should == false
38
38
  end
39
39
 
40
- it_should_behave_like "a set-only command"
40
+ it_should_behave_like 'a set-only command'
41
41
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#sort(key, options)' do
4
+ before do
5
+ @key = 'mock-redis-test:list_sort'
6
+
7
+ @redises.rpush(@key, '1')
8
+ @redises.rpush(@key, '2')
9
+
10
+ @redises.set('mock-redis-test:values_1', 'a')
11
+ @redises.set('mock-redis-test:values_2', 'b')
12
+
13
+ @redises.set('mock-redis-test:weight_1', '2')
14
+ @redises.set('mock-redis-test:weight_2', '1')
15
+
16
+ @redises.hset('mock-redis-test:hash_1', 'key', 'x')
17
+ @redises.hset('mock-redis-test:hash_2', 'key', 'y')
18
+ end
19
+
20
+ it_should_behave_like 'a sortable'
21
+ end