fakeredis 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/spec/server_spec.rb CHANGED
@@ -12,11 +12,48 @@ module FakeRedis
12
12
  @client.set("key2", "2")
13
13
  @client.set("key2", "two")
14
14
 
15
- @client.dbsize.should == 2
15
+ @client.dbsize.should be == 2
16
16
  end
17
17
 
18
18
  it "should get information and statistics about the server" do
19
- @client.info.key?("redis_version").should == true
19
+ @client.info.key?("redis_version").should be == true
20
+ end
21
+
22
+ it "should handle non-existent methods" do
23
+ lambda { @client.idontexist }.should raise_error(RuntimeError, "ERR unknown command 'idontexist'")
24
+ end
25
+
26
+ describe "multiple databases" do
27
+ it "should default to database 0" do
28
+ @client.inspect.should =~ %r#/0 \(#
29
+ end
30
+
31
+ it "should select another database" do
32
+ @client.select(1)
33
+ @client.inspect.should =~ %r#/1 \(#
34
+ end
35
+
36
+ it "should flush a database" do
37
+ @client.select(0)
38
+ @client.set("key1", "1")
39
+ @client.set("key2", "2")
40
+ @client.dbsize.should be == 2
41
+
42
+ @client.flushdb.should be == "OK"
43
+
44
+ @client.dbsize.should be == 0
45
+ end
46
+
47
+ it "should flush all databases" do
48
+ @client.select(0)
49
+ @client.set("key3", "3")
50
+ @client.set("key4", "4")
51
+ @client.dbsize.should be == 2
52
+
53
+ @client.flushall.should be == "OK"
54
+
55
+ @client.dbsize.should be == 0
56
+ end
20
57
  end
21
58
  end
22
59
  end
data/spec/sets_spec.rb CHANGED
@@ -7,17 +7,23 @@ module FakeRedis
7
7
  end
8
8
 
9
9
  it "should add a member to a set" do
10
- @client.sadd("key", "value").should == true
11
- @client.sadd("key", "value").should == false
10
+ @client.sadd("key", "value").should be == true
11
+ @client.sadd("key", "value").should be == false
12
12
 
13
- @client.smembers("key").should == ["value"]
13
+ @client.smembers("key").should be == ["value"]
14
+ end
15
+
16
+ it "should not add multiple members to a set" do
17
+ # redis.rb v2.2.2 just calls #to_s on the second argument
18
+ @client.sadd("key", %w(value other something more)).should be_true
19
+ @client.smembers("key").should be == [%w(value other something more).to_s]
14
20
  end
15
21
 
16
22
  it "should get the number of members in a set" do
17
23
  @client.sadd("key", "val1")
18
24
  @client.sadd("key", "val2")
19
25
 
20
- @client.scard("key").should == 2
26
+ @client.scard("key").should be == 2
21
27
  end
22
28
 
23
29
  it "should subtract multiple sets" do
@@ -57,7 +63,7 @@ module FakeRedis
57
63
  @client.sadd("key3", "c")
58
64
  @client.sadd("key3", "e")
59
65
 
60
- @client.sinter("key1", "key2", "key3").should == ["c"]
66
+ @client.sinter("key1", "key2", "key3").should be == ["c"]
61
67
  end
62
68
 
63
69
  it "should intersect multiple sets and store the resulting set in a key" do
@@ -70,15 +76,15 @@ module FakeRedis
70
76
  @client.sadd("key3", "c")
71
77
  @client.sadd("key3", "e")
72
78
  @client.sinterstore("key", "key1", "key2", "key3")
73
- @client.smembers("key").should == ["c"]
79
+ @client.smembers("key").should be == ["c"]
74
80
  end
75
81
 
76
82
  it "should determine if a given value is a member of a set" do
77
83
  @client.sadd("key1", "a")
78
84
 
79
- @client.sismember("key1", "a").should == true
80
- @client.sismember("key1", "b").should == false
81
- @client.sismember("key2", "a").should == false
85
+ @client.sismember("key1", "a").should be == true
86
+ @client.sismember("key1", "b").should be == false
87
+ @client.sismember("key2", "a").should be == false
82
88
  end
83
89
 
84
90
  it "should get all the members in a set" do
@@ -94,10 +100,10 @@ module FakeRedis
94
100
  @client.sadd("key1", "a")
