fakeredis 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -0
  3. data/.travis.yml +14 -5
  4. data/LICENSE +1 -1
  5. data/README.md +42 -24
  6. data/fakeredis.gemspec +1 -1
  7. data/lib/fakeredis.rb +28 -0
  8. data/lib/fakeredis/bitop_command.rb +56 -0
  9. data/lib/fakeredis/command_executor.rb +6 -9
  10. data/lib/fakeredis/expiring_hash.rb +3 -5
  11. data/lib/fakeredis/geo_commands.rb +142 -0
  12. data/lib/fakeredis/geo_set.rb +84 -0
  13. data/lib/fakeredis/minitest.rb +24 -0
  14. data/lib/fakeredis/rspec.rb +1 -0
  15. data/lib/fakeredis/sort_method.rb +3 -3
  16. data/lib/fakeredis/sorted_set_store.rb +1 -1
  17. data/lib/fakeredis/transaction_commands.rb +2 -2
  18. data/lib/fakeredis/version.rb +1 -1
  19. data/lib/fakeredis/zset.rb +8 -2
  20. data/lib/redis/connection/memory.rb +650 -82
  21. data/spec/bitop_command_spec.rb +209 -0
  22. data/spec/command_executor_spec.rb +15 -0
  23. data/spec/compatibility_spec.rb +1 -1
  24. data/spec/connection_spec.rb +21 -21
  25. data/spec/fakeredis_spec.rb +73 -0
  26. data/spec/geo_set_spec.rb +164 -0
  27. data/spec/hashes_spec.rb +138 -57
  28. data/spec/hyper_log_logs_spec.rb +50 -0
  29. data/spec/keys_spec.rb +232 -90
  30. data/spec/lists_spec.rb +91 -35
  31. data/spec/memory_spec.rb +80 -7
  32. data/spec/server_spec.rb +38 -24
  33. data/spec/sets_spec.rb +112 -46
  34. data/spec/sort_method_spec.rb +6 -0
  35. data/spec/sorted_sets_spec.rb +482 -150
  36. data/spec/spec_helper.rb +9 -18
  37. data/spec/spec_helper_live_redis.rb +4 -4
  38. data/spec/strings_spec.rb +113 -79
  39. data/spec/subscription_spec.rb +107 -0
  40. data/spec/support/shared_examples/bitwise_operation.rb +59 -0
  41. data/spec/support/shared_examples/sortable.rb +20 -16
  42. data/spec/transactions_spec.rb +34 -13
  43. data/spec/upcase_method_name_spec.rb +2 -2
  44. metadata +23 -6
data/spec/spec_helper.rb CHANGED
@@ -1,28 +1,19 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
1
  require 'rspec'
2
+
3
+ $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.join(__dir__, '..'))
5
+ Dir['spec/support/**/*.rb'].each { |f| require f }
6
+
4
7
  require 'fakeredis'
5
8
  require "fakeredis/rspec"
6
9
 
7
- require "support/shared_examples/sortable"
8
-
9
10
  RSpec.configure do |config|
10
- # replaces -b -fdoc --color in .rspec
11
- config.color = true
12
- config.default_formatter = "doc"
13
- config.backtrace_exclusion_patterns = []
14
-
15
- config.mock_with :rspec do |c|
16
- # TODO: upgrade should syntax to expect syntax
17
- c.syntax = [:should, :expect]
18
- end
11
+ # Enable memory adapter
12
+ config.before(:each) { FakeRedis.enable }
19
13
 
20
- config.expect_with :rspec do |c|
21
- # TODO: upgrade should syntax to expect syntax
22
- c.syntax = [:should, :expect]
23
- end
14
+ config.backtrace_exclusion_patterns = []
24
15
  end
25
16
 
26
17
  def fakeredis?
27
- true
18
+ FakeRedis.enabled?
28
19
  end
@@ -1,14 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
- # Remove memory so we test against actual redis
4
- Redis::Connection.drivers.pop
5
-
6
3
  RSpec.configure do |config|
7
4
  config.before(:each) do
5
+ # Disable so we test against actual redis
6
+ FakeRedis.disable
7
+
8
8
  Redis.new.flushall
9
9
  end
