spec/ripple/persistence_spec.rb in ripple-1.0.0.beta vs spec/ripple/persistence_spec.rb in ripple-1.0.0.beta2
- old
+ new
@@ -2,31 +2,29 @@
describe Ripple::Document::Persistence do
# require 'support/models/widget'
before :each do
- @backend = mock("Backend")
@client = Ripple.client
- @client.stub!(:backend).and_return(@backend)
@bucket = Ripple.client.bucket("widgets")
@widget = Widget.new(:size => 1000)
end
it "forces the content type to 'application/json'" do
@widget.robject.content_type = 'application/not-json'
- @backend.should_receive(:store_object) do |obj, *_|
+ @client.should_receive(:store_object) do |obj, *_|
obj.content_type.should == 'application/json'
end
@widget.save
end
it "should save a new object to Riak" do
- json = @widget.attributes.merge("_type" => "Widget").to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge("_type" => "Widget")
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget.save
@@ -34,13 +32,13 @@
@widget.should_not be_a_new_record
@widget.changes.should be_blank
end
it "should modify attributes and save a new object" do
- json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge("_type" => "Widget", "size" => 5)
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget.update_attributes(:size => 5)
@@ -48,13 +46,13 @@
@widget.should_not be_a_new_record
@widget.changes.should be_blank
end
it "should modify a single attribute and save a new object" do
- json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge("_type" => "Widget", "size" => 5)
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget.update_attribute(:size, 5)
@@ -63,27 +61,27 @@
@widget.changes.should be_blank
@widget.size.should == 5
end
it "should instantiate and save a new object to riak" do
- json = @widget.attributes.merge(:size => 10, :shipped_at => "2000-01-01T20:15:01Z", :_type => 'Widget').to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge(:size => 10, :shipped_at => Time.utc(2000,"jan",1,20,15,1), :_type => 'Widget')
+ @client.should_receive(:store_object) do |obj, _, _, _|
obj.key.should be_nil
+ obj.data.should == json
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget = Widget.create(:size => 10, :shipped_at => Time.utc(2000,"jan",1,20,15,1))
@widget.size.should == 10
@widget.shipped_at.should == Time.utc(2000,"jan",1,20,15,1)
@widget.should_not be_a_new_record
end
it "should instantiate and save a new object to riak and allow its attributes to be set via a block" do
- json = @widget.attributes.merge(:size => 10, :_type => 'Widget').to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge(:size => 10, :_type => 'Widget')
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget = Widget.create do |widget|
@@ -93,11 +91,11 @@
@widget.should_not be_a_new_record
end
it "should save the attributes not having a corresponding property" do
attrs = @widget.attributes.merge("_type" => "Widget", "unknown_property" => "a_value")
- @backend.should_receive(:store_object) do |obj, _, _, _|
+ @client.should_receive(:store_object) do |obj, _, _, _|
obj.data.should == attrs
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@@ -107,26 +105,26 @@
@widget.should_not be_a_new_record
@widget.changes.should be_blank
end
it "should allow unexpected exceptions to be raised" do
- robject = mock("robject", :key => @widget.key, "data=" => true, "content_type=" => true)
+ robject = mock("robject", :key => @widget.key, "data=" => true, "content_type=" => true, "indexes=" => true)
robject.should_receive(:store).and_raise(Riak::HTTPFailedRequest.new(:post, 200, 404, {}, "404 not found"))
@widget.stub!(:robject).and_return(robject)
lambda { @widget.save }.should raise_error(Riak::FailedRequest)
end
it "should reload a saved object, including associations" do
- json = @widget.attributes.merge(:_type => "Widget").to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @widget.attributes.merge(:_type => "Widget")
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key.should be_nil
# Simulate loading the response with the key
obj.key = "new_widget"
end
@widget.save
- @backend.should_receive(:reload_object) do |obj, _|
+ @client.should_receive(:reload_object) do |obj, _|
obj.key.should == "new_widget"
obj.content_type = 'application/json'
obj.raw_data = '{"name":"spring","size":10,"shipped_at":"Sat, 01 Jan 2000 20:15:01 -0000","_type":"Widget"}'
obj
end
@@ -138,44 +136,69 @@
@widget.size.should == 10
@widget.shipped_at.should == Time.utc(2000,"jan",1,20,15,1)
end
it "should destroy a saved object" do
- @backend.should_receive(:store_object).and_return(true)
+ @client.should_receive(:store_object).and_return(true)
@widget.key = "foo"
@widget.save
@widget.should_not be_new
- @backend.should_receive(:delete_object).and_return(true)
+ @client.should_receive(:delete_object).and_return(true)
@widget.destroy.should be_true
@widget.should be_frozen
+ @widget.should be_deleted
end
it "should destroy all saved objects" do
@widget.should_receive(:destroy).and_return(true)
Widget.should_receive(:list).and_yield(@widget)
Widget.destroy_all.should be_true
end
+ context 'when a delete fails' do
+ let(:error) { Riak::FailedRequest.new("Riak could not delete your object") }
+ before(:each) do
+ @widget.stub(:new? => false)
+ @widget.robject.should_receive(:delete).and_raise(error)
+ end
+
+ it 'causes destroy to return false' do
+ @widget.destroy.should be_false
+ end
+
+ it 'causes destroy! to raise an error' do
+ expect {
+ @widget.destroy!
+ }.to raise_error(Riak::FailedRequest)
+ end
+ end
+
it "should freeze an unsaved object when destroying" do
- @backend.should_not_receive(:delete_object)
+ @client.should_not_receive(:delete_object)
@widget.destroy.should be_true
@widget.should be_frozen
end
+ it "should be able to call #errors after destroying" do
+ @widget.destroy.should be_true
+ @widget.should be_frozen
+ expect { @widget.errors }.to_not raise_error
+ end
+
it "should be a root document" do
@widget._root_document.should == @widget
end
describe "when storing a class using single-bucket inheritance" do
before :each do
@cog = Cog.new(:size => 1000)
end
it "should store the _type field as the class name" do
- json = @cog.attributes.merge("_type" => "Cog").to_json
- @backend.should_receive(:store_object) do |obj, _, _, _|
- obj.raw_data.should == json
+ json = @cog.attributes.merge("_type" => "Cog")
+ @client.should_receive(:store_object) do |obj, _, _, _|
+ obj.data.should == json
obj.key = "new_widget"
end
@cog.save
@cog.should_not be_new_record
end
@@ -211,11 +234,11 @@
end
end
shared_examples_for "saving a parent document with linked child documents" do
before(:each) do
- @backend.stub(:store_object)
+ @client.stub(:store_object)
end
it 'saves new children when the parent is saved' do
children.each do |child|
child.stub(:new? => true)
@@ -277,10 +300,10 @@
end
end
shared_examples_for "embedded association persistence logic" do
before(:each) do
- @backend.stub(:store_object)
+ @client.stub(:store_object)
end
it "does not save children when the parent is saved" do
children.each do |child|
child.stub(:new? => true, :changed? => true)