From d6cb0bc17d834675a69620940036490b909a4b0d Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Wed, 12 Aug 2009 18:48:25 +0000 Subject: add native /db/_conflicts view, patch by Adam Kocolosk, closes COUCHDB-462 git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@803663 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 12 +++++- share/www/script/couch_tests.js | 1 + share/www/script/test/view_builtin.js | 74 +++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 share/www/script/test/view_builtin.js (limited to 'share/www') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index f354f52a..af3bb8fb 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -191,10 +191,18 @@ function CouchDB(name, httpHeaders) { } this.allDocs = function(options,keys) { + return this.builtinView("_all_docs", options, keys) + } + + this.conflicts = function(options,keys) { + return this.builtinView("_conflicts", options, keys) + } + + this.builtinView = function(name, options, keys) { if(!keys) { - this.last_req = this.request("GET", this.uri + "_all_docs" + encodeOptions(options)); + this.last_req = this.request("GET", this.uri + name + encodeOptions(options)); } else { - this.last_req = this.request("POST", this.uri + "_all_docs" + encodeOptions(options), { + this.last_req = this.request("POST", this.uri + name + encodeOptions(options), { headers: {"Content-Type": "application/json"}, body: JSON.stringify({keys:keys}) }); diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 86c65bb7..3d415952 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -51,6 +51,7 @@ loadTest("design_paths.js"); loadTest("content_negotiation.js"); loadTest("design_docs.js"); loadTest("invalid_docids.js"); +loadTest("view_builtin.js"); loadTest("view_collation.js"); loadTest("view_conflicts.js"); loadTest("view_errors.js"); diff --git a/share/www/script/test/view_builtin.js b/share/www/script/test/view_builtin.js new file mode 100644 index 00000000..c0f7b2e5 --- /dev/null +++ b/share/www/script/test/view_builtin.js @@ -0,0 +1,74 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +couchTests.view_builtin = function(debug) { + var db = new CouchDB("test_suite_db"); + db.deleteDb(); + db.createDb(); + if (debug) debugger; + + // test _conflicts view + + // no rows + var result = db.conflicts(); + TEquals(0, result.rows.length, "should return 0 conflicts"); + + // one doc with a conflict + var doc_a = db.save({_id:"a", a:1}); + + // create conflict + var bulk_result = db.bulkSave([{_id:"a",a:2}], {all_or_nothing:true}); + var result = db.conflicts(); + TEquals(1, result.rows.length, "should return 1 conflicts"); + TEquals("a", result.rows[0].id, "should return row id 'a'"); + TEquals("a", result.rows[0].key, "should return row key 'a'"); + TEquals(bulk_result[0].rev, result.rows[0].rev, + "should return row key 'a'"); + + // multiple docs with conflicts + var doc_b = db.save({_id:"b", a:3}); + var bulk_result = db.bulkSave([{_id:"b",a:4}], {all_or_nothing:true}); + var result = db.conflicts(); + TEquals(2, result.rows.length, "should return 2 conflicts"); + + // key, startkey, endkey, skip & count + var result = db.conflicts({key:"b"}); + TEquals(1, result.rows.length, "should return 1 conflicts"); + + var result = db.conflicts({startkey:"b"}); + TEquals(1, result.rows.length, "should return 1 conflicts"); + + var result = db.conflicts({startkey:"a", endkey:"b"}); + TEquals(2, result.rows.length, "should return 2 conflicts"); + + var result = db.conflicts({startkey:"c"}); + TEquals(0, result.rows.length, "should return 0 conflicts"); + + var result = db.conflicts({limit:1}); + TEquals(1, result.rows.length, "should return 1 conflicts"); + + var result = db.conflicts({skip:1}); + TEquals(1, result.rows.length, "should return 1 conflicts"); + TEquals("b", result.rows[0].key, "should return row key 'b'"); + + // POST is not allowed yet + try { + var result = db.conflicts({}, ["a"]); + } catch (e) { + TEquals("method_not_allowed", e.error, "should not allow POST requests"); + } + + // multi key get + // var result = db.conflicts({}, ["a"]); + // TEquals(1, result.rows.length, "should return 1 conflicts"); + // TEquals("a", result.rows[0].key, "should return row key 'a'"); +}; -- cgit v1.2.3