10
10
  end
11
11
 
12
12
  def fakeredis?
13
- false
13
+ FakeRedis.enabled?
14
14
  end
data/spec/strings_spec.rb CHANGED
@@ -13,42 +13,42 @@ module FakeRedis
13
13
  @client.set("key1", "Hello")
14
14
  @client.append("key1", " World")
15
15
 
16
- @client.get("key1").should be == "Hello World"
16
+ expect(@client.get("key1")).to eq("Hello World")
17
17
  end
18
18
 
19
19
  it "should decrement the integer value of a key by one" do
20
20
  @client.set("counter", "1")
21
21
  @client.decr("counter")
22
22
 
23
- @client.get("counter").should be == "0"
23
+ expect(@client.get("counter")).to eq("0")
24
24
  end
25
25
 
26
26
  it "should decrement the integer value of a key by the given number" do
27
27
  @client.set("counter", "10")
28
28
  @client.decrby("counter", "5")
29
29
 
30
- @client.get("counter").should be == "5"
30
+ expect(@client.get("counter")).to eq("5")
31
31
  end
32
32
 
33
33
  it "should get the value of a key" do
34
- @client.get("key2").should be == nil
34
+ expect(@client.get("key2")).to eq(nil)
35
35
  end
36
36
 
37
37
  it "should returns the bit value at offset in the string value stored at key" do
38
38
  @client.set("key1", "a")
39
39
 
40
- @client.getbit("key1", 1).should be == 1
41
- @client.getbit("key1", 2).should be == 1
42
- @client.getbit("key1", 3).should be == 0
43
- @client.getbit("key1", 4).should be == 0
44
- @client.getbit("key1", 5).should be == 0
45
- @client.getbit("key1", 6).should be == 0
46
- @client.getbit("key1", 7).should be == 1
40
+ expect(@client.getbit("key1", 1)).to eq(1)
41
+ expect(@client.getbit("key1", 2)).to eq(1)
42
+ expect(@client.getbit("key1", 3)).to eq(0)
43
+ expect(@client.getbit("key1", 4)).to eq(0)
44
+ expect(@client.getbit("key1", 5)).to eq(0)
45
+ expect(@client.getbit("key1", 6)).to eq(0)
46
+ expect(@client.getbit("key1", 7)).to eq(1)
47
47
  end
48
48
 
49
49
  it "should allow direct bit manipulation even if the string isn't set" do
50
50
  @client.setbit("key1", 10, 1)
51
- @client.getbit("key1", 10).should be == 1
51
+ expect(@client.getbit("key1", 10)).to eq(1)
52
52
  end
53
53
 
54
54
  context 'when a bit is previously set to 0' do
@@ -78,80 +78,86 @@ module FakeRedis
78
78
  it "should get a substring of the string stored at a key" do
79
79
  @client.set("key1", "This a message")
80
80
 
81
- @client.getrange("key1", 0, 3).should be == "This"
82
- @client.substr("key1", 0, 3).should be == "This"
81
+ expect(@client.getrange("key1", 0, 3)).to eq("This")
82
+ expect(@client.substr("key1", 0, 3)).to eq("This")
83
83
  end
84
84
 
85
85
  it "should set the string value of a key and return its old value" do
86
86
  @client.set("key1","value1")
87
87
 
88
- @client.getset("key1", "value2").should be == "value1"
89
- @client.get("key1").should be == "value2"
88
+ expect(@client.getset("key1", "value2")).to eq("value1")
89
+ expect(@client.get("key1")).to eq("value2")
90
90
  end
91
91
 
92
92
  it "should return nil for #getset if the key does not exist when setting" do
93
- @client.getset("key1", "value1").should be == nil
94
- @client.get("key1").should be == "value1"
93
+ expect(@client.getset("key1", "value1")).to eq(nil)
94
+ expect(@client.get("key1")).to eq("value1")
95
95
  end
96
96
 
97
97
  it "should increment the integer value of a key by one" do
98
98
  @client.set("counter", "1")
99
- @client.incr("counter").should be == 2
99
+ expect(@client.incr("counter")).to eq(2)
100
100
 