95
101
  @client.sadd("key1", "b")
96
102
  @client.sadd("key2", "c")
97
- @client.smove("key1", "key2", "a").should == true
98
- @client.smove("key1", "key2", "a").should == false
103
+ @client.smove("key1", "key2", "a").should be == true
104
+ @client.smove("key1", "key2", "a").should be == false
99
105
 
100
- @client.smembers("key1").should == ["b"]
106
+ @client.smembers("key1").should be == ["b"]
101
107
  @client.smembers("key2").should =~ ["c", "a"]
102
108
  end
103
109
 
@@ -120,10 +126,10 @@ module FakeRedis
120
126
  it "should remove a member from a set" do
121
127
  @client.sadd("key1", "a")
122
128
  @client.sadd("key1", "b")
123
- @client.srem("key1", "a").should == true
124
- @client.srem("key1", "a").should == false
129
+ @client.srem("key1", "a").should be == true
130
+ @client.srem("key1", "a").should be == false
125
131
 
126
- @client.smembers("key1").should == ["b"]
132
+ @client.smembers("key1").should be == ["b"]
127
133
  end
128
134
 
129
135
  it "should remove the set's key once it's empty" do
@@ -132,7 +138,7 @@ module FakeRedis
132
138
  @client.srem("key1", "b")
133
139
  @client.srem("key1", "a")
134
140
 
135
- @client.exists("key1").should == false
141
+ @client.exists("key1").should be == false
136
142
  end
137
143
 
138
144
  it "should add multiple sets" do
@@ -8,18 +8,39 @@ module FakeRedis
8
8
 
9
9
  it "should add a member to a sorted set, or update its score if it already exists" do
10
10
  @client.zadd("key", 1, "val").should be(true)
11
- @client.zscore("key", "val").should == "1"
11
+ @client.zscore("key", "val").should be == "1"
12
12
 
13
13
  @client.zadd("key", 2, "val").should be(false)
14
- @client.zscore("key", "val").should == "2"
14
+ @client.zscore("key", "val").should be == "2"
15
+
16
+ @client.zadd("key2", "inf", "val").should be == true
17
+ @client.zscore("key2", "val").should be == "inf"
18
+
19
+ @client.zadd("key3", "+inf", "val").should be == true
20
+ @client.zscore("key3", "val").should be == "inf"
21
+
22
+ @client.zadd("key4", "-inf", "val").should be == true
23
+ @client.zscore("key4", "val").should be == "-inf"
24
+ end
25
+
26
+ it "should return a nil score for value not in a sorted set or empty key" do
27
+ @client.zadd "key", 1, "val"
28
+
29
+ @client.zscore("key", "val").should be == "1"
30
+ @client.zscore("key", "val2").should be_nil
31
+ @client.zscore("key2", "val").should be_nil
32
+ end
33
+
34
+ it "errors adding multiple things to a set" do
35
+ lambda { @client.zadd("key", [[1, "val"], [2, 'val2']]) }.should raise_error(ArgumentError)
15
36
  end
16
37
 
17
38
  it "should allow floats as scores when adding or updating" do
18
39
  @client.zadd("key", 4.321, "val").should be(true)
19
- @client.zscore("key", "val").should == "4.321"
40
+ @client.zscore("key", "val").should =~ /^4.32/
20
41
 
21
42
  @client.zadd("key", 54.3210, "val").should be(false)
22
- @client.zscore("key", "val").should == "54.321"
43
+ @client.zscore("key", "val").should =~ /^54.32/
23
44
  end
24
45
 
25
46
  it "should remove members from sorted sets" do
@@ -31,7 +52,7 @@ module FakeRedis
31
52
  it "should remove sorted set's key when it is empty" do
32
53
  @client.zadd("key", 1, "val")
33
54
  @client.zrem("key", "val")
34
- @client.exists("key").should == false
55
+ @client.exists("key").should be == false
35
56
  end
36
57
 
37
58
  it "should get the number of members in a sorted set" do
@@ -39,7 +60,7 @@ module FakeRedis
39
60
  @client.zadd("key", 2, "val1")
40
61
  @client.zadd("key", 5, "val3")
41
62
 
42
- @client.zcard("key").should == 3
63
+ @client.zcard("key").should be == 3
43
64
  end
44
65
 
45
66
  it "should count the members in a sorted set with scores within the given values" do
