1 require File.dirname(__FILE__) + '/../spec_helper.rb'
2 require File.dirname(__FILE__) + "/integration_helper"
4 describe "Database operations" do
5 include IntegrationSpecHelper
11 it "should create a database " do
12 all_dbs = proc{ CouchObject::Database.all_databases("http://localhost:8888") }
13 all_dbs.call.include?("couchobject_test").should == false
15 create_test_db.should == {"ok" => true}
17 all_dbs.call.include?("couchobject_test").should == true
20 it "should delete a database" do
21 all_dbs = proc{ CouchObject::Database.all_databases("http://localhost:8888") }
22 all_dbs.call.include?("couchobject_test").should == false
24 create_test_db.should == {"ok" => true}
25 CouchObject::Database.delete!(
26 "http://localhost:8888",
28 ).should == {"ok" => true}
30 all_dbs.call.include?("couchobject_test").should == false
33 it "should open a db connection and know the dbname and uri" do
35 db.name.should == "couchobject_test"
36 db.url.should == "http://localhost:8888/couchobject_test"
39 it "should GET a non existing document and return 404" do
41 response = db.get("roflcopters")
42 response.code.should == 404
45 it "should POST a new document successfully" do
46 db = create_and_open_test_db
47 response = db.post("", JSON.unparse({"foo" => ["bar", "baz"]}))
48 doc = response.to_document
49 doc.ok?.should == true
50 doc.id.should_not == nil
53 it "should PUT to update and existing document" do
54 db = create_and_open_test_db
55 response = db.post("", JSON.unparse({"foo" => ["bar", "baz"]}))
56 created_doc = response.to_document
57 response = db.put(created_doc.id, JSON.unparse(
58 {"foo" => [1, 2]}.merge("_rev" => created_doc.revision)
60 response.to_document.ok?.should == true
62 updated_doc = db.get(created_doc.id).to_document
63 updated_doc.foo.should == [1,2]
66 it "should DELETE to delete an existing document" do
67 db = create_and_open_test_db
68 response = db.post("", JSON.unparse({"foo" => ["bar", "baz"]}))
69 created_doc = response.to_document
71 resp = db.delete(created_doc.id)
72 resp.code.should == 202
74 resp = db.get(created_doc.id)
75 resp.code.should == 404
78 it "should filter documents" do
79 db = create_and_open_test_db
80 db.post("", JSON.unparse({"foo" => "bar"}))
81 db.post("", JSON.unparse({"foo" => "baz"}))
82 db.post("", JSON.unparse({"foo" => "qux"}))
83 db.all_documents.size.should == 3
84 results = db.filter do |doc|
87 results.size.should == 2