101
- @client.get("counter").should be == "2"
101
+ expect(@client.get("counter")).to eq("2")
102
102
  end
103
103
 
104
104
  it "should not change the expire value of the key during incr" do
105
105
  @client.set("counter", "1")
106
- @client.expire("counter", 600).should be true
107
- @client.ttl("counter").should be == 600
108
- @client.incr("counter").should be == 2
109
- @client.ttl("counter").should be == 600
106
+ expect(@client.expire("counter", 600)).to be true
107
+ expect(@client.ttl("counter")).to eq(600)
108
+ expect(@client.incr("counter")).to eq(2)
109
+ expect(@client.ttl("counter")).to eq(600)
110
110
  end
111
111
 
112
112
  it "should decrement the integer value of a key by one" do
113
113
  @client.set("counter", "1")
114
- @client.decr("counter").should be == 0
114
+ expect(@client.decr("counter")).to eq(0)
115
115
 
116
- @client.get("counter").should be == "0"
116
+ expect(@client.get("counter")).to eq("0")
117
117
  end
118
118
 
119
119
  it "should not change the expire value of the key during decr" do
120
120
  @client.set("counter", "2")
121
- @client.expire("counter", 600).should be true
122
- @client.ttl("counter").should be == 600
123
- @client.decr("counter").should be == 1
124
- @client.ttl("counter").should be == 600
121
+ expect(@client.expire("counter", 600)).to be true
122
+ expect(@client.ttl("counter")).to eq(600)
123
+ expect(@client.decr("counter")).to eq(1)
124
+ expect(@client.ttl("counter")).to eq(600)
125
125
  end
126
126
 
127
127
  it "should increment the integer value of a key by the given number" do
128
128
  @client.set("counter", "10")
129
- @client.incrby("counter", "5").should be == 15
130
- @client.incrby("counter", 2).should be == 17
131
- @client.get("counter").should be == "17"
129
+ expect(@client.incrby("counter", "5")).to eq(15)
130
+ expect(@client.incrby("counter", 2)).to eq(17)
131
+ expect(@client.get("counter")).to eq("17")
132
+ end
133
+
134
+ it "should increment the float value of a key by the given number" do
135
+ @client.set("counter", 10.0)
136
+ expect(@client.incrbyfloat("counter", 2.1)).to eq(12.1)
137
+ expect(@client.get("counter")).to eq("12.1")
132
138
  end
133
139
 
134
140
  it "should not change the expire value of the key during incrby" do
135
141
  @client.set("counter", "1")
136
- @client.expire("counter", 600).should be true
137
- @client.ttl("counter").should be == 600
138
- @client.incrby("counter", "5").should be == 6
139
- @client.ttl("counter").should be == 600
142
+ expect(@client.expire("counter", 600)).to be true
143
+ expect(@client.ttl("counter")).to eq(600)
144
+ expect(@client.incrby("counter", "5")).to eq(6)
145
+ expect(@client.ttl("counter")).to eq(600)
140
146
  end
141
147
 
142
148
  it "should decrement the integer value of a key by the given number" do
143
149
  @client.set("counter", "10")
144
- @client.decrby("counter", "5").should be == 5
145
- @client.decrby("counter", 2).should be == 3
146
- @client.get("counter").should be == "3"
150
+ expect(@client.decrby("counter", "5")).to eq(5)
151
+ expect(@client.decrby("counter", 2)).to eq(3)
152
+ expect(@client.get("counter")).to eq("3")
147
153
  end
148
154
 
149
155
  it "should not change the expire value of the key during decrby" do
150
156
  @client.set("counter", "8")
151
- @client.expire("counter", 600).should be true
152
- @client.ttl("counter").should be == 600
153
- @client.decrby("counter", "3").should be == 5
154
- @client.ttl("counter").should be == 600
157
+ expect(@client.expire("counter", 600)).to be true
158
+ expect(@client.ttl("counter")).to eq(600)
159
+ expect(@client.decrby("counter", "3")).to eq(5)
160
+ expect(@client.ttl("counter")).to eq(600)
155
161
  end
156
162
 
157
163
  it "should get the values of all the given keys" do
@@ -159,29 +165,29 @@ module FakeRedis
159
165
  @client.set("key2", "value2")