@@ -47,34 +68,47 @@ module FakeRedis
47
68
  @client.zadd("key", 2, "val2")
48
69
  @client.zadd("key", 3, "val3")
49
70
 
50
- @client.zcount("key", 2, 3).should == 2
71
+ @client.zcount("key", 2, 3).should be == 2
51
72
  end
52
73
 
53
74
  it "should increment the score of a member in a sorted set" do
54
75
  @client.zadd("key", 1, "val1")
55
- @client.zincrby("key", 2, "val1").should == "3"
56
- @client.zscore("key", "val1").should == "3"
76
+ @client.zincrby("key", 2, "val1").should be == "3"
77
+ @client.zscore("key", "val1").should be == "3"
57
78
  end
58
79
 
59
80
  it "initializes the sorted set if the key wasnt already set" do
60
- @client.zincrby("key", 1, "val1").should == "1"
81
+ @client.zincrby("key", 1, "val1").should be == "1"
61
82
  end
62
83
 
63
84
  it "should convert the key to a string for zscore" do
64
85
  @client.zadd("key", 1, 1)
65
- @client.zscore("key", 1).should == "1"
86
+ @client.zscore("key", 1).should be == "1"
87
+ end
88
+
89
+ it "should handle infinity values when incrementing a sorted set key" do
90
+ @client.zincrby("bar", "+inf", "s2").should be == "inf"
91
+ @client.zincrby("bar", "-inf", "s1").should be == "-inf"
66
92
  end
67
- #it "should intersect multiple sorted sets and store the resulting sorted set in a new key"
68
93
 
69
94
  it "should return a range of members in a sorted set, by index" do
70
95
  @client.zadd("key", 1, "one")
71
96
  @client.zadd("key", 2, "two")
72
97
  @client.zadd("key", 3, "three")
73
98
 
74
- @client.zrange("key", 0, -1).should == ["one", "two", "three"]
75
- @client.zrange("key", 1, 2).should == ["two", "three"]
76
- @client.zrange("key", 0, -1, :withscores => true).should == ["one", "1", "two", "2", "three", "3"]
77
- @client.zrange("key", 1, 2, :with_scores => true).should == ["two", "2", "three", "3"]
99
+ @client.zrange("key", 0, -1).should be == ["one", "two", "three"]
100
+ @client.zrange("key", 1, 2).should be == ["two", "three"]
101
+ @client.zrange("key", 0, -1, :withscores => true).should be == ["one", "1", "two", "2", "three", "3"]
102
+ @client.zrange("key", 1, 2, :with_scores => true).should be == ["two", "2", "three", "3"]
103
+ end
104
+
105
+ it "should sort zrange results logically" do
106
+ @client.zadd("key", 5, "val2")
107
+ @client.zadd("key", 5, "val3")
108
+ @client.zadd("key", 5, "val1")
109
+
110
+ @client.zrange("key", 0, -1).should be == %w(val1 val2 val3)
111
+ @client.zrange("key", 0, -1, :with_scores => true).should be == ["val1", "5", "val2", "5", "val3", "5"]
78
112
  end
79
113
 
80
114
  it "should return a reversed range of members in a sorted set, by index" do
@@ -82,10 +116,10 @@ module FakeRedis
82
116
  @client.zadd("key", 2, "two")
83
117
  @client.zadd("key", 3, "three")
84
118
 
85
- @client.zrevrange("key", 0, -1).should == ["three", "two", "one"]
86
- @client.zrevrange("key", 1, 2).should == ["two", "one"]
87
- @client.zrevrange("key", 0, -1, :withscores => true).should == ["three", "3", "two", "2", "one", "1"]
88
- @client.zrevrange("key", 0, -1, :with_scores => true).should == ["three", "3", "two", "2", "one", "1"]
119
+ @client.zrevrange("key", 0, -1).should be == ["three", "two", "one"]
120
+ @client.zrevrange("key", 1, 2).should be == ["two", "one"]
121
+ @client.zrevrange("key", 0, -1, :withscores => true).should be == ["three", "3", "two", "2", "one", "1"]
122
+ @client.zrevrange("key", 0, -1, :with_scores => true).should be == ["three", "3", "two", "2", "one", "1"]
89
123
  end
90
124
 
91
125
  it "should return a range of members in a sorted set, by score" do
