fakeredis 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,9 +10,9 @@ module FakeRedis
10
10
  @client.lpush("key1", "val1")
11
11
  @client.lpush("key1", "val2")
12
12
 
13
- @client.lindex("key1", 0).should == "val2"
14
- @client.lindex("key1", -1).should == "val1"
15
- @client.lindex("key1", 3).should == nil
13
+ @client.lindex("key1", 0).should be == "val2"
14
+ @client.lindex("key1", -1).should be == "val1"
15
+ @client.lindex("key1", 3).should be == nil
16
16
  end
17
17
 
18
18
  it "should insert an element before or after another element in a list" do
@@ -20,17 +20,17 @@ module FakeRedis
20
20
  @client.rpush("key1", "v3")
21
21
  @client.linsert("key1", :before, "v3", "v2")
22
22
 
23
- @client.lrange("key1", 0, -1).should == ["v1", "v2", "v3"]
23
+ @client.lrange("key1", 0, -1).should be == ["v1", "v2", "v3"]
24
24
  end
25
-
25
+
26
26
  it 'should allow multiple values to be added to a list in a single rpush' do
27
27
  @client.rpush('key1', [1, 2, 3])
28
- @client.lrange('key1', 0, -1).should == ['1', '2', '3']
28
+ @client.lrange('key1', 0, -1).should be == ['1', '2', '3']
29
29
  end
30
-
30
+
31
31
  it 'should allow multiple values to be added to a list in a single lpush' do
32
32
  @client.lpush('key1', [1, 2, 3])
33
- @client.lrange('key1', 0, -1).should == ['3', '2', '1']
33
+ @client.lrange('key1', 0, -1).should be == ['3', '2', '1']
34
34
  end
35
35
 
36
36
  it "should error if an invalid where argument is given" do
@@ -43,8 +43,8 @@ module FakeRedis
43
43
  @client.rpush("key1", "v1")
44
44
  @client.rpush("key1", "v2")
45
45
 
46
- @client.llen("key1").should == 2
47
- @client.llen("key2").should == 0
46
+ @client.llen("key1").should be == 2
47
+ @client.llen("key2").should be == 0
48
48
  end
49
49
 
50
50
  it "should remove and get the first element in a list" do
@@ -52,15 +52,15 @@ module FakeRedis
52
52
  @client.rpush("key1", "v2")
53
53
  @client.rpush("key1", "v3")
54
54
 
55
- @client.lpop("key1").should == "v1"
56
- @client.lrange("key1", 0, -1).should == ["v2", "v3"]
55
+ @client.lpop("key1").should be == "v1"
56
+ @client.lrange("key1", 0, -1).should be == ["v2", "v3"]
57
57
  end
58
58
 
59
59
  it "should prepend a value to a list" do
60
60
  @client.rpush("key1", "v1")
61
61
  @client.rpush("key1", "v2")
62
62
 
63
- @client.lrange("key1", 0, -1).should == ["v1", "v2"]
63
+ @client.lrange("key1", 0, -1).should be == ["v1", "v2"]
64
64
  end
65
65
 
66
66
  it "should prepend a value to a list, only if the list exists" do
@@ -69,8 +69,8 @@ module FakeRedis
69
69
  @client.lpushx("key1", "v2")
70
70
  @client.lpushx("key2", "v3")
71
71
 
72
- @client.lrange("key1", 0, -1).should == ["v2", "v1"]
73
- @client.llen("key2").should == 0
72
+ @client.lrange("key1", 0, -1).should be == ["v2", "v1"]
73
+ @client.llen("key2").should be == 0
74
74
  end
75
75
 
76
76
  it "should get a range of elements from a list" do
@@ -78,7 +78,7 @@ module FakeRedis
78
78
  @client.rpush("key1", "v2")
79
79
  @client.rpush("key1", "v3")
80
80
 
81
- @client.lrange("key1", 1, -1).should == ["v2", "v3"]
81
+ @client.lrange("key1", 1, -1).should be == ["v2", "v3"]
82
82
  end
83
83
 
84
84
  it "should remove elements from a list" do
@@ -88,9 +88,9 @@ module FakeRedis
88
88
  @client.rpush("key1", "v2")
89
89
  @client.rpush("key1", "v1")
90
90
 
