spec/keys_spec.rb in fakeredis-0.8.0 vs spec/keys_spec.rb in fakeredis-0.9.0

- old
+ new

@@ -27,17 +27,22 @@ it "should return the number of '#{command}'ed keys" do @client.set("key1", "1") expect(@client.public_send(command, ["key1", "key2"])).to eq(1) end + end - it "should error '#{command}'ing no keys" do - expect { @client.public_send(command) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for '#{command}' command") - expect { @client.public_send(command, []) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for '#{command}' command") - end + it "should return 0 when deleting no keys" do + expect(@client.del).to eq(0) + expect(@client.del([])).to eq(0) end + it "should error when unlinking no keys" do + expect { @client.unlink }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'unlink' command") + expect { @client.unlink([]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'unlink' command") + end + it "should return true when setnx keys that don't exist" do expect(@client.setnx("key1", "1")).to eq(true) end it "should return false when setnx keys exist" do @@ -61,17 +66,10 @@ it "should return false when attempting to set pexpires on a key that does not exist" do expect(@client.pexpire("key1", 1)).to eq(false) end - it "should determine if a key exists" do - @client.set("key1", "1") - - expect(@client.exists("key1")).to eq(true) - expect(@client.exists("key2")).to eq(false) - end - it "should set a key's time to live in seconds" do @client.set("key1", "1") @client.expire("key1", 1) expect(@client.ttl("key1")).to eq(1) @@ -132,11 +130,11 @@ it "should not exist if expired" do @client.set("key1", "1") @client.expireat("key1", Time.now.to_i) - expect(@client.exists("key1")).to be false + expect(@client.exists?("key1")).to be false end it "should get integer and string keys" do @client.set("key1", "1") @client.set(2, "2") @@ -360,58 +358,58 @@ context "with extended options" do it "uses ex option to set the expire time, in seconds" do ttl = 7 - expect(@client.set("key1", "1", { :ex => ttl })).to eq("OK") + expect(@client.set("key1", "1", ex: ttl)).to eq("OK") expect(@client.ttl("key1")).to eq(ttl) end it "uses px option to set the expire time, in miliseconds" do ttl = 7000 - expect(@client.set("key1", "1", { :px => ttl })).to eq("OK") + expect(@client.set("key1", "1", px: ttl)).to eq("OK") expect(@client.ttl("key1")).to eq(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.set("key1", "1", px: ttl_px, ex: ttl_ex) expect(@client.ttl("key1")).to eq(ttl_px / 1000) - @client.set("key1", "1", { :ex => ttl_ex, :px => ttl_px }) + @client.set("key1", "1", ex: ttl_ex, px: ttl_px) expect(@client.ttl("key1")).to eq(ttl_px / 1000) end it "uses nx option to only set the key if it does not already exist" do - expect(@client.set("key1", "1", { :nx => true })).to eq(true) - expect(@client.set("key1", "2", { :nx => true })).to eq(false) + expect(@client.set("key1", "1", nx: true)).to eq(true) + expect(@client.set("key1", "2", nx: true)).to eq(false) expect(@client.get("key1")).to eq("1") end it "uses xx option to only set the key if it already exists" do - expect(@client.set("key2", "1", { :xx => true })).to eq(false) + expect(@client.set("key2", "1", xx: true)).to eq(false) @client.set("key2", "2") - expect(@client.set("key2", "1", { :xx => true })).to eq(true) + expect(@client.set("key2", "1", xx: true)).to eq(true) expect(@client.get("key2")).to eq("1") end it "does not set the key if both xx and nx option are specified" do - expect(@client.set("key2", "1", { :nx => true, :xx => true })).to eq(false) + expect(@client.set("key2", "1", nx: true, xx: true)).to eq(false) expect(@client.get("key2")).to be_nil end end describe "#dump" do it "returns nil for unknown key" do - expect(@client.exists("key1")).to be false + expect(@client.exists?("key1")).to be false expect(@client.dump("key1")).to be nil end it "dumps a single known key successfully" do @client.set("key1", "zomgwtf") @@ -443,11 +441,11 @@ before do @client.set("key1", "original value") @dumped_value = @client.dump("key1") @client.del("key1") - expect(@client.exists("key1")).to be false + expect(@client.exists?("key1")).to be false end it "restores to a new key successfully" do response = @client.restore("key1", 0, @dumped_value) expect(response).to eq "OK" @@ -502,11 +500,46 @@ allow(Time).to receive(:now).and_return(1000) @client.psetex("key", 2200, "value") expect(@client.pttl("key")).to be_within(0.1).of(2200) end + it "should raise an error if a non-integer is provided as the expiration" do + expect { @client.psetex("key", 100.5, "value") }.to raise_error(Redis::CommandError) + end + it "should return 'OK'" do expect(@client.psetex("key", 1000, "value")).to eq("OK") + end + end + + describe "#exists" do + it "returns 0 if none of the keys exist" do + expect(@client.exists("key1", "key2")).to eq 0 + end + + it "returns number of the keys that exist" do + @client.set("key2", "val2") + @client.set("key3", "val3") + expect(@client.exists("key1", "key2", "key3")).to eq 2 + end + + it "keys mentioned and existing multiple times counted multiple times" do + @client.set("key2", "val") + expect(@client.exists("key2", "key2")).to eq 2 + end + end + + describe "#exists?" do + it "should return true if any of the key exists" do + @client.set("key1", "1") + + expect(@client.exists?("key1")).to eq(true) + expect(@client.exists?("key1", "key2")).to eq(true) + end + + it "should return false if none of the key exists" do + expect(@client.exists?("key2")).to eq(false) + expect(@client.exists?("key2", "key3")).to eq(false) end end end end