spec/keys_spec.rb in fakeredis-0.4.3 vs spec/keys_spec.rb in fakeredis-0.5.0

- old
+ new

@@ -27,10 +27,19 @@ it "should error deleting no keys" do lambda { @client.del }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command") lambda { @client.del [] }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command") end + it "should return true when setting expires on keys that exist" do + @client.set("key1", "1") + @client.expire("key1", 1).should == true + end + + it "should return false when attempting to set expires on a key that does not exist" do + @client.expire("key1", 1).should == false + end + it "should determine if a key exists" do @client.set("key1", "1") @client.exists("key1").should be == true @client.exists("key2").should be == false @@ -90,11 +99,11 @@ it "should not exist if expired" do @client.set("key1", "1") @client.expireat("key1", Time.now.to_i) - @client.exists("key1").should be_false + @client.exists("key1").should be false end it "should find all keys matching the given pattern" do @client.set("key:a", "1") @client.set("key:b", "2") @@ -143,14 +152,10 @@ @client.get("key2").should be == "2" @client.get("key3").should be == nil @client.get("key4").should be == "3" end - it "should sort the elements in a list, set or sorted set" do - pending "SORT Command not implemented yet" - end - it "should determine the type stored at key" do # Non-existing key @client.type("key0").should be == "none" # String @@ -263,7 +268,104 @@ lambda { @client.move("key1", 0) }.should raise_error(Redis::CommandError, "ERR source and destination objects are the same") @client.select(0) @client.get("key1").should be == "1" end + + it "should scan all keys in the database" do + 100.times do |x| + @client.set("key#{x}", "#{x}") + end + + cursor = 0 + all_keys = [] + loop { + cursor, keys = @client.scan(cursor) + all_keys += keys + break if cursor == "0" + } + + all_keys.uniq.size.should == 100 + all_keys[0].should =~ /key\d+/ + end + + it "should match keys to a pattern when scanning" do + 50.times do |x| + @client.set("key#{x}", "#{x}") + end + + @client.set("miss_me", 1) + @client.set("pass_me", 2) + + cursor = 0 + all_keys = [] + loop { + cursor, keys = @client.scan(cursor, :match => "key*") + all_keys += keys + break if cursor == "0" + } + + all_keys.uniq.size.should == 50 + end + + it "should specify doing more work when scanning" do + 100.times do |x| + @client.set("key#{x}", "#{x}") + end + + cursor, all_keys = @client.scan(cursor, :count => 100) + + cursor.should == "0" + all_keys.uniq.size.should == 100 + end + + context "with extended options" do + it "uses ex option to set the expire time, in seconds" do + ttl = 7 + + @client.set("key1", "1", { :ex => ttl }).should == "OK" + @client.ttl("key1").should == ttl + end + + it "uses px option to set the expire time, in miliseconds" do + ttl = 7000 + + @client.set("key1", "1", { :px => ttl }).should == "OK" + @client.ttl("key1").should == (ttl / 1000) + end + + # Note that the redis-rb implementation will always give PX last. + # Redis seems to process each expiration option and the last one wins. + it "prefers the finer-grained PX expiration option over EX" do + ttl_px = 6000 + ttl_ex = 10 + + @client.set("key1", "1", { :px => ttl_px, :ex => ttl_ex }) + @client.ttl("key1").should == (ttl_px / 1000) + + @client.set("key1", "1", { :ex => ttl_ex, :px => ttl_px }) + @client.ttl("key1").should == (ttl_px / 1000) + end + + it "uses nx option to only set the key if it does not already exist" do + @client.set("key1", "1", { :nx => true }).should == true + @client.set("key1", "2", { :nx => true }).should == false + + @client.get("key1").should == "1" + end + + it "uses xx option to only set the key if it already exists" do + @client.set("key2", "1", { :xx => true }).should == false + @client.set("key2", "2") + @client.set("key2", "1", { :xx => true }).should == true + + @client.get("key2").should == "1" + end + + it "does not set the key if both xx and nx option are specified" do + @client.set("key2", "1", { :nx => true, :xx => true }).should == false + @client.get("key2").should be_nil + end + end end end +