Commit 7cf4f80527fc8d5855e95607d71e9e7ddb713108

- Fixing Database#query temp view posting
- Database#reduce_query has been removed in favor of :map and (optional) :reduce
  argument to #query
  
118118 end
119119
120120 # Query this database via a _temp_view view
121 def query(fun, language = "javascript")
122 resp = Response.new(post("_temp_view", {"language" => language, "map" => fun}.to_json))
123 resp.to_document
124 end
125
126 # Query this database with a +map_fun+ and a +reduce_fun+ to get the reduced result
127 def reduce_query(map_fun, reduce_fun, language = "javascript")
128 resp = Response.new(post("_temp_view", {
121 def query(funs = {}, language = "javascript")
122 unless funs[:map]
123 raise ArgumentError.new("need at least a :map argument")
124 end
125 data = {
129126 "language" => language,
130 "map" => map_fun,
131 "reduce" => reduce_fun,
132 }.to_json))
127 "map" => funs[:map]
128 }
129 data["reduce"] = funs[:reduce] if funs[:reduce]
130 resp = Response.new(post("_temp_view", data.to_json))
133131 resp.to_document
134132 end
135133
  
2323 # application/json)
2424 def post(path, data, content_type="application/json")
2525 post = Net::HTTP::Post.new(path)
26 post["content-type"] = content_type
26 post["Content-Type"] = content_type
2727 post.body = data
2828 request(post)
2929 end
3333 # application/json)
3434 def put(path, data, content_type="application/json")
3535 put = Net::HTTP::Put.new(path)
36 put["content-type"] = content_type
36 put["Content-Type"] = content_type
3737 put.body = data
3838 request(put)
3939 end
  
170170 res = db.bulk_load([CouchObject::Document.new({"foo" => "bar"}), CouchObject::Document.new({"foo" => "baz"})])
171171 end
172172
173 it "raises argument error if no options is given to #query" do
174 db = CouchObject::Database.new(@uri, "foo")
175 proc{
176 db.query(:lulz => false)
177 }.should raise_error(ArgumentError)
178 end
179
173180 it "should query the temp view with a map function" do
174181 db = CouchObject::Database.new(@uri, "foo")
175182 resp = Struct.new(:body).new
185185 "language"=>"javascript",
186186 "map" => "function(doc){ map(null, doc) }"
187187 })).and_return(resp)
188 res = db.query("function(doc){ map(null, doc) }")
188 res = db.query(:map => "function(doc){ map(null, doc) }")
189189 res.should be_instance_of(CouchObject::Document)
190190 end
191191
198198 "map" => "function(doc){ map(null, doc) }",
199199 "reduce" => "function (keys, values) { return sum(values) }"
200200 })).and_return(resp)
201 res = db.reduce_query("function(doc){ map(null, doc) }", "function (keys, values) { return sum(values) }")
201 res = db.query(:map => "function(doc){ map(null, doc) }", :reduce => "function (keys, values) { return sum(values) }")
202202 res.should be_instance_of(CouchObject::Document)
203203 end
204204
  
7575 resp.code.should == 404
7676 end
7777
78 it "should query a database with a temp view" do
79 db = create_and_open_test_db
80 doc1 = CouchObject::Document.new("foo" => "bar", "data" => 1)
81 doc2 = CouchObject::Document.new("foo" => "baz", "data" => 3)
82 db.store(doc1)
83 db.store(doc2)
84 res = db.query(:map => "function(doc){ if(doc.foo == 'bar') emit(null, doc) }")
85 res.should be_instance_of CouchObject::Document
86 res.rows.size.should == 1
87 res.rows.first["value"]["foo"].should == "bar"
88 end
89
90 it "should query a database with a map/reduce" do
91 db = create_and_open_test_db
92 doc1 = CouchObject::Document.new("foo" => "bar", "data" => 1)
93 doc2 = CouchObject::Document.new("foo" => "baz", "data" => 3)
94 db.store(doc1)
95 db.store(doc2)
96 res = db.query(:map => "function(doc){ emit(null, doc.data) }",
97 :reduce => "function(key, values){ return sum(values) }")
98 res.should be_instance_of CouchObject::Document
99 res.result.should == 4
100 end
101
78102end
79103
80104
81describe "A Database and some UT8" do
105describe "A Database and some UTF8" do
82106 include IntegrationSpecHelper
83107 # Translated form couch_test.js
84108
  
5959 it "should POST with application/json as the Content-Type header" do
6060 Net::HTTP::Post.should_receive(:new).with("/foo").and_return(@mock_request)
6161 @mock_request.stub!(:body=)
62 @mock_request.should_receive(:[]=).with("content-type", "application/json")
62 @mock_request.should_receive(:[]=).with("Content-Type", "application/json")
6363 @server.connection.should_receive(:request).with(@mock_request).and_return("response")
6464 @server.post("/foo", "bar")
6565 end
6767 it "should PUT with application/json as the Content-Tyoe header" do
6868 Net::HTTP::Put.should_receive(:new).with("/foo").and_return(@mock_request)
6969 @mock_request.stub!(:body=)
70 @mock_request.should_receive(:[]=).with("content-type", "application/json")
70 @mock_request.should_receive(:[]=).with("Content-Type", "application/json")
7171 @server.connection.should_receive(:request).with(@mock_request).and_return("response")
7272 @server.put("/foo", "bar")
7373 end