/client-libraries/ruby/spec/redis_spec.rb
Ruby | 267 lines | 233 code | 34 blank | 0 comment | 65 complexity | 15495379f4dfde6e39d5b5aa8f75de5e MD5 | raw file
Possible License(s): BSD-3-Clause
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 15 before do 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 } 23 end 24 25 it "should properly marshall objects" do 26 class MyFail; def fail; 'it will' end; end 27 28 @r['fail'] = MyFail.new 29 @r['fail'].fail.should == 'it will' 30 31 end 32 33 it "should be able to GET a key" do 34 @r['foo'].should == 'bar' 35 end 36 37 it "should be able to SET a key" do 38 @r['foo'] = 'nik' 39 @r['foo'].should == 'nik' 40 end 41 42 it "should be able to SETNX(set_unless_exists)" do 43 @r['foo'] = 'nik' 44 @r['foo'].should == 'nik' 45 @r.set_unless_exists 'foo', 'bar' 46 @r['foo'].should == 'nik' 47 end 48 49 it "should be able to INCR(increment) a key" do 50 @r.delete('counter') 51 @r.incr('counter').should == 1 52 @r.incr('counter').should == 2 53 @r.incr('counter').should == 3 54 end 55 56 it "should be able to DECR(decrement) a key" do 57 @r.delete('counter') 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 64 end 65 66 it "should be able to RANDKEY(return a random key)" do 67 @r.randkey.should_not be_nil 68 end 69 70 it "should be able to RENAME a key" do 71 @r.delete 'foo' 72 @r.delete 'bar' 73 @r['foo'] = 'hi' 74 @r.rename! 'foo', 'bar' 75 @r['bar'].should == 'hi' 76 end 77 78 it "should be able to RENAMENX(rename unless the new key already exists) a key" do 79 @r.delete 'foo' 80 @r.delete 'bar' 81 @r['foo'] = 'hi' 82 @r['bar'] = 'ohai' 83 lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisError) 84 @r['bar'].should == 'ohai' 85 end 86 87 it "should be able to EXISTS(check if key exists)" do 88 @r['foo'] = 'nik' 89 @r.key?('foo').should be_true 90 @r.delete 'foo' 91 @r.key?('foo').should be_false 92 end 93 94 it "should be able to KEYS(glob for keys)" do 95 @r.keys("f*").each do |key| 96 @r.delete key 97 end 98 @r['f'] = 'nik' 99 @r['fo'] = 'nak' 100 @r['foo'] = 'qux' 101 @r.keys("f*").sort.should == ['f','fo', 'foo'].sort 102 end 103 104 it "should be able to check the TYPE of a key" do 105 @r['foo'] = 'nik' 106 @r.type?('foo').should == "string" 107 @r.delete 'foo' 108 @r.type?('foo').should == "none" 109 end 110 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' 117 @r.delete('list') 118 end 119 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 124 @r.delete('list') 125 end 126 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' 133 @r.delete('list') 134 end 135 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' 142 @r.delete('list') 143 end 144 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 150 @r.delete('list') 151 end 152 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'] 162 @r.delete('list') 163 end 164 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'] 176 @r.delete('list') 177 end 178 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' 185 @r.delete('list') 186 end 187 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' 195 @r.delete('list') 196 end 197 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 204 @r.delete('set') 205 end 206 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']) 216 @r.delete('set') 217 end 218 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 224 @r.delete('set') 225 end 226 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 235 @r.delete('set') 236 end 237 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']) 243 @r.delete('set') 244 end 245 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']) 252 @r.delete('set') 253 end 254 255 it "should be able to do crazy SORT queries" do 256 @r['dog_1'] = 'louie' 257 @r.push_tail 'dogs', 1 258 @r['dog_2'] = 'lucy' 259 @r.push_tail 'dogs', 2 260 @r['dog_3'] = 'max' 261 @r.push_tail 'dogs', 3 262 @r['dog_4'] = 'taj' 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'] 266 end 267end