160
166
  @client.set("key3", "value3")
161
167
 
162
- @client.mget("key1", "key2", "key3").should be == ["value1", "value2", "value3"]
163
- @client.mget(["key1", "key2", "key3"]).should be == ["value1", "value2", "value3"]
168
+ expect(@client.mget("key1", "key2", "key3")).to eq(["value1", "value2", "value3"])
169
+ expect(@client.mget(["key1", "key2", "key3"])).to eq(["value1", "value2", "value3"])
164
170
  end
165
171
 
166
172
  it "returns nil for non existent keys" do
167
173
  @client.set("key1", "value1")
168
174
  @client.set("key3", "value3")
169
175
 
170
- @client.mget("key1", "key2", "key3", "key4").should be == ["value1", nil, "value3", nil]
171
- @client.mget(["key1", "key2", "key3", "key4"]).should be == ["value1", nil, "value3", nil]
176
+ expect(@client.mget("key1", "key2", "key3", "key4")).to eq(["value1", nil, "value3", nil])
177
+ expect(@client.mget(["key1", "key2", "key3", "key4"])).to eq(["value1", nil, "value3", nil])
172
178
  end
173
179
 
174
180
  it 'raises an argument error when not passed any fields' do
175
181
  @client.set("key3", "value3")
176
182
 
177
- lambda { @client.mget }.should raise_error(Redis::CommandError)
183
+ expect { @client.mget }.to raise_error(Redis::CommandError)
178
184
  end
179
185
 
180
186
  it "should set multiple keys to multiple values" do
181
187
  @client.mset(:key1, "value1", :key2, "value2")
182
188
 
183
- @client.get("key1").should be == "value1"
184
- @client.get("key2").should be == "value2"
189
+ expect(@client.get("key1")).to eq("value1")
190
+ expect(@client.get("key2")).to eq("value2")
185
191
  end
186
192
 
187
193
  it "should raise error if command arguments count is wrong" do
