fakeredis 0.3.2 → 0.3.3

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 throught FakeRedis::Redis" do
6
- lambda { redis = FakeRedis::Redis.new }.should_not raise_error
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
@@ -7,16 +7,26 @@ module FakeRedis
7
7
  @client = Redis.new
8
8
  end
9
9
 
10
+ if fakeredis?
10
11
  it "should authenticate to the server" do
11
- @client.auth("pass").should == "OK"
12
+ @client.auth("pass").should be == "OK"
12
13
  end
13
14
 
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
+
14
24
  it "should echo the given string" do
15
- @client.echo("something").should == "something"
25
+ @client.echo("something").should be == "something"
16
26
  end
17
27
 
18
28
  it "should ping the server" do
19
- @client.ping.should == "PONG"
29
+ @client.ping.should be == "PONG"
20
30
  end
21
31
  end
22
32
  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 via to_s for hset" do
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 via to_s for hget" do
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,48 +91,59 @@ 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 "throws an argument error when you don't ask for any keys" do
98
- lambda { @client.hmget("key1") }.should raise_error(ArgumentError)
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")
99
99
  end
100
100
 
101
101
  it "should reject an empty list of values" do
102
- lambda { @client.hmset("key") }.should raise_error(ArgumentError)
102
+ lambda { @client.hmset("key") }.should raise_error(RuntimeError, "ERR wrong number of arguments for 'hmset' command")
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(ArgumentError)
108
- lambda { @client.hmset("key", 'foo', 3, 'bar') }.should raise_error(ArgumentError)
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")
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
+
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,45 +12,45 @@ 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 determine if a key exists" do
19
19
  @client.set("key1", "1")
20
20
 
21
- @client.exists("key1").should == true
22
- @client.exists("key2").should == false
21
+ @client.exists("key1").should be == true
22
+ @client.exists("key2").should be == 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 == 1
29
+ @client.ttl("key1").should be == 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 == 2
36
+ @client.ttl("key1").should be == 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 == -1
44
+ @client.ttl("key1").should be == -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 == -1
51
+ @client.ttl("key1").should be == -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 == [nil, "2"]
66
+ @client.mget("key1", "key2").should be == [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 == ["key2"]
74
+ @client.keys.should be == ["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)
97
- @client.persist("key1").should == true
98
- @client.persist("key1").should == false
96
+ @client.expireat("key1", Time.now.to_i + 1)
97
+ @client.persist("key1").should be == true
98
+ @client.persist("key1").should be == false
99
99
 
100
- @client.ttl("key1").should == -1
100
+ @client.ttl("key1").should be == -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 == true
107
+ ["key1", "key2"].include?(@client.randomkey).should be == 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 == nil
115
- @client.get("key2").should == "2"
114
+ @client.get("key1").should be == nil
115
+ @client.get("key2").should be == "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 == "1"
126
- @client.get("key2").should == "2"
127
- @client.get("key3").should == nil
128
- @client.get("key4").should == "3"
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"
129
129
  end
130
130
 
131
131
  it "should sort the elements in a list, set or sorted set" do
@@ -135,16 +135,29 @@ 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 == "string"
139
- @client.type("key0").should == "none"
138
+ @client.type("key1").should be == "string"
139
+ @client.type("key0").should be == "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 == "1"
144
+ @client.get("key1").should be == "1"
145
145
 
146
146
  @client.setex("key2", 30, 1)
147
- @client.get("key2").should == "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")
148
161
  end
149
162
  end
150
163
  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 == "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,15 +20,27 @@ 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
+ 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]
24
36
  end
25
37
 
26
38
  it "should get the length of a list" do
27
39
  @client.rpush("key1", "v1")
28
40
  @client.rpush("key1", "v2")
29
41
 
30
- @client.llen("key1").should == 2
31
- @client.llen("key2").should == 0
42
+ @client.llen("key1").should be == 2
43
+ @client.llen("key2").should be == 0
32
44
  end
33
45
 
34
46
  it "should remove and get the first element in a list" do
@@ -36,15 +48,15 @@ module FakeRedis
36
48
  @client.rpush("key1", "v2")
37
49
  @client.rpush("key1", "v3")
38
50
 
39
- @client.lpop("key1").should == "v1"
40
- @client.lrange("key1", 0, -1).should == ["v2", "v3"]
51
+ @client.lpop("key1").should be == "v1"
52
+ @client.lrange("key1", 0, -1).should be == ["v2", "v3"]
41
53
  end
42
54
 
43
55
  it "should prepend a value to a list" do
44
56
  @client.rpush("key1", "v1")
