fakeredis 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -2
- data/LICENSE +1 -1
- data/README.md +3 -2
- data/fakeredis.gemspec +2 -3
- data/lib/fake_redis.rb +1 -0
- data/lib/fakeredis/expiring_hash.rb +70 -0
- data/lib/fakeredis/rspec.rb +1 -1
- data/lib/fakeredis/sorted_set_argument_handler.rb +74 -0
- data/lib/fakeredis/sorted_set_store.rb +80 -0
- data/lib/fakeredis/version.rb +1 -1
- data/lib/fakeredis/zset.rb +29 -0
- data/lib/redis/connection/memory.rb +280 -279
- data/spec/compatibility_spec.rb +2 -2
- data/spec/connection_spec.rb +62 -1
- data/spec/hashes_spec.rb +40 -29
- data/spec/keys_spec.rb +105 -25
- data/spec/lists_spec.rb +34 -31
- data/spec/server_spec.rb +80 -2
- data/spec/sets_spec.rb +18 -18
- data/spec/sorted_sets_spec.rb +246 -62
- data/spec/spec_helper_live_redis.rb +1 -1
- data/spec/strings_spec.rb +79 -55
- data/spec/transactions_spec.rb +1 -1
- metadata +12 -7
data/spec/compatibility_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module FakeRedis
|
4
4
|
describe "Compatibility" do
|
5
|
-
it "should be accessible
|
6
|
-
lambda {
|
5
|
+
it "should be accessible through FakeRedis::Redis" do
|
6
|
+
lambda { FakeRedis::Redis.new }.should_not raise_error
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/spec/connection_spec.rb
CHANGED
@@ -9,10 +9,71 @@ module FakeRedis
|
|
9
9
|
|
10
10
|
if fakeredis?
|
11
11
|
it "should authenticate to the server" do
|
12
|
-
@client.auth("pass").should == "OK"
|
12
|
+
@client.auth("pass").should be == "OK"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should re-use the same instance with the same host & port" do
|
16
|
+
@client1 = Redis.new(:host => "localhost", :port => 1234)
|
17
|
+
@client2 = Redis.new(:host => "localhost", :port => 1234)
|
18
|
+
@client3 = Redis.new(:host => "localhost", :port => 5678)
|
19
|
+
|
20
|
+
@client1.set("key1", "1")
|
21
|
+
@client2.get("key1").should be == "1"
|
22
|
+
@client3.get("key1").should be_nil
|
23
|
+
|
24
|
+
@client2.set("key2", "2")
|
25
|
+
@client1.get("key2").should be == "2"
|
26
|
+
@client3.get("key2").should be_nil
|
27
|
+
|
28
|
+
@client3.set("key3", "3")
|
29
|
+
@client1.get("key3").should be_nil
|
30
|
+
@client2.get("key3").should be_nil
|
31
|
+
|
32
|
+
@client1.dbsize.should be == 2
|
33
|
+
@client2.dbsize.should be == 2
|
34
|
+
@client3.dbsize.should be == 1
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should connect to a specific database" do
|
38
|
+
@client1 = Redis.new(:host => "localhost", :port => 1234, :db => 0)
|
39
|
+
@client1.set("key1", "1")
|
40
|
+
@client1.select(0)
|
41
|
+
@client1.get("key1").should be == "1"
|
42
|
+
|
43
|
+
@client2 = Redis.new(:host => "localhost", :port => 1234, :db => 1)
|
44
|
+
@client2.set("key1", "1")
|
45
|
+
@client2.select(1)
|
46
|
+
@client2.get("key1").should == "1"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not error with shutdown" do
|
50
|
+
lambda { @client.shutdown }.should_not raise_error
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not error with quit" do
|
54
|
+
lambda { @client.quit }.should_not raise_error
|
13
55
|
end
|
14
56
|
end
|
15
57
|
|
58
|
+
it "should handle multiple clients using the same db instance" do
|
59
|
+
@client1 = Redis.new(:host => "localhost", :port => 6379, :db => 1)
|
60
|
+
@client2 = Redis.new(:host => "localhost", :port => 6379, :db => 2)
|
61
|
+
|
62
|
+
@client1.set("key1", "one")
|
63
|
+
@client1.get("key1").should be == "one"
|
64
|
+
|
65
|
+
@client2.set("key2", "two")
|
66
|
+
@client2.get("key2").should be == "two"
|
67
|
+
|
68
|
+
@client1.get("key1").should be == "one"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not error with a disconnected client" do
|
72
|
+
@client1 = Redis.new
|
73
|
+
@client1.client.disconnect
|
74
|
+
@client1.get("key1").should be_nil
|
75
|
+
end
|
76
|
+
|
16
77
|
it "should echo the given string" do
|
17
78
|
@client.echo("something").should == "something"
|
18
79
|
end
|
data/spec/hashes_spec.rb
CHANGED
@@ -12,7 +12,7 @@ module FakeRedis
|
|
12
12
|
@client.hdel("key1", "k1")
|
13
13
|
|
14
14
|
@client.hget("key1", "k1").should be_nil
|
15
|
-
@client.hget("key1", "k2").should == "val2"
|
15
|
+
@client.hget("key1", "k2").should be == "val2"
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should remove a hash with no keys left" do
|
@@ -21,23 +21,23 @@ module FakeRedis
|
|
21
21
|
@client.hdel("key1", "k1")
|
22
22
|
@client.hdel("key1", "k2")
|
23
23
|
|
24
|
-
@client.exists("key1").should == false
|
24
|
+
@client.exists("key1").should be == false
|
25
25
|
end
|
26
26
|
|
27
|
-
it "should convert key to a string
|
27
|
+
it "should convert key to a string for hset" do
|
28
28
|
m = double("key")
|
29
29
|
m.stub(:to_s).and_return("foo")
|
30
30
|
|
31
31
|
@client.hset("key1", m, "bar")
|
32
|
-
@client.hget("key1", "foo").should == "bar"
|
32
|
+
@client.hget("key1", "foo").should be == "bar"
|
33
33
|
end
|
34
34
|
|
35
|
-
it "should convert key to a string
|
35
|
+
it "should convert key to a string for hget" do
|
36
36
|
m = double("key")
|
37
37
|
m.stub(:to_s).and_return("foo")
|
38
38
|
|
39
39
|
@client.hset("key1", "foo", "bar")
|
40
|
-
@client.hget("key1", m).should == "bar"
|
40
|
+
@client.hget("key1", m).should be == "bar"
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should determine if a hash field exists" do
|
@@ -50,25 +50,25 @@ module FakeRedis
|
|
50
50
|
it "should get the value of a hash field" do
|
51
51
|
@client.hset("key1", "index", "value")
|
52
52
|
|
53
|
-
@client.hget("key1", "index").should == "value"
|
53
|
+
@client.hget("key1", "index").should be == "value"
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should get all the fields and values in a hash" do
|
57
57
|
@client.hset("key1", "i1", "val1")
|
58
58
|
@client.hset("key1", "i2", "val2")
|
59
59
|
|
60
|
-
@client.hgetall("key1").should == {"i1" => "val1", "i2" => "val2"}
|
60
|
+
@client.hgetall("key1").should be == {"i1" => "val1", "i2" => "val2"}
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should increment the integer value of a hash field by the given number" do
|
64
64
|
@client.hset("key1", "cont1", "5")
|
65
|
-
@client.hincrby("key1", "cont1", "5").should == 10
|
66
|
-
@client.hget("key1", "cont1").should == "10"
|
65
|
+
@client.hincrby("key1", "cont1", "5").should be == 10
|
66
|
+
@client.hget("key1", "cont1").should be == "10"
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should increment non existing hash keys" do
|
70
70
|
@client.hget("key1", "cont2").should be_nil
|
71
|
-
@client.hincrby("key1", "cont2", "5").should == 5
|
71
|
+
@client.hincrby("key1", "cont2", "5").should be == 5
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should get all the fields in a hash" do
|
@@ -76,14 +76,14 @@ module FakeRedis
|
|
76
76
|
@client.hset("key1", "i2", "val2")
|
77
77
|
|
78
78
|
@client.hkeys("key1").should =~ ["i1", "i2"]
|
79
|
-
@client.hkeys("key2").should == []
|
79
|
+
@client.hkeys("key2").should be == []
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should get the number of fields in a hash" do
|
83
83
|
@client.hset("key1", "i1", "val1")
|
84
84
|
@client.hset("key1", "i2", "val2")
|
85
85
|
|
86
|
-
@client.hlen("key1").should == 2
|
86
|
+
@client.hlen("key1").should be == 2
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should get the values of all the given hash fields" do
|
@@ -91,10 +91,10 @@ module FakeRedis
|
|
91
91
|
@client.hset("key1", "i2", "val2")
|
92
92
|
|
93
93
|
@client.hmget("key1", "i1", "i2", "i3").should =~ ["val1", "val2", nil]
|
94
|
-
@client.hmget("key2", "i1", "i2").should == [nil, nil]
|
94
|
+
@client.hmget("key2", "i1", "i2").should be == [nil, nil]
|
95
95
|
end
|
96
96
|
|
97
|
-
it "
|
97
|
+
it "should throw an argument error when you don't ask for any keys" do
|
98
98
|
lambda { @client.hmget("key1") }.should raise_error(Redis::CommandError)
|
99
99
|
end
|
100
100
|
|
@@ -103,36 +103,47 @@ module FakeRedis
|
|
103
103
|
@client.exists("key").should be_false
|
104
104
|
end
|
105
105
|
|
106
|
-
it
|
106
|
+
it "rejects an insert with a key but no value" do
|
107
107
|
lambda { @client.hmset("key", 'foo') }.should raise_error(Redis::CommandError)
|
108
108
|
lambda { @client.hmset("key", 'foo', 3, 'bar') }.should raise_error(Redis::CommandError)
|
109
109
|
@client.exists("key").should be_false
|
110
110
|
end
|
111
111
|
|
112
|
+
it "should reject the wrong number of arguments" do
|
113
|
+
lambda { @client.hmset("hash", "foo1", "bar1", "foo2", "bar2", "foo3") }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for HMSET")
|
114
|
+
end
|
115
|
+
|
112
116
|
it "should set multiple hash fields to multiple values" do
|
113
117
|
@client.hmset("key", "k1", "value1", "k2", "value2")
|
114
118
|
|
115
|
-
@client.hget("key", "k1").should == "value1"
|
116
|
-
@client.hget("key", "k2").should == "value2"
|
119
|
+
@client.hget("key", "k1").should be == "value1"
|
120
|
+
@client.hget("key", "k2").should be == "value2"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should set multiple hash fields from a ruby hash to multiple values" do
|
124
|
+
@client.mapped_hmset("foo", :k1 => "value1", :k2 => "value2")
|
125
|
+
|
126
|
+
@client.hget("foo", "k1").should be == "value1"
|
127
|
+
@client.hget("foo", "k2").should be == "value2"
|
117
128
|
end
|
118
129
|
|
119
130
|
it "should set the string value of a hash field" do
|
120
|
-
@client.hset("key1", "k1", "val1").should == true
|
121
|
-
@client.hset("key1", "k1", "val1").should == false
|
131
|
+
@client.hset("key1", "k1", "val1").should be == true
|
132
|
+
@client.hset("key1", "k1", "val1").should be == false
|
122
133
|
|
123
|
-
@client.hget("key1", "k1").should == "val1"
|
134
|
+
@client.hget("key1", "k1").should be == "val1"
|
124
135
|
end
|
125
136
|
|
126
137
|
it "should set the value of a hash field, only if the field does not exist" do
|
127
138
|
@client.hset("key1", "k1", "val1")
|
128
|
-
@client.hsetnx("key1", "k1", "value").should == false
|
129
|
-
@client.hsetnx("key1", "k2", "val2").should == true
|
130
|
-
@client.hsetnx("key1", :k1, "value").should == false
|
131
|
-
@client.hsetnx("key1", :k3, "val3").should == true
|
132
|
-
|
133
|
-
@client.hget("key1", "k1").should == "val1"
|
134
|
-
@client.hget("key1", "k2").should == "val2"
|
135
|
-
@client.hget("key1", "k3").should == "val3"
|
139
|
+
@client.hsetnx("key1", "k1", "value").should be == false
|
140
|
+
@client.hsetnx("key1", "k2", "val2").should be == true
|
141
|
+
@client.hsetnx("key1", :k1, "value").should be == false
|
142
|
+
@client.hsetnx("key1", :k3, "val3").should be == true
|
143
|
+
|
144
|
+
@client.hget("key1", "k1").should be == "val1"
|
145
|
+
@client.hget("key1", "k2").should be == "val2"
|
146
|
+
@client.hget("key1", "k3").should be == "val3"
|
136
147
|
end
|
137
148
|
|
138
149
|
it "should get all the values in a hash" do
|
data/spec/keys_spec.rb
CHANGED
@@ -12,7 +12,7 @@ module FakeRedis
|
|
12
12
|
@client.set("key2", "2")
|
13
13
|
@client.del("key1", "key2")
|
14
14
|
|
15
|
-
@client.get("key1").should == nil
|
15
|
+
@client.get("key1").should be == nil
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should delete multiple keys" do
|
@@ -32,39 +32,39 @@ module FakeRedis
|
|
32
32
|
it "should determine if a key exists" do
|
33
33
|
@client.set("key1", "1")
|
34
34
|
|
35
|
-
@client.exists("key1").should == true
|
36
|
-
@client.exists("key2").should == false
|
35
|
+
@client.exists("key1").should be == true
|
36
|
+
@client.exists("key2").should be == false
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should set a key's time to live in seconds" do
|
40
40
|
@client.set("key1", "1")
|
41
41
|
@client.expire("key1", 1)
|
42
42
|
|
43
|
-
@client.ttl("key1").should == 1
|
43
|
+
@client.ttl("key1").should be == 1
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should set the expiration for a key as a UNIX timestamp" do
|
47
47
|
@client.set("key1", "1")
|
48
48
|
@client.expireat("key1", Time.now.to_i + 2)
|
49
49
|
|
50
|
-
@client.ttl("key1").should == 2
|
50
|
+
@client.ttl("key1").should be == 2
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
53
|
+
it "should not have an expiration after re-set" do
|
54
54
|
@client.set("key1", "1")
|
55
55
|
@client.expireat("key1", Time.now.to_i + 2)
|
56
56
|
@client.set("key1", "1")
|
57
57
|
|
58
|
-
@client.ttl("key1").should == -1
|
58
|
+
@client.ttl("key1").should be == -1
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should not have a ttl if expired" do
|
62
62
|
@client.set("key1", "1")
|
63
63
|
@client.expireat("key1", Time.now.to_i)
|
64
64
|
|
65
|
-
@client.ttl("key1").should == -1
|
65
|
+
@client.ttl("key1").should be == -1
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
it "should not find a key if expired" do
|
69
69
|
@client.set("key1", "1")
|
70
70
|
@client.expireat("key1", Time.now.to_i)
|
@@ -77,7 +77,7 @@ module FakeRedis
|
|
77
77
|
@client.set("key2", "2")
|
78
78
|
@client.expireat("key1", Time.now.to_i)
|
79
79
|
|
80
|
-
@client.mget("key1", "key2").should == [nil, "2"]
|
80
|
+
@client.mget("key1", "key2").should be == [nil, "2"]
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should only find keys that aren't expired" do
|
@@ -85,7 +85,7 @@ module FakeRedis
|
|
85
85
|
@client.set("key2", "2")
|
86
86
|
@client.expireat("key1", Time.now.to_i)
|
87
87
|
|
88
|
-
@client.keys.should == ["key2"]
|
88
|
+
@client.keys.should be == ["key2"]
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should not exist if expired" do
|
@@ -108,25 +108,25 @@ module FakeRedis
|
|
108
108
|
it "should remove the expiration from a key" do
|
109
109
|
@client.set("key1", "1")
|
110
110
|
@client.expireat("key1", Time.now.to_i + 1)
|
111
|
-
@client.persist("key1").should == true
|
112
|
-
@client.persist("key1").should == false
|
111
|
+
@client.persist("key1").should be == true
|
112
|
+
@client.persist("key1").should be == false
|
113
113
|
|
114
|
-
@client.ttl("key1").should == -1
|
114
|
+
@client.ttl("key1").should be == -1
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should return a random key from the keyspace" do
|
118
118
|
@client.set("key1", "1")
|
119
119
|
@client.set("key2", "2")
|
120
120
|
|
121
|
-
["key1", "key2"].include?(@client.randomkey).should == true
|
121
|
+
["key1", "key2"].include?(@client.randomkey).should be == true
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should rename a key" do
|
125
125
|
@client.set("key1", "2")
|
126
126
|
@client.rename("key1", "key2")
|
127
127
|
|
128
|
-
@client.get("key1").should == nil
|
129
|
-
@client.get("key2").should == "2"
|
128
|
+
@client.get("key1").should be == nil
|
129
|
+
@client.get("key2").should be == "2"
|
130
130
|
end
|
131
131
|
|
132
132
|
it "should rename a key, only if new key does not exist" do
|
@@ -136,10 +136,10 @@ module FakeRedis
|
|
136
136
|
@client.renamenx("key1", "key2")
|
137
137
|
@client.renamenx("key3", "key4")
|
138
138
|
|
139
|
-
@client.get("key1").should == "1"
|
140
|
-
@client.get("key2").should == "2"
|
141
|
-
@client.get("key3").should == nil
|
142
|
-
@client.get("key4").should == "3"
|
139
|
+
@client.get("key1").should be == "1"
|
140
|
+
@client.get("key2").should be == "2"
|
141
|
+
@client.get("key3").should be == nil
|
142
|
+
@client.get("key4").should be == "3"
|
143
143
|
end
|
144
144
|
|
145
145
|
it "should sort the elements in a list, set or sorted set" do
|
@@ -149,16 +149,96 @@ module FakeRedis
|
|
149
149
|
it "should determine the type stored at key" do
|
150
150
|
@client.set("key1", "1")
|
151
151
|
|
152
|
-
@client.type("key1").should == "string"
|
153
|
-
@client.type("key0").should == "none"
|
152
|
+
@client.type("key1").should be == "string"
|
153
|
+
@client.type("key0").should be == "none"
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should convert the value into a string before storing" do
|
157
157
|
@client.set("key1", 1)
|
158
|
-
@client.get("key1").should == "1"
|
158
|
+
@client.get("key1").should be == "1"
|
159
159
|
|
160
160
|
@client.setex("key2", 30, 1)
|
161
|
-
@client.get("key2").should == "1"
|
161
|
+
@client.get("key2").should be == "1"
|
162
|
+
|
163
|
+
@client.getset("key3", 1)
|
164
|
+
@client.get("key3").should be == "1"
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should convert the key into a string before storing" do
|
168
|
+
@client.set(123, "foo")
|
169
|
+
@client.keys.should include("123")
|
170
|
+
@client.get("123").should be == "foo"
|
171
|
+
|
172
|
+
@client.setex(456, 30, "foo")
|
173
|
+
@client.keys.should include("456")
|
174
|
+
@client.get("456").should be == "foo"
|
175
|
+
|
176
|
+
@client.getset(789, "foo")
|
177
|
+
@client.keys.should include("789")
|
178
|
+
@client.get("789").should be == "foo"
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should only operate against keys containing string values" do
|
182
|
+
@client.sadd("key1", "one")
|
183
|
+
lambda { @client.get("key1") }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
|
184
|
+
lambda { @client.getset("key1", 1) }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
|
185
|
+
|
186
|
+
@client.hset("key2", "one", "two")
|
187
|
+
lambda { @client.get("key2") }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
|
188
|
+
lambda { @client.getset("key2", 1) }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should move a key from one database to another successfully" do
|
192
|
+
@client.select(0)
|
193
|
+
@client.set("key1", "1")
|
194
|
+
|
195
|
+
@client.move("key1", 1).should be == true
|
196
|
+
|
197
|
+
@client.select(0)
|
198
|
+
@client.get("key1").should be_nil
|
199
|
+
|
200
|
+
@client.select(1)
|
201
|
+
@client.get("key1").should be == "1"
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should fail to move a key that does not exist in the source database" do
|
205
|
+
@client.select(0)
|
206
|
+
@client.get("key1").should be_nil
|
207
|
+
|
208
|
+
@client.move("key1", 1).should be == false
|
209
|
+
|
210
|
+
@client.select(0)
|
211
|
+
@client.get("key1").should be_nil
|
212
|
+
|
213
|
+
@client.select(1)
|
214
|
+
@client.get("key1").should be_nil
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should fail to move a key that exists in the destination database" do
|
218
|
+
@client.select(0)
|
219
|
+
@client.set("key1", "1")
|
220
|
+
|
221
|
+
@client.select(1)
|
222
|
+
@client.set("key1", "2")
|
223
|
+
|
224
|
+
@client.select(0)
|
225
|
+
@client.move("key1", 1).should be == false
|
226
|
+
|
227
|
+
@client.select(0)
|
228
|
+
@client.get("key1").should be == "1"
|
229
|
+
|
230
|
+
@client.select(1)
|
231
|
+
@client.get("key1").should be == "2"
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should fail to move a key to the same database" do
|
235
|
+
@client.select(0)
|
236
|
+
@client.set("key1", "1")
|
237
|
+
|
238
|
+
lambda { @client.move("key1", 0) }.should raise_error(Redis::CommandError, "ERR source and destination objects are the same")
|
239
|
+
|
240
|
+
@client.select(0)
|
241
|
+
@client.get("key1").should be == "1"
|
162
242
|
end
|
163
243
|
end
|
164
244
|
end
|