You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2014/10/10 21:12:24 UTC
[10/37] move JS tests into safety
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_errors.js
----------------------------------------------------------------------
diff --git a/share/test/view_errors.js b/share/test/view_errors.js
new file mode 100644
index 0000000..e8bd08e
--- /dev/null
+++ b/share/test/view_errors.js
@@ -0,0 +1,189 @@
+// 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_errors = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"true"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ run_on_modified_server(
+ [{section: "couchdb",
+ key: "os_process_timeout",
+ value: "500"}],
+ function() {
+ var doc = {integer: 1, string: "1", array: [1, 2, 3]};
+ T(db.save(doc).ok);
+
+ // emitting a key value that is undefined should result in that row
+ // being included in the view results as null
+ var results = db.query(function(doc) {
+ emit(doc.undef, null);
+ });
+ T(results.total_rows == 1);
+ T(results.rows[0].key == null);
+
+ // if a view function throws an exception, its results are not included in
+ // the view index, but the view does not itself raise an error
+ var results = db.query(function(doc) {
+ doc.undef(); // throws an error
+ });
+ T(results.total_rows == 0);
+
+ // if a view function includes an undefined value in the emitted key or
+ // value, it is treated as null
+ var results = db.query(function(doc) {
+ emit([doc._id, doc.undef], null);
+ });
+ T(results.total_rows == 1);
+ T(results.rows[0].key[1] == null);
+
+ // querying a view with invalid params should give a resonable error message
+ var xhr = CouchDB.request("POST", "/test_suite_db/_temp_view?startkey=foo", {
+ headers: {"Content-Type": "application/json"},
+ body: JSON.stringify({language: "javascript",
+ map : "function(doc){emit(doc.integer)}"
+ })
+ });
+ T(JSON.parse(xhr.responseText).error == "bad_request");
+
+ // content type must be json
+ var xhr = CouchDB.request("POST", "/test_suite_db/_temp_view", {
+ headers: {"Content-Type": "application/x-www-form-urlencoded"},
+ body: JSON.stringify({language: "javascript",
+ map : "function(doc){}"
+ })
+ });
+ T(xhr.status == 415);
+
+ var map = function (doc) {emit(doc.integer, doc.integer);};
+
+ try {
+ db.query(map, null, {group: true});
+ T(0 == 1);
+ } catch(e) {
+ T(e.error == "query_parse_error");
+ }
+
+ var designDoc = {
+ _id:"_design/test",
+ language: "javascript",
+ views: {
+ "no_reduce": {map:"function(doc) {emit(doc._id, null);}"},
+ "with_reduce": {
+ map:"function (doc) {emit(doc.integer, doc.integer)};",
+ reduce:"function (keys, values) { return sum(values); };"}
+ }
+ };
+ T(db.save(designDoc).ok);
+
+ var designDoc2 = {
+ _id:"_design/testbig",
+ language: "javascript",
+ views: {
+ "reduce_too_big" : {
+ map:"function (doc) {emit(doc.integer, doc.integer)};",
+ reduce:"function (keys, values) { var chars = []; for (var i=0; i < 1000; i++) {chars.push('wazzap');};return chars; };"}
+ }
+ };
+ T(db.save(designDoc2).ok);
+
+ try {
+ db.view("test/no_reduce", {group: true});
+ T(0 == 1);
+ } catch(e) {
+ T(db.last_req.status == 400);
+ T(e.error == "query_parse_error");
+ }
+
+ try {
+ db.view("test/no_reduce", {group_level: 1});
+ T(0 == 1);
+ } catch(e) {
+ T(db.last_req.status == 400);
+ T(e.error == "query_parse_error");
+ }
+
+ try {
+ db.view("test/no_reduce", {reduce: true});
+ T(0 == 1);
+ } catch(e) {
+ T(db.last_req.status == 400);
+ T(e.error == "query_parse_error");
+ }
+
+ db.view("test/no_reduce", {reduce: false});
+ TEquals(200, db.last_req.status, "reduce=false for map views (without"
+ + " group or group_level) is allowed");
+
+ try {
+ db.view("test/with_reduce", {group: true, reduce: false});
+ T(0 == 1);
+ } catch(e) {
+ T(db.last_req.status == 400);
+ T(e.error == "query_parse_error");
+ }
+
+ try {
+ db.view("test/with_reduce", {group_level: 1, reduce: false});
+ T(0 == 1);
+ } catch(e) {
+ T(db.last_req.status == 400);
+ T(e.error == "query_parse_error");
+ }
+
+ var designDoc3 = {
+ _id:"_design/infinite",
+ language: "javascript",
+ views: {
+ "infinite_loop" :{map:"function(doc) {while(true){emit(doc,doc);}};"}
+ }
+ };
+ T(db.save(designDoc3).ok);
+
+ try {
+ db.view("infinite/infinite_loop");
+ T(0 == 1);
+ } catch(e) {
+ T(e.error == "os_process_error");
+ }
+
+ // Check error responses for invalid multi-get bodies.
+ var path = "/test_suite_db/_design/test/_view/no_reduce";
+ var xhr = CouchDB.request("POST", path, {body: "[]"});
+ T(xhr.status == 400);
+ result = JSON.parse(xhr.responseText);
+ T(result.error == "bad_request");
+ T(result.reason == "Request body must be a JSON object");
+ var data = "{\"keys\": 1}";
+ xhr = CouchDB.request("POST", path, {body:data});
+ T(xhr.status == 400);
+ result = JSON.parse(xhr.responseText);
+ T(result.error == "bad_request");
+ T(result.reason == "`keys` member must be a array.");
+
+ // if the reduce grows to fast, throw an overflow error
+ var path = "/test_suite_db/_design/testbig/_view/reduce_too_big";
+ xhr = CouchDB.request("GET", path);
+ T(xhr.status == 500);
+ result = JSON.parse(xhr.responseText);
+ T(result.error == "reduce_overflow_error");
+
+ try {
+ db.query(function() {emit(null, null)}, null, {startkey: 2, endkey:1});
+ T(0 == 1);
+ } catch(e) {
+ T(e.error == "query_parse_error");
+ T(e.reason.match(/no rows can match/i));
+ }
+ });
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_include_docs.js
----------------------------------------------------------------------
diff --git a/share/test/view_include_docs.js b/share/test/view_include_docs.js
new file mode 100644
index 0000000..dab79b8
--- /dev/null
+++ b/share/test/view_include_docs.js
@@ -0,0 +1,192 @@
+// 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_include_docs = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ var designDoc = {
+ _id:"_design/test",
+ language: "javascript",
+ views: {
+ all_docs: {
+ map: "function(doc) { emit(doc.integer, doc.string) }"
+ },
+ with_prev: {
+ map: "function(doc){if(doc.prev) emit(doc._id,{'_rev':doc.prev}); else emit(doc._id,{'_rev':doc._rev});}"
+ },
+ with_id: {
+ map: "function(doc) {if(doc.link_id) { var value = {'_id':doc.link_id}; if (doc.link_rev) {value._rev = doc.link_rev}; emit(doc._id, value);}};"
+ },
+ summate: {
+ map:"function (doc) { if (typeof doc.integer === 'number') {emit(doc.integer, doc.integer)};}",
+ reduce:"function (keys, values) { return sum(values); };"
+ }
+ }
+ }
+ T(db.save(designDoc).ok);
+
+ var resp = db.view('test/all_docs', {include_docs: true, limit: 2});
+ T(resp.rows.length == 2);
+ T(resp.rows[0].id == "0");
+ T(resp.rows[0].doc._id == "0");
+ T(resp.rows[1].id == "1");
+ T(resp.rows[1].doc._id == "1");
+
+ resp = db.view('test/all_docs', {include_docs: true}, [29, 74]);
+ T(resp.rows.length == 2);
+ T(resp.rows[0].doc._id == "29");
+ T(resp.rows[1].doc.integer == 74);
+
+ resp = db.allDocs({limit: 2, skip: 1, include_docs: true});
+ T(resp.rows.length == 2);
+ T(resp.rows[0].doc.integer == 1);
+ T(resp.rows[1].doc.integer == 10);
+
+ resp = db.allDocs({include_docs: true}, ['not_a_doc']);
+ T(resp.rows.length == 1);
+ T(!resp.rows[0].doc);
+
+ resp = db.allDocs({include_docs: true}, ["1", "foo"]);
+ T(resp.rows.length == 2);
+ T(resp.rows[0].doc.integer == 1);
+ T(!resp.rows[1].doc);
+
+ resp = db.allDocs({include_docs: true, limit: 0});
+ T(resp.rows.length == 0);
+
+ // No reduce support
+ try {
+ resp = db.view('test/summate', {include_docs: true});
+ alert(JSON.stringify(resp));
+ T(0==1);
+ } catch (e) {
+ T(e.error == 'query_parse_error');
+ }
+
+ // Reduce support when reduce=false
+ resp = db.view('test/summate', {reduce: false, include_docs: true});
+ T(resp.rows.length == 100);
+
+ // Not an error with include_docs=false&reduce=true
+ resp = db.view('test/summate', {reduce: true, include_docs: false});
+ T(resp.rows.length == 1);
+ T(resp.rows[0].value == 4950);
+
+ T(db.save({
+ "_id": "link-to-10",
+ "link_id" : "10"
+ }).ok);
+
+ // you can link to another doc from a value.
+ resp = db.view("test/with_id", {key:"link-to-10"});
+ T(resp.rows[0].key == "link-to-10");
+ T(resp.rows[0].value["_id"] == "10");
+
+ resp = db.view("test/with_id", {key:"link-to-10",include_docs: true});
+ T(resp.rows[0].key == "link-to-10");
+ T(resp.rows[0].value["_id"] == "10");
+ T(resp.rows[0].doc._id == "10");
+
+ // Check emitted _rev controls things
+ resp = db.allDocs({include_docs: true}, ["0"]);
+ var before = resp.rows[0].doc;
+
+ var after = db.open("0");
+ after.integer = 100;
+ after.prev = after._rev;
+ resp = db.save(after)
+ T(resp.ok);
+
+ var after = db.open("0");
+ TEquals(resp.rev, after._rev, "fails with firebug running");
+ T(after._rev != after.prev, "passes");
+ TEquals(100, after.integer, "fails with firebug running");
+
+ // should emit the previous revision
+ resp = db.view("test/with_prev", {include_docs: true}, ["0"]);
+ T(resp.rows[0].doc._id == "0");
+ T(resp.rows[0].doc._rev == before._rev);
+ T(!resp.rows[0].doc.prev);
+ T(resp.rows[0].doc.integer == 0);
+
+ var xhr = CouchDB.request("POST", "/test_suite_db/_compact");
+ T(xhr.status == 202)
+ while (db.info().compact_running) {}
+
+ resp = db.view("test/with_prev", {include_docs: true}, ["0", "23"]);
+ T(resp.rows.length == 2);
+ T(resp.rows[0].key == "0");
+ T(resp.rows[0].id == "0");
+ T(!resp.rows[0].doc);
+ T(resp.rows[0].doc == null);
+ T(resp.rows[1].doc.integer == 23);
+
+ // COUCHDB-549 - include_docs=true with conflicts=true
+
+ var dbA = new CouchDB("test_suite_db_a", {"X-Couch-Full-Commit":"false"});
+ var dbB = new CouchDB("test_suite_db_b", {"X-Couch-Full-Commit":"false"});
+
+ dbA.deleteDb();
+ dbA.createDb();
+ dbB.deleteDb();
+ dbB.createDb();
+
+ var ddoc = {
+ _id: "_design/mydesign",
+ language : "javascript",
+ views : {
+ myview : {
+ map: (function(doc) {
+ emit(doc.value, 1);
+ }).toString()
+ }
+ }
+ };
+ TEquals(true, dbA.save(ddoc).ok);
+
+ var doc1a = {_id: "foo", value: 1, str: "1"};
+ TEquals(true, dbA.save(doc1a).ok);
+
+ var doc1b = {_id: "foo", value: 1, str: "666"};
+ TEquals(true, dbB.save(doc1b).ok);
+
+ var doc2 = {_id: "bar", value: 2, str: "2"};
+ TEquals(true, dbA.save(doc2).ok);
+
+ TEquals(true, CouchDB.replicate(dbA.name, dbB.name).ok);
+
+ doc1b = dbB.open("foo", {conflicts: true});
+ TEquals(true, doc1b._conflicts instanceof Array);
+ TEquals(1, doc1b._conflicts.length);
+ var conflictRev = doc1b._conflicts[0];
+
+ doc2 = dbB.open("bar", {conflicts: true});
+ TEquals("undefined", typeof doc2._conflicts);
+
+ resp = dbB.view("mydesign/myview", {include_docs: true, conflicts: true});
+
+ TEquals(2, resp.rows.length);
+ TEquals(true, resp.rows[0].doc._conflicts instanceof Array);
+ TEquals(1, resp.rows[0].doc._conflicts.length);
+ TEquals(conflictRev, resp.rows[0].doc._conflicts[0]);
+ TEquals("undefined", typeof resp.rows[1].doc._conflicts);
+
+ // cleanup
+ dbA.deleteDb();
+ dbB.deleteDb();
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_multi_key_all_docs.js
----------------------------------------------------------------------
diff --git a/share/test/view_multi_key_all_docs.js b/share/test/view_multi_key_all_docs.js
new file mode 100644
index 0000000..7c7f6f8
--- /dev/null
+++ b/share/test/view_multi_key_all_docs.js
@@ -0,0 +1,95 @@
+// 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_multi_key_all_docs = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ var keys = ["10","15","30","37","50"];
+ var rows = db.allDocs({},keys).rows;
+ T(rows.length == keys.length);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[i]);
+
+ // keys in GET parameters
+ rows = db.allDocs({keys:keys}, null).rows;
+ T(rows.length == keys.length);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[i]);
+
+ rows = db.allDocs({limit: 1}, keys).rows;
+ T(rows.length == 1);
+ T(rows[0].id == keys[0]);
+
+ // keys in GET parameters
+ rows = db.allDocs({limit: 1, keys: keys}, null).rows;
+ T(rows.length == 1);
+ T(rows[0].id == keys[0]);
+
+ rows = db.allDocs({skip: 2}, keys).rows;
+ T(rows.length == 3);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[i+2]);
+
+ // keys in GET parameters
+ rows = db.allDocs({skip: 2, keys: keys}, null).rows;
+ T(rows.length == 3);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[i+2]);
+
+ rows = db.allDocs({descending: "true"}, keys).rows;
+ T(rows.length == keys.length);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[keys.length-i-1]);
+
+ // keys in GET parameters
+ rows = db.allDocs({descending: "true", keys: keys}, null).rows;
+ T(rows.length == keys.length);
+ for(var i=0; i<rows.length; i++)
+ T(rows[i].id == keys[keys.length-i-1]);
+
+ rows = db.allDocs({descending: "true", skip: 3, limit:1}, keys).rows;
+ T(rows.length == 1);
+ T(rows[0].id == keys[1]);
+
+ // keys in GET parameters
+ rows = db.allDocs({descending: "true", skip: 3, limit:1, keys: keys}, null).rows;
+ T(rows.length == 1);
+ T(rows[0].id == keys[1]);
+
+ // Check we get invalid rows when the key doesn't exist
+ rows = db.allDocs({}, [1, "i_dont_exist", "0"]).rows;
+ T(rows.length == 3);
+ T(rows[0].error == "not_found");
+ T(!rows[0].id);
+ T(rows[1].error == "not_found");
+ T(!rows[1].id);
+ T(rows[2].id == rows[2].key && rows[2].key == "0");
+
+ // keys in GET parameters
+ rows = db.allDocs({keys: [1, "i_dont_exist", "0"]}, null).rows;
+ T(rows.length == 3);
+ T(rows[0].error == "not_found");
+ T(!rows[0].id);
+ T(rows[1].error == "not_found");
+ T(!rows[1].id);
+ T(rows[2].id == rows[2].key && rows[2].key == "0");
+
+ // empty keys
+ rows = db.allDocs({keys: []}, null).rows;
+ T(rows.length == 0);
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_multi_key_design.js
----------------------------------------------------------------------
diff --git a/share/test/view_multi_key_design.js b/share/test/view_multi_key_design.js
new file mode 100644
index 0000000..a84d07a
--- /dev/null
+++ b/share/test/view_multi_key_design.js
@@ -0,0 +1,220 @@
+// 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_multi_key_design = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ var designDoc = {
+ _id:"_design/test",
+ language: "javascript",
+ views: {
+ all_docs: {
+ map: "function(doc) { emit(doc.integer, doc.string) }"
+ },
+ multi_emit: {
+ map: "function(doc) {for(var i = 0 ; i < 3 ; i++) { emit(i, doc.integer) ; } }"
+ },
+ summate: {
+ map:"function (doc) {emit(doc.integer, doc.integer)};",
+ reduce:"function (keys, values) { return sum(values); };"
+ }
+ }
+ };
+ T(db.save(designDoc).ok);
+
+ // Test that missing keys work too
+ var keys = [101,30,15,37,50];
+ var reduce = db.view("test/summate",{group:true},keys).rows;
+ T(reduce.length == keys.length-1); // 101 is missing
+ for(var i=0; i<reduce.length; i++) {
+ T(keys.indexOf(reduce[i].key) != -1);
+ T(reduce[i].key == reduce[i].value);
+ }
+
+ // First, the goods:
+ var keys = [10,15,30,37,50];
+ var rows = db.view("test/all_docs",{},keys).rows;
+ for(var i=0; i<rows.length; i++) {
+ T(keys.indexOf(rows[i].key) != -1);
+ T(rows[i].key == rows[i].value);
+ }
+
+ // with GET keys
+ rows = db.view("test/all_docs",{keys:keys},null).rows;
+ for(var i=0;i<rows.length; i++) {
+ T(keys.indexOf(rows[i].key) != -1);
+ T(rows[i].key == rows[i].value);
+ }
+
+ // with empty keys
+ rows = db.view("test/all_docs",{keys:[]},null).rows;
+ T(rows.length == 0);
+
+ var reduce = db.view("test/summate",{group:true},keys).rows;
+ T(reduce.length == keys.length);
+ for(var i=0; i<reduce.length; i++) {
+ T(keys.indexOf(reduce[i].key) != -1);
+ T(reduce[i].key == reduce[i].value);
+ }
+
+ // with GET keys
+ reduce = db.view("test/summate",{group:true,keys:keys},null).rows;
+ T(reduce.length == keys.length);
+ for(var i=0; i<reduce.length; i++) {
+ T(keys.indexOf(reduce[i].key) != -1);
+ T(reduce[i].key == reduce[i].value);
+ }
+
+ // Test that invalid parameter combinations get rejected
+ var badargs = [{startkey:0}, {endkey:0}, {key: 0}, {group_level: 2}];
+ var getbadargs = [{startkey:0, keys:keys}, {endkey:0, keys:keys},
+ {key:0, keys:keys}, {group_level: 2, keys:keys}];
+ for(var i in badargs)
+ {
+ try {
+ db.view("test/all_docs",badargs[i],keys);
+ T(0==1);
+ } catch (e) {
+ T(e.error == "query_parse_error");
+ }
+
+ try {
+ db.view("test/all_docs",getbadargs[i],null);
+ T(0==1);
+ } catch (e) {
+ T(e.error = "query_parse_error");
+ }
+ }
+
+ try {
+ db.view("test/summate",{},keys);
+ T(0==1);
+ } catch (e) {
+ T(e.error == "query_parse_error");
+ }
+
+ try {
+ db.view("test/summate",{keys:keys},null);
+ T(0==1);
+ } catch (e) {
+ T(e.error == "query_parse_error");
+ }
+
+ // Test that a map & reduce containing func support keys when reduce=false
+ var resp = db.view("test/summate", {reduce: false}, keys);
+ T(resp.rows.length == 5);
+
+ resp = db.view("test/summate", {reduce: false, keys: keys}, null);
+ T(resp.rows.length == 5);
+
+ // Check that limiting by startkey_docid and endkey_docid get applied
+ // as expected.
+ var curr = db.view("test/multi_emit", {startkey_docid: 21, endkey_docid: 23}, [0, 2]).rows;
+ var exp_key = [ 0, 0, 0, 2, 2, 2] ;
+ var exp_val = [21, 22, 23, 21, 22, 23] ;
+ T(curr.length == 6);
+ for( var i = 0 ; i < 6 ; i++)
+ {
+ T(curr[i].key == exp_key[i]);
+ T(curr[i].value == exp_val[i]);
+ }
+
+ curr = db.view("test/multi_emit", {startkey_docid: 21, endkey_docid: 23, keys: [0, 2]}, null).rows;
+ T(curr.length == 6);
+ for( var i = 0 ; i < 6 ; i++)
+ {
+ T(curr[i].key == exp_key[i]);
+ T(curr[i].value == exp_val[i]);
+ }
+
+ // Check limit works
+ curr = db.view("test/all_docs", {limit: 1}, keys).rows;
+ T(curr.length == 1);
+ T(curr[0].key == 10);
+
+ curr = db.view("test/all_docs", {limit: 1, keys: keys}, null).rows;
+ T(curr.length == 1);
+ T(curr[0].key == 10);
+
+ // Check offset works
+ curr = db.view("test/multi_emit", {skip: 1}, [0]).rows;
+ T(curr.length == 99);
+ T(curr[0].value == 1);
+
+ curr = db.view("test/multi_emit", {skip: 1, keys: [0]}, null).rows;
+ T(curr.length == 99);
+ T(curr[0].value == 1);
+
+ // Check that dir works
+ curr = db.view("test/multi_emit", {descending: "true"}, [1]).rows;
+ T(curr.length == 100);
+ T(curr[0].value == 99);
+ T(curr[99].value == 0);
+
+ curr = db.view("test/multi_emit", {descending: "true", keys: [1]}, null).rows;
+ T(curr.length == 100);
+ T(curr[0].value == 99);
+ T(curr[99].value == 0);
+
+ // Check a couple combinations
+ curr = db.view("test/multi_emit", {descending: "true", skip: 3, limit: 2}, [2]).rows;
+ T(curr.length, 2);
+ T(curr[0].value == 96);
+ T(curr[1].value == 95);
+
+ curr = db.view("test/multi_emit", {descending: "true", skip: 3, limit: 2, keys: [2]}, null).rows;
+ T(curr.length, 2);
+ T(curr[0].value == 96);
+ T(curr[1].value == 95);
+
+ curr = db.view("test/multi_emit", {skip: 2, limit: 3, startkey_docid: "13"}, [0]).rows;
+ T(curr.length == 3);
+ T(curr[0].value == 15);
+ T(curr[1].value == 16);
+ T(curr[2].value == 17);
+
+ curr = db.view("test/multi_emit", {skip: 2, limit: 3, startkey_docid: "13", keys: [0]}, null).rows;
+ T(curr.length == 3);
+ T(curr[0].value == 15);
+ T(curr[1].value == 16);
+ T(curr[2].value == 17);
+
+ curr = db.view("test/multi_emit",
+ {skip: 1, limit: 5, startkey_docid: "25", endkey_docid: "27"}, [1]).rows;
+ T(curr.length == 2);
+ T(curr[0].value == 26);
+ T(curr[1].value == 27);
+
+ curr = db.view("test/multi_emit",
+ {skip: 1, limit: 5, startkey_docid: "25", endkey_docid: "27", keys: [1]}, null).rows;
+ T(curr.length == 2);
+ T(curr[0].value == 26);
+ T(curr[1].value == 27);
+
+ curr = db.view("test/multi_emit",
+ {skip: 1, limit: 5, startkey_docid: "28", endkey_docid: "26", descending: "true"}, [1]).rows;
+ T(curr.length == 2);
+ T(curr[0].value == 27);
+ T(curr[1].value == 26);
+
+ curr = db.view("test/multi_emit",
+ {skip: 1, limit: 5, startkey_docid: "28", endkey_docid: "26", descending: "true", keys: [1]}, null).rows;
+ T(curr.length == 2);
+ T(curr[0].value == 27);
+ T(curr[1].value == 26);
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_multi_key_temp.js
----------------------------------------------------------------------
diff --git a/share/test/view_multi_key_temp.js b/share/test/view_multi_key_temp.js
new file mode 100644
index 0000000..3c05409
--- /dev/null
+++ b/share/test/view_multi_key_temp.js
@@ -0,0 +1,40 @@
+// 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_multi_key_temp = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ var queryFun = function(doc) { emit(doc.integer, doc.integer) };
+ var reduceFun = function (keys, values) { return sum(values); };
+
+ var keys = [10,15,30,37,50];
+ var rows = db.query(queryFun, null, {}, keys).rows;
+ for(var i=0; i<rows.length; i++) {
+ T(keys.indexOf(rows[i].key) != -1);
+ T(rows[i].key == rows[i].value);
+ }
+
+ var reduce = db.query(queryFun, reduceFun, {group:true}, keys).rows;
+ for(var i=0; i<reduce.length; i++) {
+ T(keys.indexOf(reduce[i].key) != -1);
+ T(reduce[i].key == reduce[i].value);
+ }
+
+ rows = db.query(queryFun, null, {}, []).rows;
+ T(rows.length == 0);
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_offsets.js
----------------------------------------------------------------------
diff --git a/share/test/view_offsets.js b/share/test/view_offsets.js
new file mode 100644
index 0000000..464a1ae
--- /dev/null
+++ b/share/test/view_offsets.js
@@ -0,0 +1,108 @@
+// 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_offsets = function(debug) {
+ if (debug) debugger;
+
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+
+ var designDoc = {
+ _id : "_design/test",
+ views : {
+ offset : {
+ map : "function(doc) { emit([doc.letter, doc.number], doc); }",
+ }
+ }
+ };
+ T(db.save(designDoc).ok);
+
+ var docs = [
+ {_id : "a1", letter : "a", number : 1, foo: "bar"},
+ {_id : "a2", letter : "a", number : 2, foo: "bar"},
+ {_id : "a3", letter : "a", number : 3, foo: "bar"},
+ {_id : "b1", letter : "b", number : 1, foo: "bar"},
+ {_id : "b2", letter : "b", number : 2, foo: "bar"},
+ {_id : "b3", letter : "b", number : 3, foo: "bar"},
+ {_id : "b4", letter : "b", number : 4, foo: "bar"},
+ {_id : "b5", letter : "b", number : 5, foo: "bar"},
+ {_id : "c1", letter : "c", number : 1, foo: "bar"},
+ {_id : "c2", letter : "c", number : 2, foo: "bar"},
+ ];
+ db.bulkSave(docs);
+
+ var check = function(startkey, offset) {
+ var opts = {startkey: startkey, descending: true};
+ T(db.view("test/offset", opts).offset == offset);
+ };
+
+ [
+ [["c", 2], 0],
+ [["c", 1], 1],
+ [["b", 5], 2],
+ [["b", 4], 3],
+ [["b", 3], 4],
+ [["b", 2], 5],
+ [["b", 1], 6],
+ [["a", 3], 7],
+ [["a", 2], 8],
+ [["a", 1], 9]
+ ].forEach(function(row){ check(row[0], row[1]);});
+
+ var runTest = function () {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+
+ var designDoc = {
+ _id : "_design/test",
+ views : {
+ offset : {
+ map : "function(doc) { emit([doc.letter, doc.number], doc);}",
+ }
+ }
+ };
+ T(db.save(designDoc).ok);
+
+ var docs = [
+ {_id : "a1", letter : "a", number : 1, foo : "bar"},
+ {_id : "a2", letter : "a", number : 2, foo : "bar"},
+ {_id : "a3", letter : "a", number : 3, foo : "bar"},
+ {_id : "b1", letter : "b", number : 1, foo : "bar"},
+ {_id : "b2", letter : "b", number : 2, foo : "bar"},
+ {_id : "b3", letter : "b", number : 3, foo : "bar"},
+ {_id : "b4", letter : "b", number : 4, foo : "bar"},
+ {_id : "b5", letter : "b", number : 5, foo : "bar"},
+ {_id : "c1", letter : "c", number : 1, foo : "bar"},
+ {_id : "c2", letter : "c", number : 2, foo : "bar"}
+ ];
+ db.bulkSave(docs);
+
+ var res1 = db.view("test/offset", {
+ startkey: ["b",4], startkey_docid: "b4", endkey: ["b"],
+ limit: 2, descending: true, skip: 1
+ })
+
+ var res2 = db.view("test/offset", {startkey: ["c", 3]});
+ var res3 = db.view("test/offset", {
+ startkey: ["b", 6],
+ endkey: ["b", 7]
+ });
+
+ return res1.offset == 4 && res2.offset == docs.length && res3.offset == 8;
+
+ };
+
+ for(var i = 0; i < 15; i++) T(runTest());
+}
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_pagination.js
----------------------------------------------------------------------
diff --git a/share/test/view_pagination.js b/share/test/view_pagination.js
new file mode 100644
index 0000000..ed3a7ee
--- /dev/null
+++ b/share/test/view_pagination.js
@@ -0,0 +1,147 @@
+// 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_pagination = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ var queryFun = function(doc) { emit(doc.integer, null); };
+ var i;
+
+ // page through the view ascending
+ for (i = 0; i < docs.length; i += 10) {
+ var queryResults = db.query(queryFun, null, {
+ startkey: i,
+ startkey_docid: i,
+ limit: 10
+ });
+ T(queryResults.rows.length == 10);
+ T(queryResults.total_rows == docs.length);
+ T(queryResults.offset == i);
+ var j;
+ for (j = 0; j < 10;j++) {
+ T(queryResults.rows[j].key == i + j);
+ }
+
+ // test aliases start_key and start_key_doc_id
+ queryResults = db.query(queryFun, null, {
+ start_key: i,
+ start_key_doc_id: i,
+ limit: 10
+ });
+ T(queryResults.rows.length == 10);
+ T(queryResults.total_rows == docs.length);
+ T(queryResults.offset == i);
+ for (j = 0; j < 10;j++) {
+ T(queryResults.rows[j].key == i + j);
+ }
+ }
+
+ // page through the view descending
+ for (i = docs.length - 1; i >= 0; i -= 10) {
+ var queryResults = db.query(queryFun, null, {
+ startkey: i,
+ startkey_docid: i,
+ descending: true,
+ limit: 10
+ });
+ T(queryResults.rows.length == 10);
+ T(queryResults.total_rows == docs.length);
+ T(queryResults.offset == docs.length - i - 1);
+ var j;
+ for (j = 0; j < 10; j++) {
+ T(queryResults.rows[j].key == i - j);
+ }
+ }
+
+ // ignore decending=false. CouchDB should just ignore that.
+ for (i = 0; i < docs.length; i += 10) {
+ var queryResults = db.query(queryFun, null, {
+ startkey: i,
+ startkey_docid: i,
+ descending: false,
+ limit: 10
+ });
+ T(queryResults.rows.length == 10);
+ T(queryResults.total_rows == docs.length);
+ T(queryResults.offset == i);
+ var j;
+ for (j = 0; j < 10;j++) {
+ T(queryResults.rows[j].key == i + j);
+ }
+ }
+
+ function testEndkeyDocId(queryResults) {
+ T(queryResults.rows.length == 35);
+ T(queryResults.total_rows == docs.length);
+ T(queryResults.offset == 1);
+ T(queryResults.rows[0].id == "1");
+ T(queryResults.rows[1].id == "10");
+ T(queryResults.rows[2].id == "11");
+ T(queryResults.rows[3].id == "12");
+ T(queryResults.rows[4].id == "13");
+ T(queryResults.rows[5].id == "14");
+ T(queryResults.rows[6].id == "15");
+ T(queryResults.rows[7].id == "16");
+ T(queryResults.rows[8].id == "17");
+ T(queryResults.rows[9].id == "18");
+ T(queryResults.rows[10].id == "19");
+ T(queryResults.rows[11].id == "2");
+ T(queryResults.rows[12].id == "20");
+ T(queryResults.rows[13].id == "21");
+ T(queryResults.rows[14].id == "22");
+ T(queryResults.rows[15].id == "23");
+ T(queryResults.rows[16].id == "24");
+ T(queryResults.rows[17].id == "25");
+ T(queryResults.rows[18].id == "26");
+ T(queryResults.rows[19].id == "27");
+ T(queryResults.rows[20].id == "28");
+ T(queryResults.rows[21].id == "29");
+ T(queryResults.rows[22].id == "3");
+ T(queryResults.rows[23].id == "30");
+ T(queryResults.rows[24].id == "31");
+ T(queryResults.rows[25].id == "32");
+ T(queryResults.rows[26].id == "33");
+ T(queryResults.rows[27].id == "34");
+ T(queryResults.rows[28].id == "35");
+ T(queryResults.rows[29].id == "36");
+ T(queryResults.rows[30].id == "37");
+ T(queryResults.rows[31].id == "38");
+ T(queryResults.rows[32].id == "39");
+ T(queryResults.rows[33].id == "4");
+ T(queryResults.rows[34].id == "40");
+ }
+
+ // test endkey_docid
+ var queryResults = db.query(function(doc) { emit(null, null); }, null, {
+ startkey: null,
+ startkey_docid: 1,
+ endkey: null,
+ endkey_docid: 40
+ });
+ testEndkeyDocId(queryResults);
+
+ // test aliases end_key_doc_id and end_key
+ queryResults = db.query(function(doc) { emit(null, null); }, null, {
+ start_key: null,
+ start_key_doc_id: 1,
+ end_key: null,
+ end_key_doc_id: 40
+ });
+ testEndkeyDocId(queryResults);
+
+ };
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_sandboxing.js
----------------------------------------------------------------------
diff --git a/share/test/view_sandboxing.js b/share/test/view_sandboxing.js
new file mode 100644
index 0000000..5c73c5a
--- /dev/null
+++ b/share/test/view_sandboxing.js
@@ -0,0 +1,140 @@
+// 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_sandboxing = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var doc = {integer: 1, string: "1", array: [1, 2, 3]};
+ T(db.save(doc).ok);
+/*
+ // make sure that attempting to change the document throws an error
+ var results = db.query(function(doc) {
+ doc.integer = 2;
+ emit(null, doc);
+ });
+ T(results.total_rows == 0);
+
+ var results = db.query(function(doc) {
+ doc.array[0] = 0;
+ emit(null, doc);
+ });
+ T(results.total_rows == 0);
+*/
+ // make sure that a view cannot invoke interpreter internals such as the
+ // garbage collector
+ var results = db.query(function(doc) {
+ gc();
+ emit(null, doc);
+ });
+ T(results.total_rows == 0);
+
+ // make sure that a view cannot access the map_funs array defined used by
+ // the view server
+ var results = db.query(function(doc) { map_funs.push(1); emit(null, doc); });
+ T(results.total_rows == 0);
+
+ // make sure that a view cannot access the map_results array defined used by
+ // the view server
+ var results = db.query(function(doc) { map_results.push(1); emit(null, doc); });
+ T(results.total_rows == 0);
+
+ // test for COUCHDB-925
+ // altering 'doc' variable in map function affects other map functions
+ var ddoc = {
+ _id: "_design/foobar",
+ language: "javascript",
+ views: {
+ view1: {
+ map:
+ (function(doc) {
+ if (doc.values) {
+ doc.values = [666];
+ }
+ if (doc.tags) {
+ doc.tags.push("qwerty");
+ }
+ if (doc.tokens) {
+ doc.tokens["c"] = 3;
+ }
+ }).toString()
+ },
+ view2: {
+ map:
+ (function(doc) {
+ if (doc.values) {
+ emit(doc._id, doc.values);
+ }
+ if (doc.tags) {
+ emit(doc._id, doc.tags);
+ }
+ if (doc.tokens) {
+ emit(doc._id, doc.tokens);
+ }
+ }).toString()
+ }
+ }
+ };
+ var doc1 = {
+ _id: "doc1",
+ values: [1, 2, 3]
+ };
+ var doc2 = {
+ _id: "doc2",
+ tags: ["foo", "bar"],
+ tokens: {a: 1, b: 2}
+ };
+
+ db.deleteDb();
+ db.createDb();
+ T(db.save(ddoc).ok);
+ T(db.save(doc1).ok);
+ T(db.save(doc2).ok);
+
+ var view1Results = db.view(
+ "foobar/view1", {bypass_cache: Math.round(Math.random() * 1000)});
+ var view2Results = db.view(
+ "foobar/view2", {bypass_cache: Math.round(Math.random() * 1000)});
+
+ TEquals(0, view1Results.rows.length, "view1 has 0 rows");
+ TEquals(3, view2Results.rows.length, "view2 has 3 rows");
+
+ TEquals(doc1._id, view2Results.rows[0].key);
+ TEquals(doc2._id, view2Results.rows[1].key);
+ TEquals(doc2._id, view2Results.rows[2].key);
+
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=449657
+ TEquals(3, view2Results.rows[0].value.length,
+ "Warning: installed SpiderMonkey version doesn't allow sealing of arrays");
+ if (view2Results.rows[0].value.length === 3) {
+ TEquals(1, view2Results.rows[0].value[0]);
+ TEquals(2, view2Results.rows[0].value[1]);
+ TEquals(3, view2Results.rows[0].value[2]);
+ }
+
+ TEquals(1, view2Results.rows[1].value["a"]);
+ TEquals(2, view2Results.rows[1].value["b"]);
+ TEquals('undefined', typeof view2Results.rows[1].value["c"],
+ "doc2.tokens object was not sealed");
+
+ TEquals(2, view2Results.rows[2].value.length,
+ "Warning: installed SpiderMonkey version doesn't allow sealing of arrays");
+ if (view2Results.rows[2].value.length === 2) {
+ TEquals("foo", view2Results.rows[2].value[0]);
+ TEquals("bar", view2Results.rows[2].value[1]);
+ }
+
+ // cleanup
+ db.deleteDb();
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/view_update_seq.js
----------------------------------------------------------------------
diff --git a/share/test/view_update_seq.js b/share/test/view_update_seq.js
new file mode 100644
index 0000000..df92b11
--- /dev/null
+++ b/share/test/view_update_seq.js
@@ -0,0 +1,106 @@
+// 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_update_seq = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"true"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ T(db.info().update_seq == 0);
+
+ var resp = db.allDocs({update_seq:true});
+
+ T(resp.rows.length == 0);
+ T(resp.update_seq == 0);
+
+ var designDoc = {
+ _id:"_design/test",
+ language: "javascript",
+ views: {
+ all_docs: {
+ map: "function(doc) { emit(doc.integer, doc.string) }"
+ },
+ summate: {
+ map:"function (doc) { if (typeof doc.integer === 'number') { emit(doc.integer, doc.integer)}; }",
+ reduce:"function (keys, values) { return sum(values); };"
+ }
+ }
+ };
+ T(db.save(designDoc).ok);
+
+ T(db.info().update_seq == 1);
+
+ resp = db.allDocs({update_seq:true});
+
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 1);
+
+ var docs = makeDocs(0, 100);
+ db.bulkSave(docs);
+
+ resp = db.allDocs({limit: 1});
+ T(resp.rows.length == 1);
+ T(!resp.update_seq, "all docs");
+
+ resp = db.allDocs({limit: 1, update_seq:true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 101);
+
+ resp = db.view('test/all_docs', {limit: 1, update_seq:true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 101);
+
+ resp = db.view('test/all_docs', {limit: 1, update_seq:false});
+ T(resp.rows.length == 1);
+ T(!resp.update_seq, "view");
+
+ resp = db.view('test/summate', {update_seq:true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 101);
+
+ db.save({"id":"0", "integer": 1});
+ resp = db.view('test/all_docs', {limit: 1,stale: "ok", update_seq:true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 101);
+
+ db.save({"id":"00", "integer": 2});
+ resp = db.view('test/all_docs',
+ {limit: 1, stale: "update_after", update_seq: true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 101);
+
+ // wait 5 seconds for the next assertions to pass in very slow machines
+ var t0 = new Date(), t1;
+ do {
+ CouchDB.request("GET", "/");
+ t1 = new Date();
+ } while ((t1 - t0) < 5000);
+
+ resp = db.view('test/all_docs', {limit: 1, stale: "ok", update_seq: true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 103);
+
+ resp = db.view('test/all_docs', {limit: 1, update_seq:true});
+ T(resp.rows.length == 1);
+ T(resp.update_seq == 103);
+
+ resp = db.view('test/all_docs',{update_seq:true},["0","1"]);
+ T(resp.update_seq == 103);
+
+ resp = db.view('test/all_docs',{update_seq:true},["0","1"]);
+ T(resp.update_seq == 103);
+
+ resp = db.view('test/summate',{group:true, update_seq:true},[0,1]);
+ TEquals(103, resp.update_seq);
+
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/all_docs.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/all_docs.js b/share/www/script/test/all_docs.js
deleted file mode 100644
index 66ad880..0000000
--- a/share/www/script/test/all_docs.js
+++ /dev/null
@@ -1,142 +0,0 @@
-// 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.all_docs = function(debug) {
- var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
- db.deleteDb();
- db.createDb();
- if (debug) debugger;
-
- // Create some more documents.
- // Notice the use of the ok member on the return result.
- T(db.save({_id:"0",a:1,b:1}).ok);
- T(db.save({_id:"3",a:4,b:16}).ok);
- T(db.save({_id:"1",a:2,b:4}).ok);
- T(db.save({_id:"2",a:3,b:9}).ok);
-
- // Check the all docs
- var results = db.allDocs();
- var rows = results.rows;
-
- T(results.total_rows == results.rows.length);
-
- for(var i=0; i < rows.length; i++) {
- T(rows[i].id >= "0" && rows[i].id <= "4");
- }
-
- // Check _all_docs with descending=true
- var desc = db.allDocs({descending:true});
- T(desc.total_rows == desc.rows.length);
-
- // Check _all_docs offset
- var all = db.allDocs({startkey:"2"});
- T(all.offset == 2);
-
- // Confirm that queries may assume raw collation.
- var raw = db.allDocs({ startkey: "org.couchdb.user:",
- endkey : "org.couchdb.user;"
- });
- TEquals(0, raw.rows.length);
-
- // check that the docs show up in the seq view in the order they were created
- var changes = db.changes();
- var ids = ["0","3","1","2"];
- for (var i=0; i < changes.results.length; i++) {
- var row = changes.results[i];
- T(row.id == ids[i], "seq order");
- };
-
- // it should work in reverse as well
- changes = db.changes({descending:true});
- ids = ["2","1","3","0"];
- for (var i=0; i < changes.results.length; i++) {
- var row = changes.results[i];
- T(row.id == ids[i], "descending=true");
- };
-
- // check that deletions also show up right
- var doc1 = db.open("1");
- var deleted = db.deleteDoc(doc1);
- T(deleted.ok);
- changes = db.changes();
- // the deletion should make doc id 1 have the last seq num
- T(changes.results.length == 4);
- T(changes.results[3].id == "1");
- T(changes.results[3].deleted);
-
- // do an update
- var doc2 = db.open("3");
- doc2.updated = "totally";
- db.save(doc2);
- changes = db.changes();
-
- // the update should make doc id 3 have the last seq num
- T(changes.results.length == 4);
- T(changes.results[3].id == "3");
-
- // ok now lets see what happens with include docs
- changes = db.changes({include_docs: true});
- T(changes.results.length == 4);
- T(changes.results[3].id == "3");
- T(changes.results[3].doc.updated == "totally");
-
- T(changes.results[2].doc);
- T(changes.results[2].doc._deleted);
-
- rows = db.allDocs({include_docs: true}, ["1"]).rows;
- TEquals(1, rows.length);
- TEquals("1", rows[0].key);
- TEquals("1", rows[0].id);
- TEquals(true, rows[0].value.deleted);
- TEquals(null, rows[0].doc);
-
- // add conflicts
- var conflictDoc1 = {
- _id: "3", _rev: "2-aa01552213fafa022e6167113ed01087", value: "X"
- };
- var conflictDoc2 = {
- _id: "3", _rev: "2-ff01552213fafa022e6167113ed01087", value: "Z"
- };
- T(db.save(conflictDoc1, {new_edits: false}));
- T(db.save(conflictDoc2, {new_edits: false}));
-
- var winRev = db.open("3");
-
- changes = db.changes({include_docs: true, conflicts: true, style: "all_docs"});
- TEquals("3", changes.results[3].id);
- TEquals(3, changes.results[3].changes.length);
- TEquals(winRev._rev, changes.results[3].changes[0].rev);
- TEquals("3", changes.results[3].doc._id);
- TEquals(winRev._rev, changes.results[3].doc._rev);
- TEquals(true, changes.results[3].doc._conflicts instanceof Array);
- TEquals(2, changes.results[3].doc._conflicts.length);
-
- rows = db.allDocs({include_docs: true, conflicts: true}).rows;
- TEquals(3, rows.length);
- TEquals("3", rows[2].key);
- TEquals("3", rows[2].id);
- TEquals(winRev._rev, rows[2].value.rev);
- TEquals(winRev._rev, rows[2].doc._rev);
- TEquals("3", rows[2].doc._id);
- TEquals(true, rows[2].doc._conflicts instanceof Array);
- TEquals(2, rows[2].doc._conflicts.length);
-
- // test the all docs collates sanely
- db.save({_id: "Z", foo: "Z"});
- db.save({_id: "a", foo: "a"});
-
- var rows = db.allDocs({startkey: "Z", endkey: "Z"}).rows;
- T(rows.length == 1);
-
- // cleanup
- db.deleteDb();
-};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/attachment_names.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/attachment_names.js b/share/www/script/test/attachment_names.js
deleted file mode 100644
index c9a5fcc..0000000
--- a/share/www/script/test/attachment_names.js
+++ /dev/null
@@ -1,96 +0,0 @@
-// 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.attachment_names = function(debug) {
- var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
- db.deleteDb();
- db.createDb();
- if (debug) debugger;
-
- var goodDoc = {
- _id: "good_doc",
- _attachments: {
- "Колян.txt": {
- content_type:"application/octet-stream",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
-
- var save_response = db.save(goodDoc);
- T(save_response.ok);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/good_doc/Колян.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.getResponseHeader("Content-Type") == "application/octet-stream");
- TEquals("\"aEI7pOYCRBLTRQvvqYrrJQ==\"", xhr.getResponseHeader("Etag"));
-
- var binAttDoc = {
- _id: "bin_doc",
- _attachments:{
- "foo\x80txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
-
- // inline attachments
- resp = db.save(binAttDoc);
- TEquals(true, resp.ok, "attachment_name: inline attachment");
-
-
- // standalone docs
- var bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])} ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np";
-
-
- var xhr = (CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment\x80txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:bin_data
- }));
-
- var resp = JSON.parse(xhr.responseText);
- TEquals(201, xhr.status, "attachment_name: standalone API");
- TEquals(true, resp.ok, "attachment_name: standalone API");
-
- // bulk docs
- var docs = { docs: [binAttDoc] };
-
- var xhr = CouchDB.request("POST", "/test_suite_db/_bulk_docs", {
- body: JSON.stringify(docs)
- });
-
- TEquals(201, xhr.status, "attachment_name: bulk docs");
-
-
- // leading underscores
- var binAttDoc = {
- _id: "bin_doc2",
- _attachments:{
- "_foo.txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
-
- try {
- db.save(binAttDoc);
- TEquals(1, 2, "Attachment name with leading underscore saved. Should never show!");
- } catch (e) {
- TEquals("bad_request", e.error, "attachment_name: leading underscore");
- TEquals("Attachment name can't start with '_'", e.reason, "attachment_name: leading underscore");
- }
-
- // todo: form uploads, waiting for cmlenz' test case for form uploads
-
-};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/attachment_paths.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/attachment_paths.js b/share/www/script/test/attachment_paths.js
deleted file mode 100644
index 3f6ffb7..0000000
--- a/share/www/script/test/attachment_paths.js
+++ /dev/null
@@ -1,153 +0,0 @@
-// 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.attachment_paths = function(debug) {
- if (debug) debugger;
- var dbNames = ["test_suite_db", "test_suite_db/with_slashes"];
- for (var i=0; i < dbNames.length; i++) {
- var db = new CouchDB(dbNames[i]);
- var dbName = encodeURIComponent(dbNames[i]);
- db.deleteDb();
- db.createDb();
-
- // first just save a regular doc with an attachment that has a slash in the url.
- // (also gonna run an encoding check case)
- var binAttDoc = {
- _id: "bin_doc",
- _attachments:{
- "foo/bar.txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- },
- "foo%2Fbaz.txt": {
- content_type:"text/plain",
- data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
- }
- }
- };
-
- T(db.save(binAttDoc).ok);
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo/bar.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.getResponseHeader("Content-Type") == "text/plain");
-
- // lets try it with an escaped attachment id...
- // weird that it's at two urls
- var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%2Fbar.txt");
- T(xhr.status == 200);
- // xhr.responseText == "This is a base64 encoded text"
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo/baz.txt");
- T(xhr.status == 404);
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%252Fbaz.txt");
- T(xhr.status == 200);
- T(xhr.responseText == "We like percent two F.");
-
- // require a _rev to PUT
- var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/attachment.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:"Just some text"
- });
- T(xhr.status == 409);
-
- var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
- body:"This is no base64 encoded text",
- headers:{"Content-Type": "text/plain;charset=utf-8"}
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
-
- binAttDoc = db.open("bin_doc");
-
- T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
- T(binAttDoc._attachments["foo%2Fbaz.txt"] !== undefined);
- T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
- TEquals("text/plain;charset=utf-8", // thank you Safari
- binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
- "correct content-type"
- );
- T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
-
- //// now repeat the while thing with a design doc
-
- // first just save a regular doc with an attachment that has a slash in the url.
- // (also gonna run an encoding check case)
- var binAttDoc = {
- _id: "_design/bin_doc",
- _attachments:{
- "foo/bar.txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- },
- "foo%2Fbaz.txt": {
- content_type:"text/plain",
- data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
- }
- }
- };
-
- T(db.save(binAttDoc).ok);
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/bar.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.getResponseHeader("Content-Type") == "text/plain");
-
- // lets try it with an escaped attachment id...
- // weird that it's at two urls
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%2Fbar.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.status == 200);
-
- // err, 3 urls
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo%2Fbar.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.status == 200);
-
- // I mean um, 4 urls
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo/bar.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.status == 200);
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/baz.txt");
- T(xhr.status == 404);
-
- var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%252Fbaz.txt");
- T(xhr.status == 200);
- T(xhr.responseText == "We like percent two F.");
-
- // require a _rev to PUT
- var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/attachment.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:"Just some text"
- });
- T(xhr.status == 409);
-
- var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
- body:"This is no base64 encoded text",
- headers:{"Content-Type": "text/plain;charset=utf-8"}
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
-
- binAttDoc = db.open("_design/bin_doc");
-
- T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
- T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
- TEquals("text/plain;charset=utf-8", // thank you Safari
- binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
- "correct content-type"
- );
- T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
- }
-};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/attachment_ranges.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/attachment_ranges.js b/share/www/script/test/attachment_ranges.js
deleted file mode 100644
index 7d9afb5..0000000
--- a/share/www/script/test/attachment_ranges.js
+++ /dev/null
@@ -1,160 +0,0 @@
-// 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.
-
-function cacheBust() {
- return "?anti-cache=" + String(Math.round(Math.random() * 1000000));
-};
-
-couchTests.attachment_ranges = function(debug) {
- var db = new CouchDB("test_suite_db", {
- "X-Couch-Full-Commit": "false"
- });
- db.deleteDb();
- db.createDb();
-
- if (debug) debugger;
-
- if((typeof window != "undefined") && window.navigator.userAgent.match(/Chrome/)) {
- // Chrome is broken.
- return;
- }
-
- var binAttDoc = {
- _id: "bin_doc",
- _attachments: {
- "foo.txt": {
- content_type: "application/octet-stream",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
-
- var save_response = db.save(binAttDoc);
- T(save_response.ok);
-
- // Fetching the whole entity is a 206.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-28"
- }
- });
- TEquals(206, xhr.status, "fetch 0-28");
- TEquals("This is a base64 encoded text", xhr.responseText);
- TEquals("bytes 0-28/29", xhr.getResponseHeader("Content-Range"));
- TEquals("29", xhr.getResponseHeader("Content-Length"));
-
- // Fetch the whole entity without an end offset is a 200.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-"
- }
- });
- TEquals(200, xhr.status, "fetch 0-");
- TEquals("This is a base64 encoded text", xhr.responseText);
- TEquals(null, xhr.getResponseHeader("Content-Range"));
- TEquals("29", xhr.getResponseHeader("Content-Length"));
-
- // Even if you ask multiple times.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-,0-,0-"
- }
- });
- TEquals(200, xhr.status, "multiple 0-'s");
-
- // Badly formed range header is a 200.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes:0-"
- }
- });
- TEquals(200, xhr.status, "fetch with bad range header");
-
- // Fetch the end of an entity without an end offset is a 206.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=2-"
- }
- });
- TEquals(206, xhr.status, "fetch 2-");
- TEquals("is is a base64 encoded text", xhr.responseText);
- TEquals("bytes 2-28/29", xhr.getResponseHeader("Content-Range"));
- TEquals("27", xhr.getResponseHeader("Content-Length"));
-
- // Fetch past the end of the entity is a 206
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-29"
- }
- });
- TEquals(206, xhr.status, "fetch 0-29");
- TEquals("bytes 0-28/29", xhr.getResponseHeader("Content-Range"));
- TEquals("29", xhr.getResponseHeader("Content-Length"));
-
- // Fetch first part of entity is a 206
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-3"
- }
- });
- TEquals(206, xhr.status, "fetch 0-3");
- TEquals("This", xhr.responseText);
- TEquals("4", xhr.getResponseHeader("Content-Length"));
- TEquals("bytes 0-3/29", xhr.getResponseHeader("Content-Range"));
-
- // Fetch middle of entity is also a 206
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=10-15"
- }
- });
- TEquals(206, xhr.status, "fetch 10-15");
- TEquals("base64", xhr.responseText);
- TEquals("6", xhr.getResponseHeader("Content-Length"));
- TEquals("bytes 10-15/29", xhr.getResponseHeader("Content-Range"));
-
- // Fetch end of entity is also a 206
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=-3"
- }
- });
- TEquals(206, xhr.status, "fetch -3");
- TEquals("ext", xhr.responseText);
- TEquals("3", xhr.getResponseHeader("Content-Length"));
- TEquals("bytes 26-28/29", xhr.getResponseHeader("Content-Range"));
-
- // backward range is 416
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=5-3"
- }
- });
- TEquals(416, xhr.status, "fetch 5-3");
-
- // range completely outside of entity is 416
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=300-310"
- }
- });
- TEquals(416, xhr.status, "fetch 300-310");
-
- // We ignore a Range header with too many ranges
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt" + cacheBust(), {
- headers: {
- "Range": "bytes=0-1,0-1,0-1,0-1,0-1,0-1,0-1,0-1,0-1,0-1"
- }
- });
- TEquals(200, xhr.status, "too many ranges");
-
-};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/attachment_views.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/attachment_views.js b/share/www/script/test/attachment_views.js
deleted file mode 100644
index b55aabe..0000000
--- a/share/www/script/test/attachment_views.js
+++ /dev/null
@@ -1,140 +0,0 @@
-// 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.attachment_views= function(debug) {
-
- var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
- db.deleteDb();
- db.createDb();
- if (debug) debugger;
-
- // count attachments in a view
-
- var attachmentData = "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=";
-
- db.bulkSave(makeDocs(0, 10));
-
- db.bulkSave(makeDocs(10, 20, {
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: attachmentData
- }
- }
- }));
-
- db.bulkSave(makeDocs(20, 30, {
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: attachmentData
- },
- "bar.txt": {
- content_type:"text/plain",
- data: attachmentData
- }
- }
- }));
-
- db.bulkSave(makeDocs(30, 40, {
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: attachmentData
- },
- "bar.txt": {
- content_type:"text/plain",
- data: attachmentData
- },
- "baz.txt": {
- content_type:"text/plain",
- data: attachmentData
- }
- }
- }));
-
- var mapFunction = function(doc) {
- var count = 0;
-
- for(var idx in doc._attachments) {
- count = count + 1;
- }
-
- emit(parseInt(doc._id), count);
- };
-
- var reduceFunction = function(key, values) {
- return sum(values);
- };
-
- var result = db.query(mapFunction, reduceFunction);
-
- T(result.rows.length == 1);
- T(result.rows[0].value == 60);
-
- var result = db.query(mapFunction, reduceFunction, {
- startkey:10,
- endkey:19
- });
-
- T(result.rows.length == 1);
- T(result.rows[0].value == 10);
-
- var result = db.query(mapFunction, reduceFunction, {
- startkey:20,
- endkey:29
- });
-
- T(result.rows.length == 1);
- T(result.rows[0].value == 20);
-
- var result = db.query(mapFunction, null, {
- startkey: 30,
- endkey: 39,
- include_docs: true
- });
-
- T(result.rows.length == 10);
- T(result.rows[0].value == 3);
- T(result.rows[0].doc._attachments['baz.txt'].stub === true);
- T(result.rows[0].doc._attachments['baz.txt'].data === undefined);
- T(result.rows[0].doc._attachments['baz.txt'].encoding === undefined);
- T(result.rows[0].doc._attachments['baz.txt'].encoded_length === undefined);
-
- var result = db.query(mapFunction, null, {
- startkey: 30,
- endkey: 39,
- include_docs: true,
- attachments: true
- });
-
- T(result.rows.length == 10);
- T(result.rows[0].value == 3);
- T(result.rows[0].doc._attachments['baz.txt'].data === attachmentData);
- T(result.rows[0].doc._attachments['baz.txt'].stub === undefined);
- T(result.rows[0].doc._attachments['baz.txt'].encoding === undefined);
- T(result.rows[0].doc._attachments['baz.txt'].encoded_length === undefined);
-
- var result = db.query(mapFunction, null, {
- startkey: 30,
- endkey: 39,
- include_docs: true,
- att_encoding_info: true
- });
-
- T(result.rows.length == 10);
- T(result.rows[0].value == 3);
- T(result.rows[0].doc._attachments['baz.txt'].data === undefined);
- T(result.rows[0].doc._attachments['baz.txt'].stub === true);
- T(result.rows[0].doc._attachments['baz.txt'].encoding === "gzip");
- T(result.rows[0].doc._attachments['baz.txt'].encoded_length === 47);
-};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/www/script/test/attachments.js
----------------------------------------------------------------------
diff --git a/share/www/script/test/attachments.js b/share/www/script/test/attachments.js
deleted file mode 100644
index 2fa08ee..0000000
--- a/share/www/script/test/attachments.js
+++ /dev/null
@@ -1,328 +0,0 @@
-// 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.attachments= function(debug) {
- var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
- db.deleteDb();
- db.createDb();
- if (debug) debugger;
-
-
- // MD5 Digests of compressible attachments and therefore Etags
- // will vary depending on platform gzip implementation.
- // These MIME types are defined in [attachments] compressible_types
- var binAttDoc = {
- _id: "bin_doc",
- _attachments:{
- "foo.txt": {
- content_type:"application/octet-stream",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
-
- var save_response = db.save(binAttDoc);
- T(save_response.ok);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt");
- T(xhr.responseText == "This is a base64 encoded text");
- T(xhr.getResponseHeader("Content-Type") == "application/octet-stream");
- TEquals("\"aEI7pOYCRBLTRQvvqYrrJQ==\"", xhr.getResponseHeader("Etag"));
-
- // empty attachment
- var binAttDoc2 = {
- _id: "bin_doc2",
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: ""
- }
- }
- }
-
- T(db.save(binAttDoc2).ok);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo.txt");
- T(xhr.responseText.length == 0);
- T(xhr.getResponseHeader("Content-Type") == "text/plain");
-
- // test RESTful doc API
-
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt?rev=" + binAttDoc2._rev, {
- body:"This is no base64 encoded text",
- headers:{"Content-Type": "text/plain;charset=utf-8"}
- });
- T(xhr.status == 201);
- TEquals("/bin_doc2/foo2.txt",
- xhr.getResponseHeader("Location").substr(-18),
- "should return Location header to newly created or updated attachment");
-
- var rev = JSON.parse(xhr.responseText).rev;
-
- binAttDoc2 = db.open("bin_doc2");
-
- T(binAttDoc2._attachments["foo.txt"] !== undefined);
- T(binAttDoc2._attachments["foo2.txt"] !== undefined);
- TEqualsIgnoreCase("text/plain;charset=utf-8", binAttDoc2._attachments["foo2.txt"].content_type);
- T(binAttDoc2._attachments["foo2.txt"].length == 30);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo2.txt");
- T(xhr.responseText == "This is no base64 encoded text");
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- // test without rev, should fail
- var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt");
- T(xhr.status == 409);
-
- // test with rev, should not fail
- var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt?rev=" + rev);
- T(xhr.status == 200);
- TEquals(null, xhr.getResponseHeader("Location"),
- "should not return Location header on DELETE request");
-
- // test binary data
- var bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])} ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np";
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:bin_data
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
- TEquals('"' + rev + '"', xhr.getResponseHeader("Etag"));
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt");
- T(xhr.responseText == bin_data);
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- // without rev
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:bin_data
- });
- T(xhr.status == 409);
-
- // with nonexistent rev
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt" + "?rev=1-adae8575ecea588919bd08eb020c708e", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:bin_data
- });
- T(xhr.status == 409);
-
- // with current rev
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev, {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:bin_data
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
- TEquals('"' + rev + '"', xhr.getResponseHeader("Etag"));
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt");
- T(xhr.responseText == bin_data);
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
- T(xhr.responseText == bin_data);
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
- T(xhr.status == 200);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt");
- T(xhr.status == 404);
-
- // deleted attachment is still accessible with revision
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev);
- T(xhr.status == 200);
- T(xhr.responseText == bin_data);
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- // empty attachments
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc4/attachment.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:""
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc4/attachment.txt");
- T(xhr.status == 200);
- T(xhr.responseText.length == 0);
-
- // overwrite previsously empty attachment
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc4/attachment.txt?rev=" + rev, {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:"This is a string"
- });
- T(xhr.status == 201);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc4/attachment.txt");
- T(xhr.status == 200);
- T(xhr.responseText == "This is a string");
-
- // Attachment sparseness COUCHDB-220
-
- var docs = [];
- for (var i = 0; i < 5; i++) {
- var doc = {
- _id: (i).toString(),
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
- docs.push(doc);
- }
-
- var saved = db.bulkSave(docs);
- // now delete the docs, and while we are looping over them, remove the
- // '_rev' field so we can re-create after deletion.
- var to_up = [];
- for (i=0;i<saved.length;i++) {
- to_up.push({'_id': saved[i]['id'], '_rev': saved[i]['rev'], '_deleted': true});
- delete docs[i]._rev;
- }
- // delete them.
- var saved2 = db.bulkSave(to_up);
- // re-create them
- var saved3 = db.bulkSave(docs);
-
- var before = db.info().disk_size;
-
- // Compact it.
- T(db.compact().ok);
- T(db.last_req.status == 202);
- // compaction isn't instantaneous, loop until done
- while (db.info().compact_running) {};
-
- var after = db.info().disk_size;
-
- // Compaction should reduce the database slightly, but not
- // orders of magnitude (unless attachments introduce sparseness)
- T(after > before * 0.1, "before: " + before + " after: " + after);
-
-
- // test large attachments - COUCHDB-366
- var lorem = CouchDB.request("GET", "/_utils/script/test/lorem.txt").responseText;
-
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/lorem.txt", {
- headers:{"Content-Type":"text/plain;charset=utf-8"},
- body:lorem
- });
- T(xhr.status == 201);
- var rev = JSON.parse(xhr.responseText).rev;
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt");
- T(xhr.responseText == lorem);
- TEqualsIgnoreCase("text/plain;charset=utf-8", xhr.getResponseHeader("Content-Type"));
-
- // test large inline attachment too
- var lorem_b64 = CouchDB.request("GET", "/_utils/script/test/lorem_b64.txt").responseText;
- var doc = db.open("bin_doc5", {attachments:true});
- T(doc._attachments["lorem.txt"].data == lorem_b64);
-
- // test etags for attachments.
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt");
- T(xhr.status == 200);
- var etag = xhr.getResponseHeader("etag");
- xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt", {
- headers: {"if-none-match": etag}
- });
- T(xhr.status == 304);
-
- // test COUCHDB-497 - empty attachments
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/empty.txt?rev="+rev, {
- headers:{"Content-Type":"text/plain;charset=utf-8", "Content-Length": "0"},
- body:""
- });
- TEquals(201, xhr.status, "should send 201 Accepted");
- var rev = JSON.parse(xhr.responseText).rev;
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/empty.txt?rev="+rev, {
- headers:{"Content-Type":"text/plain;charset=utf-8"}
- });
- TEquals(201, xhr.status, "should send 201 Accepted");
-
- // implicit doc creation allows creating docs with a reserved id. COUCHDB-565
- var xhr = CouchDB.request("PUT", "/test_suite_db/_nonexistant/attachment.txt", {
- headers: {"Content-Type":"text/plain;charset=utf-8"},
- body: "THIS IS AN ATTACHMENT. BOOYA!"
- });
- TEquals(400, xhr.status, "should return error code 400 Bad Request");
-
- // test COUCHDB-809 - stubs should only require the 'stub' field
- var bin_doc6 = {
- _id: "bin_doc6",
- _attachments:{
- "foo.txt": {
- content_type:"text/plain",
- data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
- }
- }
- };
- T(db.save(bin_doc6).ok);
- // stub out the attachment
- bin_doc6._attachments["foo.txt"] = { stub: true };
- T(db.save(bin_doc6).ok == true);
-
- // wrong rev pos specified
-
- // stub out the attachment with the wrong revpos
- bin_doc6._attachments["foo.txt"] = { stub: true, revpos: 10};
- try {
- T(db.save(bin_doc6).ok == true);
- T(false && "Shouldn't get here!");
- } catch (e) {
- T(e.error == "missing_stub");
- }
-
- // test MD5 header
- var bin_data = "foo bar"
- var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc7/attachment.txt", {
- headers:{"Content-Type":"application/octet-stream",
- "Content-MD5":"MntvB0NYESObxH4VRDUycw=="},
- body:bin_data
- });
- TEquals(201, xhr.status);
-
- var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc7/attachment.txt");
- TEquals('MntvB0NYESObxH4VRDUycw==', xhr.getResponseHeader("Content-MD5"));
-
- // test attachment via multipart/form-data
- var bin_doc8 = {
- _id: "bin_doc8"
- };
- T(db.save(bin_doc8).ok);
- var doc = db.open("bin_doc8");
- var body = "------TF\r\n" +
- "Content-Disposition: form-data; name=\"_rev\"\r\n\r\n" +
- doc._rev + "\r\n" +
- "------TF\r\n" +
- "Content-Disposition: form-data; name=\"_attachments\"; filename=\"file.txt\"\r\n" +
- "Content-Type: text/plain\r\n\r\n" +
- "contents of file.txt\r\n\r\n" +
- "------TF--"
- xhr = CouchDB.request("POST", "/test_suite_db/bin_doc8", {
- headers: {
- "Content-Type": "multipart/form-data; boundary=----TF",
- "Content-Length": body.length
- },
- body: body
- });
- TEquals(201, xhr.status);
- TEquals(true, JSON.parse(xhr.responseText).ok);
- var doc = db.open("bin_doc8");
- T(doc._attachments);
- T(doc._attachments['file.txt']);
-
-};