45
57
  @client.rpush("key1", "v2")
46
58
 
47
- @client.lrange("key1", 0, -1).should == ["v1", "v2"]
59
+ @client.lrange("key1", 0, -1).should be == ["v1", "v2"]
48
60
  end
49
61
 
50
62
  it "should prepend a value to a list, only if the list exists" do
@@ -53,8 +65,8 @@ module FakeRedis
53
65
  @client.lpushx("key1", "v2")
54
66
  @client.lpushx("key2", "v3")
55
67
 
56
- @client.lrange("key1", 0, -1).should == ["v2", "v1"]
57
- @client.llen("key2").should == 0
68
+ @client.lrange("key1", 0, -1).should be == ["v2", "v1"]
69
+ @client.llen("key2").should be == 0
58
70
  end
59
71
 
60
72
  it "should get a range of elements from a list" do
@@ -62,7 +74,7 @@ module FakeRedis
62
74
  @client.rpush("key1", "v2")
63
75
  @client.rpush("key1", "v3")
64
76
 
65
- @client.lrange("key1", 1, -1).should == ["v2", "v3"]
77
+ @client.lrange("key1", 1, -1).should be == ["v2", "v3"]
66
78
  end
67
79
 
68
80
  it "should remove elements from a list" do
@@ -72,9 +84,9 @@ module FakeRedis
72
84
  @client.rpush("key1", "v2")
73
85
  @client.rpush("key1", "v1")
74
86
 
75
- @client.lrem("key1", 1, "v1").should == 1
76
- @client.lrem("key1", -2, "v2").should == 2
77
- @client.llen("key1").should == 2
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
78
90
  end
79
91
 
80
92
  it "should remove list's key when list is empty" do
@@ -83,7 +95,7 @@ module FakeRedis
83
95
  @client.lrem("key1", 1, "v1")
84
96
  @client.lrem("key1", 1, "v2")
85
97
 
86
- @client.exists("key1").should == false
98
+ @client.exists("key1").should be == false
87
99
  end
88
100
 
89
101
  it "should set the value of an element in a list by its index" do
@@ -93,7 +105,9 @@ module FakeRedis
93
105
 
94
106
  @client.lset("key1", 0, "four")
95
107
  @client.lset("key1", -2, "five")
96
- @client.lrange("key1", 0, -1).should == ["four", "five", "three"]
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")
97
111
  end
98
112
 
99
113
  it "should trim a list to the specified range" do
@@ -102,33 +116,34 @@ module FakeRedis
102
116
  @client.rpush("key1", "three")
103
117
 
104
118
  @client.ltrim("key1", 1, -1)
105
- @client.lrange("key1", 0, -1).should == ["two", "three"]
119
+ @client.lrange("key1", 0, -1).should be == ["two", "three"]
106
120
  end
107
121
 
108
- it "should remove and get the last element in a list" do
122
+ it "should remove and return the last element in a list" do
109
123
  @client.rpush("key1", "one")
110
124
  @client.rpush("key1", "two")
111
125
  @client.rpush("key1", "three")
112
126
 
113
- @client.rpop("key1").should == "three"
114
- @client.lrange("key1", 0, -1).should == ["one", "two"]
127
+ @client.rpop("key1").should be == "three"
128
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
115
129
  end
116
130
 
117
131
  it "should remove the last element in a list, append it to another list and return it" do
118
132
  @client.rpush("key1", "one")
119
133
  @client.rpush("key1", "two")
120
134
  @client.rpush("key1", "three")
121
- @client.rpoplpush("key1", "key2")
122
135
 
123
- @client.lrange("key1", 0, -1).should == ["one", "two"]
124
- @client.lrange("key2", 0, -1).should == ["three"]
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"]
125
140
  end
126
141
 
127
142
  it "should append a value to a list" do
128
143
  @client.rpush("key1", "one")
129
144
  @client.rpush("key1", "two")
130
145
 
131
- @client.lrange("key1", 0, -1).should == ["one", "two"]
146
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
132
147
  end
133
148
 
134
149
  it "should append a value to a list, only if the list exists" do
@@ -136,8 +151,8 @@ module FakeRedis
136
151
  @client.rpushx("key1", "two")
137
152
  @client.rpushx("key2", "two")
138
153
 
139
- @client.lrange("key1", 0, -1).should == ["one", "two"]
140
- @client.lrange("key2", 0, -1).should == nil
154
+ @client.lrange("key1", 0, -1).should be == ["one", "two"]
155
+ @client.lrange("key2", 0, -1).should be == []
141
156
  end
142
157
  end
143
158
  end