91
- @client.lrem("key1", 1, "v1").should == 1
92
- @client.lrem("key1", -2, "v2").should == 2
93
- @client.llen("key1").should == 2
91
+ @client.lrem("key1", 1, "v1").should be == 1
92
+ @client.lrem("key1", -2, "v2").should be == 2
93
+ @client.llen("key1").should be == 2
94
94
  end
95
95
 
96
96
  it "should remove list's key when list is empty" do
@@ -99,7 +99,7 @@ module FakeRedis
99
99
  @client.lrem("key1", 1, "v1")
100
100
  @client.lrem("key1", 1, "v2")
101
101
 
102
- @client.exists("key1").should == false
102
+ @client.exists("key1").should be == false
103
103
  end
104
104
 
105
105
  it "should set the value of an element in a list by its index" do
@@ -109,7 +109,9 @@ module FakeRedis
109
109
 
110
110
  @client.lset("key1", 0, "four")
111
111
  @client.lset("key1", -2, "five")
112
- @client.lrange("key1", 0, -1).should == ["four", "five", "three"]
112
+ @client.lrange("key1", 0, -1).should be == ["four", "five", "three"]
113
+
114
+ lambda { @client.lset("key1", 4, "six") }.should raise_error(Redis::CommandError, "ERR index out of range")
113
115
  end
114
116
 
115
117
  it "should trim a list to the specified range" do
@@ -118,33 +120,34 @@ module FakeRedis
118
120
  @client.rpush("key1", "three")
119
121
 
120
122
  @client.ltrim("key1", 1, -1)
121
- @client.lrange("key1", 0, -1).should == ["two", "three"]
123
+ @client.lrange("key1", 0, -1).should be == ["two", "three"]
122
124
  end
123
125
 
124
- it "should remove and get the last element in a list" do
126
+ it "should remove and return the last element in a list" do
125
127
  @client.rpush("key1", "one")
126
128
  @client.rpush("key1", "two")
127
129
  @client.rpush("key1", "three")
128
130
 
129
- @client.rpop("key1").should == "three"
130
- @client.lrange("key1", 0, -1).should == ["one", "two"]
131
+ @client.rpop("key1").should be == "three"
132
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
131
133
  end
132
134
 
133
135
  it "should remove the last element in a list, append it to another list and return it" do
134
136
  @client.rpush("key1", "one")
135
137
  @client.rpush("key1", "two")
136
138
  @client.rpush("key1", "three")
137
- @client.rpoplpush("key1", "key2")
138
139
 
139
- @client.lrange("key1", 0, -1).should == ["one", "two"]
140
- @client.lrange("key2", 0, -1).should == ["three"]
140
+ @client.rpoplpush("key1", "key2").should be == "three"
141
+
142
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
143
+ @client.lrange("key2", 0, -1).should be == ["three"]
141
144
  end
142
145
 
143
146
  it "should append a value to a list" do
144
147
  @client.rpush("key1", "one")
145
148
  @client.rpush("key1", "two")
146
149
 
147
- @client.lrange("key1", 0, -1).should == ["one", "two"]
150
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
148
151
  end
149
152
 
150
153
  it "should append a value to a list, only if the list exists" do
@@ -152,8 +155,8 @@ module FakeRedis
152
155
  @client.rpushx("key1", "two")
153
156
  @client.rpushx("key2", "two")
154
157
 
155
- @client.lrange("key1", 0, -1).should == ["one", "two"]
156
- @client.lrange("key2", 0, -1).should == []
158
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
159
+ @client.lrange("key2", 0, -1).should be == []
157
160
  end
158
161
  end
159
162
  end
