- set @revision too when creating a Document
[couchobject.git] / spec / database_spec.rb
blob6ee24937b481ae5a8f562e42722e8584fa6ea1ba
1 require File.dirname(__FILE__) + '/spec_helper.rb'
3 describe CouchObject::Database do
4   before(:each) do
5     @server = mock("Couch server")
6     @uri = "http://localhost:8888"
7     @response = mock("Net::HTTP::Response")
8     CouchObject::Server.should_receive(:new).with(@uri).and_return(@server)
9     @response.stub!(:code).and_return(200)
10     @response.stub!(:body).and_return("[\"db1\", \"db2\"]")
11     @document_response = {
12       "_id" => "123BAC", 
13       "_rev" => "946B7D1C", 
14       "attributes" => {
15         "wheels" => 3
16       }
17     }
18   end
19   
20   it "should create a database" do
21     @server.should_receive(:put).with("/foo", "").and_return(@response)
22     CouchObject::Database.create!(@uri, "foo")
23   end
24   
25   it "should delete a database" do
26     @server.should_receive(:delete).with("/foo").and_return(@response)
27     CouchObject::Database.delete!(@uri, "foo")
28   end
29   
30   it "should get all databases" do
31     @server.should_receive(:get).with("/_all_dbs").and_return(@response)
32     CouchObject::Database.all_databases(@uri)
33   end
34   
35   it "should return all databases as an array" do
36     @server.should_receive(:get).with("/_all_dbs").and_return(@response)
37     dbs = CouchObject::Database.all_databases(@uri)
38     dbs.should == ["db1", "db2"]
39   end
40   
41   it "should open a connection to the server" do
42     db = CouchObject::Database.new(@uri, "foo")
43     db.server.should == @server
44   end
45   
46   it "should have a name" do
47     db = CouchObject::Database.new(@uri, "foo")
48     db.name.should == "foo"    
49   end
50   
51   it "should lint the database name from slashes with ::open" do
52     db = CouchObject::Database.open("http://localhost:8888/foo")
53     class << db
54       attr_accessor :dbname
55     end
56     db.dbname.should == "foo"
57   end
58   
59   it "should GET" do
60     db = CouchObject::Database.new(@uri, "foo")    
61     @server.should_receive(:get).with("/foo/123").and_return(@response)
62     db.get("123")
63   end
64   
65   it "should POST" do
66     db = CouchObject::Database.new(@uri, "foo")        
67     @server.should_receive(:post).with("/foo/123", "postdata").and_return(@response)
68     db.post("123", "postdata")
69   end
70   
71   it "should PUT" do
72     db = CouchObject::Database.new(@uri, "foo")        
73     @server.should_receive(:put).with("/foo/123", "postdata").and_return(@response)
74     db.put("123", "postdata")
75   end
76   
77   it "should DELETE" do
78     db = CouchObject::Database.new(@uri, "foo")        
79     @server.should_receive(:delete).with("/foo/123").and_return(@response)
80     db.delete("123")
81   end
82   
83   it "should open a new connection from a full uri spec" do
84     proc{ CouchObject::Database.open("http://localhost:8888/foo") }.should_not raise_error
85   end
86   
87   it "should know the database name" do
88     db = CouchObject::Database.new(@uri, "foo")
89     db.name.should == "foo"    
90   end
91   
92   it "should know the full uri" do
93     db = CouchObject::Database.new(@uri, "foo")
94     db.url.should == "http://localhost:8888/foo"    
95   end
96   
97   it "should load a document from id with #[]" do
98     db = CouchObject::Database.new(@uri, "foo")
99     db.should_receive(:get).with("123").twice.and_return(@document_response)
100     proc{ db["123"] }.should_not raise_error
101     db["123"].should == @document_response
102   end
103   
104   it "should accept symbols for #[] too" do
105     db = CouchObject::Database.new(@uri, "foo")
106     db.should_receive(:get).with("foo").and_return(@document_response)
107     db[:foo]
108   end
109   
110   it "should get a document by id" do
111     db = CouchObject::Database.new(@uri, "foo")
112     db.should_receive(:get).with("foo").twice.and_return(@document_response)
113     proc{ db.document("foo") }.should_not raise_error
114     db.document("foo").should == @document_response
115   end
116   
117   it "should get a document by id and revision" do
118     db = CouchObject::Database.new(@uri, "foo")
119     db.should_receive(:get).with("foo?rev=123").twice.and_return(@document_response)
120     proc{ db.document("foo", "123") }.should_not raise_error
121     db.document("foo", "123").should == @document_response
122   end
123   
124   it "should query the view" do
125     db = CouchObject::Database.new(@uri, "foo")
126     CouchObject::View.should_receive(:new).with(db, "myview").and_return(view = mock("View mock"))
127     view.should_receive(:query).and_return(nil)
128     db.views("myview")
129   end
130   
131   it "should get a list of all documents" do
132     db = CouchObject::Database.new(@uri, "foo")
133     resp = mock("response")
134     resp.stub!(:body).and_return(JSON.unparse("rows" => [{"_rev"=>123, "_id"=>"123ABC"}]))
135     resp.stub!(:to_document).and_return(
136       CouchObject::Document.new("rows" => [{"_rev"=>123, "_id"=>"123ABC"}])
137     )
138     db.should_receive(:get).with("_all_docs").and_return(resp)
139     db.all_documents.should == [{"_rev"=>123, "_id"=>"123ABC"}]
140   end
141   
142   #it "should url encode paths"