@@ -189,96 +195,124 @@ module FakeRedis
189
195
  expect { @client.mset(:key1) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'mset' command")
190
196
  expect { @client.mset(:key1, "value", :key2) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for MSET")
191
197
 
192
- @client.get("key1").should be_nil
193
- @client.get("key2").should be_nil
198
+ expect(@client.get("key1")).to be_nil
199
+ expect(@client.get("key2")).to be_nil
194
200
  end
195
201
 
196
202
  it "should set multiple keys to multiple values, only if none of the keys exist" do
197
- @client.msetnx(:key1, "value1", :key2, "value2").should be == true
198
- @client.msetnx(:key1, "value3", :key2, "value4").should be == false
203
+ expect(@client.msetnx(:key1, "value1", :key2, "value2")).to eq(true)
204
+ expect(@client.msetnx(:key1, "value3", :key2, "value4")).to eq(false)
199
205
 
200
- @client.get("key1").should be == "value1"
201
- @client.get("key2").should be == "value2"
206
+ expect(@client.get("key1")).to eq("value1")
207
+ expect(@client.get("key2")).to eq("value2")
202
208
  end
203
209
 
204
210
  it "should set multiple keys to multiple values with a hash" do
205
211
  @client.mapped_mset(:key1 => "value1", :key2 => "value2")
206
212
 
207
- @client.get("key1").should be == "value1"
208
- @client.get("key2").should be == "value2"
213
+ expect(@client.get("key1")).to eq("value1")
214
+ expect(@client.get("key2")).to eq("value2")
209
215
  end
210
216
 
211
217
  it "should set multiple keys to multiple values with a hash, only if none of the keys exist" do
212
- @client.mapped_msetnx(:key1 => "value1", :key2 => "value2").should be == true
213
- @client.mapped_msetnx(:key1 => "value3", :key2 => "value4").should be == false
218
+ expect(@client.mapped_msetnx(:key1 => "value1", :key2 => "value2")).to eq(true)
219
+ expect(@client.mapped_msetnx(:key1 => "value3", :key2 => "value4")).to eq(false)
214
220
 
215
- @client.get("key1").should be == "value1"
216
- @client.get("key2").should be == "value2"
221
+ expect(@client.get("key1")).to eq("value1")
222
+ expect(@client.get("key2")).to eq("value2")
217
223
  end
218
224
 
219
225
  it "should set the string value of a key" do
220
226
  @client.set("key1", "1")
221
227
 
222
- @client.get("key1").should be == "1"
228
+ expect(@client.get("key1")).to eq("1")
223
229
  end
224
230
 
225
231
  it "should sets or clears the bit at offset in the string value stored at key" do
226
232
  @client.set("key1", "abc")
227
233
  @client.setbit("key1", 11, 1)
228
234
 
229
- @client.get("key1").should be == "arc"
235
+ expect(@client.get("key1")).to eq("arc")
230
236
  end
231
237
 
232
238
  it "should set the value and expiration of a key" do
233
239
  @client.setex("key1", 30, "value1")
234
240
 
235
- @client.get("key1").should be == "value1"
236
- @client.ttl("key1").should be == 30
241
+ expect(@client.get("key1")).to eq("value1")
242
+ expect(@client.ttl("key1")).to eq(30)
237
243
  end
238
244
 
239
245
  it "should set the value of a key, only if the key does not exist" do
240
- @client.set("key1", "test value")
241
- @client.setnx("key1", "new value")
246
+ expect(@client.setnx("key1", "test value")).to eq(true)
247
+ expect(@client.setnx("key1", "new value")).to eq(false)
242
248
  @client.setnx("key2", "another value")
243
249
 
244
- @client.get("key1").should be == "test value"
245
- @client.get("key2").should be == "another value"
250
+ expect(@client.get("key1")).to eq("test value")
251
+ expect(@client.get("key2")).to eq("another value")
246
252
  end
247
253
 
248
254
  it "should overwrite part of a string at key starting at the specified offset" do
249
255
  @client.set("key1", "Hello World")
250
256
  @client.setrange("key1", 6, "Redis")
251
257
 
252
- @client.get("key1").should be == "Hello Redis"
258
+ expect(@client.get("key1")).to eq("Hello Redis")
253
259
  end
254
260
 
255
261
  it "should get the length of the value stored in a key" do
256
262
  @client.set("key1", "abc")
257
263
 
258
- @client.strlen("key1").should be == 3
264
+ expect(@client.strlen("key1")).to eq(3)
259
265
  end
260
266
 
261
267
  it "should return 0 bits when there's no key" do
262
- @client.bitcount("key1").should be == 0
268
+ expect(@client.bitcount("key1")).to eq(0)
263
269
  end
264
270
 
265
271
  it "should count the number of bits of a string" do
266
272
  @client.set("key1", "foobar")
267
- @client.bitcount("key1").should be == 26
273
+ expect(@client.bitcount("key1")).to eq(26)
268
274
  end
269
275
 
270
276
  it "should count correctly with UTF-8 strings" do
271
277
  @client.set("key1", '判')
272
- @client.bitcount("key1").should be == 10
278
+ expect(@client.bitcount("key1")).to eq(10)
273
279
  end
274
280
 
275
281
  it "should count the number of bits of a string given a range" do
276
282
  @client.set("key1", "foobar")
277
283
 
278
- @client.bitcount("key1", 0, 0).should be == 4
279
- @client.bitcount("key1", 1, 1).should be == 6
280
- @client.bitcount("key1", 0, 1).should be == 10
284
+ expect(@client.bitcount("key1", 0, 0)).to eq(4)
285
+ expect(@client.bitcount("key1", 1, 1)).to eq(6)
286
+ expect(@client.bitcount("key1", 0, 1)).to eq(10)
281
287
  end
282
288
 
289
+ describe "#bitpos" do
290
+ it "should return -1 when there's no key" do
291
+ expect(@client.bitpos("key", 0)).to eq(-1)
292
+ end
293
+
294
+ it "should return -1 for empty key" do
295
+ @client.set("key", "")
296
+ expect(@client.bitpos("key", 0)).to eq(-1)
297
+ end
298
+
299
+ it "should return position of the bit in a string" do
300
+ @client.set("key", "foobar") # 01100110 01101111 01101111
301
+ expect(@client.bitpos("key", 1)).to eq(1)
302
+ end
303
+
304
+ it "should return position of the bit correctly with UTF-8 strings" do
305
+ @client.set("key", "判") # 11100101 10001000 10100100
306
+ expect(@client.bitpos("key", 0)).to eq(3)
307
+ end
308
+
309
+ it "should return position of the bit in a string given a range" do
310
+ @client.set("key", "foobar")
311
+
312
+ expect(@client.bitpos("key", 1, 0)).to eq(1)
313
+ expect(@client.bitpos("key", 1, 1, 2)).to eq(9)
314
+ expect(@client.bitpos("key", 0, 1, -1)).to eq(8)
315
+ end
316
+ end
283
317
  end
284
318
  end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+ require 'timeout' #Need to use this avoid blocking
3
+
4
+ module FakeRedis
5
+ describe "SubscriptionMethods" do
6
+ before(:each) do
7
+ @client = Redis.new
8
+ end
9
+
10
+ context "publish" do
11
+ it "should add to channels" do
12
+ expect(@client.publish("channel1", "val1")).to eq(0)
13
+ expect(@client.publish("channel1", "val2")).to eq(0)
14
+ end
15
+ end
16
+
17
+ context "subscribe" do
18
+ it "should get all messages from a channel" do
19
+ @client.publish("channel1", "val1")
20
+ @client.publish("channel1", "val2")
21
+ @client.publish("channel2", "val3")
22
+
23
+ msgs = []
24
+ subscribe_sent = unsubscribe_sent = false
25
+ Timeout.timeout(1) do
26
+ @client.subscribe("channel1") do |on|
27
+ on.subscribe do |channel|
28
+ subscribe_sent = true
29
+ expect(channel).to eq("channel1")
30
+ end
31
+
32
+ on.message do |channel,msg|
33
+ expect(channel).to eq("channel1")
34
+ msgs << msg
35
+ end
36
+
37
+ on.unsubscribe do
38
+ unsubscribe_sent = true
39
+ end
40
+ end
41
+ end
42
+
43
+ expect(msgs).to eq(["val1", "val2"])
44
+ expect(subscribe_sent).to eq(true)
45
+ expect(unsubscribe_sent).to eq(true)
46
+ end
47
+
48
+ it "should get all messages from multiple channels" do
49
+ @client.publish("channel1", "val1")
50
+ @client.publish("channel2", "val2")
51
+ @client.publish("channel2", "val3")
52
+
53
+ msgs = []
54
+ Timeout.timeout(1) do
55
+ @client.subscribe("channel1", "channel2") do |on|
56
+ on.message do |channel,msg|
57
+ msgs << [channel, msg]
58
+ end
59
+ end
60
+ end
61
+
62
+ expect(msgs[0]).to eq(["channel1", "val1"])
63
+ expect(msgs[1]).to eq(["channel2", "val2"])
64
+ expect(msgs[2]).to eq(["channel2", "val3"])
65
+ end
66
+ end
67
+
68
+ context "unsubscribe" do
69
+ end
70
+
71
+ context "with patterns" do
72
+ context "psubscribe" do
73
+ it "should get all messages using pattern" do
74
+ @client.publish("channel1", "val1")
75
+ @client.publish("channel1", "val2")
76
+ @client.publish("channel2", "val3")
77
+
78
+ msgs = []
79
+ subscribe_sent = unsubscribe_sent = false
80
+ Timeout.timeout(1) do
81
+ @client.psubscribe("channel*") do |on|
82
+ on.psubscribe do |channel|
83
+ subscribe_sent = true
84
+ end
85
+
86
+ on.pmessage do |pattern,channel,msg|
87
+ expect(pattern).to eq("channel*")
88
+ msgs << msg
89
+ end
90
+
91
+ on.punsubscribe do
92
+ unsubscribe_sent = true
93
+ end
94
+ end
95
+ end
96
+
97
+ expect(msgs).to eq(["val1", "val2", "val3"])
98
+ expect(subscribe_sent).to eq(true)
99
+ expect(unsubscribe_sent).to eq(true)
100
+ end
101
+ end
102
+
103
+ context "punsubscribe" do
104
+ end
105
+ end
106
+ end
107
+ end