@@ -12,11 +12,89 @@ 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(Redis::CommandError, "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 store keys separately in each database" do
37
+ @client.select(0).should be == "OK"
38
+ @client.set("key1", "1")
39
+ @client.set("key2", "2")
40
+
41
+ @client.select(1)
42
+ @client.set("key3", "3")
43
+ @client.set("key4", "4")
44
+ @client.set("key5", "5")
45
+
46
+ @client.select(0)
47
+ @client.dbsize.should be == 2
48
+ @client.exists("key1").should be_true
49
+ @client.exists("key3").should be_false
50
+
51
+ @client.select(1)
52
+ @client.dbsize.should be == 3
53
+ @client.exists("key4").should be_true
54
+ @client.exists("key2").should be_false
55
+
56
+ @client.flushall
57
+ @client.dbsize.should be == 0
58
+
59
+ @client.select(0)
60
+ @client.dbsize.should be == 0
61
+ end
62
+
63
+ it "should flush a database" do
64
+ @client.select(0)
65
+ @client.set("key1", "1")
66
+ @client.set("key2", "2")
67
+ @client.dbsize.should be == 2
68
+
69
+ @client.select(1)
70
+ @client.set("key3", "3")
71
+ @client.set("key4", "4")
72
+ @client.dbsize.should be == 2
73
+
74
+ @client.flushdb.should be == "OK"
75
+
76
+ @client.dbsize.should be == 0
77
+ @client.select(0)
78
+ @client.dbsize.should be == 2
79
+ end
80
+
81
+ it "should flush all databases" do
82
+ @client.select(0)
83
+ @client.set("key3", "3")
84
+ @client.set("key4", "4")
85
+ @client.dbsize.should be == 2
86
+
87
+ @client.select(1)
88
+ @client.set("key3", "3")
89
+ @client.set("key4", "4")
90
+ @client.dbsize.should be == 2
91
+
92
+ @client.flushall.should be == "OK"
93
+
94
+ @client.dbsize.should be == 0
95
+ @client.select(0)
96
+ @client.dbsize.should be == 0
97
+ end
20
98
  end
21
99
  end
22
100
  end
@@ -7,15 +7,15 @@ 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
14
  end
15
15
 
16
16
  it "should add multiple members to a set" do
17
- @client.sadd("key", %w(value other something more)).should == 4
18
- @client.sadd("key", %w(and additional values)).should == 3
17
+ @client.sadd("key", %w(value other something more)).should be == 4
18
+ @client.sadd("key", %w(and additional values)).should be == 3
19
19
  @client.smembers("key").should =~ ["value", "other", "something", "more", "and", "additional", "values"]
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ module FakeRedis
23
23
  @client.sadd("key", "val1")
24
24
  @client.sadd("key", "val2")
25
25
 
26
- @client.scard("key").should == 2
26
+ @client.scard("key").should be == 2
27
27
  end
28
28
 
29
29
  it "should subtract multiple sets" do
@@ -63,7 +63,7 @@ module FakeRedis
63
63
  @client.sadd("key3", "c")
64
64
  @client.sadd("key3", "e")
65
65
 
66
- @client.sinter("key1", "key2", "key3").should == ["c"]
66
+ @client.sinter("key1", "key2", "key3").should be == ["c"]
67
67
  end
68
68
 
69
69
  it "should intersect multiple sets and store the resulting set in a key" do
@@ -76,15 +76,15 @@ module FakeRedis
76
76
  @client.sadd("key3", "c")
77
77
  @client.sadd("key3", "e")
78
78
  @client.sinterstore("key", "key1", "key2", "key3")
79
- @client.smembers("key").should == ["c"]
79
+ @client.smembers("key").should be == ["c"]
80
80
  end
81
81
 
82
82
  it "should determine if a given value is a member of a set" do
83
83
  @client.sadd("key1", "a")
84
84
 
85
- @client.sismember("key1", "a").should == true
86
- @client.sismember("key1", "b").should == false
87
- @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
88
88
  end
89
89
 
90
90
  it "should get all the members in a set" do
@@ -100,10 +100,10 @@ module FakeRedis
100
100
  @client.sadd("key1", "a")
101
101
  @client.sadd("key1", "b")
102
102
  @client.sadd("key2", "c")
103
- @client.smove("key1", "key2", "a").should == true
104
- @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
105
105
 
106
- @client.smembers("key1").should == ["b"]
106
+ @client.smembers("key1").should be == ["b"]
107
107
  @client.smembers("key2").should =~ ["c", "a"]
108
108
  end
109
109
 
@@ -126,10 +126,10 @@ module FakeRedis
126
126
  it "should remove a member from a set" do
127
127
  @client.sadd("key1", "a")
128
128
  @client.sadd("key1", "b")
129
- @client.srem("key1", "a").should == true
130
- @client.srem("key1", "a").should == false
129
+ @client.srem("key1", "a").should be == true
130
+ @client.srem("key1", "a").should be == false
131
131
 
132
- @client.smembers("key1").should == ["b"]
132
+ @client.smembers("key1").should be == ["b"]
133
133
  end
134
134
 
135
135
  it "should remove the set's key once it's empty" do
@@ -138,7 +138,7 @@ module FakeRedis
138
138
  @client.srem("key1", "b")
139
139
  @client.srem("key1", "a")
140
140
 
141
- @client.exists("key1").should == false
141
+ @client.exists("key1").should be == false
142
142
  end
143
143
 
144
144
  it "should add multiple sets" do
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module FakeRedis
4
+ Infinity = 1.0/0.0
5
+
4
6
  describe "SortedSetsMethods" do
5
7
  before(:each) do
6
8
  @client = Redis.new
@@ -8,10 +10,28 @@ module FakeRedis
8
10
 
9
11
  it "should add a member to a sorted set, or update its score if it already exists" do
10
12
  @client.zadd("key", 1, "val").should be(true)
11
- @client.zscore("key", "val").should == 1.0
13
+ @client.zscore("key", "val").should be == 1.0
12
14
 
13
15
  @client.zadd("key", 2, "val").should be(false)
14
- @client.zscore("key", "val").should == 2.0
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
15
35
  end
16
36
 
17
37
  it "should add multiple members to a sorted set, or update its score if it already exists" do
@@ -36,10 +56,10 @@ module FakeRedis
36
56
 
37
57
  it "should allow floats as scores when adding or updating" do
38
58
  @client.zadd("key", 4.321, "val").should be(true)
39
- @client.zscore("key", "val").should == 4.321
59
+ @client.zscore("key", "val").should be == 4.321
40
60
 
41
61
  @client.zadd("key", 54.3210, "val").should be(false)
42
- @client.zscore("key", "val").should == 54.321
62
+ @client.zscore("key", "val").should be == 54.321
43
63
  end
44
64
 
45
65
  it "should remove members from sorted sets" do
@@ -48,10 +68,21 @@ module FakeRedis
48
68
  @client.zrem("key", "val").should be(true)
49
69
  end
50
70
 
71
+ it "should remove multiple members from sorted sets" do
72
+ @client.zrem("key2", %w(val)).should be == 0
73
+ @client.zrem("key2", %w(val val2 val3)).should be == 0
74
+
75
+ @client.zadd("key2", 1, "val")
76
+ @client.zadd("key2", 1, "val2")
77
+ @client.zadd("key2", 1, "val3")
78
+
79
+ @client.zrem("key2", %w(val val2 val3 val4)).should be == 3
80
+ end
81
+
51
82
  it "should remove sorted set's key when it is empty" do
52
83
  @client.zadd("key", 1, "val")
53
84
  @client.zrem("key", "val")
54
- @client.exists("key").should == false
85
+ @client.exists("key").should be == false
55
86
  end
56
87
 
57
88
  it "should get the number of members in a sorted set" do
@@ -59,7 +90,7 @@ module FakeRedis
59
90
  @client.zadd("key", 2, "val1")
60
91
  @client.zadd("key", 5, "val3")
61
92
 
62
- @client.zcard("key").should == 3
93
+ @client.zcard("key").should be == 3
63
94
  end
64
95
 
65
96
  it "should count the members in a sorted set with scores within the given values" do
@@ -67,34 +98,48 @@ module FakeRedis
67
98
  @client.zadd("key", 2, "val2")
68
99
  @client.zadd("key", 3, "val3")
69
100
 
70
- @client.zcount("key", 2, 3).should == 2
101
+ @client.zcount("key", 2, 3).should be == 2
71
102
  end
72
103
 
73
104
  it "should increment the score of a member in a sorted set" do
74
105
  @client.zadd("key", 1, "val1")
75
- @client.zincrby("key", 2, "val1").should == 3
76
- @client.zscore("key", "val1").should == 3
106
+ @client.zincrby("key", 2, "val1").should be == 3
107
+ @client.zscore("key", "val1").should be == 3
77
108
  end
78
109
 
79
110
  it "initializes the sorted set if the key wasnt already set" do
80
- @client.zincrby("key", 1, "val1").should == 1
111
+ @client.zincrby("key", 1, "val1").should be == 1
81
112
  end
82
113
 
83
114
  it "should convert the key to a string for zscore" do
84
115
  @client.zadd("key", 1, 1)
85
- @client.zscore("key", 1).should == 1
116
+ @client.zscore("key", 1).should be == 1
117
+ end
118
+
119
+ # These won't pass until redis-rb releases v3.0.2
120
+ it "should handle infinity values when incrementing a sorted set key" do
121
+ @client.zincrby("bar", "+inf", "s2").should be == Infinity
122
+ @client.zincrby("bar", "-inf", "s1").should be == -Infinity
86
123
  end
87
- #it "should intersect multiple sorted sets and store the resulting sorted set in a new key"
88
124
 
89
125
  it "should return a range of members in a sorted set, by index" do
90
126
  @client.zadd("key", 1, "one")
91
127
  @client.zadd("key", 2, "two")
92
128
  @client.zadd("key", 3, "three")
93
129
 
94
- @client.zrange("key", 0, -1).should == ["one", "two", "three"]
95
- @client.zrange("key", 1, 2).should == ["two", "three"]
96
- @client.zrange("key", 0, -1, :withscores => true).should == [["one", 1], ["two", 2], ["three", 3]]
97
- @client.zrange("key", 1, 2, :with_scores => true).should == [["two", 2], ["three", 3]]
130
+ @client.zrange("key", 0, -1).should be == ["one", "two", "three"]
131
+ @client.zrange("key", 1, 2).should be == ["two", "three"]
132
+ @client.zrange("key", 0, -1, :withscores => true).should be == [["one", 1], ["two", 2], ["three", 3]]
133
+ @client.zrange("key", 1, 2, :with_scores => true).should be == [["two", 2], ["three", 3]]
134
+ end
135
+
136
+ it "should sort zrange results logically" do
137
+ @client.zadd("key", 5, "val2")
138
+ @client.zadd("key", 5, "val3")
139
+ @client.zadd("key", 5, "val1")
140
+
141
+ @client.zrange("key", 0, -1).should be == %w(val1 val2 val3)
142
+ @client.zrange("key", 0, -1, :with_scores => true).should be == [["val1", 5], ["val2", 5], ["val3", 5]]
98
143
  end
99
144
 
100
145
  it "should return a reversed range of members in a sorted set, by index" do
@@ -102,10 +147,10 @@ module FakeRedis
102
147
  @client.zadd("key", 2, "two")
103
148
  @client.zadd("key", 3, "three")
104
149
 
105
- @client.zrevrange("key", 0, -1).should == ["three", "two", "one"]
106
- @client.zrevrange("key", 1, 2).should == ["two", "one"]
107
- @client.zrevrange("key", 0, -1, :withscores => true).should == [["three", 3], ["two", 2], ["one", 1]]
108
- @client.zrevrange("key", 0, -1, :with_scores => true).should == [["three", 3], ["two", 2], ["one", 1]]
150
+ @client.zrevrange("key", 0, -1).should be == ["three", "two", "one"]
151
+ @client.zrevrange("key", 1, 2).should be == ["two", "one"]
152
+ @client.zrevrange("key", 0, -1, :withscores => true).should be == [["three", 3], ["two", 2], ["one", 1]]
153
+ @client.zrevrange("key", 0, -1, :with_scores => true).should be == [["three", 3], ["two", 2], ["one", 1]]
109
154
  end
110
155
 
111
156
  it "should return a range of members in a sorted set, by score" do
@@ -113,16 +158,16 @@ module FakeRedis
113
158
  @client.zadd("key", 2, "two")
114
159
  @client.zadd("key", 3, "three")
115
160
 
116
- @client.zrangebyscore("key", 0, 100).should == ["one", "two", "three"]
117
- @client.zrangebyscore("key", 1, 2).should == ["one", "two"]
118
- @client.zrangebyscore("key", 0, 100, :withscores => true).should == [["one", 1], ["two", 2], ["three", 3]]
119
- @client.zrangebyscore("key", 1, 2, :with_scores => true).should == [["one", 1], ["two", 2]]
120
- @client.zrangebyscore("key", 0, 100, :limit => [0, 1]).should == ["one"]
121
- @client.zrangebyscore("key", 0, 100, :limit => [0, -1]).should == ["one", "two", "three"]
122
- @client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).should == [["two", 2], ["three", 3]]
123
- @client.zrangebyscore("key", '-inf', '+inf').should == ["one", "two", "three"]
124
- @client.zrangebyscore("key", 2, '+inf').should == ["two", "three"]
125
- @client.zrangebyscore("key", '-inf', 2).should == ['one', "two"]
161
+ @client.zrangebyscore("key", 0, 100).should be == ["one", "two", "three"]
162
+ @client.zrangebyscore("key", 1, 2).should be == ["one", "two"]
163
+ @client.zrangebyscore("key", 0, 100, :withscores => true).should be == [["one", 1], ["two", 2], ["three", 3]]
164
+ @client.zrangebyscore("key", 1, 2, :with_scores => true).should be == [["one", 1], ["two", 2]]
165
+ @client.zrangebyscore("key", 0, 100, :limit => [0, 1]).should be == ["one"]
166
+ @client.zrangebyscore("key", 0, 100, :limit => [0, -1]).should be == ["one", "two", "three"]
167
+ @client.zrangebyscore("key", 0, 100, :limit => [1, -1], :with_scores => true).should be == [["two", 2], ["three", 3]]
168
+ @client.zrangebyscore("key", '-inf', '+inf').should be == ["one", "two", "three"]
169
+ @client.zrangebyscore("key", 2, '+inf').should be == ["two", "three"]
170
+ @client.zrangebyscore("key", '-inf', 2).should be == ['one', "two"]
126
171
  end
