redis-file 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "redis-file/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "redis-file"
7
+ s.version = RedisFile::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Daniel Loureiro"]
10
+ s.email = ["loureirorg@gmail.com"]
11
+ s.homepage = "https://github.com/loureirorg/redis-file"
12
+ s.license = "MIT"
13
+ s.summary = %q{Fake (local file) driver for redis-rb.}
14
+ s.description = %q{Fake (local file) driver for redis-rb. Useful for development environment and machines without Redis.}
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency(%q<redis>, ["~> 3.0.0"])
22
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ module RedisFile
4
+ describe "Compatibility" do
5
+ it "should be accessible through RedisFile::Redis" do
6
+ lambda { RedisFile::Redis.new }.should_not raise_error
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ module RedisFile
4
+ describe "ConnectionMethods" do
5
+
6
+ before(:each) do
7
+ @client = Redis.new
8
+ end
9
+
10
+ if redisfile?
11
+ it "should authenticate to the server" do
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
55
+ end
56
+ end
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
+
77
+ it "should echo the given string" do
78
+ @client.echo("something").should == "something"
79
+ end
80
+
81
+ it "should ping the server" do
82
+ @client.ping.should == "PONG"
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+
3
+ module RedisFile
4
+ describe "HashesMethods" do
5
+ before(:each) do
6
+ @client = Redis.new
7
+ end
8
+
9
+ it "should delete a hash field" do
10
+ @client.hset("key1", "k1", "val1")
11
+ @client.hset("key1", "k2", "val2")
12
+ @client.hdel("key1", "k1")
13
+
14
+ @client.hget("key1", "k1").should be_nil
15
+ @client.hget("key1", "k2").should be == "val2"
16
+ end
17
+
18
+ it "should remove a hash with no keys left" do
19
+ @client.hset("key1", "k1", "val1")
20
+ @client.hset("key1", "k2", "val2")
21
+ @client.hdel("key1", "k1")
22
+ @client.hdel("key1", "k2")
23
+
24
+ @client.exists("key1").should be == false
25
+ end
26
+
27
+ it "should convert key to a string for hset" do
28
+ m = double("key")
29
+ m.stub(:to_s).and_return("foo")
30
+
31
+ @client.hset("key1", m, "bar")
32
+ @client.hget("key1", "foo").should be == "bar"
33
+ end
34
+
35
+ it "should convert key to a string for hget" do
36
+ m = double("key")
37
+ m.stub(:to_s).and_return("foo")
38
+
39
+ @client.hset("key1", "foo", "bar")
40
+ @client.hget("key1", m).should be == "bar"
41
+ end
42
+
43
+ it "should determine if a hash field exists" do
44
+ @client.hset("key1", "index", "value")
45
+
46
+ @client.hexists("key1", "index").should be_true
47
+ @client.hexists("key2", "i2").should be_false
48
+ end
49
+
50
+ it "should get the value of a hash field" do
51
+ @client.hset("key1", "index", "value")
52
+
53
+ @client.hget("key1", "index").should be == "value"
54
+ end
55
+
56
+ it "should get all the fields and values in a hash" do
57
+ @client.hset("key1", "i1", "val1")
58
+ @client.hset("key1", "i2", "val2")
59
+
60
+ @client.hgetall("key1").should be == {"i1" => "val1", "i2" => "val2"}
61
+ end
62
+
63
+ it "should increment the integer value of a hash field by the given number" do
64
+ @client.hset("key1", "cont1", "5")
65
+ @client.hincrby("key1", "cont1", "5").should be == 10
66
+ @client.hget("key1", "cont1").should be == "10"
67
+ end
68
+
69
+ it "should increment non existing hash keys" do
70
+ @client.hget("key1", "cont2").should be_nil
71
+ @client.hincrby("key1", "cont2", "5").should be == 5
72
+ end
73
+
74
+ it "should get all the fields in a hash" do
75
+ @client.hset("key1", "i1", "val1")
76
+ @client.hset("key1", "i2", "val2")
77
+
78
+ @client.hkeys("key1").should =~ ["i1", "i2"]
79
+ @client.hkeys("key2").should be == []
80
+ end
81
+
82
+ it "should get the number of fields in a hash" do
83
+ @client.hset("key1", "i1", "val1")
84
+ @client.hset("key1", "i2", "val2")
85
+
86
+ @client.hlen("key1").should be == 2
87
+ end
88
+
89
+ it "should get the values of all the given hash fields" do
90
+ @client.hset("key1", "i1", "val1")
91
+ @client.hset("key1", "i2", "val2")
92
+
93
+ @client.hmget("key1", "i1", "i2", "i3").should =~ ["val1", "val2", nil]
94
+ @client.hmget("key2", "i1", "i2").should be == [nil, nil]
95
+ end
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(Redis::CommandError)
99
+ end
100
+
101
+ it "should reject an empty list of values" do
102
+ lambda { @client.hmset("key") }.should raise_error(Redis::CommandError)
103
+ @client.exists("key").should be_false
104
+ end
105
+
106
+ it "rejects an insert with a key but no value" do
107
+ lambda { @client.hmset("key", 'foo') }.should raise_error(Redis::CommandError)
108
+ lambda { @client.hmset("key", 'foo', 3, 'bar') }.should raise_error(Redis::CommandError)
109
+ @client.exists("key").should be_false
110
+ end
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' command")
114
+ end
115
+
116
+ it "should set multiple hash fields to multiple values" do
117
+ @client.hmset("key", "k1", "value1", "k2", "value2")
118
+
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"
128
+ end
129
+
130
+ 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
133
+
134
+ @client.hget("key1", "k1").should be == "val1"
135
+ end
136
+
137
+ it "should set the value of a hash field, only if the field does not exist" do
138
+ @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"
147
+ end
148
+
149
+ it "should get all the values in a hash" do
150
+ @client.hset("key1", "k1", "val1")
151
+ @client.hset("key1", "k2", "val2")
152
+
153
+ @client.hvals("key1").should =~ ["val1", "val2"]
154
+ end
155
+
156
+ it "should accept a list of array pairs as arguments and not throw an invalid argument number error" do
157
+ @client.hmset("key1", [:k1, "val1"], [:k2, "val2"], [:k3, "val3"])
158
+ @client.hget("key1", :k1).should be == "val1"
159
+ @client.hget("key1", :k2).should be == "val2"
160
+ @client.hget("key1", :k3).should be == "val3"
161
+ end
162
+
163
+ it "should reject a list of arrays that contain an invalid number of arguments" do
164
+ expect { @client.hmset("key1", [:k1, "val1"], [:k2, "val2", "bogus val"]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'hmset' command")
165
+ end
166
+
167
+ it "should convert a integer field name to string for hdel" do
168
+ @client.hset("key1", "1", 1)
169
+ @client.hdel("key1", 1).should be(1)
170
+ end
171
+
172
+ it "should convert a integer field name to string for hexists" do
173
+ @client.hset("key1", "1", 1)
174
+ @client.hexists("key1", 1).should be_true
175
+ end
176
+
177
+ it "should convert a integer field name to string for hincrby" do
178
+ @client.hset("key1", 1, 0)
179
+ @client.hincrby("key1", 1, 1).should be(1)
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,248 @@
1
+ require 'spec_helper'
2
+
3
+ module RedisFile
4
+ describe "KeysMethods" do
5
+
6
+ before(:each) do
7
+ @client = Redis.new
8
+ end
9
+
10
+ it "should delete a key" do
11
+ @client.set("key1", "1")
12
+ @client.set("key2", "2")
13
+ @client.del("key1", "key2")
14
+
15
+ @client.get("key1").should be == nil
16
+ end
17
+
18
+ it "should delete multiple keys" do
19
+ @client.set("key1", "1")
20
+ @client.set("key2", "2")
21
+ @client.del(["key1", "key2"])
22
+
23
+ @client.get("key1").should be == nil
24
+ @client.get("key2").should be == nil
25
+ end
26
+
27
+ it "should error deleting no keys" do
28
+ lambda { @client.del }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
29
+ lambda { @client.del [] }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
30
+ end
31
+
32
+ it "should determine if a key exists" do
33
+ @client.set("key1", "1")
34
+
35
+ @client.exists("key1").should be == true
36
+ @client.exists("key2").should be == false
37
+ end
38
+
39
+ it "should set a key's time to live in seconds" do
40
+ @client.set("key1", "1")
41
+ @client.expire("key1", 1)
42
+
43
+ @client.ttl("key1").should be == 1
44
+ end
45
+
46
+ it "should set the expiration for a key as a UNIX timestamp" do
47
+ @client.set("key1", "1")
48
+ @client.expireat("key1", Time.now.to_i + 2)
49
+
50
+ @client.ttl("key1").should be == 2
51
+ end
52
+
53
+ it "should not have an expiration after re-set" do
54
+ @client.set("key1", "1")
55
+ @client.expireat("key1", Time.now.to_i + 2)
56
+ @client.set("key1", "1")
57
+
58
+ @client.ttl("key1").should be == -1
59
+ end
60
+
61
+ it "should not have a ttl if expired" do
62
+ @client.set("key1", "1")
63
+ @client.expireat("key1", Time.now.to_i)
64
+
65
+ @client.ttl("key1").should be == -1
66
+ end
67
+
68
+ it "should not find a key if expired" do
69
+ @client.set("key1", "1")
70
+ @client.expireat("key1", Time.now.to_i)
71
+
72
+ @client.get("key1").should be_nil
73
+ end
74
+
75
+ it "should not find multiple keys if expired" do
76
+ @client.set("key1", "1")
77
+ @client.set("key2", "2")
78
+ @client.expireat("key1", Time.now.to_i)
79
+
80
+ @client.mget("key1", "key2").should be == [nil, "2"]
81
+ end
82
+
83
+ it "should only find keys that aren't expired" do
84
+ @client.set("key1", "1")
85
+ @client.set("key2", "2")
86
+ @client.expireat("key1", Time.now.to_i)
87
+
88
+ @client.keys.should be == ["key2"]
89
+ end
90
+
91
+ it "should not exist if expired" do
92
+ @client.set("key1", "1")
93
+ @client.expireat("key1", Time.now.to_i)
94
+
95
+ @client.exists("key1").should be_false
96
+ end
97
+
98
+ it "should find all keys matching the given pattern" do
99
+ @client.set("key:a", "1")
100
+ @client.set("key:b", "2")
101
+ @client.set("key:c", "3")
102
+ @client.set("akeyd", "4")
103
+ @client.set("key1", "5")
104
+
105
+ @client.keys("key:*").should =~ ["key:a", "key:b", "key:c"]
106
+ end
107
+
108
+ it "should remove the expiration from a key" do
109
+ @client.set("key1", "1")
110
+ @client.expireat("key1", Time.now.to_i + 1)
111
+ @client.persist("key1").should be == true
112
+ @client.persist("key1").should be == false
113
+
114
+ @client.ttl("key1").should be == -1
115
+ end
116
+
117
+ it "should return a random key from the keyspace" do
118
+ @client.set("key1", "1")
119
+ @client.set("key2", "2")
120
+
121
+ ["key1", "key2"].include?(@client.randomkey).should be == true
122
+ end
123
+
124
+ it "should rename a key" do
125
+ @client.set("key1", "2")
126
+ @client.rename("key1", "key2")
127
+
128
+ @client.get("key1").should be == nil
129
+ @client.get("key2").should be == "2"
130
+ end
131
+
132
+ it "should rename a key, only if new key does not exist" do
133
+ @client.set("key1", "1")
134
+ @client.set("key2", "2")
135
+ @client.set("key3", "3")
136
+ @client.renamenx("key1", "key2")
137
+ @client.renamenx("key3", "key4")
138
+
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
+ end
144
+
145
+ it "should sort the elements in a list, set or sorted set" do
146
+ pending "SORT Command not implemented yet"
147
+ end
148
+
149
+ it "should determine the type stored at key" do
150
+ @client.set("key1", "1")
151
+
152
+ @client.type("key1").should be == "string"
153
+ @client.type("key0").should be == "none"
154
+ end
155
+
156
+ it "should convert the value into a string before storing" do
157
+ @client.set("key1", 1)
158
+ @client.get("key1").should be == "1"
159
+
160
+ @client.setex("key2", 30, 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 return 'OK' for the setex command" do
168
+ @client.setex("key4", 30, 1).should be == "OK"
169
+ end
170
+
171
+ it "should convert the key into a string before storing" do
172
+ @client.set(123, "foo")
173
+ @client.keys.should include("123")
174
+ @client.get("123").should be == "foo"
175
+
176
+ @client.setex(456, 30, "foo")
177
+ @client.keys.should include("456")
178
+ @client.get("456").should be == "foo"
179
+
180
+ @client.getset(789, "foo")
181
+ @client.keys.should include("789")
182
+ @client.get("789").should be == "foo"
183
+ end
184
+
185
+ it "should only operate against keys containing string values" do
186
+ @client.sadd("key1", "one")
187
+ lambda { @client.get("key1") }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
188
+ lambda { @client.getset("key1", 1) }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
189
+
190
+ @client.hset("key2", "one", "two")
191
+ lambda { @client.get("key2") }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
192
+ lambda { @client.getset("key2", 1) }.should raise_error(Redis::CommandError, "ERR Operation against a key holding the wrong kind of value")
193
+ end
194
+
195
+ it "should move a key from one database to another successfully" do
196
+ @client.select(0)
197
+ @client.set("key1", "1")
198
+
199
+ @client.move("key1", 1).should be == true
200
+
201
+ @client.select(0)
202
+ @client.get("key1").should be_nil
203
+
204
+ @client.select(1)
205
+ @client.get("key1").should be == "1"
206
+ end
207
+
208
+ it "should fail to move a key that does not exist in the source database" do
209
+ @client.select(0)
210
+ @client.get("key1").should be_nil
211
+
212
+ @client.move("key1", 1).should be == false
213
+
214
+ @client.select(0)
215
+ @client.get("key1").should be_nil
216
+
217
+ @client.select(1)
218
+ @client.get("key1").should be_nil
219
+ end
220
+
221
+ it "should fail to move a key that exists in the destination database" do
222
+ @client.select(0)
223
+ @client.set("key1", "1")
224
+
225
+ @client.select(1)
226
+ @client.set("key1", "2")
227
+
228
+ @client.select(0)
229
+ @client.move("key1", 1).should be == false
230
+
231
+ @client.select(0)
232
+ @client.get("key1").should be == "1"
233
+
234
+ @client.select(1)
235
+ @client.get("key1").should be == "2"
236
+ end
237
+
238
+ it "should fail to move a key to the same database" do
239
+ @client.select(0)
240
+ @client.set("key1", "1")
241
+
242
+ lambda { @client.move("key1", 0) }.should raise_error(Redis::CommandError, "ERR source and destination objects are the same")
243
+
244
+ @client.select(0)
245
+ @client.get("key1").should be == "1"
246
+ end
247
+ end
248
+ end