X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/0df1ead745f74d7643b73e3065f5c85b240b38fa..d13f767cf2c23f47fd976380c8c8833ec525eccd:/client-libraries/ruby/spec/redis_spec.rb diff --git a/client-libraries/ruby/spec/redis_spec.rb b/client-libraries/ruby/spec/redis_spec.rb index d4dde964..0da249e1 100644 --- a/client-libraries/ruby/spec/redis_spec.rb +++ b/client-libraries/ruby/spec/redis_spec.rb @@ -1,15 +1,16 @@ require File.dirname(__FILE__) + '/spec_helper' +require 'logger' class Foo attr_accessor :bar def initialize(bar) @bar = bar end - + def ==(other) @bar == other.bar end -end +end describe "redis" do before(:all) do @@ -23,30 +24,41 @@ describe "redis" do after(:each) do @r.keys('*').each {|k| @r.del k} - end + end after(:all) do @r.quit - end + end + + it "should be able connect without a timeout" do + lambda { Redis.new :timeout => 0 }.should_not raise_error + end + + it "should be able to provide a logger" do + log = StringIO.new + r = Redis.new :db => 15, :logger => Logger.new(log) + r.ping + log.string.should include("ping") + end - it 'should be able to PING' do - @r.ping.should == 'PONG' + it "should be able to PING" do + @r.ping.should == 'PONG' end it "should be able to GET a key" do @r['foo'].should == 'bar' end - + it "should be able to SET a key" do @r['foo'] = 'nik' @r['foo'].should == 'nik' end - + it "should properly handle trailing newline characters" do @r['foo'] = "bar\n" @r['foo'].should == "bar\n" end - + it "should store and retrieve all possible characters at the beginning and the end of a string" do (0..255).each do |char_idx| string = "#{char_idx.chr}---#{char_idx.chr}" @@ -54,28 +66,45 @@ describe "redis" do @r['foo'].should == string end end - + it "should be able to SET a key with an expiry" do @r.set('foo', 'bar', 1) @r['foo'].should == 'bar' sleep 2 @r['foo'].should == nil end - + + it "should be able to return a TTL for a key" do + @r.set('foo', 'bar', 1) + @r.ttl('foo').should == 1 + end + it "should be able to SETNX" do @r['foo'] = 'nik' @r['foo'].should == 'nik' @r.setnx 'foo', 'bar' @r['foo'].should == 'nik' end - # + # + it "should be able to GETSET" do + @r.getset('foo', 'baz').should == 'bar' + @r['foo'].should == 'baz' + end + # it "should be able to INCR a key" do @r.del('counter') @r.incr('counter').should == 1 @r.incr('counter').should == 2 @r.incr('counter').should == 3 end - # + # + it "should be able to INCRBY a key" do + @r.del('counter') + @r.incrby('counter', 1).should == 1 + @r.incrby('counter', 2).should == 3 + @r.incrby('counter', 3).should == 6 + end + # it "should be able to DECR a key" do @r.del('counter') @r.incr('counter').should == 1 @@ -84,11 +113,11 @@ describe "redis" do @r.decr('counter').should == 2 @r.decr('counter', 2).should == 0 end - # + # it "should be able to RANDKEY" do @r.randkey.should_not be_nil end - # + # it "should be able to RENAME a key" do @r.del 'foo' @r.del'bar' @@ -96,7 +125,7 @@ describe "redis" do @r.rename 'foo', 'bar' @r['bar'].should == 'hi' end - # + # it "should be able to RENAMENX a key" do @r.del 'foo' @r.del 'bar' @@ -129,7 +158,7 @@ describe "redis" do @r.del 'foo' @r.exists('foo').should be_false end - # + # it "should be able to KEYS" do @r.keys("f*").each { |key| @r.del key } @r['f'] = 'nik' @@ -137,14 +166,18 @@ describe "redis" do @r['foo'] = 'qux' @r.keys("f*").sort.should == ['f','fo', 'foo'].sort end - #BTM - TODO + # + it "should be able to return a random key (RANDOMKEY)" do + 3.times { @r.exists(@r.randomkey).should be_true } + end + # it "should be able to check the TYPE of a key" do @r['foo'] = 'nik' @r.type('foo').should == "string" @r.del 'foo' @r.type('foo').should == "none" end - # + # it "should be able to push to the head of a list (LPUSH)" do @r.lpush "list", 'hello' @r.lpush "list", 42 @@ -152,13 +185,13 @@ describe "redis" do @r.llen('list').should == 2 @r.lpop('list').should == '42' end - # + # it "should be able to push to the tail of a list (RPUSH)" do @r.rpush "list", 'hello' @r.type('list').should == "list" @r.llen('list').should == 1 end - # + # it "should be able to pop the tail of a list (RPOP)" do @r.rpush "list", 'hello' @r.rpush"list", 'goodbye' @@ -166,7 +199,7 @@ describe "redis" do @r.llen('list').should == 2 @r.rpop('list').should == 'goodbye' end - # + # it "should be able to pop the head of a list (LPOP)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @@ -174,14 +207,14 @@ describe "redis" do @r.llen('list').should == 2 @r.lpop('list').should == 'hello' end - # + # it "should be able to get the length of a list (LLEN)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @r.type('list').should == "list" @r.llen('list').should == 2 end - # + # it "should be able to get a range of values from a list (LRANGE)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @@ -192,7 +225,7 @@ describe "redis" do @r.llen('list').should == 5 @r.lrange('list', 2, -1).should == ['1', '2', '3'] end - # + # it "should be able to trim a list (LTRIM)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @@ -205,7 +238,7 @@ describe "redis" do @r.llen('list').should == 2 @r.lrange('list', 0, -1).should == ['hello', 'goodbye'] end - # + # it "should be able to get a value by indexing into a list (LINDEX)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @@ -213,7 +246,7 @@ describe "redis" do @r.llen('list').should == 2 @r.lindex('list', 1).should == 'goodbye' end - # + # it "should be able to set a value by indexing into a list (LSET)" do @r.rpush "list", 'hello' @r.rpush "list", 'hello' @@ -222,7 +255,7 @@ describe "redis" do @r.lset('list', 1, 'goodbye').should == 'OK' @r.lindex('list', 1).should == 'goodbye' end - # + # it "should be able to remove values from a list (LREM)" do @r.rpush "list", 'hello' @r.rpush "list", 'goodbye' @@ -231,7 +264,7 @@ describe "redis" do @r.lrem('list', 1, 'hello').should == 1 @r.lrange('list', 0, -1).should == ['goodbye'] end - # + # it "should be able add members to a set (SADD)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @@ -239,7 +272,7 @@ describe "redis" do @r.scard('set').should == 2 @r.smembers('set').sort.should == ['key1', 'key2'].sort end - # + # it "should be able delete members to a set (SREM)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @@ -250,14 +283,14 @@ describe "redis" do @r.scard('set').should == 1 @r.smembers('set').should == ['key2'] end - # + # it "should be able count the members of a set (SCARD)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @r.type('set').should == "set" @r.scard('set').should == 2 end - # + # it "should be able test for set membership (SISMEMBER)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @@ -267,14 +300,14 @@ describe "redis" do @r.sismember('set', 'key2').should be_true @r.sismember('set', 'notthere').should be_false end - # + # it "should be able to do set intersection (SINTER)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @r.sadd "set2", 'key2' @r.sinter('set', 'set2').should == ['key2'] end - # + # it "should be able to do set intersection and store the results in a key (SINTERSTORE)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @@ -290,7 +323,7 @@ describe "redis" do @r.sadd "set2", 'key3' @r.sunion('set', 'set2').sort.should == ['key1','key2','key3'].sort end - # + # it "should be able to do set union and store the results in a key (SUNIONSTORE)" do @r.sadd "set", 'key1' @r.sadd "set", 'key2' @@ -299,7 +332,7 @@ describe "redis" do @r.sunionstore('newone', 'set', 'set2').should == 3 @r.smembers('newone').sort.should == ['key1','key2','key3'].sort end - # + # it "should be able to do set difference (SDIFF)" do @r.sadd "set", 'a' @r.sadd "set", 'b' @@ -307,7 +340,7 @@ describe "redis" do @r.sadd "set2", 'c' @r.sdiff('set', 'set2').should == ['a'] end - # + # it "should be able to do set difference and store the results in a key (SDIFFSTORE)" do @r.sadd "set", 'a' @r.sadd "set", 'b' @@ -316,27 +349,28 @@ describe "redis" do @r.sdiffstore('newone', 'set', 'set2') @r.smembers('newone').should == ['a'] end - # + # it "should be able move elements from one set to another (SMOVE)" do @r.sadd 'set1', 'a' @r.sadd 'set1', 'b' @r.sadd 'set2', 'x' - @r.smove('set1', 'set2', 'a').should be_true + @r.smove('set1', 'set2', 'a').should be_true @r.sismember('set2', 'a').should be_true @r.delete('set1') end # it "should be able to do crazy SORT queries" do + # The 'Dogs' is capitialized on purpose @r['dog_1'] = 'louie' - @r.rpush 'dogs', 1 + @r.rpush 'Dogs', 1 @r['dog_2'] = 'lucy' - @r.rpush 'dogs', 2 + @r.rpush 'Dogs', 2 @r['dog_3'] = 'max' - @r.rpush 'dogs', 3 + @r.rpush 'Dogs', 3 @r['dog_4'] = 'taj' - @r.rpush 'dogs', 4 - @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie'] - @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj'] + @r.rpush 'Dogs', 4 + @r.sort('Dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie'] + @r.sort('Dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj'] end it "should be able to handle array of :get using SORT" do @@ -355,38 +389,61 @@ describe "redis" do @r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1]).should == ['louie', 'mutt'] @r.sort('dogs', :get => ['dog:*:name', 'dog:*:breed'], :limit => [0,1], :order => 'desc alpha').should == ['taj', 'terrier'] end - # - it "should provide info" do + # + it "should provide info (INFO)" do [: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| @r.info.keys.should include(x) end end - # - it "should be able to flush the database" do + # + it "should be able to flush the database (FLUSHDB)" do @r['key1'] = 'keyone' @r['key2'] = 'keytwo' @r.keys('*').sort.should == ['foo', 'key1', 'key2'].sort #foo from before @r.flushdb @r.keys('*').should == [] end - # + # + it "should raise exception when manually try to change the database" do + lambda { @r.select(0) }.should raise_error + end + # it "should be able to provide the last save time (LASTSAVE)" do savetime = @r.lastsave Time.at(savetime).class.should == Time Time.at(savetime).should <= Time.now end - + it "should be able to MGET keys" do @r['foo'] = 1000 @r['bar'] = 2000 @r.mget('foo', 'bar').should == ['1000', '2000'] @r.mget('foo', 'bar', 'baz').should == ['1000', '2000', nil] end - + + it "should be able to mapped MGET keys" do + @r['foo'] = 1000 + @r['bar'] = 2000 + @r.mapped_mget('foo', 'bar').should == { 'foo' => '1000', 'bar' => '2000'} + @r.mapped_mget('foo', 'baz', 'bar').should == { 'foo' => '1000', 'bar' => '2000'} + end + it "should bgsave" do @r.bgsave.should == 'OK' end - + + it "should be able to ECHO" do + @r.echo("message in a bottle\n").should == "message in a bottle\n" + end + + it "should raise error when invoke MONITOR" do + lambda { @r.monitor }.should raise_error + end + + it "should raise error when invoke SYNC" do + lambda { @r.sync }.should raise_error + end + it "should handle multiple servers" do require 'dist_redis' @r = DistRedis.new(:hosts=> ['localhost:6379', '127.0.0.1:6379'], :db => 15) @@ -405,10 +462,17 @@ describe "redis" do pipeline.lpush 'list', "hello" pipeline.lpush 'list', 42 end - + @r.type('list').should == "list" @r.llen('list').should == 2 @r.lpop('list').should == '42' end - + + it "should AUTH when connecting with a password" do + r = Redis.new(:password => 'secret') + r.stub!(:connect_to) + r.should_receive(:call_command).with(['auth', 'secret']) + r.connect_to_server + end + end