1 require File.dirname(__FILE__) + '/spec_helper.rb'
4 include CouchObject::Persistable
15 def self.from_couch(attributes)
17 bike.wheels = attributes["wheels"]
22 describe CouchObject::Persistable, "when mixed into a Class" do
28 @ok_response = {"ok" => true}
29 @document_response = {
38 it "should give the class a save method that saves the object" do
39 CouchObject::Database.should_receive(:open).and_return(@db)
40 @db.should_receive(:post).with("", @bike.to_json).and_return(@empty_response)
44 it "should raise if no to_couch on class" do
45 klass = Class.new{ include CouchObject::Persistable }
46 proc{ klass.new.to_json }.should raise_error(CouchObject::Persistable::NoToCouchMethodError)
49 it "should raise if no to_couch on class" do
51 include CouchObject::Persistable
54 proc{ klass.get_by_id("foo", "bar") }.should raise_error(CouchObject::Persistable::NoFromCouchMethodError)
57 it "should return the doc id on successfull save" do
58 CouchObject::Database.should_receive(:open).and_return(@db)
59 @db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
60 @bike.save("foo")["_id"].should == "123BAC"
63 it "should assign the returned id to itself on successful save" do
64 CouchObject::Database.should_receive(:open).and_return(@db)
65 @db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
67 @bike.id.should == "123BAC"
70 it "should know if itself is a new object" do
71 CouchObject::Database.should_receive(:open).and_return(@db)
72 @db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
73 @bike.new?.should == true
75 @bike.new?.should == false
78 it "should get document by id" do
79 CouchObject::Database.should_receive(:open).and_return(@db)
80 @db.should_receive(:get).with("123BAC").and_return(@document_response)
81 Bike.get_by_id("foo", "123BAC")
84 it "should instantiate a new object based on their #to_couch" do
85 CouchObject::Database.should_receive(:open).and_return(@db)
86 @db.should_receive(:get).with("123BAC").and_return(@document_response)
87 bike = Bike.get_by_id("foo", "123BAC")
88 bike.class.should == Bike
89 bike.wheels.should == 3