]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/spec/redis_spec.rb
1 require File
.dirname(__FILE__
) +
'/spec_helper'
16 # use database 15 for testing so we dont accidentally step on you real data
17 @r = Redis
.new
:db => 15
25 @r.keys('*').each
{|k
| @r.del k
}
32 it
'should be able to PING' do
33 @r.ping
.should
== 'PONG'
36 it
"should be able to GET a key" do
37 @r['foo'].should
== 'bar'
40 it
"should be able to SET a key" do
42 @r['foo'].should
== 'nik'
45 it
"should properly handle trailing newline characters" do
47 @r['foo'].should
== "bar\n"
50 it
"should store and retrieve all possible characters at the beginning and the end of a string" do
51 (0..255).each
do |char_idx
|
52 string
= "#{char_idx.chr}---#{char_idx.chr}"
54 @r['foo'].should
== string
58 it
"should be able to SET a key with an expiry" do
59 @r.set('foo', 'bar', 1)
60 @r['foo'].should
== 'bar'
62 @r['foo'].should
== nil
65 it
"should be able to SETNX" do
67 @r['foo'].should
== 'nik'
69 @r['foo'].should
== 'nik'
72 it
"should be able to INCR a key" do
74 @r.incr('counter').should
== 1
75 @r.incr('counter').should
== 2
76 @r.incr('counter').should
== 3
79 it
"should be able to DECR a key" do
81 @r.incr('counter').should
== 1
82 @r.incr('counter').should
== 2
83 @r.incr('counter').should
== 3
84 @r.decr('counter').should
== 2
85 @r.decr('counter', 2).should
== 0
88 it
"should be able to RANDKEY" do
89 @r.randkey
.should_not be_nil
92 it
"should be able to RENAME a key" do
96 @r.rename
'foo', 'bar'
97 @r['bar'].should
== 'hi'
100 it
"should be able to RENAMENX a key" do
105 @r.renamenx
'foo', 'bar'
106 @r['bar'].should
== 'ohai'
109 it
"should be able to get DBSIZE of the database" do
111 dbsize_without_foo
= @r.dbsize
113 dbsize_with_foo
= @r.dbsize
115 dbsize_with_foo
.should
== dbsize_without_foo +
1
118 it
"should be able to EXPIRE a key" do
121 @r['foo'].should
== "bar"
123 @r['foo'].should
== nil
126 it
"should be able to EXISTS" do
128 @r.exists('foo').should be_true
130 @r.exists('foo').should be_false
133 it
"should be able to KEYS" do
134 @r.keys("f*").each
{ |key
| @r.del key
}
138 @r.keys("f*").sort
.should
== ['f','fo', 'foo'].sort
141 it
"should be able to check the TYPE of a key" do
143 @r.type('foo').should
== "string"
145 @r.type('foo').should
== "none"
148 it
"should be able to push to the head of a list (LPUSH)" do
149 @r.lpush
"list", 'hello'
151 @r.type('list').should
== "list"
152 @r.llen('list').should
== 2
153 @r.lpop('list').should
== '42'
156 it
"should be able to push to the tail of a list (RPUSH)" do
157 @r.rpush
"list", 'hello'
158 @r.type('list').should
== "list"
159 @r.llen('list').should
== 1
162 it
"should be able to pop the tail of a list (RPOP)" do
163 @r.rpush
"list", 'hello'
164 @r.rpush
"list", 'goodbye'
165 @r.type('list').should
== "list"
166 @r.llen('list').should
== 2
167 @r.rpop('list').should
== 'goodbye'
170 it
"should be able to pop the head of a list (LPOP)" do
171 @r.rpush
"list", 'hello'
172 @r.rpush
"list", 'goodbye'
173 @r.type('list').should
== "list"
174 @r.llen('list').should
== 2
175 @r.lpop('list').should
== 'hello'
178 it
"should be able to get the length of a list (LLEN)" do
179 @r.rpush
"list", 'hello'
180 @r.rpush
"list", 'goodbye'
181 @r.type('list').should
== "list"
182 @r.llen('list').should
== 2
185 it
"should be able to get a range of values from a list (LRANGE)" do
186 @r.rpush
"list", 'hello'
187 @r.rpush
"list", 'goodbye'
191 @r.type('list').should
== "list"
192 @r.llen('list').should
== 5
193 @r.lrange('list', 2, -1).should
== ['1', '2', '3']
196 it
"should be able to trim a list (LTRIM)" do
197 @r.rpush
"list", 'hello'
198 @r.rpush
"list", 'goodbye'
202 @r.type('list').should
== "list"
203 @r.llen('list').should
== 5
204 @r.ltrim
'list', 0, 1
205 @r.llen('list').should
== 2
206 @r.lrange('list', 0, -1).should
== ['hello', 'goodbye']
209 it
"should be able to get a value by indexing into a list (LINDEX)" do
210 @r.rpush
"list", 'hello'
211 @r.rpush
"list", 'goodbye'
212 @r.type('list').should
== "list"
213 @r.llen('list').should
== 2
214 @r.lindex('list', 1).should
== 'goodbye'
217 it
"should be able to set a value by indexing into a list (LSET)" do
218 @r.rpush
"list", 'hello'
219 @r.rpush
"list", 'hello'
220 @r.type('list').should
== "list"
221 @r.llen('list').should
== 2
222 @r.lset('list', 1, 'goodbye').should
== 'OK'
223 @r.lindex('list', 1).should
== 'goodbye'
226 it
"should be able to remove values from a list (LREM)" do
227 @r.rpush
"list", 'hello'
228 @r.rpush
"list", 'goodbye'
229 @r.type('list').should
== "list"
230 @r.llen('list').should
== 2
231 @r.lrem('list', 1, 'hello').should
== 1
232 @r.lrange('list', 0, -1).should
== ['goodbye']
235 it
"should be able add members to a set (SADD)" do
236 @r.sadd
"set", 'key1'
237 @r.sadd
"set", 'key2'
238 @r.type('set').should
== "set"
239 @r.scard('set').should
== 2
240 @r.smembers('set').sort
.should
== ['key1', 'key2'].sort
243 it
"should be able delete members to a set (SREM)" do
244 @r.sadd
"set", 'key1'
245 @r.sadd
"set", 'key2'
246 @r.type('set').should
== "set"
247 @r.scard('set').should
== 2
248 @r.smembers('set').sort
.should
== ['key1', 'key2'].sort
249 @r.srem('set', 'key1')
250 @r.scard('set').should
== 1
251 @r.smembers('set').should
== ['key2']
254 it
"should be able count the members of a set (SCARD)" do
255 @r.sadd
"set", 'key1'
256 @r.sadd
"set", 'key2'
257 @r.type('set').should
== "set"
258 @r.scard('set').should
== 2
261 it
"should be able test for set membership (SISMEMBER)" do
262 @r.sadd
"set", 'key1'
263 @r.sadd
"set", 'key2'
264 @r.type('set').should
== "set"
265 @r.scard('set').should
== 2
266 @r.sismember('set', 'key1').should be_true
267 @r.sismember('set', 'key2').should be_true
268 @r.sismember('set', 'notthere').should be_false
271 it
"should be able to do set intersection (SINTER)" do
272 @r.sadd
"set", 'key1'
273 @r.sadd
"set", 'key2'
274 @r.sadd
"set2", 'key2'
275 @r.sinter('set', 'set2').should
== ['key2']
278 it
"should be able to do set intersection and store the results in a key (SINTERSTORE)" do
279 @r.sadd
"set", 'key1'
280 @r.sadd
"set", 'key2'
281 @r.sadd
"set2", 'key2'
282 @r.sinterstore('newone', 'set', 'set2').should
== 1
283 @r.smembers('newone').should
== ['key2']
286 it
"should be able to do set union (SUNION)" do
287 @r.sadd
"set", 'key1'
288 @r.sadd
"set", 'key2'
289 @r.sadd
"set2", 'key2'
290 @r.sadd
"set2", 'key3'
291 @r.sunion('set', 'set2').sort
.should
== ['key1','key2','key3'].sort
294 it
"should be able to do set union and store the results in a key (SUNIONSTORE)" do
295 @r.sadd
"set", 'key1'
296 @r.sadd
"set", 'key2'
297 @r.sadd
"set2", 'key2'
298 @r.sadd
"set2", 'key3'
299 @r.sunionstore('newone', 'set', 'set2').should
== 3
300 @r.smembers('newone').sort
.should
== ['key1','key2','key3'].sort
303 it
"should be able to do set difference (SDIFF)" do
308 @r.sdiff('set', 'set2').should
== ['a']
311 it
"should be able to do set difference and store the results in a key (SDIFFSTORE)" do
316 @r.sdiffstore('newone', 'set', 'set2')
317 @r.smembers('newone').should
== ['a']
320 it
"should be able move elements from one set to another (SMOVE)" do
324 @r.smove('set1', 'set2', 'a').should be_true
325 @r.sismember('set2', 'a').should be_true
329 it
"should be able to do crazy SORT queries" do
330 @r['dog_1'] = 'louie'
338 @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should
== ['louie']
339 @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should
== ['taj']
342 it
"should be able to handle array of :get using SORT" do
343 @r['dog:1:name'] = 'louie'
344 @r['dog:1:breed'] = 'mutt'
346 @r['dog:2:name'] = 'lucy'
347 @r['dog:2:breed'] = 'poodle'
349 @r['dog:3:name'] = 'max'
350 @r['dog:3:breed'] = 'hound'
352 @r['dog:4:name'] = 'taj'
353 @r['dog:4:breed'] = 'terrier'
355 @r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1]).should
== ['louie', 'mutt']
356 @r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1], :order => 'desc alpha').should
== ['taj', 'terrier']
359 it
"should provide info" do
360 [: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
|
361 @r.info
.keys
.should
include(x
)
365 it
"should be able to flush the database" do
366 @r['key1'] = 'keyone'
367 @r['key2'] = 'keytwo'
368 @r.keys('*').sort
.should
== ['foo', 'key1', 'key2'].sort
#foo from before
370 @r.keys('*').should
== []
373 it
"should be able to provide the last save time (LASTSAVE)" do
374 savetime
= @r.lastsave
375 Time
.at(savetime
).class.should
== Time
376 Time
.at(savetime
).should
<= Time
.now
379 it
"should be able to MGET keys" do
382 @r.mget('foo', 'bar').should
== ['1000', '2000']
383 @r.mget('foo', 'bar', 'baz').should
== ['1000', '2000', nil]
386 it
"should bgsave" do
387 @r.bgsave
.should
== 'OK'
390 it
"should handle multiple servers" do
392 @r = DistRedis
.new(:hosts=> ['localhost:6379', '127.0.0.1:6379'], :db => 15)
395 @r[idx
] = "foo#{idx}"
399 @r[idx
].should
== "foo#{idx}"
403 it
"should be able to pipeline writes" do
404 @r.pipelined
do |pipeline
|
405 pipeline
.lpush
'list', "hello"
406 pipeline
.lpush
'list', 42
409 @r.type('list').should
== "list"
410 @r.llen('list').should
== 2
411 @r.lpop('list').should
== '42'