]> git.saurik.com Git - redis.git/blame - client-libraries/ruby/spec/redis_spec.rb
Lua client updated
[redis.git] / client-libraries / ruby / spec / redis_spec.rb
CommitLineData
ed9b544e 1require File.dirname(__FILE__) + '/spec_helper'
2
3class Foo
4 attr_accessor :bar
5 def initialize(bar)
6 @bar = bar
7 end
8
9 def ==(other)
10 @bar == other.bar
11 end
12end
13
14describe "redis" do
29fac617 15 before(:each) do
ed9b544e 16 @r = Redis.new
17 @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
18 @r['foo'] = 'bar'
19 end
20
21 after do
22 @r.keys('*').each {|k| @r.delete k }
29fac617 23 @r.quit
ed9b544e 24 end
25
ed9b544e 26
27 it "should be able to GET a key" do
28 @r['foo'].should == 'bar'
29 end
30
31 it "should be able to SET a key" do
32 @r['foo'] = 'nik'
33 @r['foo'].should == 'nik'
34 end
35
36 it "should be able to SETNX(set_unless_exists)" do
37 @r['foo'] = 'nik'
38 @r['foo'].should == 'nik'
39 @r.set_unless_exists 'foo', 'bar'
40 @r['foo'].should == 'nik'
41 end
29fac617 42 #
ed9b544e 43 it "should be able to INCR(increment) a key" do
44 @r.delete('counter')
45 @r.incr('counter').should == 1
46 @r.incr('counter').should == 2
47 @r.incr('counter').should == 3
48 end
29fac617 49 #
ed9b544e 50 it "should be able to DECR(decrement) a key" do
51 @r.delete('counter')
52 @r.incr('counter').should == 1
53 @r.incr('counter').should == 2
54 @r.incr('counter').should == 3
55 @r.decr('counter').should == 2
56 @r.decr('counter').should == 1
57 @r.decr('counter').should == 0
58 end
29fac617 59 #
ed9b544e 60 it "should be able to RANDKEY(return a random key)" do
61 @r.randkey.should_not be_nil
62 end
29fac617 63 #
ed9b544e 64 it "should be able to RENAME a key" do
65 @r.delete 'foo'
66 @r.delete 'bar'
67 @r['foo'] = 'hi'
68 @r.rename! 'foo', 'bar'
69 @r['bar'].should == 'hi'
70 end
29fac617 71 #
ed9b544e 72 it "should be able to RENAMENX(rename unless the new key already exists) a key" do
73 @r.delete 'foo'
74 @r.delete 'bar'
75 @r['foo'] = 'hi'
76 @r['bar'] = 'ohai'
29fac617 77 lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisRenameError)
ed9b544e 78 @r['bar'].should == 'ohai'
79 end
29fac617 80 #
ed9b544e 81 it "should be able to EXISTS(check if key exists)" do
82 @r['foo'] = 'nik'
83 @r.key?('foo').should be_true
84 @r.delete 'foo'
85 @r.key?('foo').should be_false
86 end
29fac617 87 #
ed9b544e 88 it "should be able to KEYS(glob for keys)" do
89 @r.keys("f*").each do |key|
90 @r.delete key
91 end
92 @r['f'] = 'nik'
93 @r['fo'] = 'nak'
94 @r['foo'] = 'qux'
95 @r.keys("f*").sort.should == ['f','fo', 'foo'].sort
96 end
29fac617 97 #
ed9b544e 98 it "should be able to check the TYPE of a key" do
99 @r['foo'] = 'nik'
100 @r.type?('foo').should == "string"
101 @r.delete 'foo'
102 @r.type?('foo').should == "none"
103 end
29fac617 104 #
ed9b544e 105 it "should be able to push to the head of a list" do
106 @r.push_head "list", 'hello'
107 @r.push_head "list", 42
108 @r.type?('list').should == "list"
109 @r.list_length('list').should == 2
110 @r.pop_head('list').should == '42'
111 @r.delete('list')
112 end
29fac617 113 #
ed9b544e 114 it "should be able to push to the tail of a list" do
115 @r.push_tail "list", 'hello'
116 @r.type?('list').should == "list"
117 @r.list_length('list').should == 1
118 @r.delete('list')
119 end
29fac617 120 #
ed9b544e 121 it "should be able to pop the tail of a list" do
122 @r.push_tail "list", 'hello'
123 @r.push_tail "list", 'goodbye'
124 @r.type?('list').should == "list"
125 @r.list_length('list').should == 2
126 @r.pop_tail('list').should == 'goodbye'
127 @r.delete('list')
128 end
29fac617 129 #
ed9b544e 130 it "should be able to pop the head of a list" do
131 @r.push_tail "list", 'hello'
132 @r.push_tail "list", 'goodbye'
133 @r.type?('list').should == "list"
134 @r.list_length('list').should == 2
135 @r.pop_head('list').should == 'hello'
136 @r.delete('list')
137 end
29fac617 138 #
ed9b544e 139 it "should be able to get the length of a list" do
140 @r.push_tail "list", 'hello'
141 @r.push_tail "list", 'goodbye'
142 @r.type?('list').should == "list"
143 @r.list_length('list').should == 2
144 @r.delete('list')
145 end
29fac617 146 #
ed9b544e 147 it "should be able to get a range of values from a list" do
148 @r.push_tail "list", 'hello'
149 @r.push_tail "list", 'goodbye'
150 @r.push_tail "list", '1'
151 @r.push_tail "list", '2'
152 @r.push_tail "list", '3'
153 @r.type?('list').should == "list"
154 @r.list_length('list').should == 5
155 @r.list_range('list', 2, -1).should == ['1', '2', '3']
156 @r.delete('list')
157 end
29fac617 158 #
ed9b544e 159 it "should be able to trim a list" do
160 @r.push_tail "list", 'hello'
161 @r.push_tail "list", 'goodbye'
162 @r.push_tail "list", '1'
163 @r.push_tail "list", '2'
164 @r.push_tail "list", '3'
165 @r.type?('list').should == "list"
166 @r.list_length('list').should == 5
167 @r.list_trim 'list', 0, 1
168 @r.list_length('list').should == 2
169 @r.list_range('list', 0, -1).should == ['hello', 'goodbye']
170 @r.delete('list')
171 end
29fac617 172 #
ed9b544e 173 it "should be able to get a value by indexing into a list" do
174 @r.push_tail "list", 'hello'
175 @r.push_tail "list", 'goodbye'
176 @r.type?('list').should == "list"
177 @r.list_length('list').should == 2
178 @r.list_index('list', 1).should == 'goodbye'
179 @r.delete('list')
180 end
29fac617 181 #
ed9b544e 182 it "should be able to set a value by indexing into a list" do
183 @r.push_tail "list", 'hello'
184 @r.push_tail "list", 'hello'
185 @r.type?('list').should == "list"
186 @r.list_length('list').should == 2
187 @r.list_set('list', 1, 'goodbye').should be_true
188 @r.list_index('list', 1).should == 'goodbye'
189 @r.delete('list')
190 end
29fac617 191 #
192 it "should be able to remove values from a list LREM" do
193 @r.push_tail "list", 'hello'
194 @r.push_tail "list", 'goodbye'
195 @r.type?('list').should == "list"
196 @r.list_length('list').should == 2
197 @r.list_rm('list', 1, 'hello').should == 1
198 @r.list_range('list', 0, -1).should == ['goodbye']
199 @r.delete('list')
200 end
201 #
ed9b544e 202 it "should be able add members to a set" do
203 @r.set_add "set", 'key1'
204 @r.set_add "set", 'key2'
205 @r.type?('set').should == "set"
206 @r.set_count('set').should == 2
207 @r.set_members('set').sort.should == ['key1', 'key2'].sort
208 @r.delete('set')
209 end
29fac617 210 #
ed9b544e 211 it "should be able delete members to a set" do
212 @r.set_add "set", 'key1'
213 @r.set_add "set", 'key2'
214 @r.type?('set').should == "set"
215 @r.set_count('set').should == 2
216 @r.set_members('set').should == Set.new(['key1', 'key2'])
217 @r.set_delete('set', 'key1')
218 @r.set_count('set').should == 1
219 @r.set_members('set').should == Set.new(['key2'])
220 @r.delete('set')
221 end
29fac617 222 #
ed9b544e 223 it "should be able count the members of a set" do
224 @r.set_add "set", 'key1'
225 @r.set_add "set", 'key2'
226 @r.type?('set').should == "set"
227 @r.set_count('set').should == 2
228 @r.delete('set')
229 end
29fac617 230 #
ed9b544e 231 it "should be able test for set membership" do
232 @r.set_add "set", 'key1'
233 @r.set_add "set", 'key2'
234 @r.type?('set').should == "set"
235 @r.set_count('set').should == 2
236 @r.set_member?('set', 'key1').should be_true
237 @r.set_member?('set', 'key2').should be_true
238 @r.set_member?('set', 'notthere').should be_false
239 @r.delete('set')
240 end
29fac617 241 #
ed9b544e 242 it "should be able to do set intersection" do
243 @r.set_add "set", 'key1'
244 @r.set_add "set", 'key2'
245 @r.set_add "set2", 'key2'
246 @r.set_intersect('set', 'set2').should == Set.new(['key2'])
247 @r.delete('set')
248 end
29fac617 249 #
ed9b544e 250 it "should be able to do set intersection and store the results in a key" do
251 @r.set_add "set", 'key1'
252 @r.set_add "set", 'key2'
253 @r.set_add "set2", 'key2'
254 @r.set_inter_store('newone', 'set', 'set2')
255 @r.set_members('newone').should == Set.new(['key2'])
256 @r.delete('set')
257 end
29fac617 258 #
ed9b544e 259 it "should be able to do crazy SORT queries" do
260 @r['dog_1'] = 'louie'
261 @r.push_tail 'dogs', 1
262 @r['dog_2'] = 'lucy'
263 @r.push_tail 'dogs', 2
264 @r['dog_3'] = 'max'
265 @r.push_tail 'dogs', 3
266 @r['dog_4'] = 'taj'
267 @r.push_tail 'dogs', 4
268 @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie']
269 @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj']
270 end
29fac617 271 #
272 it "should provide info" do
273 [:last_save_time, :redis_version, :total_connections_received, :connected_clients, :total_commands_processed, :connected_slaves, :uptime_in_seconds, :used_memory, :uptime_in_days, :changes_since_last_save].each do |x|
274 @r.info.keys.should include(x)
275 end
276 end
277 #
278 it "should be able to flush the database" do
279 @r['key1'] = 'keyone'
280 @r['key2'] = 'keytwo'
281 @r.keys('*').sort.should == ['foo', 'key1', 'key2'] #foo from before
282 @r.flush_db
283 @r.keys('*').should == []
284 end
285 #
286 it "should be able to provide the last save time" do
287 savetime = @r.last_save
288 Time.at(savetime).class.should == Time
289 Time.at(savetime).should <= Time.now
290 end
291
292 it "should be able to MGET keys" do
293 @r['foo'] = 1000
294 @r['bar'] = 2000
295 @r.mget('foo', 'bar').should == ['1000', '2000']
296 @r.mget('foo', 'bar', 'baz').should == ['1000', '2000', nil]
297 end
298
299 it "should bgsave" do
300 lambda {@r.bgsave}.should_not raise_error(RedisError)
301 end
302
303 it "should handle multiple servers" do
304 require 'dist_redis'
305 @r = DistRedis.new('localhost:6379', '127.0.0.1:6379')
306 @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
307
308 100.times do |idx|
309 @r[idx] = "foo#{idx}"
310 end
311
312 100.times do |idx|
313 @r[idx].should == "foo#{idx}"
314 end
315 end
316
ed9b544e 317end