127
172
 
128
173
  it "should return a reversed range of members in a sorted set, by score" do
@@ -130,13 +175,13 @@ module FakeRedis
130
175
  @client.zadd("key", 2, "two")
131
176
  @client.zadd("key", 3, "three")
132
177
 
133
- @client.zrevrangebyscore("key", 100, 0).should == ["three", "two", "one"]
134
- @client.zrevrangebyscore("key", 2, 1).should == ["two", "one"]
135
- @client.zrevrangebyscore("key", 1, 2).should == []
136
- @client.zrevrangebyscore("key", 2, 1, :with_scores => true).should == [["two", 2], ["one", 1]]
137
- @client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).should == ["three"]
138
- @client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).should == ["three", "two", "one"]
139
- @client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).should == [["two", 2], ["one", 1]]
178
+ @client.zrevrangebyscore("key", 100, 0).should be == ["three", "two", "one"]
179
+ @client.zrevrangebyscore("key", 2, 1).should be == ["two", "one"]
180
+ @client.zrevrangebyscore("key", 1, 2).should be == []
181
+ @client.zrevrangebyscore("key", 2, 1, :with_scores => true).should be == [["two", 2], ["one", 1]]
182
+ @client.zrevrangebyscore("key", 100, 0, :limit => [0, 1]).should be == ["three"]
183
+ @client.zrevrangebyscore("key", 100, 0, :limit => [0, -1]).should be == ["three", "two", "one"]
184
+ @client.zrevrangebyscore("key", 100, 0, :limit => [1, -1], :with_scores => true).should be == [["two", 2], ["one", 1]]
140
185
  end
