fakeredis 0.3.3 → 0.4.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.
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  module FakeRedis
4
4
  describe "Compatibility" do
5
- it "should be accessible through FakeRedis::Redis" do
6
- lambda { FakeRedis::Redis.new }.should_not raise_error
5
+ it "should be accessible throught FakeRedis::Redis" do
6
+ lambda { redis = FakeRedis::Redis.new }.should_not raise_error
7
7
  end
8
8
  end
9
9
  end
@@ -7,26 +7,16 @@ module FakeRedis
7
7
  @client = Redis.new
8
8
  end
9
9
 
10
- if fakeredis?
11
10
  it "should authenticate to the server" do
12
- @client.auth("pass").should be == "OK"
11
+ @client.auth("pass").should == "OK"
13
12
  end
14
13
 
15
- it "should not error with shutdown" do
16
- lambda { @client.shutdown }.should_not raise_error
17
- end
18
-
19
- it "should not error with quit" do
20
- lambda { @client.quit }.should_not raise_error
21
- end
22
- end
23
-
24
14
  it "should echo the given string" do
25
- @client.echo("something").should be == "something"
15
+ @client.echo("something").should == "something"
26
16
  end
27
17
 
28
18
  it "should ping the server" do
29
- @client.ping.should be == "PONG"
19
+ @client.ping.should == "PONG"
30
20
  end
31
21
  end
32
22
  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 be == "val2"
15
+ @client.hget("key1", "k2").should == "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 be == false
24
+ @client.exists("key1").should == false
25
25
  end
26
26
 
27
- it "should convert key to a string for hset" do
27
+ it "should convert key to a string via to_s 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 be == "bar"
32
+ @client.hget("key1", "foo").should == "bar"
33
33
  end
34
34
 
