- Added Database#filter{|doc| .. } for doing temp queries with ruby
[couchobject.git] / spec / persistable_spec.rb
blob8c6a8a1dc6de9f8bab42a4233d21d57539debf9c
1 require File.dirname(__FILE__) + '/spec_helper.rb'
3 class Bike
4   include CouchObject::Persistable
5   
6   def initialize
7     @wheels = 2
8   end
9   attr_accessor :wheels
10   
11   def to_couch
12     {:wheels => @wheels}
13   end
14   
15   def self.from_couch(attributes)
16     bike = new
17     bike.wheels = attributes["wheels"]
18     bike
19   end
20 end
22 describe CouchObject::Persistable, "when mixed into a Class" do
23   before(:each) do
24     @bike = Bike.new
25     @db = mock("mock db")
26     
27     @empty_response = {}    
28     @ok_response = {"ok" => true}
29     @document_response = {
30       "_id" => "123BAC", 
31       "_rev" => "946B7D1C", 
32       "attributes" => {
33         "wheels" => 3
34       }
35     }
36   end
37   
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)
41     @bike.save("foo")
42   end
43   
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)
47   end
48   
49   it "should raise if no to_couch on class" do
50     klass = Class.new{ 
51       include CouchObject::Persistable 
52       def to_couch() end
53     }
54     proc{ klass.get_by_id("foo", "bar") }.should raise_error(CouchObject::Persistable::NoFromCouchMethodError)
55   end
56   
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"
61   end  
62   
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)
66     @bike.save("foo")
67     @bike.id.should == "123BAC"
68   end
69   
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
74     @bike.save("foo")
75     @bike.new?.should == false
76   end
77   
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")
82   end
83   
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
90   end
91 end