spec/riak/object_spec.rb in riak-client-0.8.0 vs spec/riak/object_spec.rb in riak-client-0.8.1

- old
+ new

@@ -116,10 +116,68 @@ @object.deserialize(@payload).should == {"foo" => "bar"} end end end + describe "data access methods" do + before :each do + @object = Riak::RObject.new(@bucket, "bar") + @object.content_type = "application/json" + end + + describe "for raw data" do + describe "when unserialized data was already provided" do + before do + @object.data = { :some => :data } + end + + it "should reset unserialized forms when stored" do + @object.raw_data = value = '{ "raw": "json" }' + + @object.raw_data.should == value + @object.data.should == { "raw" => "json" } + end + + it "should lazily serialize when read" do + @object.raw_data.should == '{"some":"data"}' + end + end + + it "should not unnecessarily marshal/demarshal" do + @object.should_not_receive(:serialize) + @object.should_not_receive(:deserialize) + @object.raw_data = value = "{not even valid json!}}" + @object.raw_data.should == value + end + end + + describe "for unserialized data" do + describe "when raw data was already provided" do + before do + @object.raw_data = '{"some":"data"}' + end + + it "should reset previously stored raw data" do + @object.data = value = { "new" => "data" } + @object.raw_data.should == '{"new":"data"}' + @object.data.should == value + end + + it "should lazily deserialize when read" do + @object.data.should == { "some" => "data" } + end + end + + it "should not unnecessarily marshal/demarshal" do + @object.should_not_receive(:serialize) + @object.should_not_receive(:deserialize) + @object.data = value = { "some" => "data" } + @object.data.should == value + end + end + end + describe "loading data from the response" do before :each do @object = Riak::RObject.new(@bucket, "bar") end @@ -128,13 +186,20 @@ @object.content_type.should == "application/json" end it "should load the body data" do @object.load({:headers => {"content-type" => ["application/json"]}, :body => '{"foo":"bar"}'}) + @object.raw_data.should be_present @object.data.should be_present end + it "should handle raw data properly" do + @object.should_not_receive(:deserialize) # optimize for the raw_data case, don't penalize people for using raw_data + @object.load({:headers => {"content-type" => ["application/json"]}, :body => body = '{"foo":"bar"}'}) + @object.raw_data.should == body + end + it "should deserialize the body data" do @object.should_receive(:deserialize).with("{}").and_return({}) @object.load({:headers => {"content-type" => ["application/json"]}, :body => "{}"}) @object.data.should == {} end @@ -333,10 +398,27 @@ it "should exclude the vclock when nil" do @object.vclock = nil @object.store_headers.should_not have_key("X-Riak-Vclock") end + describe "when conditional PUTs are requested" do + before :each do + @object.prevent_stale_writes = true + end + + it "should include an If-None-Match: * header" do + @object.store_headers.should have_key("If-None-Match") + @object.store_headers["If-None-Match"].should == "*" + end + + it "should include an If-Match header with the etag when an etag is present" do + @object.etag = "foobar" + @object.store_headers.should have_key("If-Match") + @object.store_headers["If-Match"].should == @object.etag + end + end + describe "when links are defined" do before :each do @object.links << Riak::Link.new("/riak/foo/baz", "next") end @@ -443,9 +525,25 @@ end describe "when the object has a key" do before :each do @object.key = "bar" + end + + describe "when the content is of a known serializable type" do + before :each do + @object.content_type = "application/json" + @headers = @object.store_headers + end + + it "should not serialize content if #raw_data is used" do + storing = @object.raw_data = "{this is probably invalid json}}" + @http.should_receive(:put).with([200,204,300], "/riak/", "foo/bar", {:returnbody => true}, storing, @headers).and_return({:headers => {"x-riak-vclock" => ["areallylonghashvalue"]}, :code => 204}) + @object.should_not_receive(:serialize) + @object.should_not_receive(:deserialize) + @object.store + @object.raw_data.should == storing + end end it "should issue a PUT request to the bucket, and update the object properties (returning the body by default)" do @http.should_receive(:put).with([200,204,300], "/riak/", "foo/bar", {:returnbody => true}, "This is some text.", @headers).and_return({:headers => {'location' => ["/riak/foo/somereallylongstring"], "x-riak-vclock" => ["areallylonghashvalue"]}, :code => 204}) @object.store