35
- it "should convert key to a string for hget" do
35
+ it "should convert key to a string via to_s 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 be == "bar"
40
+ @client.hget("key1", m).should == "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 be == "value"
53
+ @client.hget("key1", "index").should == "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 be == {"i1" => "val1", "i2" => "val2"}
60
+ @client.hgetall("key1").should == {"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 be == 10
66
- @client.hget("key1", "cont1").should be == "10"
65
+ @client.hincrby("key1", "cont1", "5").should == 10
66
+ @client.hget("key1", "cont1").should == "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 be == 5
71
+ @client.hincrby("key1", "cont2", "5").should == 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 be == []
79
+ @client.hkeys("key2").should == []
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 be == 2
86
+ @client.hlen("key1").should == 2
87
87
  end
88
88
 
89
89
  it "should get the values of all the given hash fields" do
@@ -91,59 +91,48 @@ 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 be == [nil, nil]
94
+ @client.hmget("key2", "i1", "i2").should == [nil, nil]
95
95
  end
96
96
 
97
- it "should throw an argument error when you don't ask for any keys" do
98
- lambda { @client.hmget("key1") }.should raise_error(RuntimeError, "ERR wrong number of arguments for 'hmget' command")
97
+ it "throws an argument error when you don't ask for any keys" do
98
+ lambda { @client.hmget("key1") }.should raise_error(ArgumentError)
99
99
  end
100
100
 
101
101
  it "should reject an empty list of values" do
102
- lambda { @client.hmset("key") }.should raise_error(RuntimeError, "ERR wrong number of arguments for 'hmset' command")
102
+ lambda { @client.hmset("key") }.should raise_error(ArgumentError)
103
103
  @client.exists("key").should be_false
104
104
  end
105
105
 
106
- it "rejects an insert with a key but no value" do
107
- lambda { @client.hmset("key", 'foo') }.should raise_error(RuntimeError, "ERR wrong number of arguments for 'hmset' command")
108
- lambda { @client.hmset("key", 'foo', 3, 'bar') }.should raise_error(RuntimeError, "ERR wrong number of arguments for HMSET")
106
+ it 'rejects an insert with a key but no value' do
107
+ lambda { @client.hmset("key", 'foo') }.should raise_error(ArgumentError)
108
+ lambda { @client.hmset("key", 'foo', 3, 'bar') }.should raise_error(ArgumentError)
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(RuntimeError, "ERR wrong number of arguments for HMSET")
114
- end
115
-
116
112
  it "should set multiple hash fields to multiple values" do
117
113
  @client.hmset("key", "k1", "value1", "k2", "value2")
118
114
 
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"
115
+ @client.hget("key", "k1").should == "value1"
116
+ @client.hget("key", "k2").should == "value2"
128
117
  end
129
118
 
130
119
  it "should set the string value of a hash field" do
131
- @client.hset("key1", "k1", "val1").should be == true
132
- @client.hset("key1", "k1", "val1").should be == false
120
+ @client.hset("key1", "k1", "val1").should == true
121
+ @client.hset("key1", "k1", "val1").should == false
133
122
 
134
- @client.hget("key1", "k1").should be == "val1"
123
+ @client.hget("key1", "k1").should == "val1"
135
124
  end
136
125
 
137
126
  it "should set the value of a hash field, only if the field does not exist" do
138
127
  @client.hset("key1", "k1", "val1")
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"
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"
147
136
  end
148
137
 
149
138
  it "should get all the values in a hash" do
data/spec/keys_spec.rb CHANGED
@@ -12,45 +12,45 @@ module FakeRedis
12
12
  @client.set("key2", "2")
13
13
  @client.del("key1", "key2")
14
14
 
15
- @client.get("key1").should be == nil
15
+ @client.get("key1").should == nil
16
16
  end
17
17
 
18
18
  it "should determine if a key exists" do
19
19
  @client.set("key1", "1")
20
20
 
21
- @client.exists("key1").should be == true
22
- @client.exists("key2").should be == false
21
+ @client.exists("key1").should == true
22
+ @client.exists("key2").should == false
23
23
  end
24
24
 
25
25
  it "should set a key's time to live in seconds" do
26
26
  @client.set("key1", "1")
27
27
  @client.expire("key1", 1)
28
28
 
29
- @client.ttl("key1").should be == 1
29
+ @client.ttl("key1").should == 1
30
30
  end
31
31
 
32
32
  it "should set the expiration for a key as a UNIX timestamp" do
33
33
  @client.set("key1", "1")
34
34
  @client.expireat("key1", Time.now.to_i + 2)
35
35
 
36
- @client.ttl("key1").should be == 2
36
+ @client.ttl("key1").should == 2
37
37
  end
38
38
 
39
- it "should not have an expiration after re-set" do
39
+ it 'should not have an expiration after re-set' do
40
40
  @client.set("key1", "1")
41
41
  @client.expireat("key1", Time.now.to_i + 2)
42
42
  @client.set("key1", "1")
43
43
 
44
- @client.ttl("key1").should be == -1
44
+ @client.ttl("key1").should == -1
45
45
  end
46
46
 
47
47
  it "should not have a ttl if expired" do
48
48
  @client.set("key1", "1")
49
49
  @client.expireat("key1", Time.now.to_i)
50
50
 
51
- @client.ttl("key1").should be == -1
51
+ @client.ttl("key1").should == -1
52
52
  end
53
-
53
+
54
54
  it "should not find a key if expired" do
55
55
  @client.set("key1", "1")
56
56
  @client.expireat("key1", Time.now.to_i)
@@ -63,7 +63,7 @@ module FakeRedis
63
63
  @client.set("key2", "2")
64
64
  @client.expireat("key1", Time.now.to_i)
65
65
 
66
- @client.mget("key1", "key2").should be == [nil, "2"]
66
+ @client.mget("key1", "key2").should == [nil, "2"]
67
67
  end
68
68
 
69
69
  it "should only find keys that aren't expired" do
@@ -71,7 +71,7 @@ module FakeRedis
71
71
  @client.set("key2", "2")
72
72
  @client.expireat("key1", Time.now.to_i)
73
73
 
74
- @client.keys.should be == ["key2"]
74
+ @client.keys.should == ["key2"]
75
75
  end
76
76
 
77
77
  it "should not exist if expired" do
@@ -88,31 +88,31 @@ module FakeRedis
88
88
  @client.set("akeyd", "4")
89
89
  @client.set("key1", "5")
90
90
 
91
- @client.keys("key:*").should =~ ["key:a", "key:b", "key:c"]
91
+ @client.keys("key:").should =~ ["key:a", "key:b", "key:c"]
92
92
  end
93
93
 
94
94
  it "should remove the expiration from a key" do
95
95
  @client.set("key1", "1")
96
- @client.expireat("key1", Time.now.to_i + 1)
97
- @client.persist("key1").should be == true
98
- @client.persist("key1").should be == false
96
+ @client.expireat("key1", Time.now.to_i)
97
+ @client.persist("key1").should == true
98
+ @client.persist("key1").should == false
99
99
 
100
- @client.ttl("key1").should be == -1
100
+ @client.ttl("key1").should == -1
101
101
  end
102
102
 
103
103
  it "should return a random key from the keyspace" do
104
104
  @client.set("key1", "1")
105
105
  @client.set("key2", "2")
106
106
 
107
- ["key1", "key2"].include?(@client.randomkey).should be == true
107
+ ["key1", "key2"].include?(@client.randomkey).should == true
108
108
  end
109
109
 
110
110
  it "should rename a key" do
111
111
  @client.set("key1", "2")
112
112
  @client.rename("key1", "key2")
113
113
 
114
- @client.get("key1").should be == nil
115
- @client.get("key2").should be == "2"
114
+ @client.get("key1").should == nil
115
+ @client.get("key2").should == "2"
116
116
  end
117
117
 
118
118
  it "should rename a key, only if new key does not exist" do
@@ -122,10 +122,10 @@ module FakeRedis
122
122
  @client.renamenx("key1", "key2")
123
123
  @client.renamenx("key3", "key4")
124
124
 
125
- @client.get("key1").should be == "1"
126
- @client.get("key2").should be == "2"
127
- @client.get("key3").should be == nil
128
- @client.get("key4").should be == "3"
125
+ @client.get("key1").should == "1"
126
+ @client.get("key2").should == "2"
127
+ @client.get("key3").should == nil
128
+ @client.get("key4").should == "3"
129
129
  end
130
130
 
131
131
  it "should sort the elements in a list, set or sorted set" do
@@ -135,29 +135,16 @@ module FakeRedis
135
135
  it "should determine the type stored at key" do
136
136
  @client.set("key1", "1")
137
137
 
138
- @client.type("key1").should be == "string"
139
- @client.type("key0").should be == "none"
138
+ @client.type("key1").should == "string"
139
+ @client.type("key0").should == "none"
140
140
  end
141
141
 
142
142
  it "should convert the value into a string before storing" do
143
143
  @client.set("key1", 1)
144
- @client.get("key1").should be == "1"
144
+ @client.get("key1").should == "1"
145
145
 
146
146
  @client.setex("key2", 30, 1)
147
- @client.get("key2").should be == "1"
148
-
149
- @client.getset("key3", 1)
150
- @client.get("key3").should be == "1"
151
- end
152
-
153
- it "should only operate against keys containing string values" do
154
- @client.sadd("key1", "one")
155
- lambda { @client.get("key1") }.should raise_error(RuntimeError, "ERR Operation against a key holding the wrong kind of value")
156
- lambda { @client.getset("key1", 1) }.should raise_error(RuntimeError, "ERR Operation against a key holding the wrong kind of value")
157
-
158
- @client.hset("key2", "one", "two")
159
- lambda { @client.get("key2") }.should raise_error(RuntimeError, "ERR Operation against a key holding the wrong kind of value")
160
- lambda { @client.getset("key2", 1) }.should raise_error(RuntimeError, "ERR Operation against a key holding the wrong kind of value")
147
+ @client.get("key2").should == "1"
161
148
  end
162
149
  end
163
150
  end
data/spec/lists_spec.rb CHANGED
@@ -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 be == "val2"
14
- @client.lindex("key1", -1).should be == "val1"
15
- @client.lindex("key1", 3).should be == nil
13
+ @client.lindex("key1", 0).should == "val2"
14
+ @client.lindex("key1", -1).should == "val1"
15
+ @client.lindex("key1", 3).should == nil
16
16
  end
17
17
 
18
18
  it "should insert an element before or after another element in a list" do
@@ -20,27 +20,15 @@ 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 be == ["v1", "v2", "v3"]
24
- end
25
-
26
- it "should not allow multiple values to be added to a list in a single rpush" do
27
- # redis-rb v2.2.2 calls #to_s on the second argument
28
- @client.rpush('key1', [1, 2, 3])
29
- @client.lrange('key1', 0, -1).should be == [[1, 2, 3].to_s]
30
- end
31
-
32
- it "should allow multiple values to be added to a list in a single lpush" do
33
- # redis-rb v2.2.2 calls #to_s on the second argument
34
- @client.lpush('key1', [1, 2, 3])
35
- @client.lrange('key1', 0, -1).should be == [[1, 2, 3].to_s]
23
+ @client.lrange("key1", 0, -1).should == ["v1", "v2", "v3"]
36
24
  end
37
25
 
38
26
  it "should get the length of a list" do
39
27
  @client.rpush("key1", "v1")
40
28
  @client.rpush("key1", "v2")
41
29
 
42
- @client.llen("key1").should be == 2
43
- @client.llen("key2").should be == 0
30
+ @client.llen("key1").should == 2
31
+ @client.llen("key2").should == 0
44
32
  end
45
33
 
46
34
  it "should remove and get the first element in a list" do
@@ -48,15 +36,15 @@ module FakeRedis
48
36
  @client.rpush("key1", "v2")
49
37
  @client.rpush("key1", "v3")
50
38
 
51
- @client.lpop("key1").should be == "v1"
52
- @client.lrange("key1", 0, -1).should be == ["v2", "v3"]
39
+ @client.lpop("key1").should == "v1"
40
+ @client.lrange("key1", 0, -1).should == ["v2", "v3"]
53
41
  end
54
42
 
55
43
  it "should prepend a value to a list" do
56
44
  @client.rpush("key1", "v1")
57
45
  @client.rpush("key1", "v2")
58
46
 
59
- @client.lrange("key1", 0, -1).should be == ["v1", "v2"]
47
+ @client.lrange("key1", 0, -1).should == ["v1", "v2"]
60
48
  end
61
49
 
62
50
  it "should prepend a value to a list, only if the list exists" do
@@ -65,8 +53,8 @@ module FakeRedis
65
53
  @client.lpushx("key1", "v2")
66
54
  @client.lpushx("key2", "v3")
67
55
 
68
- @client.lrange("key1", 0, -1).should be == ["v2", "v1"]
69
- @client.llen("key2").should be == 0
56
+ @client.lrange("key1", 0, -1).should == ["v2", "v1"]
57
+ @client.llen("key2").should == 0
70
58
  end
71
59
 
72
60
  it "should get a range of elements from a list" do
@@ -74,7 +62,7 @@ module FakeRedis
74
62
  @client.rpush("key1", "v2")
75
63
  @client.rpush("key1", "v3")
76
64
 
77
- @client.lrange("key1", 1, -1).should be == ["v2", "v3"]
65
+ @client.lrange("key1", 1, -1).should == ["v2", "v3"]
78
66
  end
79
67
 
80
68
  it "should remove elements from a list" do
@@ -84,9 +72,9 @@ module FakeRedis
84
72
  @client.rpush("key1", "v2")
85
73
  @client.rpush("key1", "v1")
86
74
 
87
- @client.lrem("key1", 1, "v1").should be == 1
88
- @client.lrem("key1", -2, "v2").should be == 2
89
- @client.llen("key1").should be == 2
75
+ @client.lrem("key1", 1, "v1").should == 1
76
+ @client.lrem("key1", -2, "v2").should == 2
77
+ @client.llen("key1").should == 2
90
78
  end
91
79
 
92
80
  it "should remove list's key when list is empty" do
@@ -95,7 +83,7 @@ module FakeRedis
95
83
  @client.lrem("key1", 1, "v1")
96
84
  @client.lrem("key1", 1, "v2")
97
85
 
98
- @client.exists("key1").should be == false
86
+ @client.exists("key1").should == false
99
87
  end
100
88
 
101
89
  it "should set the value of an element in a list by its index" do
@@ -105,9 +93,7 @@ module FakeRedis
105
93
 
106
94
  @client.lset("key1", 0, "four")
107
95
  @client.lset("key1", -2, "five")
108
- @client.lrange("key1", 0, -1).should be == ["four", "five", "three"]
109
-
110
- lambda { @client.lset("key1", 4, "six") }.should raise_error(RuntimeError, "ERR index out of range")
96
+ @client.lrange("key1", 0, -1).should == ["four", "five", "three"]
111
97
  end
112
98
 
113
99
  it "should trim a list to the specified range" do
@@ -116,34 +102,33 @@ module FakeRedis
116
102
  @client.rpush("key1", "three")
117
103
 
118
104
  @client.ltrim("key1", 1, -1)
119
- @client.lrange("key1", 0, -1).should be == ["two", "three"]
105
+ @client.lrange("key1", 0, -1).should == ["two", "three"]
120
106
  end
121
107
 
122
- it "should remove and return the last element in a list" do
108
+ it "should remove and get the last element in a list" do
123
109
  @client.rpush("key1", "one")
124
110
  @client.rpush("key1", "two")
125
111
  @client.rpush("key1", "three")
126
112
 
127
- @client.rpop("key1").should be == "three"
128
- @client.lrange("key1", 0, -1).should be == ["one", "two"]
113
+ @client.rpop("key1").should == "three"
114
+ @client.lrange("key1", 0, -1).should == ["one", "two"]
129
115
  end
130
116
 
131
117
  it "should remove the last element in a list, append it to another list and return it" do
132
118
  @client.rpush("key1", "one")
133
119
  @client.rpush("key1", "two")
134
120
  @client.rpush("key1", "three")
121
+ @client.rpoplpush("key1", "key2")
135
122
 
136
- @client.rpoplpush("key1", "key2").should be == "three"
137
-
138
- @client.lrange("key1", 0, -1).should be == ["one", "two"]
139
- @client.lrange("key2", 0, -1).should be == ["three"]
123
+ @client.lrange("key1", 0, -1).should == ["one", "two"]
124
+ @client.lrange("key2", 0, -1).should == ["three"]
140
125
  end
141
126
 
142
127
  it "should append a value to a list" do
143
128
  @client.rpush("key1", "one")
144
129
  @client.rpush("key1", "two")
145
130
 
146
- @client.lrange("key1", 0, -1).should be == ["one", "two"]
131
+ @client.lrange("key1", 0, -1).should == ["one", "two"]
147
132
  end
148
133
 
149
134
  it "should append a value to a list, only if the list exists" do
@@ -151,8 +136,8 @@ module FakeRedis
151
136
  @client.rpushx("key1", "two")
152
137
  @client.rpushx("key2", "two")
153
138
 
154
- @client.lrange("key1", 0, -1).should be == ["one", "two"]
155
- @client.lrange("key2", 0, -1).should be == []
139
+ @client.lrange("key1", 0, -1).should == ["one", "two"]
140
+ @client.lrange("key2", 0, -1).should == nil
156
141
  end
157
142
  end
158
143
  end