summaryrefslogtreecommitdiff
path: root/share/www/script/test/design_docs.js
blob: 8f003efa8ae84173ab26c6968e336b87c326f4e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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.design_docs = function(debug) {
  var db = new CouchDB("test_suite_db");
  db.deleteDb();
  db.createDb();
  if (debug) debugger;

  run_on_modified_server(
    [{section: "query_server_config",
      key: "reduce_limit",
      value: "false"}],
function() {

  var numDocs = 500;

  function makebigstring(power) {
    var str = "a";
    while(power-- > 0) {
      str = str + str;
    }
    return str;
  }

  var designDoc = {
    _id:"_design/test", // turn off couch.js id escaping?
    language: "javascript",
    views: {
      all_docs_twice: {map: "function(doc) { emit(doc.integer, null); emit(doc.integer, null) }"},
      no_docs: {map: "function(doc) {}"},
      single_doc: {map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"},
      summate: {map:"function (doc) {emit(doc.integer, doc.integer)};",
                reduce:"function (keys, values) { return sum(values); };"},
      summate2: {map:"function (doc) {emit(doc.integer, doc.integer)};",
                reduce:"function (keys, values) { return sum(values); };"},
      huge_src_and_results: {map: "function(doc) { if (doc._id == \"1\") { emit(\"" + makebigstring(16) + "\", null) }}",
                reduce:"function (keys, values) { return \"" + makebigstring(16) + "\"; };"}
    }
  }
  T(db.save(designDoc).ok);

  db.bulkSave(makeDocs(1, numDocs + 1));

  // test that the _all_docs view returns correctly with keys
  var results = db.allDocs({startkey:"_design", endkey:"_design0"});
  T(results.rows.length == 1);

  for (var loop = 0; loop < 2; loop++) {
    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/no_docs").total_rows == 0)
    T(db.view("test/single_doc").total_rows == 1)
    T(db.ensureFullCommit().ok);
    restartServer();
  };
  
  // test when language not specified, Javascript is implied
  var designDoc2 = {
    _id:"_design/test2",
    // language: "javascript", 
    views: {
      single_doc: {map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"}
    }
  };
  
  T(db.save(designDoc2).ok);
  T(db.view("test2/single_doc").total_rows == 1);

  var summate = function(N) {return (N+1)*N/2;};
  var result = db.view("test/summate");
  T(result.rows[0].value == summate(numDocs));

  result = db.view("test/summate", {startkey:4,endkey:4});
  T(result.rows[0].value == 4);

  result = db.view("test/summate", {startkey:4,endkey:5});
  T(result.rows[0].value == 9);

  result = db.view("test/summate", {startkey:4,endkey:6});
  T(result.rows[0].value == 15);

  // Verify that a shared index (view def is an exact copy of "summate")
  // does not confuse the reduce stage
  result = db.view("test/summate2", {startkey:4,endkey:6});
  T(result.rows[0].value == 15);

  for(var i=1; i<numDocs/2; i+=30) {
    result = db.view("test/summate", {startkey:i,endkey:numDocs-i});
    T(result.rows[0].value == summate(numDocs-i) - summate(i-1));
  }

  T(db.deleteDoc(designDoc).ok);
  T(db.open(designDoc._id) == null);
  T(db.view("test/no_docs") == null);
  
  T(db.ensureFullCommit().ok);
  restartServer();
  T(db.open(designDoc._id) == null);
  T(db.view("test/no_docs") == null);
});
};