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.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.travis.yml +16 -5
- data/LICENSE +1 -1
- data/README.md +18 -2
- data/fakeredis.gemspec +1 -1
- data/gemfiles/redisrb-master.gemfile +14 -0
- data/lib/fakeredis/bitop_command.rb +56 -0
- data/lib/fakeredis/expiring_hash.rb +1 -1
- data/lib/fakeredis/minitest.rb +24 -0
- data/lib/fakeredis/rspec.rb +1 -0
- data/lib/fakeredis/sort_method.rb +3 -2
- data/lib/fakeredis/transaction_commands.rb +1 -1
- data/lib/fakeredis/version.rb +1 -1
- data/lib/fakeredis/zset.rb +7 -1
- data/lib/redis/connection/memory.rb +424 -38
- data/spec/bitop_command_spec.rb +209 -0
- data/spec/compatibility_spec.rb +1 -1
- data/spec/connection_spec.rb +20 -20
- data/spec/hashes_spec.rb +123 -57
- data/spec/keys_spec.rb +197 -80
- data/spec/lists_spec.rb +61 -34
- data/spec/memory_spec.rb +60 -7
- data/spec/server_spec.rb +24 -24
- data/spec/sets_spec.rb +95 -46
- data/spec/sort_method_spec.rb +6 -0
- data/spec/sorted_sets_spec.rb +288 -150
- data/spec/spec_helper.rb +1 -0
- data/spec/strings_spec.rb +83 -78
- data/spec/subscription_spec.rb +107 -0
- data/spec/support/shared_examples/bitwise_operation.rb +59 -0
- data/spec/support/shared_examples/sortable.rb +20 -16
- data/spec/transactions_spec.rb +20 -12
- data/spec/upcase_method_name_spec.rb +2 -2
- metadata +14 -5
data/spec/sort_method_spec.rb
CHANGED
data/spec/sorted_sets_spec.rb
CHANGED
@@ -9,84 +9,98 @@ module FakeRedis
|
|
9
9
|
end
|
10
10
|
|
11
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").
|
13
|
-
@client.zscore("key", "val").
|
12
|
+
expect(@client.zadd("key", 1, "val")).to be(true)
|
13
|
+
expect(@client.zscore("key", "val")).to eq(1.0)
|
14
14
|
|
15
|
-
@client.zadd("key", 2, "val").
|
16
|
-
@client.zscore("key", "val").
|
15
|
+
expect(@client.zadd("key", 2, "val")).to be(false)
|
16
|
+
expect(@client.zscore("key", "val")).to eq(2.0)
|
17
17
|
|
18
18
|
# These assertions only work in redis-rb v3.0.2 or higher
|
19
|
-
@client.zadd("key2", "inf", "val").
|
20
|
-
@client.zscore("key2", "val").
|
19
|
+
expect(@client.zadd("key2", "inf", "val")).to eq(true)
|
20
|
+
expect(@client.zscore("key2", "val")).to eq(Infinity)
|
21
21
|
|
22
|
-
@client.zadd("key3", "+inf", "val").
|
23
|
-
@client.zscore("key3", "val").
|
22
|
+
expect(@client.zadd("key3", "+inf", "val")).to eq(true)
|
23
|
+
expect(@client.zscore("key3", "val")).to eq(Infinity)
|
24
24
|
|
25
|
-
@client.zadd("key4", "-inf", "val").
|
26
|
-
@client.zscore("key4", "val").
|
25
|
+
expect(@client.zadd("key4", "-inf", "val")).to eq(true)
|
26
|
+
expect(@client.zscore("key4", "val")).to eq(-Infinity)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should return a nil score for value not in a sorted set or empty key" do
|
30
30
|
@client.zadd "key", 1, "val"
|
31
31
|
|
32
|
-
@client.zscore("key", "val").
|
33
|
-
@client.zscore("key", "val2").
|
34
|
-
@client.zscore("key2", "val").
|
32
|
+
expect(@client.zscore("key", "val")).to eq(1.0)
|
33
|
+
expect(@client.zscore("key", "val2")).to be_nil
|
34
|
+
expect(@client.zscore("key2", "val")).to be_nil
|
35
35
|
end
|
36
36
|
|
37
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"]).
|
39
|
-
@client.zscore("key", "val").
|
40
|
-
@client.zscore("key", "val2").
|
41
|
-
|
42
|
-
@client.zadd("key", [[5, "val"], [3, "val3"], [4, "val4"]]).
|
43
|
-
@client.zscore("key", "val").
|
44
|
-
@client.zscore("key", "val2").
|
45
|
-
@client.zscore("key", "val3").
|
46
|
-
@client.zscore("key", "val4").
|
47
|
-
|
48
|
-
@client.zadd("key", [[5, "val5"], [3, "val6"]]).
|
49
|
-
@client.zscore("key", "val5").
|
50
|
-
@client.zscore("key", "val6").
|
38
|
+
expect(@client.zadd("key", [1, "val", 2, "val2"])).to eq(2)
|
39
|
+
expect(@client.zscore("key", "val")).to eq(1)
|
40
|
+
expect(@client.zscore("key", "val2")).to eq(2)
|
41
|
+
|
42
|
+
expect(@client.zadd("key", [[5, "val"], [3, "val3"], [4, "val4"]])).to eq(2)
|
43
|
+
expect(@client.zscore("key", "val")).to eq(5)
|
44
|
+
expect(@client.zscore("key", "val2")).to eq(2)
|
45
|
+
expect(@client.zscore("key", "val3")).to eq(3)
|
46
|
+
expect(@client.zscore("key", "val4")).to eq(4)
|
47
|
+
|
48
|
+
expect(@client.zadd("key", [[5, "val5"], [3, "val6"]])).to eq(2)
|
49
|
+
expect(@client.zscore("key", "val5")).to eq(5)
|
50
|
+
expect(@client.zscore("key", "val6")).to eq(3)
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should error with wrong number of arguments when adding members" do
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
expect { @client.zadd("key") }.to raise_error(ArgumentError, "wrong number of arguments")
|
55
|
+
expect { @client.zadd("key", 1) }.to raise_error(ArgumentError, "wrong number of arguments")
|
56
|
+
expect { @client.zadd("key", [1]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'zadd' command")
|
57
|
+
expect { @client.zadd("key", [1, "val", 2]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
58
|
+
expect { @client.zadd("key", [[1, "val"], [2]]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should allow floats as scores when adding or updating" do
|
62
|
-
@client.zadd("key", 4.321, "val").
|
63
|
-
@client.zscore("key", "val").
|
62
|
+
expect(@client.zadd("key", 4.321, "val")).to be(true)
|
63
|
+
expect(@client.zscore("key", "val")).to eq(4.321)
|
64
|
+
|
65
|
+
expect(@client.zadd("key", 54.3210, "val")).to be(false)
|
66
|
+
expect(@client.zscore("key", "val")).to eq(54.321)
|
67
|
+
end
|
64
68
|
|
65
|
-
|
66
|
-
@client.
|
69
|
+
it "should allow strings that can be parsed as float when adding or updating" do
|
70
|
+
expect(@client.zadd("key", "4.321", "val")).to be(true)
|
71
|
+
expect(@client.zscore("key", "val")).to eq(4.321)
|
72
|
+
|
73
|
+
expect(@client.zadd("key", "54.3210", "val")).to be(false)
|
74
|
+
expect(@client.zscore("key", "val")).to eq(54.321)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should error when the score is not a valid float" do
|
78
|
+
expect { @client.zadd("key", "score", "val") }.to raise_error(Redis::CommandError, "ERR value is not a valid float")
|
79
|
+
expect { @client.zadd("key", {}, "val") }.to raise_error(Redis::CommandError, "ERR value is not a valid float")
|
80
|
+
expect { @client.zadd("key", Time.now, "val") }.to raise_error(Redis::CommandError, "ERR value is not a valid float")
|
67
81
|
end
|
68
82
|
|
69
83
|
it "should remove members from sorted sets" do
|
70
|
-
@client.zrem("key", "val").
|
71
|
-
@client.zadd("key", 1, "val").
|
72
|
-
@client.zrem("key", "val").
|
84
|
+
expect(@client.zrem("key", "val")).to be(false)
|
85
|
+
expect(@client.zadd("key", 1, "val")).to be(true)
|
86
|
+
expect(@client.zrem("key", "val")).to be(true)
|
73
87
|
end
|
74
88
|
|
75
89
|
it "should remove multiple members from sorted sets" do
|
76
|
-
@client.zrem("key2", %w(val)).
|
77
|
-
@client.zrem("key2", %w(val val2 val3)).
|
90
|
+
expect(@client.zrem("key2", %w(val))).to eq(0)
|
91
|
+
expect(@client.zrem("key2", %w(val val2 val3))).to eq(0)
|
78
92
|
|
79
93
|
@client.zadd("key2", 1, "val")
|
80
94
|
@client.zadd("key2", 1, "val2")
|
81
95
|
@client.zadd("key2", 1, "val3")
|
82
96
|
|
83
|
-
@client.zrem("key2", %w(val val2 val3 val4)).
|
97
|
+
expect(@client.zrem("key2", %w(val val2 val3 val4))).to eq(3)
|
84
98
|
end
|
85
99
|
|
86
100
|
it "should remove sorted set's key when it is empty" do
|
87
101
|
@client.zadd("key", 1, "val")
|
88
102
|
@client.zrem("key", "val")
|
89
|
-
@client.exists("key").
|
103
|
+
expect(@client.exists("key")).to eq(false)
|
90
104
|
end
|
91
105
|
|
92
106
|
it "should get the number of members in a sorted set" do
|
@@ -94,7 +108,7 @@ module FakeRedis
|
|
94
108
|
@client.zadd("key", 2, "val1")
|
95
109
|
@client.zadd("key", 5, "val3")
|
96
110
|
|
97
|
-
@client.zcard("key").
|
111
|
+
expect(@client.zcard("key")).to eq(3)
|
98
112
|
end
|
99
113
|
|
100
114
|
it "should count the members in a sorted set with scores within the given values" do
|
@@ -102,28 +116,28 @@ module FakeRedis
|
|
102
116
|
@client.zadd("key", 2, "val2")
|
103
117
|
@client.zadd("key", 3, "val3")
|
104
118
|
|
105
|
-
@client.zcount("key", 2, 3).
|
119
|
+
expect(@client.zcount("key", 2, 3)).to eq(2)
|
106
120
|
end
|
107
121
|
|
108
122
|
it "should increment the score of a member in a sorted set" do
|
109
123
|
@client.zadd("key", 1, "val1")
|
110
|
-
@client.zincrby("key", 2, "val1").
|
111
|
-
@client.zscore("key", "val1").
|
124
|
+
expect(@client.zincrby("key", 2, "val1")).to eq(3)
|
125
|
+
expect(@client.zscore("key", "val1")).to eq(3)
|
112
126
|
end
|
113
127
|
|
114
128
|
it "initializes the sorted set if the key wasnt already set" do
|
115
|
-
@client.zincrby("key", 1, "val1").
|
129
|
+
expect(@client.zincrby("key", 1, "val1")).to eq(1)
|
116
130
|
end
|
117
131
|
|
118
132
|
it "should convert the key to a string for zscore" do
|
119
133
|
@client.zadd("key", 1, 1)
|
120
|
-
@client.zscore("key", 1).
|
134
|
+
expect(@client.zscore("key", 1)).to eq(1)
|
121
135
|
end
|
122
136
|
|
123
137
|
# These won't pass until redis-rb releases v3.0.2
|
124
138
|
it "should handle infinity values when incrementing a sorted set key" do
|
125
|
-
@client.zincrby("bar", "+inf", "s2").
|
126
|
-
@client.zincrby("bar", "-inf", "s1").
|
139
|
+
expect(@client.zincrby("bar", "+inf", "s2")).to eq(Infinity)
|
140
|
+
expect(@client.zincrby("bar", "-inf", "s1")).to eq(-Infinity)
|
127
141
|
end
|
128
142
|
|
129
143
|
it "should return a range of members in a sorted set, by index" do
|
@@ -131,10 +145,10 @@ module FakeRedis
|
|
131
145
|
@client.zadd("key", 2, "two")
|
132
146
|
@client.zadd("key", 3, "three")
|
133
147
|
|
134
|
-
@client.zrange("key", 0, -1).
|
135
|
-
@client.zrange("key", 1, 2).
|
136
|
-
@client.zrange("key", 0, -1, :withscores => true).
|
137
|
-
@client.zrange("key", 1, 2, :with_scores => true).
|
148
|
+
expect(@client.zrange("key", 0, -1)).to eq(["one", "two", "three"])
|
149
|
+
expect(@client.zrange("key", 1, 2)).to eq(["two", "three"])
|
150
|
+
expect(@client.zrange("key", 0, -1, :withscores => true)).to eq([["one", 1], ["two", 2], ["three", 3]])
|
151
|
+
expect(@client.zrange("key", 1, 2, :with_scores => true)).to eq([["two", 2], ["three", 3]])
|
138
152
|
end
|
139
153
|
|
140
154
|
it "should sort zrange results logically" do
|
@@ -142,8 +156,8 @@ module FakeRedis
|
|
142
156
|
@client.zadd("key", 5, "val3")
|
143
157
|
@client.zadd("key", 5, "val1")
|
144
158
|
|
145
|
-
@client.zrange("key", 0, -1).
|
146
|
-
@client.zrange("key", 0, -1, :with_scores => true).
|
159
|
+
expect(@client.zrange("key", 0, -1)).to eq(%w(val1 val2 val3))
|
160
|
+
expect(@client.zrange("key", 0, -1, :with_scores => true)).to eq([["val1", 5], ["val2", 5], ["val3", 5]])
|
147
161
|
end
|
148
162
|
|
149
163
|
it "should return a reversed range of members in a sorted set, by index" do
|
@@ -151,10 +165,10 @@ module FakeRedis
|
|
151
165
|
@client.zadd("key", 2, "two")
|
152
166
|
@client.zadd("key", 3, "three")
|
153
167
|
|
154
|
-
@client.zrevrange("key", 0, -1).
|
155
|
-
@client.zrevrange("key", 1, 2).
|
156
|
-
@client.zrevrange("key", 0, -1, :withscores => true).
|
157
|
-
@client.zrevrange("key", 0, -1, :with_scores => true).
|
168
|
+
expect(@client.zrevrange("key", 0, -1)).to eq(["three", "two", "one"])
|
169
|
+
expect(@client.zrevrange("key", 1, 2)).to eq(["two", "one"])
|
170
|
+
expect(@client.zrevrange("key", 0, -1, :withscores => true)).to eq([["three", 3], ["two", 2], ["one", 1]])
|
171
|
+
expect(@client.zrevrange("key", 0, -1, :with_scores => true)).to eq([["three", 3], ["two", 2], ["one", 1]])
|
158
172
|
end
|
159
173
|
|
160
174
|
it "should return a range of members in a sorted set, by score" do
|
@@ -162,21 +176,21 @@ module FakeRedis
|
|
162
176
|
@client.zadd("key", 2, "two")
|
163
177
|
@client.zadd("key", 3, "three")
|
164
178
|
|
165
|
-
@client.zrangebyscore("key", 0, 100).
|
166
|
-
@client.zrangebyscore("key", 1, 2).
|
167
|
-
@client.zrangebyscore("key", 1, '(2').
|
168
|
-
@client.zrangebyscore("key", '(1', 2).
|
169
|
-
@client.zrangebyscore("key", '(1', '(2').
|
170
|
-
@client.zrangebyscore("key", 0, 100, :withscores => true).
|
171
|
-
@client.zrangebyscore("key", 1, 2, :with_scores => true).
|
172
|
-
@client.zrangebyscore("key", 0, 100, :limit => [0, 1]).
|
173
|
-
@client.zrangebyscore("key", 0, 100, :limit => [0, -1]).
|
174
|
-
@client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).
|
175
|
-
@client.zrangebyscore("key", '-inf', '+inf').
|
176
|
-
@client.zrangebyscore("key", 2, '+inf').
|
177
|
-
@client.zrangebyscore("key", '-inf', 2).
|
178
|
-
|
179
|
-
@client.zrangebyscore("badkey", 1, 2).
|
179
|
+
expect(@client.zrangebyscore("key", 0, 100)).to eq(["one", "two", "three"])
|
180
|
+
expect(@client.zrangebyscore("key", 1, 2)).to eq(["one", "two"])
|
181
|
+
expect(@client.zrangebyscore("key", 1, '(2')).to eq(['one'])
|
182
|
+
expect(@client.zrangebyscore("key", '(1', 2)).to eq(['two'])
|
183
|
+
expect(@client.zrangebyscore("key", '(1', '(2')).to eq([])
|
184
|
+
expect(@client.zrangebyscore("key", 0, 100, :withscores => true)).to eq([["one", 1], ["two", 2], ["three", 3]])
|
185
|
+
expect(@client.zrangebyscore("key", 1, 2, :with_scores => true)).to eq([["one", 1], ["two", 2]])
|
186
|
+
expect(@client.zrangebyscore("key", 0, 100, :limit => [0, 1])).to eq(["one"])
|
187
|
+
expect(@client.zrangebyscore("key", 0, 100, :limit => [0, -1])).to eq(["one", "two", "three"])
|
188
|
+
expect(@client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true)).to eq([["two", 2], ["three", 3]])
|
189
|
+
expect(@client.zrangebyscore("key", '-inf', '+inf')).to eq(["one", "two", "three"])
|
190
|
+
expect(@client.zrangebyscore("key", 2, '+inf')).to eq(["two", "three"])
|
191
|
+
expect(@client.zrangebyscore("key", '-inf', 2)).to eq(['one', "two"])
|
192
|
+
|
193
|
+
expect(@client.zrangebyscore("badkey", 1, 2)).to eq([])
|
180
194
|
end
|
181
195
|
|
182
196
|
it "should return a reversed range of members in a sorted set, by score" do
|
@@ -184,13 +198,13 @@ module FakeRedis
|
|
184
198
|
@client.zadd("key", 2, "two")
|
185
199
|
@client.zadd("key", 3, "three")
|
186
200
|
|
187
|
-
@client.zrevrangebyscore("key", 100, 0).
|
188
|
-
@client.zrevrangebyscore("key", 2, 1).
|
189
|
-
@client.zrevrangebyscore("key", 1, 2).
|
190
|
-
@client.zrevrangebyscore("key", 2, 1, :with_scores => true).
|
191
|
-
@client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).
|
192
|
-
@client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).
|
193
|
-
@client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).
|
201
|
+
expect(@client.zrevrangebyscore("key", 100, 0)).to eq(["three", "two", "one"])
|
202
|
+
expect(@client.zrevrangebyscore("key", 2, 1)).to eq(["two", "one"])
|
203
|
+
expect(@client.zrevrangebyscore("key", 1, 2)).to eq([])
|
204
|
+
expect(@client.zrevrangebyscore("key", 2, 1, :with_scores => true)).to eq([["two", 2.0], ["one", 1.0]])
|
205
|
+
expect(@client.zrevrangebyscore("key", 100, 0, :limit => [0, 1])).to eq(["three"])
|
206
|
+
expect(@client.zrevrangebyscore("key", 100, 0, :limit => [0, -1])).to eq(["three", "two", "one"])
|
207
|
+
expect(@client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true)).to eq([["two", 2.0], ["one", 1.0]])
|
194
208
|
end
|
195
209
|
|
196
210
|
it "should determine the index of a member in a sorted set" do
|
@@ -198,8 +212,8 @@ module FakeRedis
|
|
198
212
|
@client.zadd("key", 2, "two")
|
199
213
|
@client.zadd("key", 3, "three")
|
200
214
|
|
201
|
-
@client.zrank("key", "three").
|
202
|
-
@client.zrank("key", "four").
|
215
|
+
expect(@client.zrank("key", "three")).to eq(2)
|
216
|
+
expect(@client.zrank("key", "four")).to be_nil
|
203
217
|
end
|
204
218
|
|
205
219
|
it "should determine the reversed index of a member in a sorted set" do
|
@@ -207,16 +221,16 @@ module FakeRedis
|
|
207
221
|
@client.zadd("key", 2, "two")
|
208
222
|
@client.zadd("key", 3, "three")
|
209
223
|
|
210
|
-
@client.zrevrank("key", "three").
|
211
|
-
@client.zrevrank("key", "four").
|
224
|
+
expect(@client.zrevrank("key", "three")).to eq(0)
|
225
|
+
expect(@client.zrevrank("key", "four")).to be_nil
|
212
226
|
end
|
213
227
|
|
214
228
|
it "should not raise errors for zrank() on accessing a non-existing key in a sorted set" do
|
215
|
-
@client.zrank("no_such_key", "no_suck_id").
|
229
|
+
expect(@client.zrank("no_such_key", "no_suck_id")).to be_nil
|
216
230
|
end
|
217
231
|
|
218
232
|
it "should not raise errors for zrevrank() on accessing a non-existing key in a sorted set" do
|
219
|
-
@client.zrevrank("no_such_key", "no_suck_id").
|
233
|
+
expect(@client.zrevrank("no_such_key", "no_suck_id")).to be_nil
|
220
234
|
end
|
221
235
|
|
222
236
|
describe "#zinterstore" do
|
@@ -231,66 +245,66 @@ module FakeRedis
|
|
231
245
|
end
|
232
246
|
|
233
247
|
it "should intersect two keys with custom scores" do
|
234
|
-
@client.zinterstore("out", ["key1", "key2"]).
|
235
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
248
|
+
expect(@client.zinterstore("out", ["key1", "key2"])).to eq(2)
|
249
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([['two', (2 + 5)], ['three', (3 + 7)]])
|
236
250
|
end
|
237
251
|
|
238
252
|
it "should intersect two keys with a default score" do
|
239
|
-
@client.zinterstore("out", ["key1", "key3"]).
|
240
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
253
|
+
expect(@client.zinterstore("out", ["key1", "key3"])).to eq(2)
|
254
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([['one', (1 + 1)], ['two', (2 + 1)]])
|
241
255
|
end
|
242
256
|
|
243
257
|
it "should intersect more than two keys" do
|
244
|
-
@client.zinterstore("out", ["key1", "key2", "key3"]).
|
245
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
258
|
+
expect(@client.zinterstore("out", ["key1", "key2", "key3"])).to eq(1)
|
259
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([['two', (2 + 5 + 1)]])
|
246
260
|
end
|
247
261
|
|
248
262
|
it "should not intersect an unknown key" do
|
249
|
-
@client.zinterstore("out", ["key1", "no_key"]).
|
250
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
263
|
+
expect(@client.zinterstore("out", ["key1", "no_key"])).to eq(0)
|
264
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([])
|
251
265
|
end
|
252
266
|
|
253
267
|
it "should intersect two keys by minimum values" do
|
254
|
-
@client.zinterstore("out", ["key1", "key2"], :aggregate => :min).
|
255
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
268
|
+
expect(@client.zinterstore("out", ["key1", "key2"], :aggregate => :min)).to eq(2)
|
269
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", 2], ["three", 3]])
|
256
270
|
end
|
257
271
|
|
258
272
|
it "should intersect two keys by maximum values" do
|
259
|
-
@client.zinterstore("out", ["key1", "key2"], :aggregate => :max).
|
260
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
273
|
+
expect(@client.zinterstore("out", ["key1", "key2"], :aggregate => :max)).to eq(2)
|
274
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", 5], ["three", 7]])
|
261
275
|
end
|
262
276
|
|
263
277
|
it "should intersect two keys by explicitly summing values" do
|
264
|
-
@client.zinterstore("out", %w(key1 key2), :aggregate => :sum).
|
265
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
278
|
+
expect(@client.zinterstore("out", %w(key1 key2), :aggregate => :sum)).to eq(2)
|
279
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", (2 + 5)], ["three", (3 + 7)]])
|
266
280
|
end
|
267
281
|
|
268
282
|
it "should intersect two keys with weighted values" do
|
269
|
-
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1]).
|
270
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
283
|
+
expect(@client.zinterstore("out", %w(key1 key2), :weights => [10, 1])).to eq(2)
|
284
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", (2 * 10 + 5)], ["three", (3 * 10 + 7)]])
|
271
285
|
end
|
272
286
|
|
273
287
|
it "should intersect two keys with weighted minimum values" do
|
274
|
-
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).
|
275
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
288
|
+
expect(@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min)).to eq(2)
|
289
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", 5], ["three", 7]])
|
276
290
|
end
|
277
291
|
|
278
292
|
it "should intersect two keys with weighted maximum values" do
|
279
|
-
@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).
|
280
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
293
|
+
expect(@client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max)).to eq(2)
|
294
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["two", (2 * 10)], ["three", (3 * 10)]])
|
281
295
|
end
|
282
296
|
|
283
297
|
it "should error without enough weights given" do
|
284
|
-
|
285
|
-
|
298
|
+
expect { @client.zinterstore("out", %w(key1 key2), :weights => []) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
299
|
+
expect { @client.zinterstore("out", %w(key1 key2), :weights => [10]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
286
300
|
end
|
287
301
|
|
288
302
|
it "should error with too many weights given" do
|
289
|
-
|
303
|
+
expect { @client.zinterstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
290
304
|
end
|
291
305
|
|
292
306
|
it "should error with an invalid aggregate" do
|
293
|
-
|
307
|
+
expect { @client.zinterstore("out", %w(key1 key2), :aggregate => :invalid) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
294
308
|
end
|
295
309
|
end
|
296
310
|
|
@@ -300,23 +314,23 @@ module FakeRedis
|
|
300
314
|
@client.zadd("key", 2, "two")
|
301
315
|
@client.zadd("key", 3, "three")
|
302
316
|
|
303
|
-
@client.zremrangebyscore("key", 0, 2).
|
304
|
-
@client.zcard("key").
|
317
|
+
expect(@client.zremrangebyscore("key", 0, 2)).to eq(2)
|
318
|
+
expect(@client.zcard("key")).to eq(1)
|
305
319
|
end
|
306
320
|
|
307
321
|
it "should remove items by score with infinity" do # Issue #50
|
308
322
|
@client.zadd("key", 10.0, "one")
|
309
323
|
@client.zadd("key", 20.0, "two")
|
310
324
|
@client.zadd("key", 30.0, "three")
|
311
|
-
@client.zremrangebyscore("key", "-inf", "+inf").
|
312
|
-
@client.zcard("key").
|
313
|
-
@client.zscore("key", "one").
|
314
|
-
@client.zscore("key", "two").
|
315
|
-
@client.zscore("key", "three").
|
325
|
+
expect(@client.zremrangebyscore("key", "-inf", "+inf")).to eq(3)
|
326
|
+
expect(@client.zcard("key")).to eq(0)
|
327
|
+
expect(@client.zscore("key", "one")).to be_nil
|
328
|
+
expect(@client.zscore("key", "two")).to be_nil
|
329
|
+
expect(@client.zscore("key", "three")).to be_nil
|
316
330
|
end
|
317
331
|
|
318
332
|
it "should return 0 if the key didn't exist" do
|
319
|
-
@client.zremrangebyscore("key", 0, 2).
|
333
|
+
expect(@client.zremrangebyscore("key", 0, 2)).to eq(0)
|
320
334
|
end
|
321
335
|
end
|
322
336
|
|
@@ -326,8 +340,8 @@ module FakeRedis
|
|
326
340
|
@client.zadd("key", 2, "two")
|
327
341
|
@client.zadd("key", 3, "three")
|
328
342
|
|
329
|
-
@client.zremrangebyrank("key", 0, 1).
|
330
|
-
@client.zcard('key').
|
343
|
+
expect(@client.zremrangebyrank("key", 0, 1)).to eq(2)
|
344
|
+
expect(@client.zcard('key')).to eq(1)
|
331
345
|
end
|
332
346
|
|
333
347
|
it 'handles out of range requests' do
|
@@ -335,12 +349,12 @@ module FakeRedis
|
|
335
349
|
@client.zadd("key", 2, "two")
|
336
350
|
@client.zadd("key", 3, "three")
|
337
351
|
|
338
|
-
@client.zremrangebyrank("key", 25, -1).
|
339
|
-
@client.zcard('key').
|
352
|
+
expect(@client.zremrangebyrank("key", 25, -1)).to eq(0)
|
353
|
+
expect(@client.zcard('key')).to eq(3)
|
340
354
|
end
|
341
355
|
|
342
356
|
it "should return 0 if the key didn't exist" do
|
343
|
-
@client.zremrangebyrank("key", 0, 1).
|
357
|
+
expect(@client.zremrangebyrank("key", 0, 1)).to eq(0)
|
344
358
|
end
|
345
359
|
end
|
346
360
|
|
@@ -356,66 +370,66 @@ module FakeRedis
|
|
356
370
|
end
|
357
371
|
|
358
372
|
it "should union two keys with custom scores" do
|
359
|
-
@client.zunionstore("out", %w(key1 key2)).
|
360
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
373
|
+
expect(@client.zunionstore("out", %w(key1 key2))).to eq(3)
|
374
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]])
|
361
375
|
end
|
362
376
|
|
363
377
|
it "should union two keys with a default score" do
|
364
|
-
@client.zunionstore("out", %w(key1 key3)).
|
365
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
378
|
+
expect(@client.zunionstore("out", %w(key1 key3))).to eq(3)
|
379
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", (1 + 1)], ["val2", (2 + 1)], ["val3", 3]])
|
366
380
|
end
|
367
381
|
|
368
382
|
it "should union more than two keys" do
|
369
|
-
@client.zunionstore("out", %w(key1 key2 key3)).
|
370
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
383
|
+
expect(@client.zunionstore("out", %w(key1 key2 key3))).to eq(3)
|
384
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", (1 + 1)], ["val2", (2 + 5 + 1)], ["val3", (3 + 7)]])
|
371
385
|
end
|
372
386
|
|
373
387
|
it "should union with an unknown key" do
|
374
|
-
@client.zunionstore("out", %w(key1 no_key)).
|
375
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
388
|
+
expect(@client.zunionstore("out", %w(key1 no_key))).to eq(3)
|
389
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", 1], ["val2", 2], ["val3", 3]])
|
376
390
|
end
|
377
391
|
|
378
392
|
it "should union two keys by minimum values" do
|
379
|
-
@client.zunionstore("out", %w(key1 key2), :aggregate => :min).
|
380
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
393
|
+
expect(@client.zunionstore("out", %w(key1 key2), :aggregate => :min)).to eq(3)
|
394
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", 1], ["val2", 2], ["val3", 3]])
|
381
395
|
end
|
382
396
|
|
383
397
|
it "should union two keys by maximum values" do
|
384
|
-
@client.zunionstore("out", %w(key1 key2), :aggregate => :max).
|
385
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
398
|
+
expect(@client.zunionstore("out", %w(key1 key2), :aggregate => :max)).to eq(3)
|
399
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", 1], ["val2", 5], ["val3", 7]])
|
386
400
|
end
|
387
401
|
|
388
402
|
it "should union two keys by explicitly summing values" do
|
389
|
-
@client.zunionstore("out", %w(key1 key2), :aggregate => :sum).
|
390
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
403
|
+
expect(@client.zunionstore("out", %w(key1 key2), :aggregate => :sum)).to eq(3)
|
404
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]])
|
391
405
|
end
|
392
406
|
|
393
407
|
it "should union two keys with weighted values" do
|
394
|
-
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1]).
|
395
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
408
|
+
expect(@client.zunionstore("out", %w(key1 key2), :weights => [10, 1])).to eq(3)
|
409
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", (1 * 10)], ["val2", (2 * 10 + 5)], ["val3", (3 * 10 + 7)]])
|
396
410
|
end
|
397
411
|
|
398
412
|
it "should union two keys with weighted minimum values" do
|
399
|
-
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).
|
400
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
413
|
+
expect(@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min)).to eq(3)
|
414
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val2", 5], ["val3", 7], ["val1", (1 * 10)]])
|
401
415
|
end
|
402
416
|
|
403
417
|
it "should union two keys with weighted maximum values" do
|
404
|
-
@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).
|
405
|
-
@client.zrange("out", 0, -1, :with_scores => true).
|
418
|
+
expect(@client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max)).to eq(3)
|
419
|
+
expect(@client.zrange("out", 0, -1, :with_scores => true)).to eq([["val1", (1 * 10)], ["val2", (2 * 10)], ["val3", (3 * 10)]])
|
406
420
|
end
|
407
421
|
|
408
422
|
it "should error without enough weights given" do
|
409
|
-
|
410
|
-
|
423
|
+
expect { @client.zunionstore("out", %w(key1 key2), :weights => []) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
424
|
+
expect { @client.zunionstore("out", %w(key1 key2), :weights => [10]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
411
425
|
end
|
412
426
|
|
413
427
|
it "should error with too many weights given" do
|
414
|
-
|
428
|
+
expect { @client.zunionstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
415
429
|
end
|
416
430
|
|
417
431
|
it "should error with an invalid aggregate" do
|
418
|
-
|
432
|
+
expect { @client.zunionstore("out", %w(key1 key2), :aggregate => :invalid) }.to raise_error(Redis::CommandError, "ERR syntax error")
|
419
433
|
end
|
420
434
|
end
|
421
435
|
|
@@ -434,7 +448,131 @@ module FakeRedis
|
|
434
448
|
it "zrem should remove members add by zadd" do
|
435
449
|
@client.zadd("key1", 1, 3)
|
436
450
|
@client.zrem("key1", 3)
|
437
|
-
@client.zscore("key1", 3).
|
451
|
+
expect(@client.zscore("key1", 3)).to be_nil
|
452
|
+
end
|
453
|
+
|
454
|
+
describe "#zscan" do
|
455
|
+
before { 50.times { |x| @client.zadd("key", x, "key #{x}") } }
|
456
|
+
|
457
|
+
it 'with no arguments should return 10 numbers in ascending order' do
|
458
|
+
result = @client.zscan("key", 0)[1]
|
459
|
+
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
|
460
|
+
expect(result.count).to eq(10)
|
461
|
+
end
|
462
|
+
|
463
|
+
it 'with a count should return that number of members' do
|
464
|
+
expect(@client.zscan("key", 0, count: 2)).to eq(["2", [["key 0", 0.0], ["key 1", 1.0]]])
|
465
|
+
end
|
466
|
+
|
467
|
+
it 'with a count greater than the number of members, returns all the members in asc order' do
|
468
|
+
result = @client.zscan("key", 0, count: 1000)[1]
|
469
|
+
expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
|
470
|
+
expect(result.size).to eq(50)
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'with match, should return key-values where the key matches' do
|
474
|
+
@client.zadd("key", 1.0, "blah")
|
475
|
+
@client.zadd("key", 2.0, "bluh")
|
476
|
+
result = @client.zscan("key", 0, count: 100, match: "key*")[1]
|
477
|
+
expect(result).to_not include(["blah", 1.0])
|
478
|
+
expect(result).to_not include(["bluh", 2.0])
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
describe "#zscan_each" do
|
483
|
+
before { 50.times { |x| @client.zadd("key", x, "key #{x}") } }
|
484
|
+
|
485
|
+
it 'enumerates over the items in the sorted set' do
|
486
|
+
expect(@client.zscan_each("key").to_a).to eq(@client.zscan("key", 0, count: 50)[1])
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
describe '#zrangebylex' do
|
491
|
+
before { @client.zadd "myzset", [0, 'a', 0, 'b', 0, 'd', 0, 'c'] }
|
492
|
+
|
493
|
+
it "should return empty list for '+'..'-' range" do
|
494
|
+
ranged = @client.zrangebylex "myzset", "+", "-"
|
495
|
+
expect(ranged).to be_empty
|
496
|
+
end
|
497
|
+
|
498
|
+
it "should return all elements for '-'..'+' range" do
|
499
|
+
ranged = @client.zrangebylex "myzset", "-", "+"
|
500
|
+
expect(ranged).to eq %w(a b c d)
|
501
|
+
end
|
502
|
+
|
503
|
+
it "should include values starting with [ symbol" do
|
504
|
+
ranged = @client.zrangebylex "myzset", "-", "[c"
|
505
|
+
expect(ranged).to eq %w(a b c)
|
506
|
+
end
|
507
|
+
|
508
|
+
it "should exclude values with ( symbol" do
|
509
|
+
ranged = @client.zrangebylex "myzset", "-", "(c"
|
510
|
+
expect(ranged).to eq %w(a b)
|
511
|
+
end
|
512
|
+
|
513
|
+
it "should work with both [ and ( properly" do
|
514
|
+
ranged = @client.zrangebylex "myzset", "[aaa", "(d"
|
515
|
+
expect(ranged).to eq %w(b c)
|
516
|
+
end
|
517
|
+
|
518
|
+
it "should return empty array if key is not exist" do
|
519
|
+
ranged = @client.zrangebylex "puppies", "-", "+"
|
520
|
+
expect(ranged).to be_empty
|
521
|
+
end
|
522
|
+
|
523
|
+
it 'should raise error for invalid range when range is invalid' do
|
524
|
+
expect{ @client.zrangebylex "myzset", "-", "d" }.to raise_error(Redis::CommandError, "ERR min or max not valid string range item")
|
525
|
+
end
|
526
|
+
|
527
|
+
it "should limit and offset values as 4th argument" do
|
528
|
+
ranged = @client.zrangebylex "myzset", "-", "+", limit: [1, 3]
|
529
|
+
expect(ranged).to eq %w(b c d)
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
describe "#zrevrangebylex" do
|
534
|
+
before { @client.zadd "myzset", [0, 'a', 0, 'b', 0, 'd', 0, 'c'] }
|
535
|
+
|
536
|
+
it "should return empty list for '-'..'+' range" do
|
537
|
+
ranged = @client.zrevrangebylex "myzset", "-", "+"
|
538
|
+
expect(ranged).to be_empty
|
539
|
+
end
|
540
|
+
|
541
|
+
it "should return all elements for '+'..'-' range in descending order" do
|
542
|
+
ranged = @client.zrevrangebylex "myzset", "+", "-"
|
543
|
+
expect(ranged).to eq %w(d c b a)
|
544
|
+
end
|
545
|
+
|
546
|
+
it "should include values starting with [ symbol" do
|
547
|
+
ranged = @client.zrevrangebylex "myzset", "[c", "-"
|
548
|
+
expect(ranged).to eq %w(c b a)
|
549
|
+
end
|
550
|
+
|
551
|
+
it "should exclude values with ( symbol" do
|
552
|
+
ranged = @client.zrevrangebylex "myzset", "+", "(c"
|
553
|
+
expect(ranged).to eq %w(d)
|
554
|
+
end
|
555
|
+
|
556
|
+
it "should work with both [ and ( properly" do
|
557
|
+
ranged = @client.zrevrangebylex "myzset", "(d", "[aaa"
|
558
|
+
expect(ranged).to eq %w(c b)
|
559
|
+
end
|
560
|
+
|
561
|
+
it "should return empty array if key is not exist" do
|
562
|
+
ranged = @client.zrevrangebylex "puppies", "+", "-"
|
563
|
+
expect(ranged).to be_empty
|
564
|
+
end
|
565
|
+
|
566
|
+
it 'should raise error for invalid range when range is invalid' do
|
567
|
+
expect { @client.zrevrangebylex "myzset", "-", "d" }.to raise_error(Redis::CommandError, "ERR min or max not valid string range item")
|
568
|
+
end
|
569
|
+
|
570
|
+
it "should limit and offset values as 4th argument" do
|
571
|
+
pending "current stable (3.2.0) redis-rb doesn't support limit option"
|
572
|
+
|
573
|
+
ranged = @client.zrevrangebylex "myzset", "+", "-", limit: [0, 3]
|
574
|
+
expect(ranged).to eq %w(d c b)
|
575
|
+
end
|
438
576
|
end
|
439
577
|
end
|
440
578
|
end
|