spec/hashes_spec.rb in fakeredis-0.2.2 vs spec/hashes_spec.rb in fakeredis-0.3.0

- old
+ new

@@ -13,10 +13,35 @@ @client.hget("key1", "k1").should be_nil @client.hget("key1", "k2").should == "val2" end + it "should remove a hash with no keys left" do + @client.hset("key1", "k1", "val1") + @client.hset("key1", "k2", "val2") + @client.hdel("key1", "k1") + @client.hdel("key1", "k2") + + @client.exists("key1").should == false + end + + it "should convert key to a string via to_s for hset" do + m = double("key") + m.stub(:to_s).and_return("foo") + + @client.hset("key1", m, "bar") + @client.hget("key1", "foo").should == "bar" + end + + it "should convert key to a string via to_s for hget" do + m = double("key") + m.stub(:to_s).and_return("foo") + + @client.hset("key1", "foo", "bar") + @client.hget("key1", m).should == "bar" + end + it "should determine if a hash field exists" do @client.hset("key1", "index", "value") @client.hexists("key1", "index").should be_true @client.hexists("key2", "i2").should be_false @@ -35,15 +60,19 @@ @client.hgetall("key1").should == {"i1" => "val1", "i2" => "val2"} end it "should increment the integer value of a hash field by the given number" do @client.hset("key1", "cont1", "5") - @client.hincrby("key1", "cont1", "5") - + @client.hincrby("key1", "cont1", "5").should == 10 @client.hget("key1", "cont1").should == "10" end + it "should increment non existing hash keys" do + @client.hget("key1", "cont2").should be_nil + @client.hincrby("key1", "cont2", "5").should == 5 + end + it "should get all the fields in a hash" do @client.hset("key1", "i1", "val1") @client.hset("key1", "i2", "val2") @client.hkeys("key1").should =~ ["i1", "i2"] @@ -71,21 +100,25 @@ @client.hget("key", "k1").should == "value1" @client.hget("key", "k2").should == "value2" end it "should set the string value of a hash field" do - @client.hset("key1", "k1", "val1") + @client.hset("key1", "k1", "val1").should == true + @client.hset("key1", "k1", "val1").should == false @client.hget("key1", "k1").should == "val1" end it "should set the value of a hash field, only if the field does not exist" do @client.hset("key1", "k1", "val1") - @client.hsetnx("key1", "k1", "value") - @client.hsetnx("key1", "k2", "val2") + @client.hsetnx("key1", "k1", "value").should == false + @client.hsetnx("key1", "k2", "val2").should == true + @client.hsetnx("key1", :k1, "value").should == false + @client.hsetnx("key1", :k3, "val3").should == true @client.hget("key1", "k1").should == "val1" @client.hget("key1", "k2").should == "val2" + @client.hget("key1", "k3").should == "val3" end it "should get all the values in a hash" do @client.hset("key1", "k1", "val1") @client.hset("key1", "k2", "val2")