require File.dirname(__FILE__) + '/persistable_helper.rb' describe CouchObject::Persistable, "timestamp related actions:" do before(:each) do @bike = Bike.new @timestampclass = WithTimeStamp.new @db = mock("mock db") content = HTTPResponse.new(JSON.unparse({ "_id" => "123BAC", "_rev" => "946B7D1C", "created_at" => Time.new, "updated_at" => Time.new, "class" => "WithTimeStamp", "attributes" => { "wheels" => 3 } })) CouchObject::Response.stub!(:body).and_return(content) @empty_response = {} @ok_response = {"ok" => true} @document_response = CouchObject::Response.new(content) end it "timestamps should be nil for classes that don't include them" do @bike.created_at.should == nil @bike.updated_at.should == nil end it "timestamps should be set when saving an object" do CouchObject::Database.should_receive(:open).and_return(@db) timestampclass_json = @timestampclass.to_json @timestampclass.stub!(:to_json).and_return(timestampclass_json) @db.should_receive(:post). with("", timestampclass_json).and_return(@document_response) @timestampclass.save("foo") @timestampclass.created_at.should_not == nil @timestampclass.updated_at.should_not == nil end it "should update updated_at when saving an object again" do CouchObject::Database.stub!(:open).and_return(@db) timestampclass_json = @timestampclass.to_json @timestampclass.stub!(:to_json).and_return(timestampclass_json) @db.should_receive(:post). with("", timestampclass_json).and_return(@document_response) @db.should_receive(:put). with("123BAC", timestampclass_json).and_return(@document_response) @timestampclass.save("foo") updated_at = @timestampclass.updated_at @timestampclass.save @timestampclass.updated_at.should_not == updated_at end it "loaded object should have their timestamps set" do CouchObject::Database.should_receive(:open).and_return(@db) @db.should_receive(:get).with("123BAC").and_return(@document_response) timestampclass = WithTimeStamp.get_by_id("123BAC", "foo") timestampclass.class.should == WithTimeStamp timestampclass.created_at.should_not == nil timestampclass.updated_at.should_not == nil end end