141
186
 
142
187
  it "should determine the index of a member in a sorted set" do
@@ -144,7 +189,7 @@ module FakeRedis
144
189
  @client.zadd("key", 2, "two")
145
190
  @client.zadd("key", 3, "three")
146
191
 
147
- @client.zrank("key", "three").should == 2
192
+ @client.zrank("key", "three").should be == 2
148
193
  @client.zrank("key", "four").should be_nil
149
194
  end
150
195
 
@@ -153,30 +198,83 @@ module FakeRedis
153
198
  @client.zadd("key", 2, "two")
154
199
  @client.zadd("key", 3, "three")
155
200
 
156
- @client.zrevrank("key", "three").should == 0
201
+ @client.zrevrank("key", "three").should be == 0
157
202
  @client.zrevrank("key", "four").should be_nil
158
203
  end
159
204
 
160
- it "should create intersections between multiple (sorted) sets and store the resulting sorted set in a new key" do
161
- @client.zadd("key1", 1, "one")
162
- @client.zadd("key1", 2, "two")
163
- @client.zadd("key1", 3, "three")
164
- @client.zadd("key2", 5, "two")
165
- @client.zadd("key2", 7, "three")
166
- @client.sadd("key3", 'one')
167
- @client.sadd("key3", 'two')
205
+ describe "#zinterstore" do
206
+ before do
207
+ @client.zadd("key1", 1, "one")
208
+ @client.zadd("key1", 2, "two")
209
+ @client.zadd("key1", 3, "three")
210
+ @client.zadd("key2", 5, "two")
211
+ @client.zadd("key2", 7, "three")
212
+ @client.sadd("key3", 'one')
213
+ @client.sadd("key3", 'two')
214
+ end
215
+
216
+ it "should intersect two keys with custom scores" do
217
+ @client.zinterstore("out", ["key1", "key2"]).should be == 2
218
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [['two', (2 + 5)], ['three', (3 + 7)]]
219
+ end
220
+
221
+ it "should intersect two keys with a default score" do
222
+ @client.zinterstore("out", ["key1", "key3"]).should be == 2
223
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [['one', (1 + 1)], ['two', (2 + 1)]]
224
+ end
225
+
226
+ it "should intersect more than two keys" do
227
+ @client.zinterstore("out", ["key1", "key2", "key3"]).should be == 1
228
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [['two', (2 + 5 + 1)]]
229
+ end
230
+
231
+ it "should not intersect an unknown key" do
232
+ @client.zinterstore("out", ["key1", "no_key"]).should be == 0
233
+ @client.zrange("out", 0, -1, :with_scores => true).should be == []
234
+ end
235
+
236
+ it "should intersect two keys by minimum values" do
237
+ @client.zinterstore("out", ["key1", "key2"], :aggregate => :min).should be == 2
238
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 2], ["three", 3]]
239
+ end
168
240
 