@@ -93,13 +127,13 @@ module FakeRedis
93
127
  @client.zadd("key", 2, "two")
94
128
  @client.zadd("key", 3, "three")
95
129
 
96
- @client.zrangebyscore("key", 0, 100).should == ["one", "two", "three"]
97
- @client.zrangebyscore("key", 1, 2).should == ["one", "two"]
98
- @client.zrangebyscore("key", 0, 100, :withscores => true).should == ["one", "1", "two", "2", "three", "3"]
99
- @client.zrangebyscore("key", 1, 2, :with_scores => true).should == ["one", "1", "two", "2"]
100
- @client.zrangebyscore("key", 0, 100, :limit => [0, 1]).should == ["one"]
101
- @client.zrangebyscore("key", 0, 100, :limit => [0, -1]).should == ["one", "two", "three"]
102
- @client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).should == ["two", "2", "three", "3"]
130
+ @client.zrangebyscore("key", 0, 100).should be == ["one", "two", "three"]
131
+ @client.zrangebyscore("key", 1, 2).should be == ["one", "two"]
132
+ @client.zrangebyscore("key", 0, 100, :withscores => true).should be == ["one", "1", "two", "2", "three", "3"]
133
+ @client.zrangebyscore("key", 1, 2, :with_scores => true).should be == ["one", "1", "two", "2"]
134
+ @client.zrangebyscore("key", 0, 100, :limit => [0, 1]).should be == ["one"]
135
+ @client.zrangebyscore("key", 0, 100, :limit => [0, -1]).should be == ["one", "two", "three"]
136
+ @client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).should be == ["two", "2", "three", "3"]
103
137
  end
104
138
 
105
139
  it "should return a reversed range of members in a sorted set, by score" do
@@ -107,13 +141,13 @@ module FakeRedis
107
141
  @client.zadd("key", 2, "two")
108
142
  @client.zadd("key", 3, "three")
109
143
 
110
- @client.zrevrangebyscore("key", 100, 0).should == ["three", "two", "one"]
111
- @client.zrevrangebyscore("key", 2, 1).should == ["two", "one"]
112
- @client.zrevrangebyscore("key", 1, 2).should == []
113
- @client.zrevrangebyscore("key", 2, 1, :with_scores => true).should == ["two", "2", "one", "1"]
114
- @client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).should == ["three"]
115
- @client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).should == ["three", "two", "one"]
116
- @client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).should == ["two", "2", "one", "1"]
144
+ @client.zrevrangebyscore("key", 100, 0).should be == ["three", "two", "one"]
145
+ @client.zrevrangebyscore("key", 2, 1).should be == ["two", "one"]
146
+ @client.zrevrangebyscore("key", 1, 2).should be == []
147
+ @client.zrevrangebyscore("key", 2, 1, :with_scores => true).should be == ["two", "2", "one", "1"]
148
+ @client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).should be == ["three"]
149
+ @client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).should be == ["three", "two", "one"]
150
+ @client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).should be == ["two", "2", "one", "1"]
117
151
  end
118
152
 
119
153
  it "should determine the index of a member in a sorted set" do
@@ -121,7 +155,7 @@ module FakeRedis
121
155
  @client.zadd("key", 2, "two")
122
156
  @client.zadd("key", 3, "three")
123
157
 
124
- @client.zrank("key", "three").should == 2
158
+ @client.zrank("key", "three").should be == 2
125
159
  @client.zrank("key", "four").should be_nil
126
160
  end
127
161
 
@@ -130,30 +164,83 @@ module FakeRedis
130
164
  @client.zadd("key", 2, "two")
131
165
  @client.zadd("key", 3, "three")
132
166
 
133
- @client.zrevrank("key", "three").should == 0
167
+ @client.zrevrank("key", "three").should be == 0
134
168
  @client.zrevrank("key", "four").should be_nil
135
169
  end
136
170
 
