]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/spec/redis_spec.rb
1 require File
.dirname(__FILE__
) +
'/spec_helper'
17 @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
25 @r.keys('*').each
{|k
| @r.delete k
}
33 it
"should be able to GET a key" do
34 @r['foo'].should
== 'bar'
37 it
"should be able to SET a key" do
39 @r['foo'].should
== 'nik'
42 it
"should properly handle trailing newline characters" do
44 @r['foo'].should
== "bar\n"
47 it
"should store and retrieve all possible characters at the beginning and the end of a string" do
48 (0..255).each
do |char_idx
|
49 string
= "#{char_idx.chr}---#{char_idx.chr}"
51 @r['foo'].should
== string
55 it
"should be able to SET a key with an expiry" do
56 @r.set('foo', 'bar', 1)
57 @r['foo'].should
== 'bar'
59 @r['foo'].should
== nil
62 it
"should be able to SETNX(set_unless_exists)" do
64 @r['foo'].should
== 'nik'
65 @r.set_unless_exists
'foo', 'bar'
66 @r['foo'].should
== 'nik'
69 it
"should be able to INCR(increment) a key" do
71 @r.incr('counter').should
== 1
72 @r.incr('counter').should
== 2
73 @r.incr('counter').should
== 3
76 it
"should be able to DECR(decrement) a key" do
78 @r.incr('counter').should
== 1
79 @r.incr('counter').should
== 2
80 @r.incr('counter').should
== 3
81 @r.decr('counter').should
== 2
82 @r.decr('counter', 2).should
== 0
85 it
"should be able to RANDKEY(return a random key)" do
86 @r.randkey
.should_not be_nil
89 it
"should be able to RENAME a key" do
93 @r.rename!
'foo', 'bar'
94 @r['bar'].should
== 'hi'
97 it
"should be able to RENAMENX(rename unless the new key already exists) a key" do
102 lambda
{@r.rename
'foo', 'bar'}.should
raise_error(RedisRenameError
)
103 @r['bar'].should
== 'ohai'
106 it
"should be able to EXPIRE a key" do
109 @r['foo'].should
== "bar"
111 @r['foo'].should
== nil
114 it
"should be able to EXISTS(check if key exists)" do
116 @r.key
?('foo').should be_true
118 @r.key
?('foo').should be_false
121 it
"should be able to KEYS(glob for keys)" do
122 @r.keys("f*").each
do |key
|
128 @r.keys("f*").sort
.should
== ['f','fo', 'foo'].sort
131 it
"should be able to check the TYPE of a key" do
133 @r.type
?('foo').should
== "string"
135 @r.type
?('foo').should
== "none"
138 it
"should be able to push to the head of a list" do
139 @r.push_head
"list", 'hello'
140 @r.push_head
"list", 42
141 @r.type
?('list').should
== "list"
142 @r.list_length('list').should
== 2
143 @r.pop_head('list').should
== '42'
147 it
"should be able to push to the tail of a list" do
148 @r.push_tail
"list", 'hello'
149 @r.type
?('list').should
== "list"
150 @r.list_length('list').should
== 1
154 it
"should be able to pop the tail of a list" do
155 @r.push_tail
"list", 'hello'
156 @r.push_tail
"list", 'goodbye'
157 @r.type
?('list').should
== "list"
158 @r.list_length('list').should
== 2
159 @r.pop_tail('list').should
== 'goodbye'
163 it
"should be able to pop the head of a list" do
164 @r.push_tail
"list", 'hello'
165 @r.push_tail
"list", 'goodbye'
166 @r.type
?('list').should
== "list"
167 @r.list_length('list').should
== 2
168 @r.pop_head('list').should
== 'hello'
172 it
"should be able to get the length of a list" do
173 @r.push_tail
"list", 'hello'
174 @r.push_tail
"list", 'goodbye'
175 @r.type
?('list').should
== "list"
176 @r.list_length('list').should
== 2
180 it
"should be able to get a range of values from a list" do
181 @r.push_tail
"list", 'hello'
182 @r.push_tail
"list", 'goodbye'
183 @r.push_tail
"list", '1'
184 @r.push_tail
"list", '2'
185 @r.push_tail
"list", '3'
186 @r.type
?('list').should
== "list"
187 @r.list_length('list').should
== 5
188 @r.list_range('list', 2, -1).should
== ['1', '2', '3']
192 it
"should be able to trim a list" do
193 @r.push_tail
"list", 'hello'
194 @r.push_tail
"list", 'goodbye'
195 @r.push_tail
"list", '1'
196 @r.push_tail
"list", '2'
197 @r.push_tail
"list", '3'
198 @r.type
?('list').should
== "list"
199 @r.list_length('list').should
== 5
200 @r.list_trim
'list', 0, 1
201 @r.list_length('list').should
== 2
202 @r.list_range('list', 0, -1).should
== ['hello', 'goodbye']
206 it
"should be able to get a value by indexing into a list" do
207 @r.push_tail
"list", 'hello'
208 @r.push_tail
"list", 'goodbye'
209 @r.type
?('list').should
== "list"
210 @r.list_length('list').should
== 2
211 @r.list_index('list', 1).should
== 'goodbye'
215 it
"should be able to set a value by indexing into a list" do
216 @r.push_tail
"list", 'hello'
217 @r.push_tail
"list", 'hello'
218 @r.type
?('list').should
== "list"
219 @r.list_length('list').should
== 2
220 @r.list_set('list', 1, 'goodbye').should be_true
221 @r.list_index('list', 1).should
== 'goodbye'
225 it
"should be able to remove values from a list LREM" do
226 @r.push_tail
"list", 'hello'
227 @r.push_tail
"list", 'goodbye'
228 @r.type
?('list').should
== "list"
229 @r.list_length('list').should
== 2
230 @r.list_rm('list', 1, 'hello').should
== 1
231 @r.list_range('list', 0, -1).should
== ['goodbye']
235 it
"should be able add members to a set" do
236 @r.set_add
"set", 'key1'
237 @r.set_add
"set", 'key2'
238 @r.type
?('set').should
== "set"
239 @r.set_count('set').should
== 2
240 @r.set_members('set').sort
.should
== ['key1', 'key2'].sort
244 it
"should be able delete members to a set" do
245 @r.set_add
"set", 'key1'
246 @r.set_add
"set", 'key2'
247 @r.type
?('set').should
== "set"
248 @r.set_count('set').should
== 2
249 @r.set_members('set').should
== Set
.new(['key1', 'key2'])
250 @r.set_delete('set', 'key1')
251 @r.set_count('set').should
== 1
252 @r.set_members('set').should
== Set
.new(['key2'])
256 it
"should be able count the members of a set" do
257 @r.set_add
"set", 'key1'
258 @r.set_add
"set", 'key2'
259 @r.type
?('set').should
== "set"
260 @r.set_count('set').should
== 2
264 it
"should be able test for set membership" do
265 @r.set_add
"set", 'key1'
266 @r.set_add
"set", 'key2'
267 @r.type
?('set').should
== "set"
268 @r.set_count('set').should
== 2
269 @r.set_member
?('set', 'key1').should be_true
270 @r.set_member
?('set', 'key2').should be_true
271 @r.set_member
?('set', 'notthere').should be_false
275 it
"should be able to do set intersection" do
276 @r.set_add
"set", 'key1'
277 @r.set_add
"set", 'key2'
278 @r.set_add
"set2", 'key2'
279 @r.set_intersect('set', 'set2').should
== Set
.new(['key2'])
283 it
"should be able to do set intersection and store the results in a key" do
284 @r.set_add
"set", 'key1'
285 @r.set_add
"set", 'key2'
286 @r.set_add
"set2", 'key2'
287 @r.set_inter_store('newone', 'set', 'set2').should
== 'OK'
288 @r.set_members('newone').should
== Set
.new(['key2'])
292 it
"should be able to do set union" do
293 @r.set_add
"set", 'key1'
294 @r.set_add
"set", 'key2'
295 @r.set_add
"set2", 'key2'
296 @r.set_add
"set2", 'key3'
297 @r.set_union('set', 'set2').should
== Set
.new(['key1','key2','key3'])
301 it
"should be able to do set union and store the results in a key" do
302 @r.set_add
"set", 'key1'
303 @r.set_add
"set", 'key2'
304 @r.set_add
"set2", 'key2'
305 @r.set_add
"set2", 'key3'
306 @r.set_union_store('newone', 'set', 'set2').should
== 'OK'
307 @r.set_members('newone').should
== Set
.new(['key1','key2','key3'])
311 # these don't seem to be implemented in redis head?
312 # it "should be able to do set difference" do
313 # @r.set_add "set", 'key1'
314 # @r.set_add "set", 'key2'
315 # @r.set_add "set2", 'key2'
316 # @r.set_add "set2", 'key3'
317 # @r.set_diff('set', 'set2').should == Set.new(['key1','key3'])
321 # it "should be able to do set difference and store the results in a key" do
322 # @r.set_add "set", 'key1'
323 # @r.set_add "set", 'key2'
324 # @r.set_add "set2", 'key2'
325 # @r.set_add "set2", 'key3'
326 # count = @r.set_diff_store('newone', 'set', 'set2')
328 # @r.set_members('newone').should == Set.new(['key1','key3'])
332 it
"should be able move elements from one set to another" do
333 @r.set_add
'set1', 'a'
334 @r.set_add
'set1', 'b'
335 @r.set_add
'set2', 'x'
336 @r.set_move('set1', 'set2', 'a').should
== true
337 @r.set_member
?('set2', 'a').should
== true
341 it
"should be able to do crazy SORT queries" do
342 @r['dog_1'] = 'louie'
343 @r.push_tail
'dogs', 1
345 @r.push_tail
'dogs', 2
347 @r.push_tail
'dogs', 3
349 @r.push_tail
'dogs', 4
350 @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should
== ['louie']
351 @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should
== ['taj']
354 it
"should provide info" do
355 [: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
|
356 @r.info
.keys
.should
include(x
)
360 it
"should be able to flush the database" do
361 @r['key1'] = 'keyone'
362 @r['key2'] = 'keytwo'
363 @r.keys('*').sort
.should
== ['foo', 'key1', 'key2'] #foo from before
365 @r.keys('*').should
== []
368 it
"should be able to provide the last save time" do
369 savetime
= @r.last_save
370 Time
.at(savetime
).class.should
== Time
371 Time
.at(savetime
).should
<= Time
.now
374 it
"should be able to MGET keys" do
377 @r.mget('foo', 'bar').should
== ['1000', '2000']
378 @r.mget('foo', 'bar', 'baz').should
== ['1000', '2000', nil]
381 it
"should bgsave" do
382 lambda
{@r.bgsave
}.should_not
raise_error(RedisError
)
385 it
"should handle multiple servers" do
387 @r = DistRedis
.new('localhost:6379', '127.0.0.1:6379')
388 @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
391 @r[idx
] = "foo#{idx}"
395 @r[idx
].should
== "foo#{idx}"
399 it
"should be able to pipeline writes" do
400 @r.pipelined
do |pipeline
|
401 pipeline
.push_head
"list", "hello"
402 pipeline
.push_head
"list", 42
405 @r.type
?('list').should
== "list"
406 @r.list_length('list').should
== 2
407 @r.pop_head('list').should
== '42'