couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject [14/37] move JS tests into safety
Date Fri, 10 Oct 2014 19:12:28 GMT
http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/purge.js
----------------------------------------------------------------------
diff --git a/share/test/purge.js b/share/test/purge.js
new file mode 100644
index 0000000..2968913
--- /dev/null
+++ b/share/test/purge.js
@@ -0,0 +1,145 @@
+// 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.purge = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+
+  /*
+   purge is not to be confused with a document deletion.  It removes the
+   document and all edit history from the local instance of the database.
+  */
+
+  var numDocs = 10;
+
+  var designDoc = {
+    _id:"_design/test",
+    language: "javascript",
+    views: {
+      all_docs_twice: {map: "function(doc) { emit(doc.integer, null); emit(doc.integer, null)
}"},
+      single_doc: {map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"}
+    }
+  };
+
+  T(db.save(designDoc).ok);
+
+  db.bulkSave(makeDocs(1, numDocs + 1));
+
+  // go ahead and validate the views before purging
+  var rows = db.view("test/all_docs_twice").rows;
+  for (var i = 0; i < numDocs; i++) {
+    T(rows[2*i].key == i+1);
+    T(rows[(2*i)+1].key == i+1);
+  }
+  T(db.view("test/single_doc").total_rows == 1);
+
+  var info = db.info();
+  var doc1 = db.open("1");
+  var doc2 = db.open("2");
+
+  // purge the documents
+  var xhr = CouchDB.request("POST", "/test_suite_db/_purge", {
+    body: JSON.stringify({"1":[doc1._rev], "2":[doc2._rev]})
+  });
+  T(xhr.status == 200);
+
+  var result = JSON.parse(xhr.responseText);
+  var newInfo = db.info();
+  
+  // purging increments the update sequence
+  T(info.update_seq+1 == newInfo.update_seq);
+  // and it increments the purge_seq
+  T(info.purge_seq+1 == newInfo.purge_seq);
+  T(result.purge_seq == newInfo.purge_seq);
+
+  T(result.purged["1"][0] == doc1._rev);
+  T(result.purged["2"][0] == doc2._rev);
+
+  T(db.open("1") == null);
+  T(db.open("2") == null);
+
+  var rows = db.view("test/all_docs_twice").rows;
+  for (var i = 2; i < numDocs; i++) {
+    T(rows[2*(i-2)].key == i+1);
+    T(rows[(2*(i-2))+1].key == i+1);
+  }
+  T(db.view("test/single_doc").total_rows == 0);
+
+  // purge sequences are preserved after compaction (COUCHDB-1021)
+  T(db.compact().ok);
+  T(db.last_req.status == 202);
+  // compaction isn't instantaneous, loop until done
+  while (db.info().compact_running) {};
+  var compactInfo = db.info();
+  T(compactInfo.purge_seq == newInfo.purge_seq);
+
+  // purge documents twice in a row without loading views
+  // (causes full view rebuilds)
+
+  var doc3 = db.open("3");
+  var doc4 = db.open("4");
+
+  xhr = CouchDB.request("POST", "/test_suite_db/_purge", {
+    body: JSON.stringify({"3":[doc3._rev]})
+  });
+
+  T(xhr.status == 200);
+
+  xhr = CouchDB.request("POST", "/test_suite_db/_purge", {
+    body: JSON.stringify({"4":[doc4._rev]})
+  });
+
+  T(xhr.status == 200);
+  result = JSON.parse(xhr.responseText);
+  T(result.purge_seq == db.info().purge_seq);
+
+  var rows = db.view("test/all_docs_twice").rows;
+  for (var i = 4; i < numDocs; i++) {
+    T(rows[2*(i-4)].key == i+1);
+    T(rows[(2*(i-4))+1].key == i+1);
+  }
+  T(db.view("test/single_doc").total_rows == 0);
+
+  // COUCHDB-1065
+  var dbA = new CouchDB("test_suite_db_a");
+  var dbB = new CouchDB("test_suite_db_b");
+  dbA.deleteDb();
+  dbA.createDb();
+  dbB.deleteDb();
+  dbB.createDb();
+  var docA = {_id:"test", a:1};
+  var docB = {_id:"test", a:2};
+  dbA.save(docA);
+  dbB.save(docB);
+  CouchDB.replicate(dbA.name, dbB.name);
+  var xhr = CouchDB.request("POST", "/" + dbB.name + "/_purge", {
+    body: JSON.stringify({"test":[docA._rev]})
+  });
+  TEquals(200, xhr.status, "single rev purge after replication succeeds");
+
+  var xhr = CouchDB.request("GET", "/" + dbB.name + "/test?rev=" + docA._rev);
+  TEquals(404, xhr.status, "single rev purge removes revision");
+
+  var xhr = CouchDB.request("POST", "/" + dbB.name + "/_purge", {
+    body: JSON.stringify({"test":[docB._rev]})
+  });
+  TEquals(200, xhr.status, "single rev purge after replication succeeds");
+  var xhr = CouchDB.request("GET", "/" + dbB.name + "/test?rev=" + docB._rev);
+  TEquals(404, xhr.status, "single rev purge removes revision");
+
+  var xhr = CouchDB.request("POST", "/" + dbB.name + "/_purge", {
+    body: JSON.stringify({"test":[docA._rev, docB._rev]})
+  });
+  TEquals(200, xhr.status, "all rev purge after replication succeeds");
+};

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/reader_acl.js
----------------------------------------------------------------------
diff --git a/share/test/reader_acl.js b/share/test/reader_acl.js
new file mode 100644
index 0000000..ff770c7
--- /dev/null
+++ b/share/test/reader_acl.js
@@ -0,0 +1,219 @@
+// 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.reader_acl = function(debug) {
+  // this tests read access control
+
+  var usersDb = new CouchDB("test_suite_users", {"X-Couch-Full-Commit":"false"});
+  var secretDb = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  function testFun() {
+    try {
+      usersDb.deleteDb();
+      try {
+        usersDb.createDb();
+      } catch(e) {
+        if(usersDb.last_req.status != 412) {
+         throw e;
+        }
+      }
+      secretDb.deleteDb();
+      secretDb.createDb();
+
+      // create a user with top-secret-clearance
+      var jchrisUserDoc = CouchDB.prepareUserDoc({
+        name: "jchris@apache.org",
+        roles : ["top-secret"]
+      }, "funnybone");
+      T(usersDb.save(jchrisUserDoc).ok);
+      usersDb.ensureFullCommit();
+
+      T(CouchDB.session().userCtx.name == null);
+
+      // set secret db to be read controlled
+      T(secretDb.save({_id:"baz",foo:"bar"}).ok);
+      T(secretDb.open("baz").foo == "bar");
+
+      T(secretDb.setSecObj({
+        "members" : {
+          roles : ["super-secret-club"],
+          names : ["joe","barb"]
+        }
+      }).ok);
+    } finally {
+      CouchDB.logout();
+    }
+  }
+  
+  // split into 2 funs so we can test restart behavior
+  function testFun2() {
+    try {
+      // can't read it as jchris b/c he's missing the needed role
+      T(CouchDB.login("jchris@apache.org", "funnybone").ok);
+      T(CouchDB.session().userCtx.name == "jchris@apache.org");
+
+      try {
+        secretDb.open("baz");
+        T(false && "can't open a doc from a secret db") ;
+      } catch(e) {
+        T(true)
+      }
+
+      CouchDB.logout();
+      
+      // make anyone with the top-secret role an admin
+      // db admins are automatically members
+      T(secretDb.setSecObj({
+        "admins" : {
+          roles : ["top-secret"],
+          names : []
+        },
+        "members" : {
+          roles : ["super-secret-club"],
+          names : ["joe","barb"]
+        }
+      }).ok);
+
+
+      T(CouchDB.login("jchris@apache.org", "funnybone").ok);
+
+      // db admin can read
+      T(secretDb.open("baz").foo == "bar");
+
+      // and run temp views
+      TEquals(secretDb.query(function(doc) {
+        emit(null, null)
+      }).total_rows, 1);
+
+      CouchDB.logout();
+      T(CouchDB.session().userCtx.roles.indexOf("_admin") != -1);
+
+      // admin now adds the top-secret role to the db's members
+      // and removes db-admins
+      T(secretDb.setSecObj({
+        "admins" : {
+          roles : [],
+          names : []
+        },
+        "members" : {
+          roles : ["super-secret-club", "top-secret"],
+          names : ["joe","barb"]
+        }
+      }).ok);
+
+      // server _admin can always read
+      T(secretDb.open("baz").foo == "bar");
+
+      // and run temp views
+      TEquals(secretDb.query(function(doc) {
+        emit(null, null)
+      }).total_rows, 1);
+
+      T(secretDb.save({
+        "_id" : "_design/foo",
+        views : {
+          bar : {
+            map : "function(doc){emit(null, null)}"
+          }
+        }
+      }).ok)
+
+      // now top-secret users can read too
+      T(CouchDB.login("jchris@apache.org", "funnybone").ok);
+      T(CouchDB.session().userCtx.roles.indexOf("_admin") == -1);
+      T(secretDb.open("baz").foo == "bar");
+      // members can query stored views
+      T(secretDb.view("foo/bar").total_rows == 1);
+      
+      // members can't do temp views
+      try {
+        var results = secretDb.query(function(doc) {
+          emit(null, null);
+        });
+        T(false && "temp view should be admin only");
+      } catch (e) {
+        T(true && "temp view is admin only");
+      }
+      
+      CouchDB.logout();
+
+      // works with readers (backwards compat with 1.0)
+      T(secretDb.setSecObj({
+        "admins" : {
+          roles : [],
+          names : []
+        },
+        "readers" : {
+          roles : ["super-secret-club", "top-secret"],
+          names : ["joe","barb"]
+        }
+      }).ok);
+
+      T(CouchDB.login("jchris@apache.org", "funnybone").ok);
+      T(CouchDB.session().userCtx.roles.indexOf("_admin") == -1);
+      T(secretDb.open("baz").foo == "bar");
+
+      // can't set non string reader names or roles
+      try {
+        secretDb.setSecObj({
+          "members" : {
+            roles : ["super-secret-club", {"top-secret":"awesome"}],
+            names : ["joe","barb"]
+          }
+        })
+        T(false && "only string roles");
+      } catch (e) {}
+
+      try {
+        secretDb.setSecObj({
+          "members" : {
+            roles : ["super-secret-club", {"top-secret":"awesome"}],
+            names : ["joe",22]
+          }
+        });
+        T(false && "only string names");
+      } catch (e) {}
+      
+      try {
+        secretDb.setSecObj({
+          "members" : {
+            roles : ["super-secret-club", {"top-secret":"awesome"}],
+            names : "joe"
+          }
+        });
+        T(false && "only lists of names");
+      } catch (e) {}
+    } finally {
+      CouchDB.logout();
+    }
+  };
+
+  run_on_modified_server(
+    [{section: "httpd",
+      key: "authentication_handlers",
+      value: "{couch_httpd_auth, cookie_authentication_handler}, {couch_httpd_auth, default_authentication_handler}"},
+     {section: "couch_httpd_auth",
+      key: "authentication_db", value: "test_suite_users"}],
+    testFun
+  );
+        
+  // security changes will always commit synchronously
+  restartServer();
+  
+  run_on_modified_server(
+    [{section: "httpd",
+      key: "authentication_handlers",
+      value: "{couch_httpd_auth, cookie_authentication_handler}, {couch_httpd_auth, default_authentication_handler}"},
+     {section: "couch_httpd_auth",
+      key: "authentication_db", value: "test_suite_users"}],
+    testFun2
+  );
+}

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/recreate_doc.js
----------------------------------------------------------------------
diff --git a/share/test/recreate_doc.js b/share/test/recreate_doc.js
new file mode 100644
index 0000000..f972379
--- /dev/null
+++ b/share/test/recreate_doc.js
@@ -0,0 +1,145 @@
+// 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.recreate_doc = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+
+  // First create a new document with the ID "foo", and delete it again
+  var doc = {_id: "foo", a: "bar", b: 42};
+  var result = db.save(doc);
+  T(result.ok);
+  var firstRev = result.rev;
+  T(db.deleteDoc(doc).ok);
+
+  // Now create a new document with the same ID, save it, and then modify it
+  for (var i = 0; i < 10; i++) {
+    doc = {_id: "foo"};
+    T(db.save(doc).ok);
+    doc = db.open("foo");
+    doc.a = "baz";
+    T(db.save(doc).ok);
+    T(db.deleteDoc(doc).rev != undefined);
+  }
+
+  try {
+    // COUCHDB-292 now attempt to save the document with a prev that's since
+    // been deleted and this should generate a conflict exception
+    db.save({_id:"foo", _rev:firstRev, bar:1});
+    T("no save conflict 1" && false); // we shouldn't hit here
+  } catch (e) {
+    T(e.error == "conflict");
+  }
+  
+  var binAttDoc = {
+    _id: "foo",
+    _rev:firstRev,
+    _attachments:{
+      "foo.txt": {
+        content_type:"text/plain",
+        data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
+      }
+    }
+  };
+  try {
+    // same as before, but with binary
+    db.save(binAttDoc);
+    T("no save conflict 2" && false); // we shouldn't hit here
+  } catch (e) {
+    T(e.error == "conflict");
+  }
+
+
+  try {
+    // random non-existant prev rev
+    db.save({_id:"foo", _rev:"1-asfafasdf", bar:1});
+    T("no save conflict 3" && false); // we shouldn't hit here
+  } catch (e) {
+    T(e.error == "conflict");
+  }
+  
+  try {
+    // random non-existant prev rev with bin
+    binAttDoc._rev = "1-aasasfasdf";
+    db.save(binAttDoc);
+    T("no save conflict 4" && false); // we shouldn't hit here
+  } catch (e) {
+    T(e.error == "conflict");
+  }
+
+  db.deleteDb();
+  db.createDb();
+
+  // Helper function to create a doc with multiple revisions
+  // that are compacted away to ?REV_MISSING.
+
+  var createDoc = function(docid) {
+    var ret = [{_id: docid, count: 0}];
+    T(db.save(ret[0]).ok);
+    for(var i = 0; i < 2; i++) {
+      ret[ret.length] = {
+        _id: docid,
+        _rev: ret[ret.length-1]._rev,
+        count: ret[ret.length-1].count+1
+      };
+      T(db.save(ret[ret.length-1]).ok);
+    }
+    db.compact();
+    while(db.info().compact_running) {}
+    return ret;
+  }
+
+  // Helper function to check that there are no duplicates
+  // in the changes feed and that it has proper update
+  // sequence ordering.
+
+  var checkChanges = function() {
+    // Assert that there are no duplicates in _changes.
+    var req = CouchDB.request("GET", "/test_suite_db/_changes");
+    var resp = JSON.parse(req.responseText);
+    var docids = {};
+    var prev_seq = -1;
+    for(var i = 0; i < resp.results.length; i++) {
+      row = resp.results[i];
+      T(row.seq > prev_seq, "Unordered _changes feed.");
+      T(docids[row.id] === undefined, "Duplicates in _changes feed.");
+      prev_seq = row.seq;
+      docids[row.id] = true;
+    }
+  };
+
+  // COUCHDB-1265 - Check that the changes feed remains proper
+  // after we try and break the update_seq tree.
+
+  // This first case is the one originally reported and "fixed"
+  // in COUCHDB-1265. Reinserting an old revision into the
+  // revision tree causes duplicates in the update_seq tree.
+
+  var revs = createDoc("a");
+  T(db.save(revs[1], {new_edits: false}).ok);
+  T(db.save(revs[revs.length-1]).ok);
+  checkChanges();
+
+  // The original fix for COUCHDB-1265 is not entirely correct
+  // as it didn't consider the possibility that a compaction
+  // might run after the original tree screw up.
+
+  revs = createDoc("b");
+  T(db.save(revs[1], {new_edits: false}).ok);
+  db.compact();
+  while(db.info().compact_running) {}
+  T(db.save(revs[revs.length-1]).ok);
+  checkChanges();
+
+};

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/reduce.js
----------------------------------------------------------------------
diff --git a/share/test/reduce.js b/share/test/reduce.js
new file mode 100644
index 0000000..36e5cb7
--- /dev/null
+++ b/share/test/reduce.js
@@ -0,0 +1,414 @@
+// 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.reduce = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+  var numDocs = 500;
+  var docs = makeDocs(1,numDocs + 1);
+  db.bulkSave(docs);
+  var summate = function(N) {return (N+1)*N/2;};
+
+  var map = function (doc) {
+      emit(doc.integer, doc.integer);
+      emit(doc.integer, doc.integer);
+  };
+  var reduce = function (keys, values) { return sum(values); };
+  var result = db.query(map, reduce);
+  T(result.rows[0].value == 2*summate(numDocs));
+
+  result = db.query(map, reduce, {startkey: 4, endkey: 4});
+  T(result.rows[0].value == 8);
+
+  result = db.query(map, reduce, {startkey: 4, endkey: 5});
+  T(result.rows[0].value == 18);
+
+  result = db.query(map, reduce, {startkey: 4, endkey: 6});
+  T(result.rows[0].value == 30);
+
+  result = db.query(map, reduce, {group:true, limit:3});
+  T(result.rows[0].value == 2);
+  T(result.rows[1].value == 4);
+  T(result.rows[2].value == 6);
+
+  for(var i=1; i<numDocs/2; i+=30) {
+    result = db.query(map, reduce, {startkey: i, endkey: numDocs - i});
+    T(result.rows[0].value == 2*(summate(numDocs-i) - summate(i-1)));
+  }
+
+  db.deleteDb();
+  db.createDb();
+
+  for(var i=1; i <= 5; i++) {
+
+    for(var j=0; j < 10; j++) {
+      // these docs are in the order of the keys collation, for clarity
+      var docs = [];
+      docs.push({keys:["a"]});
+      docs.push({keys:["a"]});
+      docs.push({keys:["a", "b"]});
+      docs.push({keys:["a", "b"]});
+      docs.push({keys:["a", "b", "c"]});
+      docs.push({keys:["a", "b", "d"]});
+      docs.push({keys:["a", "c", "d"]});
+      docs.push({keys:["d"]});
+      docs.push({keys:["d", "a"]});
+      docs.push({keys:["d", "b"]});
+      docs.push({keys:["d", "c"]});
+      db.bulkSave(docs);
+      T(db.info().doc_count == ((i - 1) * 10 * 11) + ((j + 1) * 11));
+    }
+
+    map = function (doc) { emit(doc.keys, 1); };
+    reduce = function (keys, values) { return sum(values); };
+
+    var results = db.query(map, reduce, {group:true});
+
+    //group by exact key match
+    T(equals(results.rows[0], {key:["a"],value:20*i}));
+    T(equals(results.rows[1], {key:["a","b"],value:20*i}));
+    T(equals(results.rows[2], {key:["a", "b", "c"],value:10*i}));
+    T(equals(results.rows[3], {key:["a", "b", "d"],value:10*i}));
+
+    // test to make sure group reduce and limit params provide valid json
+    var results = db.query(map, reduce, {group: true, limit: 2});
+    T(equals(results.rows[0], {key: ["a"], value: 20*i}));
+    T(equals(results.rows.length, 2));
+
+    //group by the first element in the key array
+    var results = db.query(map, reduce, {group_level:1});
+    T(equals(results.rows[0], {key:["a"],value:70*i}));
+    T(equals(results.rows[1], {key:["d"],value:40*i}));
+
+    //group by the first 2 elements in the key array
+    var results = db.query(map, reduce, {group_level:2});
+    T(equals(results.rows[0], {key:["a"],value:20*i}));
+    T(equals(results.rows[1], {key:["a","b"],value:40*i}));
+    T(equals(results.rows[2], {key:["a","c"],value:10*i}));
+    T(equals(results.rows[3], {key:["d"],value:10*i}));
+    T(equals(results.rows[4], {key:["d","a"],value:10*i}));
+    T(equals(results.rows[5], {key:["d","b"],value:10*i}));
+    T(equals(results.rows[6], {key:["d","c"],value:10*i}));
+
+    // endkey test with inclusive_end=true
+    var results = db.query(map, reduce, {group_level:2,endkey:["d"],inclusive_end:true});
+    T(equals(results.rows[0], {key:["a"],value:20*i}));
+    T(equals(results.rows[1], {key:["a","b"],value:40*i}));
+    T(equals(results.rows[2], {key:["a","c"],value:10*i}));
+    T(equals(results.rows[3], {key:["d"],value:10*i}));
+    TEquals(4, results.rows.length);
+
+    // endkey test with inclusive_end=false
+    var results = db.query(map, reduce, {group_level:2,endkey:["d"],inclusive_end:false});
+    T(equals(results.rows[0], {key:["a"],value:20*i}));
+    T(equals(results.rows[1], {key:["a","b"],value:40*i}));
+    T(equals(results.rows[2], {key:["a","c"],value:10*i}));
+    TEquals(3, results.rows.length);
+  }
+
+  // now test out more complex reductions that need to use the combine option.
+
+  db.deleteDb();
+  db.createDb();
+
+
+  var map = function (doc) { emit(doc.val, doc.val); };
+  var reduceCombine = function (keys, values, rereduce) {
+      // This computes the standard deviation of the mapped results
+      var stdDeviation=0.0;
+      var count=0;
+      var total=0.0;
+      var sqrTotal=0.0;
+
+      if (!rereduce) {
+        // This is the reduce phase, we are reducing over emitted values from
+        // the map functions.
+        for(var i in values) {
+          total = total + values[i];
+          sqrTotal = sqrTotal + (values[i] * values[i]);
+        }
+        count = values.length;
+      }
+      else {
+        // This is the rereduce phase, we are re-reducing previosuly
+        // reduced values.
+        for(var i in values) {
+          count = count + values[i].count;
+          total = total + values[i].total;
+          sqrTotal = sqrTotal + values[i].sqrTotal;
+        }
+      }
+
+      var variance =  (sqrTotal - ((total * total)/count)) / count;
+      stdDeviation = Math.sqrt(variance);
+
+      // the reduce result. It contains enough information to be rereduced
+      // with other reduce results.
+      return {"stdDeviation":stdDeviation,"count":count,
+          "total":total,"sqrTotal":sqrTotal};
+    };
+
+    // Save a bunch a docs.
+
+  for(var i=0; i < 10; i++) {
+    var docs = [];
+    docs.push({val:10});
+    docs.push({val:20});
+    docs.push({val:30});
+    docs.push({val:40});
+    docs.push({val:50});
+    docs.push({val:60});
+    docs.push({val:70});
+    docs.push({val:80});
+    docs.push({val:90});
+    docs.push({val:100});
+    db.bulkSave(docs);
+  }
+
+  var results = db.query(map, reduceCombine);
+
+  var difference = results.rows[0].value.stdDeviation - 28.722813232690143;
+  // account for floating point rounding error
+  T(Math.abs(difference) < 0.0000000001);
+
+  function testReducePagination() {
+    var ddoc = {
+      "_id": "_design/test",
+      "language": "javascript",
+      "views": {
+        "view1": {
+          "map": "function(doc) {" +
+             "emit(doc.int, doc._id);" +
+             "emit(doc.int + 1, doc._id);" +
+             "emit(doc.int + 2, doc._id);" +
+          "}",
+          "reduce": "_count"
+        }
+      }
+    };
+    var result, docs = [];
+
+    function randVal() {
+        return Math.random() * 100000000;
+    }
+
+    db.deleteDb();
+    db.createDb();
+
+    for (var i = 0; i < 1123; i++) {
+      docs.push({"_id": String(i), "int": i});
+    }
+    db.bulkSave(docs.concat([ddoc]));
+
+    // ?group=false tests
+    result = db.view('test/view1', {startkey: 400, endkey: 402, foobar: randVal()});
+    TEquals(9, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 402, endkey: 400, descending: true,
+      foobar: randVal()});
+    TEquals(9, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 400, endkey: 402, inclusive_end: false,
+      foobar: randVal()});
+    TEquals(6, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 402, endkey: 400, inclusive_end: false,
+      descending: true, foobar: randVal()});
+    TEquals(6, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "400",
+      foobar: randVal()});
+    TEquals(7, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "400",
+      inclusive_end: false, foobar: randVal()});
+    TEquals(6, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "401",
+      foobar: randVal()});
+    TEquals(8, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "401",
+      inclusive_end: false, foobar: randVal()});
+    TEquals(7, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "402",
+      foobar: randVal()});
+    TEquals(9, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 400, endkey: 402, endkey_docid: "402",
+      inclusive_end: false, foobar: randVal()});
+    TEquals(8, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "398",
+      descending: true, foobar: randVal()});
+    TEquals(9, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "398",
+      descending: true, inclusive_end: false, foobar: randVal()}),
+    TEquals(8, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "399",
+      descending: true, foobar: randVal()});
+    TEquals(8, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "399",
+      descending: true, inclusive_end: false, foobar: randVal()}),
+    TEquals(7, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "400",
+      descending: true, foobar: randVal()}),
+    TEquals(7, result.rows[0].value);
+    result = db.view('test/view1', {startkey: 402, endkey: 400, endkey_docid: "400",
+      descending: true, inclusive_end: false, foobar: randVal()}),
+    TEquals(6, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 402, startkey_docid: "400", endkey: 400,
+      descending: true, foobar: randVal()});
+    TEquals(7, result.rows[0].value);
+
+    result = db.view('test/view1', {startkey: 402, startkey_docid: "401", endkey: 400,
+      descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(5, result.rows[0].value);
+
+    // ?group=true tests
+    result = db.view('test/view1', {group: true, startkey: 400, endkey: 402,
+      foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(400, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(402, result.rows[2].key);
+    TEquals(3, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      descending: true, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(3, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 400, endkey: 402,
+      inclusive_end: false, foobar: randVal()});
+    TEquals(2, result.rows.length);
+    TEquals(400, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(2, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+
+    result = db.view('test/view1', {group: true, startkey: 400, endkey: 402,
+      endkey_docid: "401", foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(400, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(402, result.rows[2].key);
+    TEquals(2, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 400, endkey: 402,
+      endkey_docid: "400", foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(400, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(402, result.rows[2].key);
+    TEquals(1, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, startkey_docid: "401",
+      endkey: 400, descending: true, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(2, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(3, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, startkey_docid: "400",
+      endkey: 400, descending: true, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(1, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(3, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, startkey_docid: "401",
+      endkey: 400, descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(2, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(2, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, startkey_docid: "400",
+      endkey: 400, descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(2, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(1, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      endkey_docid: "398", descending: true, inclusive_end: true, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(3, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      endkey_docid: "399", descending: true, inclusive_end: true, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(2, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      endkey_docid: "399", descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(3, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+    TEquals(400, result.rows[2].key);
+    TEquals(1, result.rows[2].value);
+
+    result = db.view('test/view1', {group: true, startkey: 402, endkey: 400,
+      endkey_docid: "400", descending: true, inclusive_end: false, foobar: randVal()});
+    TEquals(2, result.rows.length);
+    TEquals(402, result.rows[0].key);
+    TEquals(3, result.rows[0].value);
+    TEquals(401, result.rows[1].key);
+    TEquals(3, result.rows[1].value);
+
+    db.deleteDb();
+  }
+
+  testReducePagination();
+
+};

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/reduce_builtin.js
----------------------------------------------------------------------
diff --git a/share/test/reduce_builtin.js b/share/test/reduce_builtin.js
new file mode 100644
index 0000000..b3cc3cc
--- /dev/null
+++ b/share/test/reduce_builtin.js
@@ -0,0 +1,179 @@
+// 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.reduce_builtin = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+
+  var numDocs = 500;
+  var docs = makeDocs(1,numDocs + 1);
+  db.bulkSave(docs);
+
+  var summate = function(N) {return (N+1)*N/2;};
+
+  var sumsqr = function(N) { 
+    var acc = 0;
+    for (var i=1; i<=N; ++i) {
+      acc += i*i;
+    }
+    return acc;
+  };
+
+  // this is the same test as the reduce.js test
+  // only we'll let CouchDB run reduce in Erlang
+  var map = function (doc) {
+      emit(doc.integer, doc.integer);
+      emit(doc.integer, doc.integer);
+  };
+
+  var result = db.query(map, "_sum");
+  T(result.rows[0].value == 2*summate(numDocs));
+  result = db.query(map, "_count");
+  T(result.rows[0].value == 1000);
+  result = db.query(map, "_stats");
+  T(result.rows[0].value.sum == 2*summate(numDocs));
+  T(result.rows[0].value.count == 1000);
+  T(result.rows[0].value.min == 1);
+  T(result.rows[0].value.max == 500);
+  T(result.rows[0].value.sumsqr == 2*sumsqr(numDocs));
+
+  result = db.query(map, "_sum", {startkey: 4, endkey: 4});
+  T(result.rows[0].value == 8);
+  result = db.query(map, "_count", {startkey: 4, endkey: 4});
+  T(result.rows[0].value == 2);
+
+  result = db.query(map, "_sum", {startkey: 4, endkey: 5});
+  T(result.rows[0].value == 18);
+  result = db.query(map, "_count", {startkey: 4, endkey: 5});
+  T(result.rows[0].value == 4);
+
+  result = db.query(map, "_sum", {startkey: 4, endkey: 6});
+  T(result.rows[0].value == 30);
+  result = db.query(map, "_count", {startkey: 4, endkey: 6});
+  T(result.rows[0].value == 6);
+
+  result = db.query(map, "_sum", {group:true, limit:3});
+  T(result.rows[0].value == 2);
+  T(result.rows[1].value == 4);
+  T(result.rows[2].value == 6);
+
+  for(var i=1; i<numDocs/2; i+=30) {
+    result = db.query(map, "_sum", {startkey: i, endkey: numDocs - i});
+    T(result.rows[0].value == 2*(summate(numDocs-i) - summate(i-1)));
+  }
+
+  // test for trailing characters after builtin functions, desired behaviour
+  // is to disregard any trailing characters
+  // I think the behavior should be a prefix test, so that even "_statsorama" 
+  // or "_stats\nare\awesome" should work just as "_stats" does. - JChris
+
+  var trailing = ["\u000a", "orama", "\nare\nawesome", " ", "     \n  "];
+
+  for(var i=0; i < trailing.length; i++) {
+    result = db.query(map, "_sum" + trailing[i]);
+    T(result.rows[0].value == 2*summate(numDocs));
+    result = db.query(map, "_count" + trailing[i]);
+    T(result.rows[0].value == 1000);
+    result = db.query(map, "_stats" + trailing[i]);
+    T(result.rows[0].value.sum == 2*summate(numDocs));
+    T(result.rows[0].value.count == 1000);
+    T(result.rows[0].value.min == 1);
+    T(result.rows[0].value.max == 500);
+    T(result.rows[0].value.sumsqr == 2*sumsqr(numDocs));
+  }
+
+  db.deleteDb();
+  db.createDb();
+
+  for(var i=1; i <= 5; i++) {
+
+    for(var j=0; j < 10; j++) {
+      // these docs are in the order of the keys collation, for clarity
+      var docs = [];
+      docs.push({keys:["a"]});
+      docs.push({keys:["a"]});
+      docs.push({keys:["a", "b"]});
+      docs.push({keys:["a", "b"]});
+      docs.push({keys:["a", "b", "c"]});
+      docs.push({keys:["a", "b", "d"]});
+      docs.push({keys:["a", "c", "d"]});
+      docs.push({keys:["d"]});
+      docs.push({keys:["d", "a"]});
+      docs.push({keys:["d", "b"]});
+      docs.push({keys:["d", "c"]});
+      db.bulkSave(docs);
+      T(db.info().doc_count == ((i - 1) * 10 * 11) + ((j + 1) * 11));
+    }
+
+    map = function (doc) { emit(doc.keys, 1); };
+    // with emitted values being 1, count should be the same as sum
+    var builtins = ["_sum", "_count"];
+
+    for (var b=0; b < builtins.length; b++) {
+      var fun = builtins[b];
+      var results = db.query(map, fun, {group:true});
+
+      //group by exact key match
+      T(equals(results.rows[0], {key:["a"],value:20*i}));
+      T(equals(results.rows[1], {key:["a","b"],value:20*i}));
+      T(equals(results.rows[2], {key:["a", "b", "c"],value:10*i}));
+      T(equals(results.rows[3], {key:["a", "b", "d"],value:10*i}));
+
+      // test to make sure group reduce and limit params provide valid json
+      var results = db.query(map, fun, {group: true, limit: 2});
+      T(equals(results.rows[0], {key: ["a"], value: 20*i}));
+      T(equals(results.rows.length, 2));
+
+      //group by the first element in the key array
+      var results = db.query(map, fun, {group_level:1});
+      T(equals(results.rows[0], {key:["a"],value:70*i}));
+      T(equals(results.rows[1], {key:["d"],value:40*i}));
+
+      //group by the first 2 elements in the key array
+      var results = db.query(map, fun, {group_level:2});
+      T(equals(results.rows[0], {key:["a"],value:20*i}));
+      T(equals(results.rows[1], {key:["a","b"],value:40*i}));
+      T(equals(results.rows[2], {key:["a","c"],value:10*i}));
+      T(equals(results.rows[3], {key:["d"],value:10*i}));
+      T(equals(results.rows[4], {key:["d","a"],value:10*i}));
+      T(equals(results.rows[5], {key:["d","b"],value:10*i}));
+      T(equals(results.rows[6], {key:["d","c"],value:10*i}));
+    };
+
+    map = function (doc) { emit(doc.keys, [1, 1]); };
+
+    var results = db.query(map, "_sum", {group:true});
+    T(equals(results.rows[0], {key:["a"],value:[20*i,20*i]}));
+    T(equals(results.rows[1], {key:["a","b"],value:[20*i,20*i]}));
+    T(equals(results.rows[2], {key:["a", "b", "c"],value:[10*i,10*i]}));
+    T(equals(results.rows[3], {key:["a", "b", "d"],value:[10*i,10*i]}));
+
+    var results = db.query(map, "_sum", {group: true, limit: 2});
+    T(equals(results.rows[0], {key: ["a"], value: [20*i,20*i]}));
+    T(equals(results.rows.length, 2));
+
+    var results = db.query(map, "_sum", {group_level:1});
+    T(equals(results.rows[0], {key:["a"],value:[70*i,70*i]}));
+    T(equals(results.rows[1], {key:["d"],value:[40*i,40*i]}));
+
+    var results = db.query(map, "_sum", {group_level:2});
+    T(equals(results.rows[0], {key:["a"],value:[20*i,20*i]}));
+    T(equals(results.rows[1], {key:["a","b"],value:[40*i,40*i]}));
+    T(equals(results.rows[2], {key:["a","c"],value:[10*i,10*i]}));
+    T(equals(results.rows[3], {key:["d"],value:[10*i,10*i]}));
+    T(equals(results.rows[4], {key:["d","a"],value:[10*i,10*i]}));
+    T(equals(results.rows[5], {key:["d","b"],value:[10*i,10*i]}));
+    T(equals(results.rows[6], {key:["d","c"],value:[10*i,10*i]}));
+  }
+}

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/reduce_false.js
----------------------------------------------------------------------
diff --git a/share/test/reduce_false.js b/share/test/reduce_false.js
new file mode 100644
index 0000000..699b258
--- /dev/null
+++ b/share/test/reduce_false.js
@@ -0,0 +1,44 @@
+// 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.reduce_false = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+
+  var numDocs = 5;
+  var docs = makeDocs(1,numDocs + 1);
+  db.bulkSave(docs);
+  var summate = function(N) {return (N+1)*N/2;};
+
+  var designDoc = {
+    _id:"_design/test",
+    language: "javascript",
+    views: {
+      summate: {map:"function (doc) { emit(doc.integer, doc.integer); }",
+                reduce:"function (keys, values) { return sum(values); }"},
+    }
+  };
+  T(db.save(designDoc).ok);
+
+  // Test that the reduce works
+  var res = db.view('test/summate');
+  T(res.rows.length == 1 && res.rows[0].value == summate(5));
+
+  //Test that we get our docs back
+  res = db.view('test/summate', {reduce: false});
+  T(res.rows.length == 5);
+  for(var i=0; i<5; i++) {
+    T(res.rows[i].value == i+1);
+  }
+};

http://git-wip-us.apache.org/repos/asf/couchdb/blob/3ba4fc0b/share/test/reduce_false_temp.js
----------------------------------------------------------------------
diff --git a/share/test/reduce_false_temp.js b/share/test/reduce_false_temp.js
new file mode 100644
index 0000000..d45f05b
--- /dev/null
+++ b/share/test/reduce_false_temp.js
@@ -0,0 +1,37 @@
+// 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.reduce_false_temp = function(debug) {
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+  db.deleteDb();
+  db.createDb();
+  if (debug) debugger;
+
+  var numDocs = 5;
+  var docs = makeDocs(1,numDocs + 1);
+  db.bulkSave(docs);
+  var summate = function(N) {return (N+1)*N/2;};
+
+  var mapFun = "function (doc) { emit(doc.integer, doc.integer); }";
+  var reduceFun = "function (keys, values) { return sum(values); }";
+
+  // Test that the reduce works
+  var res = db.query(mapFun, reduceFun);
+  T(res.rows.length == 1 && res.rows[0].value == summate(5));
+
+  //Test that we get our docs back
+  res = db.query(mapFun, reduceFun, {reduce: false});
+  T(res.rows.length == 5);
+  for(var i=0; i<5; i++) {
+    T(res.rows[i].value == i+1);
+  }
+};


Mime
View raw message