Commit 7cf4f80527fc8d5855e95607d71e9e7ddb713108
- Diff rendering mode:
- inline
- side by side
lib/couch_object/database.rb
(9 / 11)
|   | |||
| 118 | 118 | end | |
| 119 | 119 | ||
| 120 | 120 | # 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 = { | ||
| 129 | 126 | "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)) | ||
| 133 | 131 | resp.to_document | |
| 134 | 132 | end | |
| 135 | 133 |
lib/couch_object/server.rb
(2 / 2)
|   | |||
| 23 | 23 | # application/json) | |
| 24 | 24 | def post(path, data, content_type="application/json") | |
| 25 | 25 | post = Net::HTTP::Post.new(path) | |
| 26 | post["content-type"] = content_type | ||
| 26 | post["Content-Type"] = content_type | ||
| 27 | 27 | post.body = data | |
| 28 | 28 | request(post) | |
| 29 | 29 | end | |
| … | … | ||
| 33 | 33 | # application/json) | |
| 34 | 34 | def put(path, data, content_type="application/json") | |
| 35 | 35 | put = Net::HTTP::Put.new(path) | |
| 36 | put["content-type"] = content_type | ||
| 36 | put["Content-Type"] = content_type | ||
| 37 | 37 | put.body = data | |
| 38 | 38 | request(put) | |
| 39 | 39 | end |
spec/database_spec.rb
(9 / 2)
|   | |||
| 170 | 170 | res = db.bulk_load([CouchObject::Document.new({"foo" => "bar"}), CouchObject::Document.new({"foo" => "baz"})]) | |
| 171 | 171 | end | |
| 172 | 172 | ||
| 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 | |||
| 173 | 180 | it "should query the temp view with a map function" do | |
| 174 | 181 | db = CouchObject::Database.new(@uri, "foo") | |
| 175 | 182 | resp = Struct.new(:body).new | |
| … | … | ||
| 185 | 185 | "language"=>"javascript", | |
| 186 | 186 | "map" => "function(doc){ map(null, doc) }" | |
| 187 | 187 | })).and_return(resp) | |
| 188 | res = db.query("function(doc){ map(null, doc) }") | ||
| 188 | res = db.query(:map => "function(doc){ map(null, doc) }") | ||
| 189 | 189 | res.should be_instance_of(CouchObject::Document) | |
| 190 | 190 | end | |
| 191 | 191 | ||
| … | … | ||
| 198 | 198 | "map" => "function(doc){ map(null, doc) }", | |
| 199 | 199 | "reduce" => "function (keys, values) { return sum(values) }" | |
| 200 | 200 | })).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) }") | ||
| 202 | 202 | res.should be_instance_of(CouchObject::Document) | |
| 203 | 203 | end | |
| 204 | 204 |
|   | |||
| 75 | 75 | resp.code.should == 404 | |
| 76 | 76 | end | |
| 77 | 77 | ||
| 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 | |||
| 78 | 102 | end | |
| 79 | 103 | ||
| 80 | 104 | ||
| 81 | describe "A Database and some UT8" do | ||
| 105 | describe "A Database and some UTF8" do | ||
| 82 | 106 | include IntegrationSpecHelper | |
| 83 | 107 | # Translated form couch_test.js | |
| 84 | 108 |
spec/server_spec.rb
(2 / 2)
|   | |||
| 59 | 59 | it "should POST with application/json as the Content-Type header" do | |
| 60 | 60 | Net::HTTP::Post.should_receive(:new).with("/foo").and_return(@mock_request) | |
| 61 | 61 | @mock_request.stub!(:body=) | |
| 62 | @mock_request.should_receive(:[]=).with("content-type", "application/json") | ||
| 62 | @mock_request.should_receive(:[]=).with("Content-Type", "application/json") | ||
| 63 | 63 | @server.connection.should_receive(:request).with(@mock_request).and_return("response") | |
| 64 | 64 | @server.post("/foo", "bar") | |
| 65 | 65 | end | |
| … | … | ||
| 67 | 67 | it "should PUT with application/json as the Content-Tyoe header" do | |
| 68 | 68 | Net::HTTP::Put.should_receive(:new).with("/foo").and_return(@mock_request) | |
| 69 | 69 | @mock_request.stub!(:body=) | |
| 70 | @mock_request.should_receive(:[]=).with("content-type", "application/json") | ||
| 70 | @mock_request.should_receive(:[]=).with("Content-Type", "application/json") | ||
| 71 | 71 | @server.connection.should_receive(:request).with(@mock_request).and_return("response") | |
| 72 | 72 | @server.put("/foo", "bar") | |
| 73 | 73 | end |

