- Added Database#filter{|doc| .. } for doing temp queries with ruby
[couchobject.git] / spec / integration / database_integration_spec.rb
blob804cede4fe338b5f40fd18db969deec81cb34712
1 require File.dirname(__FILE__) + '/../spec_helper.rb'
2 require File.dirname(__FILE__) + "/integration_helper"
4 describe "Database operations" do
5   include IntegrationSpecHelper
6   
7   before(:each) do
8     delete_test_db
9   end
10   
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
14     
15     create_test_db.should == {"ok" => true}
16     
17     all_dbs.call.include?("couchobject_test").should == true
18   end
19   
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
23     
24     create_test_db.should == {"ok" => true}
25     CouchObject::Database.delete!(
26       "http://localhost:8888", 
27       "couchobject_test"
28     ).should == {"ok" => true}
29     
30     all_dbs.call.include?("couchobject_test").should == false
31   end
32   
33   it "should open a db connection and know the dbname and uri" do
34     db = open_test_db
35     db.name.should == "couchobject_test"
36     db.url.should == "http://localhost:8888/couchobject_test"
37   end
38   
39   it "should GET a non existing document and return 404" do
40     db = open_test_db
41     response = db.get("roflcopters")
42     response.code.should == 404
43   end
44   
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
51   end
52   
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)
59     ))
60     response.to_document.ok?.should == true
61     
62     updated_doc = db.get(created_doc.id).to_document
63     updated_doc.foo.should == [1,2]
64   end
65   
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
70     
71     resp = db.delete(created_doc.id)
72     resp.code.should == 202
73     
74     resp = db.get(created_doc.id)
75     resp.code.should == 404
76   end
77   
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|
85       doc["foo"] =~ /ba/
86     end
87     results.size.should == 2
88     
89   end
90   
91 end