137
- it "should create untersections between multiple (sorted) sets and store the resulting sorted set in a new key" do
138
- @client.zadd("key1", 1, "one")
139
- @client.zadd("key1", 2, "two")
140
- @client.zadd("key1", 3, "three")
141
- @client.zadd("key2", 5, "two")
142
- @client.zadd("key2", 7, "three")
143
- @client.sadd("key3", 'one')
144
- @client.sadd("key3", 'two')
171
+ describe "#zinterstore" do
172
+ before do
173
+ @client.zadd("key1", 1, "one")
174
+ @client.zadd("key1", 2, "two")
175
+ @client.zadd("key1", 3, "three")
176
+ @client.zadd("key2", 5, "two")
177
+ @client.zadd("key2", 7, "three")
178
+ @client.sadd("key3", 'one')
179
+ @client.sadd("key3", 'two')
180
+ end
181
+
182
+ it "should intersect two keys with custom scores" do
183
+ @client.zinterstore("out", ["key1", "key2"]).should be == 2
184
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ['two', (2 + 5).to_s, 'three', (3 + 7).to_s]
185
+ end
186
+
187
+ it "should intersect two keys with a default score" do
188
+ @client.zinterstore("out", ["key1", "key3"]).should be == 2
189
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ['one', (1 + 1).to_s, 'two', (2 + 1).to_s]
190
+ end
191
+
192
+ it "should intersect more than two keys" do
193
+ @client.zinterstore("out", ["key1", "key2", "key3"]).should be == 1
194
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ['two', (2 + 5 + 1).to_s]
195
+ end
196
+
197
+ it "should not intersect an unknown key" do
198
+ @client.zinterstore("out", ["key1", "no_key"]).should be == 0
199
+ @client.zrange("out", 0, -1, :with_scores => true).should be == []
200
+ end
201
+
202
+ it "should intersect two keys by minimum values" do
203
+ @client.zinterstore("out", ["key1", "key2"], :aggregate => :min).should be == 2
204
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", "2", "three", "3"]
205
+ end
206
+
207
+ it "should intersect two keys by maximum values" do
208
+ @client.zinterstore("out", ["key1", "key2"], :aggregate => :max).should be == 2
209
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", "5", "three", "7"]
210
+ end
145
211
 
146
- @client.zinterstore("out", ["key1", "key2"]).should == 2
147
- @client.zrange("out", 0, 100, :with_scores => true).should == ['two', '7', 'three', '10']
212
+ it "should intersect two keys by explicitly summing values" do
213
+ @client.zinterstore("out", %w(key1 key2), :aggregate => :sum).should be == 2
214
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", (2 + 5).to_s, "three", (3 + 7).to_s]
215
+ end
148
216
 
149
- @client.zinterstore("out", ["key1", "key3"]).should == 2
150
- @client.zrange("out", 0, 100, :with_scores => true).should == ['one', '1', 'two', '2']
217
+ it "should intersect two keys with weighted values" do
218
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1]).should be == 2
219
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", (2 * 10 + 5).to_s, "three", (3 * 10 + 7).to_s]
220
+ end
151
221
 
152
- @client.zinterstore("out", ["key1", "key2", "key3"]).should == 1
153
- @client.zrange("out", 0, 100, :with_scores => true).should == ['two', '7']
222
+ it "should intersect two keys with weighted minimum values" do
223
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 2
224
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", "5", "three", "7"]
225
+ end
226
+
227
+ it "should intersect two keys with weighted maximum values" do
228
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 2
229
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["two", (2 * 10).to_s, "three", (3 * 10).to_s]
230
+ end
231
+
232
+ it "should error without enough weights given" do
233
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => []) }.should raise_error(RuntimeError, "ERR syntax error")
234
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(RuntimeError, "ERR syntax error")
235
+ end
236
+
237
+ it "should error with too many weights given" do
238
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(RuntimeError, "ERR syntax error")
239
+ end
154
240
 
