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

- old
+ new

@@ -5,30 +5,37 @@ before(:each) do @client = Redis.new end - it "should delete a key" do - @client.set("key1", "1") - @client.set("key2", "2") - @client.del("key1", "key2") + [:del, :unlink].each do |command| + it "should #{command} a key" do + @client.set("key1", "1") + @client.set("key2", "2") + @client.public_send(command, "key1", "key2") - expect(@client.get("key1")).to eq(nil) - end + expect(@client.get("key1")).to eq(nil) + end - it "should delete multiple keys" do - @client.set("key1", "1") - @client.set("key2", "2") - @client.del(["key1", "key2"]) + it "should #{command} multiple keys" do + @client.set("key1", "1") + @client.set("key2", "2") + @client.public_send(command, ["key1", "key2"]) - expect(@client.get("key1")).to eq(nil) - expect(@client.get("key2")).to eq(nil) - end + expect(@client.get("key1")).to eq(nil) + expect(@client.get("key2")).to eq(nil) + end - it "should error deleting no keys" do - expect { @client.del }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command") - expect { @client.del [] }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command") + 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 + + 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 end it "should return true when setnx keys that don't exist" do expect(@client.setnx("key1", "1")).to eq(true) end @@ -128,10 +135,17 @@ @client.expireat("key1", Time.now.to_i) expect(@client.exists("key1")).to be false end + it "should get integer and string keys" do + @client.set("key1", "1") + @client.set(2, "2") + + expect(@client.mget("key1", 2)).to eq(["1", "2"]) + end + it "should find all keys matching the given pattern" do @client.set("key:a", "1") @client.set("key:b", "2") @client.set("key:c", "3") @client.set("akeyd", "4") @@ -481,8 +495,19 @@ expect(@client.type("key2")).to eq "set" end end end + describe "#psetex" do + it "should set a key's time to live in milliseconds" do + 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 return 'OK'" do + expect(@client.psetex("key", 1000, "value")).to eq("OK") + end + end end end