]>
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
22 @r.keys('*').each
{|k
| @r.delete k
}
25 it
"should properly marshall objects" do
26 class MyFail
; def fail
; 'it will' end; end
28 @r['fail'] = MyFail
.new
29 @r['fail'].fail
.should
== 'it will'
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 be able to SETNX(set_unless_exists)" do
44 @r['foo'].should
== 'nik'
45 @r.set_unless_exists
'foo', 'bar'
46 @r['foo'].should
== 'nik'
49 it
"should be able to INCR(increment) a key" do
51 @r.incr('counter').should
== 1
52 @r.incr('counter').should
== 2
53 @r.incr('counter').should
== 3
56 it
"should be able to DECR(decrement) a key" do
58 @r.incr('counter').should
== 1
59 @r.incr('counter').should
== 2
60 @r.incr('counter').should
== 3
61 @r.decr('counter').should
== 2
62 @r.decr('counter').should
== 1
63 @r.decr('counter').should
== 0
66 it
"should be able to RANDKEY(return a random key)" do
67 @r.randkey
.should_not be_nil
70 it
"should be able to RENAME a key" do
74 @r.rename!
'foo', 'bar'
75 @r['bar'].should
== 'hi'
78 it
"should be able to RENAMENX(rename unless the new key already exists) a key" do
83 lambda
{@r.rename
'foo', 'bar'}.should
raise_error(RedisError
)
84 @r['bar'].should
== 'ohai'
87 it
"should be able to EXISTS(check if key exists)" do
89 @r.key
?('foo').should be_true
91 @r.key
?('foo').should be_false
94 it
"should be able to KEYS(glob for keys)" do
95 @r.keys("f*").each
do |key
|
101 @r.keys("f*").sort
.should
== ['f','fo', 'foo'].sort
104 it
"should be able to check the TYPE of a key" do
106 @r.type
?('foo').should
== "string"
108 @r.type
?('foo').should
== "none"
111 it
"should be able to push to the head of a list" do
112 @r.push_head
"list", 'hello'
113 @r.push_head
"list", 42
114 @r.type
?('list').should
== "list"
115 @r.list_length('list').should
== 2
116 @r.pop_head('list').should
== '42'
120 it
"should be able to push to the tail of a list" do
121 @r.push_tail
"list", 'hello'
122 @r.type
?('list').should
== "list"
123 @r.list_length('list').should
== 1
127 it
"should be able to pop the tail of a list" do
128 @r.push_tail
"list", 'hello'
129 @r.push_tail
"list", 'goodbye'
130 @r.type
?('list').should
== "list"
131 @r.list_length('list').should
== 2
132 @r.pop_tail('list').should
== 'goodbye'
136 it
"should be able to pop the head of a list" do
137 @r.push_tail
"list", 'hello'
138 @r.push_tail
"list", 'goodbye'
139 @r.type
?('list').should
== "list"
140 @r.list_length('list').should
== 2
141 @r.pop_head('list').should
== 'hello'
145 it
"should be able to get the length of a list" do
146 @r.push_tail
"list", 'hello'
147 @r.push_tail
"list", 'goodbye'
148 @r.type
?('list').should
== "list"
149 @r.list_length('list').should
== 2
153 it
"should be able to get a range of values from a list" do
154 @r.push_tail
"list", 'hello'
155 @r.push_tail
"list", 'goodbye'
156 @r.push_tail
"list", '1'
157 @r.push_tail
"list", '2'
158 @r.push_tail
"list", '3'
159 @r.type
?('list').should
== "list"
160 @r.list_length('list').should
== 5
161 @r.list_range('list', 2, -1).should
== ['1', '2', '3']
165 it
"should be able to trim a list" do
166 @r.push_tail
"list", 'hello'
167 @r.push_tail
"list", 'goodbye'
168 @r.push_tail
"list", '1'
169 @r.push_tail
"list", '2'
170 @r.push_tail
"list", '3'
171 @r.type
?('list').should
== "list"
172 @r.list_length('list').should
== 5
173 @r.list_trim
'list', 0, 1
174 @r.list_length('list').should
== 2
175 @r.list_range('list', 0, -1).should
== ['hello', 'goodbye']
179 it
"should be able to get a value by indexing into a list" do
180 @r.push_tail
"list", 'hello'
181 @r.push_tail
"list", 'goodbye'
182 @r.type
?('list').should
== "list"
183 @r.list_length('list').should
== 2
184 @r.list_index('list', 1).should
== 'goodbye'
188 it
"should be able to set a value by indexing into a list" do
189 @r.push_tail
"list", 'hello'
190 @r.push_tail
"list", 'hello'
191 @r.type
?('list').should
== "list"
192 @r.list_length('list').should
== 2
193 @r.list_set('list', 1, 'goodbye').should be_true
194 @r.list_index('list', 1).should
== 'goodbye'
198 it
"should be able add members to a set" do
199 @r.set_add
"set", 'key1'
200 @r.set_add
"set", 'key2'
201 @r.type
?('set').should
== "set"
202 @r.set_count('set').should
== 2
203 @r.set_members('set').sort
.should
== ['key1', 'key2'].sort
207 it
"should be able delete members to a set" do
208 @r.set_add
"set", 'key1'
209 @r.set_add
"set", 'key2'
210 @r.type
?('set').should
== "set"
211 @r.set_count('set').should
== 2
212 @r.set_members('set').should
== Set
.new(['key1', 'key2'])
213 @r.set_delete('set', 'key1')
214 @r.set_count('set').should
== 1
215 @r.set_members('set').should
== Set
.new(['key2'])
219 it
"should be able count the members of a set" do
220 @r.set_add
"set", 'key1'
221 @r.set_add
"set", 'key2'
222 @r.type
?('set').should
== "set"
223 @r.set_count('set').should
== 2
227 it
"should be able test for set membership" do
228 @r.set_add
"set", 'key1'
229 @r.set_add
"set", 'key2'
230 @r.type
?('set').should
== "set"
231 @r.set_count('set').should
== 2
232 @r.set_member
?('set', 'key1').should be_true
233 @r.set_member
?('set', 'key2').should be_true
234 @r.set_member
?('set', 'notthere').should be_false
238 it
"should be able to do set intersection" do
239 @r.set_add
"set", 'key1'
240 @r.set_add
"set", 'key2'
241 @r.set_add
"set2", 'key2'
242 @r.set_intersect('set', 'set2').should
== Set
.new(['key2'])
246 it
"should be able to do set intersection and store the results in a key" do
247 @r.set_add
"set", 'key1'
248 @r.set_add
"set", 'key2'
249 @r.set_add
"set2", 'key2'
250 @r.set_inter_store('newone', 'set', 'set2')
251 @r.set_members('newone').should
== Set
.new(['key2'])
255 it
"should be able to do crazy SORT queries" do
256 @r['dog_1'] = 'louie'
257 @r.push_tail
'dogs', 1
259 @r.push_tail
'dogs', 2
261 @r.push_tail
'dogs', 3
263 @r.push_tail
'dogs', 4
264 @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should
== ['louie']
265 @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should
== ['taj']