155
- @client.zinterstore("out", ["key1", "no_key"]).should == 0
156
- @client.zrange("out", 0, 100, :with_scores => true).should == []
241
+ it "should error with an invalid aggregate" do
242
+ lambda { @client.zinterstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(RuntimeError, "ERR syntax error")
243
+ end
157
244
  end
158
245
 
159
246
  context "zremrangebyscore" do
@@ -162,23 +249,107 @@ module FakeRedis
162
249
  @client.zadd("key", 2, "two")
163
250
  @client.zadd("key", 3, "three")
164
251
 
165
- @client.zremrangebyscore("key", 0, 2).should == 2
166
- @client.zcard("key").should == 1
252
+ @client.zremrangebyscore("key", 0, 2).should be == 2
253
+ @client.zcard("key").should be == 1
167
254
  end
168
255
 
169
256
  it "should return 0 if the key didn't exist" do
170
- @client.zremrangebyscore("key", 0, 2).should == 0
257
+ @client.zremrangebyscore("key", 0, 2).should be == 0
171
258
  end
172
259
  end
173
260
 
174
261
  context '#zremrangebyrank' do
175
- it 'removes all elements with in the given range' do
262
+ it "removes all elements with in the given range" do
176
263
  @client.zadd("key", 1, "one")
177
264
  @client.zadd("key", 2, "two")
178
265
  @client.zadd("key", 3, "three")
179
266
 
180
- @client.zremrangebyrank("key", 0, 1).should == 2
181
- @client.zcard('key').should == 1
267
+ @client.zremrangebyrank("key", 0, 1).should be == 2
268
+ @client.zcard('key').should be == 1
269
+ end
270
+
271
+ it "handles out of range requests" do
272
+ @client.zadd("key", 1, "one")
273
+ @client.zadd("key", 2, "two")
274
+ @client.zadd("key", 3, "three")
275
+
276
+ @client.zremrangebyrank("key", 25, -1).should be == 0
277
+ @client.zcard('key').should be == 3
278
+ end
279
+ end
280
+
281
+ describe "#zunionstore" do
282
+ before do
283
+ @client.zadd("key1", 1, "val1")
284
+ @client.zadd("key1", 2, "val2")
285
+ @client.zadd("key1", 3, "val3")
286
+ @client.zadd("key2", 5, "val2")
287
+ @client.zadd("key2", 7, "val3")
288
+ @client.sadd("key3", "val1")
289
+ @client.sadd("key3", "val2")
290
+ end
291
+
292
+ it "should union two keys with custom scores" do
293
+ @client.zunionstore("out", %w(key1 key2)).should be == 3
294
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", "1", "val2", (2 + 5).to_s, "val3", (3 + 7).to_s]
295
+ end
296
+
297
+ it "should union two keys with a default score" do
298
+ @client.zunionstore("out", %w(key1 key3)).should be == 3
299
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", (1 + 1).to_s, "val2", (2 + 1).to_s, "val3", "3"]
300
+ end
301
+
302
+ it "should union more than two keys" do
303
+ @client.zunionstore("out", %w(key1 key2 key3)).should be == 3
304
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", (1 + 1).to_s, "val2", (2 + 5 + 1).to_s, "val3", (3 + 7).to_s]
305
+ end
306
+
307
+ it "should union with an unknown key" do
308
+ @client.zunionstore("out", %w(key1 no_key)).should be == 3
309
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", "1", "val2", "2", "val3", "3"]
310
+ end
311
+
312
+ it "should union two keys by minimum values" do
313
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :min).should be == 3
314
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", "1", "val2", "2", "val3", "3"]
315
+ end
316
+
317
+ it "should union two keys by maximum values" do
318
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :max).should be == 3
319
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", "1", "val2", "5", "val3", "7"]
320
+ end
321
+
322
+ it "should union two keys by explicitly summing values" do
323
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :sum).should be == 3
324
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", "1", "val2", (2 + 5).to_s, "val3", (3 + 7).to_s]
325
+ end
326
+
327
+ it "should union two keys with weighted values" do
328
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1]).should be == 3
329
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", (1 * 10).to_s, "val2", (2 * 10 + 5).to_s, "val3", (3 * 10 + 7).to_s]
330
+ end
331
+
332
+ it "should union two keys with weighted minimum values" do
333
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 3
334
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val2", "5", "val3", "7", "val1", (1 * 10).to_s]
335
+ end
336
+
337
+ it "should union two keys with weighted maximum values" do
338
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 3
339
+ @client.zrange("out", 0, -1, :with_scores => true).should be == ["val1", (1 * 10).to_s, "val2", (2 * 10).to_s, "val3", (3 * 10).to_s]
340
+ end
341
+
342
+ it "should error without enough weights given" do
343
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => []) }.should raise_error(RuntimeError, "ERR syntax error")
344
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(RuntimeError, "ERR syntax error")
345
+ end
346
+
347
+ it "should error with too many weights given" do
348
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(RuntimeError, "ERR syntax error")
349
+ end
350
+
351
+ it "should error with an invalid aggregate" do
352
+ lambda { @client.zunionstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(RuntimeError, "ERR syntax error")
182
353
  end
183
354
  end
184
355