169
- @client.zinterstore("out", ["key1", "key2"]).should == 2
170
- @client.zrange("out", 0, 100, :with_scores => true).should == [['two', 7], ['three', 10]]
241
+ it "should intersect two keys by maximum values" do
242
+ @client.zinterstore("out", ["key1", "key2"], :aggregate => :max).should be == 2
243
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 5], ["three", 7]]
244
+ end
171
245
 
172
- @client.zinterstore("out", ["key1", "key3"]).should == 2
173
- @client.zrange("out", 0, 100, :with_scores => true).should == [['one', 2], ['two', 3]]
246
+ it "should intersect two keys by explicitly summing values" do
247
+ @client.zinterstore("out", %w(key1 key2), :aggregate => :sum).should be == 2
248
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 + 5)], ["three", (3 + 7)]]
249
+ end
174
250
 
175
- @client.zinterstore("out", ["key1", "key2", "key3"]).should == 1
176
- @client.zrange("out", 0, 100, :with_scores => true).should == [['two', 8]]
251
+ it "should intersect two keys with weighted values" do
252
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1]).should be == 2
253
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 * 10 + 5)], ["three", (3 * 10 + 7)]]
254
+ end
177
255
 
178
- @client.zinterstore("out", ["key1", "no_key"]).should == 0
179
- @client.zrange("out", 0, 100, :with_scores => true).should == []
256
+ it "should intersect two keys with weighted minimum values" do
257
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 2
258
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", 5], ["three", 7]]
259
+ end
260
+
261
+ it "should intersect two keys with weighted maximum values" do
262
+ @client.zinterstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 2
263
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["two", (2 * 10)], ["three", (3 * 10)]]
264
+ end
265
+
266
+ it "should error without enough weights given" do
267
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => []) }.should raise_error(Redis::CommandError, "ERR syntax error")
268
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(Redis::CommandError, "ERR syntax error")
269
+ end
270
+
271
+ it "should error with too many weights given" do
272
+ lambda { @client.zinterstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(Redis::CommandError, "ERR syntax error")
273
+ end
274
+
275
+ it "should error with an invalid aggregate" do
276
+ lambda { @client.zinterstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(Redis::CommandError, "ERR syntax error")
277
+ end
180
278
  end
181
279
 
182
280
  context "zremrangebyscore" do
@@ -185,12 +283,23 @@ module FakeRedis
185
283
  @client.zadd("key", 2, "two")
186
284
  @client.zadd("key", 3, "three")
187
285
 
188
- @client.zremrangebyscore("key", 0, 2).should == 2
189
- @client.zcard("key").should == 1
286
+ @client.zremrangebyscore("key", 0, 2).should be == 2
287
+ @client.zcard("key").should be == 1
288
+ end
289
+
290
+ it "should remove items by score with infinity" do # Issue #50
291
+ @client.zadd("key", 10.0, "one")
292
+ @client.zadd("key", 20.0, "two")
293
+ @client.zadd("key", 30.0, "three")
294
+ @client.zremrangebyscore("key", "-inf", "+inf").should be == 3
295
+ @client.zcard("key").should be == 0
296
+ @client.zscore("key", "one").should be_nil
297
+ @client.zscore("key", "two").should be_nil
298
+ @client.zscore("key", "three").should be_nil
190
299
  end
191
300
 
192
301
  it "should return 0 if the key didn't exist" do
193
- @client.zremrangebyscore("key", 0, 2).should == 0
302
+ @client.zremrangebyscore("key", 0, 2).should be == 0
194
303
  end
195
304
  end
196
305
 
@@ -200,8 +309,8 @@ module FakeRedis
200
309
  @client.zadd("key", 2, "two")
201
310
  @client.zadd("key", 3, "three")
202
311
 
203
- @client.zremrangebyrank("key", 0, 1).should == 2
204
- @client.zcard('key').should == 1
312
+ @client.zremrangebyrank("key", 0, 1).should be == 2
313
+ @client.zcard('key').should be == 1
205
314
  end
206
315
 
207
316
  it 'handles out of range requests' do
@@ -209,8 +318,83 @@ module FakeRedis
209
318
  @client.zadd("key", 2, "two")
210
319
  @client.zadd("key", 3, "three")
211
320
 
212
- @client.zremrangebyrank("key", 25, -1).should == 0
213
- @client.zcard('key').should == 3
321
+ @client.zremrangebyrank("key", 25, -1).should be == 0
322
+ @client.zcard('key').should be == 3
323
+ end
324
+ end
325
+
326
+ describe "#zunionstore" do
327
+ before do
328
+ @client.zadd("key1", 1, "val1")
329
+ @client.zadd("key1", 2, "val2")
330
+ @client.zadd("key1", 3, "val3")
331
+ @client.zadd("key2", 5, "val2")
332
+ @client.zadd("key2", 7, "val3")
333
+ @client.sadd("key3", "val1")
334
+ @client.sadd("key3", "val2")
335
+ end
336
+
337
+ it "should union two keys with custom scores" do
338
+ @client.zunionstore("out", %w(key1 key2)).should be == 3
339
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]]
340
+ end
341
+
342
+ it "should union two keys with a default score" do
343
+ @client.zunionstore("out", %w(key1 key3)).should be == 3
344
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 + 1)], ["val2", (2 + 1)], ["val3", 3]]
345
+ end
346
+
347
+ it "should union more than two keys" do
348
+ @client.zunionstore("out", %w(key1 key2 key3)).should be == 3
349
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 + 1)], ["val2", (2 + 5 + 1)], ["val3", (3 + 7)]]
350
+ end
351
+
352
+ it "should union with an unknown key" do
353
+ @client.zunionstore("out", %w(key1 no_key)).should be == 3
354
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 2], ["val3", 3]]
355
+ end
356
+
357
+ it "should union two keys by minimum values" do
358
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :min).should be == 3
359
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 2], ["val3", 3]]
360
+ end
361
+
362
+ it "should union two keys by maximum values" do
363
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :max).should be == 3
364
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", 5], ["val3", 7]]
365
+ end
366
+
367
+ it "should union two keys by explicitly summing values" do
368
+ @client.zunionstore("out", %w(key1 key2), :aggregate => :sum).should be == 3
369
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", 1], ["val2", (2 + 5)], ["val3", (3 + 7)]]
370
+ end
371
+
372
+ it "should union two keys with weighted values" do
373
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1]).should be == 3
374
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 * 10)], ["val2", (2 * 10 + 5)], ["val3", (3 * 10 + 7)]]
375
+ end
376
+
377
+ it "should union two keys with weighted minimum values" do
378
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :min).should be == 3
379
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val2", 5], ["val3", 7], ["val1", (1 * 10)]]
380
+ end
381
+
382
+ it "should union two keys with weighted maximum values" do
383
+ @client.zunionstore("out", %w(key1 key2), :weights => [10, 1], :aggregate => :max).should be == 3
384
+ @client.zrange("out", 0, -1, :with_scores => true).should be == [["val1", (1 * 10)], ["val2", (2 * 10)], ["val3", (3 * 10)]]
385
+ end
386
+
387
+ it "should error without enough weights given" do
388
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => []) }.should raise_error(Redis::CommandError, "ERR syntax error")
389
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10]) }.should raise_error(Redis::CommandError, "ERR syntax error")
390
+ end
391
+
392
+ it "should error with too many weights given" do
393
+ lambda { @client.zunionstore("out", %w(key1 key2), :weights => [10, 1, 1]) }.should raise_error(Redis::CommandError, "ERR syntax error")
394
+ end
395
+
396
+ it "should error with an invalid aggregate" do
397
+ lambda { @client.zunionstore("out", %w(key1 key2), :aggregate => :invalid) }.should raise_error(Redis::CommandError, "ERR syntax error")
214
398
  end
215
399
  end
216
400