1 require File.dirname(__FILE__) + '/spec_helper.rb'
3 describe CouchObject::Database 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 = {
20 it "should create a database" do
21 @server.should_receive(:put).with("/foo", "").and_return(@response)
22 CouchObject::Database.create!(@uri, "foo")
25 it "should delete a database" do
26 @server.should_receive(:delete).with("/foo").and_return(@response)
27 CouchObject::Database.delete!(@uri, "foo")
30 it "should get all databases" do
31 @server.should_receive(:get).with("/_all_dbs").and_return(@response)
32 CouchObject::Database.all_databases(@uri)
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"]
41 it "should open a connection to the server" do
42 db = CouchObject::Database.new(@uri, "foo")
43 db.server.should == @server
46 it "should have a name" do
47 db = CouchObject::Database.new(@uri, "foo")
48 db.name.should == "foo"
51 it "should lint the database name from slashes with ::open" do
52 db = CouchObject::Database.open("http://localhost:8888/foo")
56 db.dbname.should == "foo"
60 db = CouchObject::Database.new(@uri, "foo")
61 @server.should_receive(:get).with("/foo/123").and_return(@response)
66 db = CouchObject::Database.new(@uri, "foo")
67 @server.should_receive(:post).with("/foo/123", "postdata").and_return(@response)
68 db.post("123", "postdata")
72 db = CouchObject::Database.new(@uri, "foo")
73 @server.should_receive(:put).with("/foo/123", "postdata").and_return(@response)
74 db.put("123", "postdata")
78 db = CouchObject::Database.new(@uri, "foo")
79 @server.should_receive(:delete).with("/foo/123").and_return(@response)
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
87 it "should know the database name" do
88 db = CouchObject::Database.new(@uri, "foo")
89 db.name.should == "foo"
92 it "should know the full uri" do
93 db = CouchObject::Database.new(@uri, "foo")
94 db.url.should == "http://localhost:8888/foo"
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
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)
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
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
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)
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"}])
138 db.should_receive(:get).with("_all_docs").and_return(resp)
139 db.all_documents.should == [{"_rev"=>123, "_id"=>"123ABC"}]
142 #it "should url encode paths"