spec/sets_spec.rb in fakeredis-0.4.3 vs spec/sets_spec.rb in fakeredis-0.5.0
- old
+ new
@@ -123,20 +123,20 @@
it "should remove and return a random member from a set" do
@client.sadd("key1", "a")
@client.sadd("key1", "b")
- ["a", "b"].include?(@client.spop("key1")).should be_true
- ["a", "b"].include?(@client.spop("key1")).should be_true
+ ["a", "b"].include?(@client.spop("key1")).should be true
+ ["a", "b"].include?(@client.spop("key1")).should be true
@client.spop("key1").should be_nil
end
it "should get a random member from a set" do
@client.sadd("key1", "a")
@client.sadd("key1", "b")
- ["a", "b"].include?(@client.spop("key1")).should be_true
+ ["a", "b"].include?(@client.spop("key1")).should be true
end
it "should remove a member from a set" do
@client.sadd("key1", "a")
@client.sadd("key1", "b")
@@ -144,10 +144,18 @@
@client.srem("key1", "a").should be == false
@client.smembers("key1").should be == ["b"]
end
+ it "should remove multiple members from a set" do
+ @client.sadd("key1", "a")
+ @client.sadd("key1", "b")
+
+ @client.srem("key1", [ "a", "b"]).should == 2
+ @client.smembers("key1").should be_empty
+ end
+
it "should remove the set's key once it's empty" do
@client.sadd("key1", "a")
@client.sadd("key1", "b")
@client.srem("key1", "b")
@client.srem("key1", "a")
@@ -178,8 +186,84 @@
@client.sadd("key3", "c")
@client.sadd("key3", "e")
@client.sunionstore("key", "key1", "key2", "key3")
@client.smembers("key").should =~ ["a", "b", "c", "d", "e"]
+ end
+ end
+
+ describe 'srandmember' do
+ before(:each) do
+ @client = Redis.new
+ end
+
+ context 'with a set that has three elements' do
+ before do
+ @client.sadd("key1", "a")
+ @client.sadd("key1", "b")
+ @client.sadd("key1", "c")
+ end
+
+ context 'when called without the optional number parameter' do
+ it 'is a random element from the set' do
+ random_element = @client.srandmember("key1")
+
+ ['a', 'b', 'c'].include?(random_element).should be true
+ end
+ end
+
+ context 'when called with the optional number parameter of 1' do
+ it 'is an array of one random element from the set' do
+ random_elements = @client.srandmember("key1", 1)
+
+ [['a'], ['b'], ['c']].include?(@client.srandmember("key1", 1)).should be true
+ end
+ end
+
+ context 'when called with the optional number parameter of 2' do
+ it 'is an array of two unique, random elements from the set' do
+ random_elements = @client.srandmember("key1", 2)
+
+ random_elements.count.should == 2
+ random_elements.uniq.count.should == 2
+ random_elements.all? do |element|
+ ['a', 'b', 'c'].include?(element).should be true
+ end
+ end
+ end
+
+ context 'when called with an optional parameter of -100' do
+ it 'is an array of 100 random elements from the set, some of which are repeated' do
+ random_elements = @client.srandmember("key1", -100)
+
+ random_elements.count.should == 100
+ random_elements.uniq.count.should <= 3
+ random_elements.all? do |element|
+ ['a', 'b', 'c'].include?(element).should be true
+ end
+ end
+ end
+
+ context 'when called with an optional parameter of 100' do
+ it 'is an array of all of the elements from the set, none of which are repeated' do
+ random_elements = @client.srandmember("key1", 100)
+
+ random_elements.count.should == 3
+ random_elements.uniq.count.should == 3
+ random_elements.should =~ ['a', 'b', 'c']
+ end
+ end
+ end
+
+ context 'with an empty set' do
+ before { @client.del("key1") }
+
+ it 'is nil without the extra parameter' do
+ @client.srandmember("key1").should be_nil
+ end
+
+ it 'is an empty array with an extra parameter' do
+ @client.srandmember("key1", 1).should == []
+ end
end
end
end