spec/riak/bucket_spec.rb in ripple-0.5.1 vs spec/riak/bucket_spec.rb in ripple-0.6.0
- old
+ new
@@ -23,11 +23,11 @@
@bucket.load({
:body => '{"props":{"name":"foo","allow_mult":false,"big_vclock":50,"chash_keyfun":{"mod":"riak_util","fun":"chash_std_keyfun"},"linkfun":{"mod":"jiak_object","fun":"mapreduce_linkfun"},"n_val":3,"old_vclock":86400,"small_vclock":10,"young_vclock":20},"keys":["bar"]}',
:headers => {
"vary" => ["Accept-Encoding"],
"server" => ["MochiWeb/1.1 WebMachine/1.5.1 (hack the charles gibson)"],
- "link" => ['</raw/foo/bar>; riaktag="contained"'],
+ "link" => ['</riak/foo/bar>; riaktag="contained"'],
"date" => ["Tue, 12 Jan 2010 15:30:43 GMT"],
"content-type" => ["application/json"],
"content-length" => ["257"]
}
}.merge(overrides))
@@ -78,32 +78,32 @@
@http = mock("HTTPBackend")
@client.stub!(:http).and_return(@http)
end
it "should load the keys if not present" do
- @http.should_receive(:get).with(200, "/raw/", "foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar"]}'})
+ @http.should_receive(:get).with(200, "/riak/", "foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar"]}'})
@bucket.keys.should == ["bar"]
end
it "should allow reloading of the keys" do
- @http.should_receive(:get).with(200, "/raw/","foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar"]}'})
+ @http.should_receive(:get).with(200, "/riak/","foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar"]}'})
do_load # Ensures they're already loaded
@bucket.keys(:reload => true).should == ["bar"]
end
it "should allow streaming keys through block" do
# pending "Needs support in the raw_http_resource"
- @http.should_receive(:get).with(200, "/raw/","foo", {:props => false}, {}).and_yield("{}").and_yield('{"keys":[]}').and_yield('{"keys":["bar"]}').and_yield('{"keys":["baz"]}')
+ @http.should_receive(:get).with(200, "/riak/","foo", {:props => false}, {}).and_yield("{}").and_yield('{"keys":[]}').and_yield('{"keys":["bar"]}').and_yield('{"keys":["baz"]}')
all_keys = []
@bucket.keys do |list|
all_keys.concat(list)
end
all_keys.should == ["bar", "baz"]
end
it "should unescape key names" do
- @http.should_receive(:get).with(200, "/raw/","foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar%20baz"]}'})
+ @http.should_receive(:get).with(200, "/riak/","foo", {:props => false}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"keys":["bar%20baz"]}'})
@bucket.keys.should == ["bar baz"]
end
end
describe "setting the bucket properties" do
@@ -111,11 +111,11 @@
@http = mock("HTTPBackend")
@client.stub!(:http).and_return(@http)
end
it "should PUT the new properties to the bucket" do
- @http.should_receive(:put).with(204, "/raw/","foo", '{"props":{"name":"foo"}}', {"Content-Type" => "application/json"}).and_return({:body => "", :headers => {}})
+ @http.should_receive(:put).with(204, "/riak/","foo", '{"props":{"name":"foo"}}', {"Content-Type" => "application/json"}).and_return({:body => "", :headers => {}})
@bucket.props = { :name => "foo" }
end
it "should raise an error if an invalid property is given" do
lambda { @bucket.props = "blah" }.should raise_error(ArgumentError)
@@ -127,15 +127,86 @@
@http = mock("HTTPBackend")
@client.stub!(:http).and_return(@http)
end
it "should load the object from the server as a Riak::RObject" do
- @http.should_receive(:get).with(200, "/raw/","foo", "db", {}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
+ @http.should_receive(:get).with(200, "/riak/","foo", "db", {}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
@bucket.get("db").should be_kind_of(Riak::RObject)
end
it "should use the given query parameters (for R value, etc)" do
- @http.should_receive(:get).with(200, "/raw/","foo", "db", {:r => 2}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
+ @http.should_receive(:get).with(200, "/riak/","foo", "db", {:r => 2}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
@bucket.get("db", :r => 2).should be_kind_of(Riak::RObject)
+ end
+
+ it "should allow 300 responses if allow_mult is set" do
+ @bucket.instance_variable_set(:@props, {'allow_mult' => true})
+ @http.should_receive(:get).with([200,300], "/riak/","foo", "db", {}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
+ @bucket.get('db')
+ end
+ 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'
+ end
+ end
+
+ describe "fetching or creating a new object" do
+ before :each do
+ @http = mock("HTTPBackend")
+ @client.stub!(:http).and_return(@http)
+ end
+
+ it "should return the existing object if present" do
+ @http.should_receive(:get).with(200, "/riak/","foo", "db", {}, {}).and_return({:headers => {"content-type" => ["application/json"]}, :body => '{"name":"Riak","company":"Basho"}'})
+ obj = @bucket.get_or_new('db')
+ obj.key.should == 'db'
+ obj.data['name'].should == "Riak"
+ end
+
+ it "should create a new blank object if the key does not exist" do
+ @http.should_receive(:get).and_raise(Riak::FailedRequest.new(:get, 200, 404, {}, "File not found"))
+ obj = @bucket.get_or_new('db')
+ obj.key.should == 'db'
+ obj.data.should be_blank
+ end
+
+ it "should bubble up non-ok non-missing errors" do
+ @http.should_receive(:get).and_raise(Riak::FailedRequest.new(:get, 200, 500, {}, "File not found"))
+ lambda { @bucket.get_or_new('db') }.should raise_error(Riak::FailedRequest)
+ end
+ end
+
+ describe "get/set allow_mult property" do
+ before :each do
+ do_load
+ end
+
+ it "should extract the allow_mult property" do
+ @bucket.allow_mult.should be_false
+ end
+
+ it "should set the allow_mult property" do
+ @bucket.should_receive(:props=).with(hash_including('allow_mult' => true))
+ @bucket.allow_mult = true
+ end
+ end
+
+ describe "get/set the N value" do
+ before :each do
+ do_load
+ end
+
+ it "should extract the N value" do
+ @bucket.n_value.should == 3
+ end
+
+ it "should set the N value" do
+ @bucket.should_receive(:props=).with(hash_including('n_val' => 1))
+ @bucket.n_value = 1
end
end
end