spec/riak/bucket_spec.rb in riak-client-1.4.5 vs spec/riak/bucket_spec.rb in riak-client-2.0.0.rc1

- old
+ new

@@ -1,128 +1,128 @@ require 'spec_helper' describe Riak::Bucket do before :each do @client = Riak::Client.new - @backend = mock("Backend") - @client.stub!(:backend).and_yield(@backend) - @client.stub!(:http).and_yield(@backend) + @backend = double("Backend") + allow(@client).to receive(:backend).and_yield(@backend) + allow(@client).to receive(:http).and_yield(@backend) @bucket = Riak::Bucket.new(@client, "foo") end describe "when initializing" do it "should require a client and a name" do - lambda { Riak::Bucket.new }.should raise_error - lambda { Riak::Bucket.new(@client) }.should raise_error - lambda { Riak::Bucket.new("foo") }.should raise_error - lambda { Riak::Bucket.new("foo", @client) }.should raise_error - lambda { Riak::Bucket.new(@client, "foo") }.should_not raise_error + expect { Riak::Bucket.new }.to raise_error + expect { Riak::Bucket.new(@client) }.to raise_error + expect { Riak::Bucket.new("foo") }.to raise_error + expect { Riak::Bucket.new("foo", @client) }.to raise_error + expect { Riak::Bucket.new(@client, "foo") }.not_to raise_error expect { Riak::Bucket.new(@client, '') }.to raise_error(ArgumentError) end it "should set the client and name attributes" do bucket = Riak::Bucket.new(@client, "foo") - bucket.client.should == @client - bucket.name.should == "foo" + expect(bucket.client).to eq(@client) + expect(bucket.name).to eq("foo") end end describe "accessing keys" do it "should list the keys" do - @backend.should_receive(:list_keys).with(@bucket, {}).and_return(["bar"]) - @bucket.keys.should == ["bar"] + expect(@backend).to receive(:list_keys).with(@bucket, {}).and_return(["bar"]) + expect(@bucket.keys).to eq(["bar"]) end it "should allow streaming keys through block" do - @backend.should_receive(:list_keys).with(@bucket, {}).and_yield([]).and_yield(["bar"]).and_yield(["baz"]) + expect(@backend).to receive(:list_keys).with(@bucket, {}).and_yield([]).and_yield(["bar"]).and_yield(["baz"]) all_keys = [] @bucket.keys do |list| all_keys.concat(list) end - all_keys.should == ["bar", "baz"] + expect(all_keys).to eq(["bar", "baz"]) end it "should not cache the list of keys" do - @backend.should_receive(:list_keys).with(@bucket, {}).twice.and_return(["bar"]) - 2.times { @bucket.keys.should == ['bar'] } + expect(@backend).to receive(:list_keys).with(@bucket, {}).twice.and_return(["bar"]) + 2.times { expect(@bucket.keys).to eq(['bar']) } end it "should warn about the expense of list-keys when warnings are not disabled" do Riak.disable_list_keys_warnings = false - @backend.stub!(:list_keys).and_return(%w{test test2}) - @bucket.should_receive(:warn) + allow(@backend).to receive(:list_keys).and_return(%w{test test2}) + expect(@bucket).to receive(:warn) @bucket.keys Riak.disable_list_keys_warnings = true end it "should allow a specified timeout when listing keys" do - @backend.should_receive(:list_keys).with(@bucket, timeout: 1234).and_return(%w{bar}) + expect(@backend).to receive(:list_keys).with(@bucket, timeout: 1234).and_return(%w{bar}) keys = @bucket.keys timeout: 1234 - keys.should == %w{bar} + expect(keys).to eq(%w{bar}) end end describe "accessing a counter" do it "should return a counter object" do - Riak::Counter.should_receive(:new).with(@bucket, 'asdf').and_return('example counter') + expect(Riak::Counter).to receive(:new).with(@bucket, 'asdf').and_return('example counter') new_counter = @bucket.counter 'asdf' - new_counter.should == 'example counter' + expect(new_counter).to eq('example counter') end end describe "setting the bucket properties" do it "should prefetch the properties when they are not present" do - @backend.stub!(:set_bucket_props) - @backend.should_receive(:get_bucket_props).with(@bucket).and_return({"name" => "foo"}) + allow(@backend).to receive(:set_bucket_props) + expect(@backend).to receive(:get_bucket_props).with(@bucket, { }).and_return({"name" => "foo"}) @bucket.props = {"precommit" => []} end it "should set the new properties on the bucket" do @bucket.instance_variable_set(:@props, {}) # Pretend they are there - @backend.should_receive(:set_bucket_props).with(@bucket, { :name => "foo" }) + expect(@backend).to receive(:set_bucket_props).with(@bucket, { :name => "foo" }, nil) @bucket.props = { :name => "foo" } end it "should raise an error if an invalid type is given" do - lambda { @bucket.props = "blah" }.should raise_error(ArgumentError) + expect { @bucket.props = "blah" }.to raise_error(ArgumentError) end end describe "fetching the bucket properties" do it "should fetch properties on first access" do - @bucket.instance_variable_get(:@props).should be_nil - @backend.should_receive(:get_bucket_props).with(@bucket).and_return({"name" => "foo"}) - @bucket.props.should == {"name" => "foo"} + expect(@bucket.instance_variable_get(:@props)).to be_nil + expect(@backend).to receive(:get_bucket_props).with(@bucket, { }).and_return({"name" => "foo"}) + expect(@bucket.props).to eq({"name" => "foo"}) end it "should memoize fetched properties" do - @backend.should_receive(:get_bucket_props).once.with(@bucket).and_return({"name" => "foo"}) - @bucket.props.should == {"name" => "foo"} - @bucket.props.should == {"name" => "foo"} + expect(@backend).to receive(:get_bucket_props).once.with(@bucket, { }).and_return({"name" => "foo"}) + expect(@bucket.props).to eq({"name" => "foo"}) + expect(@bucket.props).to eq({"name" => "foo"}) end end describe "clearing the bucket properties" do it "should make the request and delete the internal properties cache" do - @client.should_receive(:clear_bucket_props).with(@bucket).and_return(true) - @bucket.clear_props.should be_true - @bucket.instance_variable_get(:@props).should be_nil + expect(@client).to receive(:clear_bucket_props).with(@bucket).and_return(true) + expect(@bucket.clear_props).to be_truthy + expect(@bucket.instance_variable_get(:@props)).to be_nil end end describe "fetching an object" do it "should fetch the object via the backend" do - @backend.should_receive(:fetch_object).with(@bucket, "db", {}).and_return(nil) + expect(@backend).to receive(:fetch_object).with(@bucket, "db", {}).and_return(nil) @bucket.get("db") end it "should use the specified R quroum" do - @backend.should_receive(:fetch_object).with(@bucket, "db", {:r => 2}).and_return(nil) + expect(@backend).to receive(:fetch_object).with(@bucket, "db", {:r => 2}).and_return(nil) @bucket.get("db", :r => 2) end it "should disallow fetching an object with a zero-length key" do ## TODO: This actually tests the Client object, but there is no suite @@ -132,140 +132,144 @@ end describe "creating a new blank object" do it "should instantiate the object with the given key, default to JSON" do obj = @bucket.new('bar') - obj.should be_kind_of(Riak::RObject) - obj.key.should == 'bar' - obj.content_type.should == 'application/json' + expect(obj).to be_kind_of(Riak::RObject) + expect(obj.key).to eq('bar') + expect(obj.content_type).to eq('application/json') end end describe "fetching or creating a new object" do + let(:not_found_error){ Riak::ProtobuffsFailedRequest.new :not_found, 'not found' } + let(:other_error){ Riak::ProtobuffsFailedRequest.new :server_error, 'server error' } + it "should return the existing object if present" do - @object = mock("RObject") - @backend.should_receive(:fetch_object).with(@bucket,"db", {}).and_return(@object) - @bucket.get_or_new('db').should == @object + @object = double("RObject") + expect(@backend).to receive(:fetch_object).with(@bucket,"db", {}).and_return(@object) + expect(@bucket.get_or_new('db')).to eq(@object) end it "should create a new blank object if the key does not exist" do - @backend.should_receive(:fetch_object).and_raise(Riak::HTTPFailedRequest.new(:get, 200, 404, {}, "File not found")) + expect(@backend).to receive(:fetch_object).and_raise(not_found_error) obj = @bucket.get_or_new('db') - obj.key.should == 'db' - obj.data.should be_blank + expect(obj.key).to eq('db') + expect(obj.data).to be_blank end it "should bubble up non-ok non-missing errors" do - @backend.should_receive(:fetch_object).and_raise(Riak::HTTPFailedRequest.new(:get, 200, 500, {}, "File not found")) - lambda { @bucket.get_or_new('db') }.should raise_error(Riak::HTTPFailedRequest) + expect(@backend).to receive(:fetch_object).and_raise(other_error) + expect { @bucket.get_or_new('db') }.to raise_error(Riak::ProtobuffsFailedRequest) end it "should pass along the given R quorum parameter" do - @object = mock("RObject") - @backend.should_receive(:fetch_object).with(@bucket,"db", {:r => "all"}).and_return(@object) - @bucket.get_or_new('db', :r => "all").should == @object + @object = double("RObject") + expect(@backend).to receive(:fetch_object).with(@bucket,"db", {:r => "all"}).and_return(@object) + expect(@bucket.get_or_new('db', :r => "all")).to eq(@object) end end describe "fetching multiple objects" do it 'should get each object individually' do - @object1 = mock('obj1') - @object2 = mock('obj2') - @bucket.should_receive(:[]).with('key1').and_return(@object1) - @bucket.should_receive(:[]).with('key2').and_return(@object2) + @object1 = double('obj1') + @object2 = double('obj2') + expect(@bucket).to receive(:[]).with('key1').and_return(@object1) + expect(@bucket).to receive(:[]).with('key2').and_return(@object2) @results = @bucket.get_many %w{key1 key2} - @results['key1'].should == @object1 - @results['key2'].should == @object2 + expect(@results['key1']).to eq(@object1) + expect(@results['key2']).to eq(@object2) end end describe "querying an index" do it "should list the matching keys" do - @backend. - should_receive(:get_index). + expect(@backend). + to receive(:get_index). with(@bucket, "test_bin", "testing", {return_terms: true}). and_return(Riak::IndexCollection.new_from_json({ 'results' => [ {'testing' => 'asdf'}, {'testing' => 'hjkl'}] }.to_json)) result = @bucket.get_index("test_bin", "testing", return_terms: true) - result.should be_a Riak::IndexCollection - result.to_a.should == %w{asdf hjkl} - result.with_terms.should == {'testing' => %w{asdf hjkl}} + expect(result).to be_a Riak::IndexCollection + expect(result.to_a).to eq(%w{asdf hjkl}) + expect(result.with_terms).to eq({'testing' => %w{asdf hjkl}}) end end describe "get/set allow_mult property" do before :each do - @backend.stub!(:get_bucket_props).and_return({"allow_mult" => false}) + allow(@backend).to receive(:get_bucket_props).and_return({"allow_mult" => false}) end it "should extract the allow_mult property" do - @bucket.allow_mult.should be_false + expect(@bucket.allow_mult).to be_falsey end it "should set the allow_mult property" do - @bucket.should_receive(:props=).with(hash_including('allow_mult' => true)) + expect(@bucket).to receive(:props=).with(hash_including('allow_mult' => true)) @bucket.allow_mult = true end end describe "get/set the N value" do before :each do - @backend.stub!(:get_bucket_props).and_return({"n_val" => 3}) + allow(@backend).to receive(:get_bucket_props).and_return({"n_val" => 3}) end it "should extract the N value" do - @bucket.n_value.should == 3 + expect(@bucket.n_value).to eq(3) end it "should set the N value" do - @bucket.should_receive(:props=).with(hash_including('n_val' => 1)) + expect(@bucket).to receive(:props=).with(hash_including('n_val' => 1)) @bucket.n_value = 1 end end [:r, :w, :dw, :rw].each do |q| describe "get/set the default #{q} quorum" do before :each do - @backend.stub!(:get_bucket_props).and_return({"r" => "quorum", "w" => "quorum", "dw" => "quorum", "rw" => "quorum"}) + allow(@backend).to receive(:get_bucket_props).and_return({"r" => "quorum", "w" => "quorum", "dw" => "quorum", "rw" => "quorum"}) end it "should extract the default #{q} quorum" do - @bucket.send(q).should == "quorum" + expect(@bucket.send(q)).to eq("quorum") end it "should set the #{q} quorum" do - @bucket.should_receive(:props=).with(hash_including("#{q}" => 1)) + expect(@bucket).to receive(:props=).with(hash_including("#{q}" => 1)) @bucket.send("#{q}=",1) end end end describe "checking whether a key exists" do it "should return true if the object does exist" do - @backend.should_receive(:fetch_object).and_return(mock) - @bucket.exists?("foo").should be_true + expect(@backend).to receive(:fetch_object).and_return(double) + expect(@bucket.exists?("foo")).to be_truthy end it "should return false if the object doesn't exist" do - @backend.should_receive(:fetch_object).and_raise(Riak::HTTPFailedRequest.new(:get, [200,300], 404, {}, "not found")) - @bucket.exists?("foo").should be_false + expect(@backend).to receive(:fetch_object). + and_raise(Riak::ProtobuffsFailedRequest.new(:not_found, "not found")) + expect(@bucket.exists?("foo")).to be_falsey end end describe "deleting an object" do it "should delete a key from within the bucket" do - @backend.should_receive(:delete_object).with(@bucket, "bar", {}) + expect(@backend).to receive(:delete_object).with(@bucket, "bar", {}) @bucket.delete('bar') end it "should use the specified RW quorum" do - @backend.should_receive(:delete_object).with(@bucket, "bar", {:rw => "all"}) + expect(@backend).to receive(:delete_object).with(@bucket, "bar", {:rw => "all"}) @bucket.delete('bar', :rw => "all") end end end