fakeredis 0.5.0 → 0.6.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.
@@ -5,6 +5,7 @@ require 'fakeredis'
5
5
  require "fakeredis/rspec"
6
6
 
7
7
  require "support/shared_examples/sortable"
8
+ require "support/shared_examples/bitwise_operation"
8
9
 
9
10
  RSpec.configure do |config|
10
11
  # replaces -b -fdoc --color in .rspec
@@ -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,51 +195,51 @@ 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
@@ -241,44 +247,43 @@ module FakeRedis
241
247
  @client.setnx("key1", "new value")
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
-
283
288
  end
284
289
  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
+ @client.publish("channel1", "val1").should be == 0
13
+ @client.publish("channel1", "val2").should be == 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
+ channel.should be == "channel1"
30
+ end
31
+
32
+ on.message do |channel,msg|
33
+ channel.should be == "channel1"
34
+ msgs << msg
35
+ end
36
+
37
+ on.unsubscribe do
38
+ unsubscribe_sent = true
39
+ end
40
+ end
41
+ end
42
+
43
+ msgs.should be == ["val1", "val2"]
44
+ subscribe_sent.should
45
+ unsubscribe_sent.should
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
+ msgs[0].should be == ["channel1", "val1"]
63
+ msgs[1].should be == ["channel2", "val2"]
64
+ msgs[2].should be == ["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
+ pattern.should be == "channel*"
88
+ msgs << msg
89
+ end
90
+
91
+ on.punsubscribe do
92
+ unsubscribe_sent = true
93
+ end
94
+ end
95
+ end
96
+
97
+ msgs.should be == ["val1", "val2", "val3"]
98
+ subscribe_sent.should
99
+ unsubscribe_sent.should
100
+ end
101
+ end
102
+
103
+ context "punsubscribe" do
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,59 @@
1
+ shared_examples_for "a bitwise operation" do |operator|
2
+ it 'raises an argument error when not passed any source keys' do
3
+ lambda { @client.bitop(operator, "destkey") }.should raise_error(Redis::CommandError)
4
+ end
5
+
6
+ it "should not create destination key if nothing found" do
7
+ @client.bitop(operator, "dest1", "nothing_here1").should be == 0
8
+ @client.exists("dest1").should be false
9
+ end
10
+
11
+ it "should accept operator as a case-insensitive symbol" do
12
+ @client.set("key1", "foobar")
13
+ @client.bitop(operator.to_s.downcase.to_sym, "dest1", "key1")
14
+ @client.bitop(operator.to_s.upcase.to_sym, "dest2", "key1")
15
+
16
+ @client.get("dest1").should be == "foobar"
17
+ @client.get("dest2").should be == "foobar"
18
+ end
19
+
20
+ it "should accept operator as a case-insensitive string" do
21
+ @client.set("key1", "foobar")
22
+ @client.bitop(operator.to_s.downcase, "dest1", "key1")
23
+ @client.bitop(operator.to_s.upcase, "dest2", "key1")
24
+
25
+ @client.get("dest1").should be == "foobar"
26
+ @client.get("dest2").should be == "foobar"
27
+ end
28
+
29
+ it "should copy original string for single key" do
30
+ @client.set("key1", "foobar")
31
+ @client.bitop(operator, "dest1", "key1")
32
+
33
+ @client.get("dest1").should be == "foobar"
34
+ end
35
+
36
+ it "should copy original string for single key" do
37
+ @client.set("key1", "foobar")
38
+ @client.bitop(operator, "dest1", "key1")
39
+
40
+ @client.get("dest1").should be == "foobar"
41
+ end
42
+
43
+ it "should return length of the string stored in the destination key" do
44
+ @client.set("key1", "foobar")
45
+ @client.set("key2", "baz")
46
+
47
+ @client.bitop(operator, "dest1", "key1").should be == 6
48
+ @client.bitop(operator, "dest2", "key2").should be == 3
49
+ end
50
+
51
+ it "should overwrite previous value with new one" do
52
+ @client.set("key1", "foobar")
53
+ @client.set("key2", "baz")
54
+ @client.bitop(operator, "dest1", "key1")
55
+ @client.bitop(operator, "dest1", "key2")
56
+
57
+ @client.get("dest1").should be == "baz"
58
+ end
59
+ end