redis-file 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +7 -0
- data/LICENSE +21 -0
- data/README.md +86 -0
- data/Rakefile +27 -0
- data/lib/redis-file.rb +6 -0
- data/lib/redis-file/expiring_hash.rb +70 -0
- data/lib/redis-file/rspec.rb +23 -0
- data/lib/redis-file/sorted_set_argument_handler.rb +74 -0
- data/lib/redis-file/sorted_set_store.rb +80 -0
- data/lib/redis-file/version.rb +3 -0
- data/lib/redis-file/zset.rb +29 -0
- data/lib/redis/connection/file.rb +960 -0
- data/redis-file.gemspec +23 -0
- data/spec/compatibility_spec.rb +9 -0
- data/spec/connection_spec.rb +85 -0
- data/spec/hashes_spec.rb +182 -0
- data/spec/keys_spec.rb +248 -0
- data/spec/lists_spec.rb +195 -0
- data/spec/server_spec.rb +100 -0
- data/spec/sets_spec.rb +178 -0
- data/spec/sorted_sets_spec.rb +425 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/spec_helper_live_redis.rb +14 -0
- data/spec/strings_spec.rb +236 -0
- data/spec/transactions_spec.rb +19 -0
- data/spec/upcase_method_name_spec.rb +18 -0
- metadata +103 -0
@@ -0,0 +1,425 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RedisFile
|
4
|
+
Infinity = 1.0/0.0
|
5
|
+
|
6
|
+
describe "SortedSetsMethods" do
|
7
|
+
before(:each) do
|
8
|
+
@client = Redis.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should add a member to a sorted set, or update its score if it already exists" do
|
12
|
+
@client.zadd("key", 1, "val").should be(true)
|
13
|
+
@client.zscore("key", "val").should be == 1.0
|
14
|
+
|
15
|
+
@client.zadd("key", 2, "val").should be(false)
|
16
|
+
@client.zscore("key", "val").should be == 2.0
|
17
|
+
|
18
|
+
# These assertions only work in redis-rb v3.0.2 or higher
|
19
|
+
@client.zadd("key2", "inf", "val").should be == true
|
20
|
+
@client.zscore("key2", "val").should be == Infinity
|
21
|
+
|
22
|
+
@client.zadd("key3", "+inf", "val").should be == true
|
23
|
+
@client.zscore("key3", "val").should be == Infinity
|
24
|
+
|
25
|
+
@client.zadd("key4", "-inf", "val").should be == true
|
26
|
+
@client.zscore("key4", "val").should be == -Infinity
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return a nil score for value not in a sorted set or empty key" do
|
30
|
+
@client.zadd "key", 1, "val"
|
31
|
+
|
32
|
+
@client.zscore("key", "val").should be == 1.0
|
33
|
+
@client.zscore("key", "val2").should be_nil
|
34
|
+
@client.zscore("key2", "val").should be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should add multiple members to a sorted set, or update its score if it already exists" do
|
38
|
+
@client.zadd("key", [1, "val", 2, "val2"]).should be == 2
|
39
|
+
@client.zscore("key", "val").should be == 1
|
40
|
+
@client.zscore("key", "val2").should be == 2
|
41
|
+
|
42
|
+
@client.zadd("key", [[5, "val"], [3, "val3"], [4, "val4"]]).should be == 2
|
43
|
+
@client.zscore("key", "val").should be == 5
|
44
|
+
@client.zscore("key", "val2").should be == 2
|
45
|
+
@client.zscore("key", "val3").should be == 3
|
46
|
+
@client.zscore("key", "val4").should be == 4
|
47
|
+
|
48
|
+
@client.zadd("key", [[5, "val5"], [3, "val6"]]).should be == 2
|
49
|
+
@client.zscore("key", "val5").should be == 5
|
50
|
+
@client.zscore("key", "val6").should be == 3
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should error with wrong number of arguments when adding members" do
|
54
|
+
lambda { @client.zadd("key") }.should raise_error(ArgumentError, "wrong number of arguments")
|
55
|
+
lambda { @client.zadd("key", 1) }.should raise_error(ArgumentError, "wrong number of arguments")
|
56
|
+
lambda { @client.zadd("key", [1]) }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'zadd' command")
|
57
|
+
lambda { @client.zadd("key", [1, "val", 2]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
58
|
+
lambda { @client.zadd("key", [[1, "val"], [2]]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should allow floats as scores when adding or updating" do
|
62
|
+
@client.zadd("key", 4.321, "val").should be(true)
|
63
|
+
@client.zscore("key", "val").should be == 4.321
|
64
|
+
|
65
|
+
@client.zadd("key", 54.3210, "val").should be(false)
|
66
|
+
@client.zscore("key", "val").should be == 54.321
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should remove members from sorted sets" do
|
70
|
+
@client.zrem("key", "val").should be(false)
|
71
|
+
@client.zadd("key", 1, "val").should be(true)
|
72
|
+
@client.zrem("key", "val").should be(true)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should remove multiple members from sorted sets" do
|
76
|
+
@client.zrem("key2", %w(val)).should be == 0
|
77
|
+
@client.zrem("key2", %w(val val2 val3)).should be == 0
|
78
|
+
|
79
|
+
@client.zadd("key2", 1, "val")
|
80
|
+
@client.zadd("key2", 1, "val2")
|
81
|
+
@client.zadd("key2", 1, "val3")
|
82
|
+
|
83
|
+
@client.zrem("key2", %w(val val2 val3 val4)).should be == 3
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should remove sorted set's key when it is empty" do
|
87
|
+
@client.zadd("key", 1, "val")
|
88
|
+
@client.zrem("key", "val")
|
89
|
+
@client.exists("key").should be == false
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should get the number of members in a sorted set" do
|
93
|
+
@client.zadd("key", 1, "val2")
|
94
|
+
@client.zadd("key", 2, "val1")
|
95
|
+
@client.zadd("key", 5, "val3")
|
96
|
+
|
97
|
+
@client.zcard("key").should be == 3
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should count the members in a sorted set with scores within the given values" do
|
101
|
+
@client.zadd("key", 1, "val1")
|
102
|
+
@client.zadd("key", 2, "val2")
|
103
|
+
@client.zadd("key", 3, "val3")
|
104
|
+
|
105
|
+
@client.zcount("key", 2, 3).should be == 2
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should increment the score of a member in a sorted set" do
|
109
|
+
@client.zadd("key", 1, "val1")
|
110
|
+
@client.zincrby("key", 2, "val1").should be == 3
|
111
|
+
@client.zscore("key", "val1").should be == 3
|
112
|
+
end
|
113
|
+
|
114
|
+
it "initializes the sorted set if the key wasnt already set" do
|
115
|
+
@client.zincrby("key", 1, "val1").should be == 1
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should convert the key to a string for zscore" do
|
119
|
+
@client.zadd("key", 1, 1)
|
120
|
+
@client.zscore("key", 1).should be == 1
|
121
|
+
end
|
122
|
+
|
123
|
+
# These won't pass until redis-rb releases v3.0.2
|
124
|
+
it "should handle infinity values when incrementing a sorted set key" do
|
125
|
+
@client.zincrby("bar", "+inf", "s2").should be == Infinity
|
126
|
+
@client.zincrby("bar", "-inf", "s1").should be == -Infinity
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should return a range of members in a sorted set, by index" do
|
130
|
+
@client.zadd("key", 1, "one")
|
131
|
+
@client.zadd("key", 2, "two")
|
132
|
+
@client.zadd("key", 3, "three")
|
133
|
+
|
134
|
+
@client.zrange("key", 0, -1).should be == ["one", "two", "three"]
|
135
|
+
@client.zrange("key", 1, 2).should be == ["two", "three"]
|
136
|
+
@client.zrange("key", 0, -1, :withscores => true).should be == [["one", 1], ["two", 2], ["three", 3]]
|
137
|
+
@client.zrange("key", 1, 2, :with_scores => true).should be == [["two", 2], ["three", 3]]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should sort zrange results logically" do
|
141
|
+
@client.zadd("key", 5, "val2")
|
142
|
+
@client.zadd("key", 5, "val3")
|
143
|
+
@client.zadd("key", 5, "val1")
|
144
|
+
|
145
|
+
@client.zrange("key", 0, -1).should be == %w(val1 val2 val3)
|
146
|
+
@client.zrange("key", 0, -1, :with_scores => true).should be == [["val1", 5], ["val2", 5], ["val3", 5]]
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should return a reversed range of members in a sorted set, by index" do
|
150
|
+
@client.zadd("key", 1, "one")
|
151
|
+
@client.zadd("key", 2, "two")
|
152
|
+
@client.zadd("key", 3, "three")
|
153
|
+
|
154
|
+
@client.zrevrange("key", 0, -1).should be == ["three", "two", "one"]
|
155
|
+
@client.zrevrange("key", 1, 2).should be == ["two", "one"]
|
156
|
+
@client.zrevrange("key", 0, -1, :withscores => true).should be == [["three", 3], ["two", 2], ["one", 1]]
|
157
|
+
@client.zrevrange("key", 0, -1, :with_scores => true).should be == [["three", 3], ["two", 2], ["one", 1]]
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should return a range of members in a sorted set, by score" do
|
161
|
+
@client.zadd("key", 1, "one")
|
162
|
+
@client.zadd("key", 2, "two")
|
163
|
+
@client.zadd("key", 3, "three")
|
164
|
+
|
165
|
+
@client.zrangebyscore("key", 0, 100).should be == ["one", "two", "three"]
|
166
|
+
@client.zrangebyscore("key", 1, 2).should be == ["one", "two"]
|
167
|
+
@client.zrangebyscore("key", 0, 100, :withscores => true).should be == [["one", 1], ["two", 2], ["three", 3]]
|
168
|
+
@client.zrangebyscore("key", 1, 2, :with_scores => true).should be == [["one", 1], ["two", 2]]
|
169
|
+
@client.zrangebyscore("key", 0, 100, :limit => [0, 1]).should be == ["one"]
|
170
|
+
@client.zrangebyscore("key", 0, 100, :limit => [0, -1]).should be == ["one", "two", "three"]
|
171
|
+
@client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).should be == [["two", 2], ["three", 3]]
|
172
|
+
@client.zrangebyscore("key", '-inf', '+inf').should be == ["one", "two", "three"]
|
173
|
+
@client.zrangebyscore("key", 2, '+inf').should be == ["two", "three"]
|
174
|
+
@client.zrangebyscore("key", '-inf', 2).should be == ['one', "two"]
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should return a reversed range of members in a sorted set, by score" do
|
178
|
+
@client.zadd("key", 1, "one")
|
179
|
+
@client.zadd("key", 2, "two")
|
180
|
+
@client.zadd("key", 3, "three")
|
181
|
+
|
182
|
+
@client.zrevrangebyscore("key", 100, 0).should be == ["three", "two", "one"]
|
183
|
+
@client.zrevrangebyscore("key", 2, 1).should be == ["two", "one"]
|
184
|
+
@client.zrevrangebyscore("key", 1, 2).should be == []
|
185
|
+
@client.zrevrangebyscore("key", 2, 1, :with_scores => true).should be == [["two", 2], ["one", 1]]
|
186
|
+
@client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).should be == ["three"]
|
187
|
+
@client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).should be == ["three", "two", "one"]
|
188
|
+
@client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).should be == [["two", 2], ["one", 1]]
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should determine the index of a member in a sorted set" do
|
192
|
+
@client.zadd("key", 1, "one")
|
193
|
+
@client.zadd("key", 2, "two")
|
194
|
+
@client.zadd("key", 3, "three")
|
195
|
+
|
196
|
+
@client.zrank("key", "three").should be == 2
|
197
|
+
@client.zrank("key", "four").should be_nil
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should determine the reversed index of a member in a sorted set" do
|
201
|
+
@client.zadd("key", 1, "one")
|
202
|
+
@client.zadd("key", 2, "two")
|
203
|
+
@client.zadd("key", 3, "three")
|
204
|
+
|
205
|
+
@client.zrevrank("key", "three").should be == 0
|
206
|
+
@client.zrevrank("key", "four").should be_nil
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should not raise errors for zrank() on accessing a non-existing key in a sorted set" do
|
210
|
+
@client.zrank("no_such_key", "no_suck_id").should be_nil
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should not raise errors for zrevrank() on accessing a non-existing key in a sorted set" do
|
214
|
+
@client.zrevrank("no_such_key", "no_suck_id").should be_nil
|
215
|
+
end
|
216
|
+
|
217
|
+
describe "#zinterstore" do
|
218
|
+
before do
|
219
|
+
@client.zadd("key1", 1, "one")
|
220
|
+
@client.zadd("key1", 2, "two")
|
221
|
+
@client.zadd("key1", 3, "three")
|
222
|
+
@client.zadd("key2", 5, "two")
|
223
|
+
@client.zadd("key2", 7, "three")
|
224
|
+
@client.sadd("key3", 'one')
|
225
|
+
@client.sadd("key3", 'two')
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should intersect two keys with custom scores" do
|
229
|
+
@client.zinterstore("out", ["key1", "key2"]).should be == 2
|
230
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [['two', (2 + 5)], ['three', (3 + 7)]]
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should intersect two keys with a default score" do
|
234
|
+
@client.zinterstore("out", ["key1", "key3"]).should be == 2
|
235
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [['one', (1 + 1)], ['two', (2 + 1)]]
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should intersect more than two keys" do
|
239
|
+
@client.zinterstore("out", ["key1", "key2", "key3"]).should be == 1
|
240
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [['two', (2 + 5 + 1)]]
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should not intersect an unknown key" do
|
244
|
+
@client.zinterstore("out", ["key1", "no_key"]).should be == 0
|
245
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == []
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should intersect two keys by minimum values" do
|
249
|
+
@client.zinterstore("out", ["key1", "key2"], :aggregate => :min).should be == 2
|
250
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 2], ["three", 3]]
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should intersect two keys by maximum values" do
|
254
|
+
@client.zinterstore("out", ["key1", "key2"], :aggregate => :max).should be == 2
|
255
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 5], ["three", 7]]
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should intersect two keys by explicitly summing values" do
|
259
|
+
@client.zinterstore("out", %w(key1 key2), :aggregate => :sum).should be == 2
|
260
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 + 5)], ["three", (3 + 7)]]
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should intersect two keys with weighted values" do
|
264
|
+
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1]).should be == 2
|
265
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 * 10 + 5)], ["three", (3 * 10 + 7)]]
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should intersect two keys with weighted minimum values" do
|
269
|
+
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 2
|
270
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 5], ["three", 7]]
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should intersect two keys with weighted maximum values" do
|
274
|
+
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 2
|
275
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 * 10)], ["three", (3 * 10)]]
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should error without enough weights given" do
|
279
|
+
lambda { @client.zinterstore("out", %w(key1 key2), :weights => []) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
280
|
+
lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should error with too many weights given" do
|
284
|
+
lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should error with an invalid aggregate" do
|
288
|
+
lambda { @client.zinterstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
context "zremrangebyscore" do
|
293
|
+
it "should remove items by score" do
|
294
|
+
@client.zadd("key", 1, "one")
|
295
|
+
@client.zadd("key", 2, "two")
|
296
|
+
@client.zadd("key", 3, "three")
|
297
|
+
|
298
|
+
@client.zremrangebyscore("key", 0, 2).should be == 2
|
299
|
+
@client.zcard("key").should be == 1
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should remove items by score with infinity" do # Issue #50
|
303
|
+
@client.zadd("key", 10.0, "one")
|
304
|
+
@client.zadd("key", 20.0, "two")
|
305
|
+
@client.zadd("key", 30.0, "three")
|
306
|
+
@client.zremrangebyscore("key", "-inf", "+inf").should be == 3
|
307
|
+
@client.zcard("key").should be == 0
|
308
|
+
@client.zscore("key", "one").should be_nil
|
309
|
+
@client.zscore("key", "two").should be_nil
|
310
|
+
@client.zscore("key", "three").should be_nil
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should return 0 if the key didn't exist" do
|
314
|
+
@client.zremrangebyscore("key", 0, 2).should be == 0
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
context '#zremrangebyrank' do
|
319
|
+
it 'removes all elements with in the given range' do
|
320
|
+
@client.zadd("key", 1, "one")
|
321
|
+
@client.zadd("key", 2, "two")
|
322
|
+
@client.zadd("key", 3, "three")
|
323
|
+
|
324
|
+
@client.zremrangebyrank("key", 0, 1).should be == 2
|
325
|
+
@client.zcard('key').should be == 1
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'handles out of range requests' do
|
329
|
+
@client.zadd("key", 1, "one")
|
330
|
+
@client.zadd("key", 2, "two")
|
331
|
+
@client.zadd("key", 3, "three")
|
332
|
+
|
333
|
+
@client.zremrangebyrank("key", 25, -1).should be == 0
|
334
|
+
@client.zcard('key').should be == 3
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
describe "#zunionstore" do
|
339
|
+
before do
|
340
|
+
@client.zadd("key1", 1, "val1")
|
341
|
+
@client.zadd("key1", 2, "val2")
|
342
|
+
@client.zadd("key1", 3, "val3")
|
343
|
+
@client.zadd("key2", 5, "val2")
|
344
|
+
@client.zadd("key2", 7, "val3")
|
345
|
+
@client.sadd("key3", "val1")
|
346
|
+
@client.sadd("key3", "val2")
|
347
|
+
end
|
348
|
+
|
349
|
+
it "should union two keys with custom scores" do
|
350
|
+
@client.zunionstore("out", %w(key1 key2)).should be == 3
|
351
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]]
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should union two keys with a default score" do
|
355
|
+
@client.zunionstore("out", %w(key1 key3)).should be == 3
|
356
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 + 1)], ["val2", (2 + 1)], ["val3", 3]]
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should union more than two keys" do
|
360
|
+
@client.zunionstore("out", %w(key1 key2 key3)).should be == 3
|
361
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 + 1)], ["val2", (2 + 5 + 1)], ["val3", (3 + 7)]]
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should union with an unknown key" do
|
365
|
+
@client.zunionstore("out", %w(key1 no_key)).should be == 3
|
366
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 2], ["val3", 3]]
|
367
|
+
end
|
368
|
+
|
369
|
+
it "should union two keys by minimum values" do
|
370
|
+
@client.zunionstore("out", %w(key1 key2), :aggregate => :min).should be == 3
|
371
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 2], ["val3", 3]]
|
372
|
+
end
|
373
|
+
|
374
|
+
it "should union two keys by maximum values" do
|
375
|
+
@client.zunionstore("out", %w(key1 key2), :aggregate => :max).should be == 3
|
376
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 5], ["val3", 7]]
|
377
|
+
end
|
378
|
+
|
379
|
+
it "should union two keys by explicitly summing values" do
|
380
|
+
@client.zunionstore("out", %w(key1 key2), :aggregate => :sum).should be == 3
|
381
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]]
|
382
|
+
end
|
383
|
+
|
384
|
+
it "should union two keys with weighted values" do
|
385
|
+
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1]).should be == 3
|
386
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 * 10)], ["val2", (2 * 10 + 5)], ["val3", (3 * 10 + 7)]]
|
387
|
+
end
|
388
|
+
|
389
|
+
it "should union two keys with weighted minimum values" do
|
390
|
+
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 3
|
391
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val2", 5], ["val3", 7], ["val1", (1 * 10)]]
|
392
|
+
end
|
393
|
+
|
394
|
+
it "should union two keys with weighted maximum values" do
|
395
|
+
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 3
|
396
|
+
@client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 * 10)], ["val2", (2 * 10)], ["val3", (3 * 10)]]
|
397
|
+
end
|
398
|
+
|
399
|
+
it "should error without enough weights given" do
|
400
|
+
lambda { @client.zunionstore("out", %w(key1 key2), :weights => []) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
401
|
+
lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
402
|
+
end
|
403
|
+
|
404
|
+
it "should error with too many weights given" do
|
405
|
+
lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
406
|
+
end
|
407
|
+
|
408
|
+
it "should error with an invalid aggregate" do
|
409
|
+
lambda { @client.zunionstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(Redis::CommandError, "ERR syntax error")
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
#it "should remove all members in a sorted set within the given indexes"
|
414
|
+
|
415
|
+
#it "should return a range of members in a sorted set, by index, with scores ordered from high to low"
|
416
|
+
|
417
|
+
#it "should return a range of members in a sorted set, by score, with scores ordered from high to low"
|
418
|
+
|
419
|
+
#it "should determine the index of a member in a sorted set, with scores ordered from high to low"
|
420
|
+
|
421
|
+
#it "should get the score associated with the given member in a sorted set"
|
422
|
+
|
423
|
+
#it "should add multiple sorted sets and store the resulting sorted set in a new key"
|
424
|
+
end
|
425
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RedisFile
|
4
|
+
describe "StringsMethods" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@client = Redis.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should append a value to key" do
|
11
|
+
@client.set("key1", "Hello")
|
12
|
+
@client.append("key1", " World")
|
13
|
+
|
14
|
+
@client.get("key1").should be == "Hello World"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should decrement the integer value of a key by one" do
|
18
|
+
@client.set("counter", "1")
|
19
|
+
@client.decr("counter")
|
20
|
+
|
21
|
+
@client.get("counter").should be == "0"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should decrement the integer value of a key by the given number" do
|
25
|
+
@client.set("counter", "10")
|
26
|
+
@client.decrby("counter", "5")
|
27
|
+
|
28
|
+
@client.get("counter").should be == "5"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should get the value of a key" do
|
32
|
+
@client.get("key2").should be == nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should returns the bit value at offset in the string value stored at key" do
|
36
|
+
@client.set("key1", "a")
|
37
|
+
|
38
|
+
@client.getbit("key1", 1).should be == 1
|
39
|
+
@client.getbit("key1", 2).should be == 1
|
40
|
+
@client.getbit("key1", 3).should be == 0
|
41
|
+
@client.getbit("key1", 4).should be == 0
|
42
|
+
@client.getbit("key1", 5).should be == 0
|
43
|
+
@client.getbit("key1", 6).should be == 0
|
44
|
+
@client.getbit("key1", 7).should be == 1
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should allow direct bit manipulation even if the string isn't set" do
|
48
|
+
@client.setbit("key1", 10, 1)
|
49
|
+
@client.getbit("key1", 10).should be == 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should get a substring of the string stored at a key" do
|
53
|
+
@client.set("key1", "This a message")
|
54
|
+
|
55
|
+
@client.getrange("key1", 0, 3).should be == "This"
|
56
|
+
@client.substr("key1", 0, 3).should be == "This"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should set the string value of a key and return its old value" do
|
60
|
+
@client.set("key1","value1")
|
61
|
+
|
62
|
+
@client.getset("key1", "value2").should be == "value1"
|
63
|
+
@client.get("key1").should be == "value2"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should return nil for #getset if the key does not exist when setting" do
|
67
|
+
@client.getset("key1", "value1").should be == nil
|
68
|
+
@client.get("key1").should be == "value1"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should increment the integer value of a key by one" do
|
72
|
+
@client.set("counter", "1")
|
73
|
+
@client.incr("counter").should be == 2
|
74
|
+
|
75
|
+
@client.get("counter").should be == "2"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not change the expire value of the key during incr" do
|
79
|
+
@client.set("counter", "1")
|
80
|
+
@client.expire("counter", 600).should be_true
|
81
|
+
@client.ttl("counter").should be == 600
|
82
|
+
@client.incr("counter").should be == 2
|
83
|
+
@client.ttl("counter").should be == 600
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should decrement the integer value of a key by one" do
|
87
|
+
@client.set("counter", "1")
|
88
|
+
@client.decr("counter").should be == 0
|
89
|
+
|
90
|
+
@client.get("counter").should be == "0"
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should not change the expire value of the key during decr" do
|
94
|
+
@client.set("counter", "2")
|
95
|
+
@client.expire("counter", 600).should be_true
|
96
|
+
@client.ttl("counter").should be == 600
|
97
|
+
@client.decr("counter").should be == 1
|
98
|
+
@client.ttl("counter").should be == 600
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should increment the integer value of a key by the given number" do
|
102
|
+
@client.set("counter", "10")
|
103
|
+
@client.incrby("counter", "5").should be == 15
|
104
|
+
@client.incrby("counter", 2).should be == 17
|
105
|
+
@client.get("counter").should be == "17"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should not change the expire value of the key during incrby" do
|
109
|
+
@client.set("counter", "1")
|
110
|
+
@client.expire("counter", 600).should be_true
|
111
|
+
@client.ttl("counter").should be == 600
|
112
|
+
@client.incrby("counter", "5").should be == 6
|
113
|
+
@client.ttl("counter").should be == 600
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should decrement the integer value of a key by the given number" do
|
117
|
+
@client.set("counter", "10")
|
118
|
+
@client.decrby("counter", "5").should be == 5
|
119
|
+
@client.decrby("counter", 2).should be == 3
|
120
|
+
@client.get("counter").should be == "3"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should not change the expire value of the key during decrby" do
|
124
|
+
@client.set("counter", "8")
|
125
|
+
@client.expire("counter", 600).should be_true
|
126
|
+
@client.ttl("counter").should be == 600
|
127
|
+
@client.decrby("counter", "3").should be == 5
|
128
|
+
@client.ttl("counter").should be == 600
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should get the values of all the given keys" do
|
132
|
+
@client.set("key1", "value1")
|
133
|
+
@client.set("key2", "value2")
|
134
|
+
@client.set("key3", "value3")
|
135
|
+
|
136
|
+
@client.mget("key1", "key2", "key3").should be == ["value1", "value2", "value3"]
|
137
|
+
@client.mget(["key1", "key2", "key3"]).should be == ["value1", "value2", "value3"]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "returns nil for non existent keys" do
|
141
|
+
@client.set("key1", "value1")
|
142
|
+
@client.set("key3", "value3")
|
143
|
+
|
144
|
+
@client.mget("key1", "key2", "key3", "key4").should be == ["value1", nil, "value3", nil]
|
145
|
+
@client.mget(["key1", "key2", "key3", "key4"]).should be == ["value1", nil, "value3", nil]
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'raises an argument error when not passed any fields' do
|
149
|
+
@client.set("key3", "value3")
|
150
|
+
|
151
|
+
lambda { @client.mget }.should raise_error(Redis::CommandError)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should set multiple keys to multiple values" do
|
155
|
+
@client.mset(:key1, "value1", :key2, "value2")
|
156
|
+
|
157
|
+
@client.get("key1").should be == "value1"
|
158
|
+
@client.get("key2").should be == "value2"
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should raise error if command arguments count is wrong" do
|
162
|
+
expect { @client.mset }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'mset' command")
|
163
|
+
expect { @client.mset(:key1) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'mset' command")
|
164
|
+
expect { @client.mset(:key1, "value", :key2) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'mset' command")
|
165
|
+
|
166
|
+
@client.get("key1").should be_nil
|
167
|
+
@client.get("key2").should be_nil
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should set multiple keys to multiple values, only if none of the keys exist" do
|
171
|
+
@client.msetnx(:key1, "value1", :key2, "value2").should be == true
|
172
|
+
@client.msetnx(:key1, "value3", :key2, "value4").should be == false
|
173
|
+
|
174
|
+
@client.get("key1").should be == "value1"
|
175
|
+
@client.get("key2").should be == "value2"
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should set multiple keys to multiple values with a hash" do
|
179
|
+
@client.mapped_mset(:key1 => "value1", :key2 => "value2")
|
180
|
+
|
181
|
+
@client.get("key1").should be == "value1"
|
182
|
+
@client.get("key2").should be == "value2"
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should set multiple keys to multiple values with a hash, only if none of the keys exist" do
|
186
|
+
@client.mapped_msetnx(:key1 => "value1", :key2 => "value2").should be == true
|
187
|
+
@client.mapped_msetnx(:key1 => "value3", :key2 => "value4").should be == false
|
188
|
+
|
189
|
+
@client.get("key1").should be == "value1"
|
190
|
+
@client.get("key2").should be == "value2"
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should set the string value of a key" do
|
194
|
+
@client.set("key1", "1")
|
195
|
+
|
196
|
+
@client.get("key1").should be == "1"
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should sets or clears the bit at offset in the string value stored at key" do
|
200
|
+
@client.set("key1", "abc")
|
201
|
+
@client.setbit("key1", 11, 1)
|
202
|
+
|
203
|
+
@client.get("key1").should be == "arc"
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should set the value and expiration of a key" do
|
207
|
+
@client.setex("key1", 30, "value1")
|
208
|
+
|
209
|
+
@client.get("key1").should be == "value1"
|
210
|
+
@client.ttl("key1").should be == 30
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should set the value of a key, only if the key does not exist" do
|
214
|
+
@client.set("key1", "test value")
|
215
|
+
@client.setnx("key1", "new value")
|
216
|
+
@client.setnx("key2", "another value")
|
217
|
+
|
218
|
+
@client.get("key1").should be == "test value"
|
219
|
+
@client.get("key2").should be == "another value"
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should overwrite part of a string at key starting at the specified offset" do
|
223
|
+
@client.set("key1", "Hello World")
|
224
|
+
@client.setrange("key1", 6, "Redis")
|
225
|
+
|
226
|
+
@client.get("key1").should be == "Hello Redis"
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should get the length of the value stored in a key" do
|
230
|
+
@client.set("key1", "abc")
|
231
|
+
|
232
|
+
@client.strlen("key1").should be == 3
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
end
|