summaryrefslogtreecommitdiff
path: root/share/www/script/test/list_views.js
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2009-02-15 23:59:38 +0000
committerJohn Christopher Anderson <jchris@apache.org>2009-02-15 23:59:38 +0000
commit59a60a319bead55aa188e8cb1516d5e5146c4492 (patch)
tree223063b38170d593def1e31ed12b6505dcd70c79 /share/www/script/test/list_views.js
parent146bc594aef47b675670e7a7fd7f89b7c6a10843 (diff)
Reorganize the tests into one file per test. No other changes.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@744782 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share/www/script/test/list_views.js')
-rw-r--r--share/www/script/test/list_views.js216
1 files changed, 216 insertions, 0 deletions
diff --git a/share/www/script/test/list_views.js b/share/www/script/test/list_views.js
new file mode 100644
index 00000000..de593035
--- /dev/null
+++ b/share/www/script/test/list_views.js
@@ -0,0 +1,216 @@
+// 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.list_views = function(debug) {
+ var db = new CouchDB("test_suite_db");
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var designDoc = {
+ _id:"_design/lists",
+ language: "javascript",
+ views : {
+ basicView : {
+ map : stringFun(function(doc) {
+ emit(doc.integer, doc.string);
+ })
+ },
+ withReduce : {
+ map : stringFun(function(doc) {
+ emit(doc.integer, doc.string);
+ }),
+ reduce : stringFun(function(keys, values, rereduce) {
+ if (rereduce) {
+ return sum(values);
+ } else {
+ return values.length;
+ }
+ })
+ }
+ },
+ lists: {
+ simpleForm: stringFun(function(head, row, req, row_info) {
+ if (row) {
+ // we ignore headers on rows and tail
+ return {
+ body : '\n<li>Key: '+row.key
+ +' Value: '+row.value
+ +' LineNo: '+row_info.row_number+'</li>'
+ };
+ } else if (head) {
+ // we return an object (like those used by external and show)
+ // so that we can specify headers
+ return {
+ body : '<h1>Total Rows: '
+ + head.total_rows
+ + ' Offset: ' + head.offset
+ + '</h1><ul>'
+ };
+ } else {
+ // tail
+ return {body : '</ul>'+
+ '<p>FirstKey: '+row_info.first_key+
+ ' LastKey: '+row_info.prev_key+'</p>'};
+ }
+ }),
+ acceptSwitch: stringFun(function(head, row, req, row_info) {
+ return respondWith(req, {
+ html : function() {
+ // If you're outputting text and you're not setting
+ // any headers, you can just return a string.
+ if (head) {
+ return "HTML <ul>";
+ } else if (row) {
+ return '\n<li>Key: '
+ +row.key+' Value: '+row.value
+ +' LineNo: '+row_info.row_number+'</li>';
+ } else { // tail
+ return '</ul>';
+
+ }
+ },
+ xml : function() {
+ if (head) {
+ return '<feed xmlns="http://www.w3.org/2005/Atom">'
+ +'<title>Test XML Feed</title>';
+ } else if (row) {
+ // Becase Safari can't stand to see that dastardly
+ // E4X outside of a string. Outside of tests you
+ // can just use E4X literals.
+ var entry = new XML('<entry/>');
+ entry.id = row.id;
+ entry.title = row.key;
+ entry.content = row.value;
+ // We'll also let you return just an E4X object
+ // if you aren't setting headers.
+ return entry;
+ } else {
+ return "</feed>";
+ }
+ }
+ })
+ }),
+ qsParams: stringFun(function(head, row, req, row_info) {
+ if(head) return {body: req.query.foo};
+ else return {body: "\n"};
+ }),
+ stopIter: stringFun(function(head, row, req, row_info) {
+ if(head) {
+ return {body: "head"};
+ } else if(row) {
+ if(row_info.row_number > 2) return {stop: true};
+ return {body: " " + row_info.row_number};
+ } else {
+ return {body: " tail"};
+ }
+ }),
+ stopIter2: stringFun(function(head, row, req, row_info) {
+ return respondWith(req, {
+ html: function() {
+ if(head) {
+ return "head";
+ } else if(row) {
+ if(row_info.row_number > 2) return {stop: true};
+ return " " + row_info.row_number;
+ } else {
+ return " tail";
+ }
+ }
+ });
+ })
+ }
+ };
+
+ T(db.save(designDoc).ok);
+
+ var docs = makeDocs(0, 10);
+ var saveResult = db.bulkSave(docs);
+ T(saveResult.ok);
+
+ var view = db.view('lists/basicView');
+ T(view.total_rows == 10);
+
+ // standard get
+ var xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/simpleForm/basicView");
+ T(xhr.status == 200);
+ T(/Total Rows/.test(xhr.responseText));
+ T(/Key: 1/.test(xhr.responseText));
+ T(/LineNo: 0/.test(xhr.responseText));
+ T(/LineNo: 5/.test(xhr.responseText));
+ T(/FirstKey: 0/.test(xhr.responseText));
+ T(/LastKey: 9/.test(xhr.responseText));
+
+
+ var lines = xhr.responseText.split('\n');
+ T(/LineNo: 5/.test(lines[6]));
+
+ // test that etags are available
+ var etag = xhr.getResponseHeader("etag");
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/simpleForm/basicView", {
+ headers: {"if-none-match": etag}
+ });
+ T(xhr.status == 304);
+
+ // get with query params
+ var xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/simpleForm/basicView?startkey=3");
+ T(xhr.status == 200);
+ T(/Total Rows/.test(xhr.responseText));
+ T(!(/Key: 1/.test(xhr.responseText)));
+ T(/FirstKey: 3/.test(xhr.responseText));
+ T(/LastKey: 9/.test(xhr.responseText));
+
+
+ // with 0 rows
+ var xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/simpleForm/basicView?startkey=30");
+ T(xhr.status == 200);
+ T(/Total Rows/.test(xhr.responseText));
+ T(/Offset: null/.test(xhr.responseText));
+
+ // when there is a reduce present, but not used
+ var xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/simpleForm/withReduce?reduce=false");
+ T(xhr.status == 200);
+ T(/Total Rows/.test(xhr.responseText));
+ T(/Key: 1/.test(xhr.responseText));
+
+ // with accept headers for HTML
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/acceptSwitch/basicView", {
+ headers: {
+ "Accept": 'text/html'
+ }
+ });
+ T(xhr.getResponseHeader("Content-Type") == "text/html");
+ T(xhr.responseText.match(/HTML/));
+ T(xhr.responseText.match(/Value/));
+
+ // now with xml
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/acceptSwitch/basicView", {
+ headers: {
+ "Accept": 'application/xml'
+ }
+ });
+ T(xhr.getResponseHeader("Content-Type") == "application/xml");
+ T(xhr.responseText.match(/XML/));
+ T(xhr.responseText.match(/entry/));
+
+ // now with extra qs params
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/qsParams/basicView?foo=blam");
+ T(xhr.responseText.match(/blam/));
+
+
+ // aborting iteration
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/stopIter/basicView");
+ T(xhr.responseText.match(/^head 0 1 2 tail$/));
+ xhr = CouchDB.request("GET", "/test_suite_db/_list/lists/stopIter2/basicView");
+ T(xhr.responseText.match(/^head 0 1 2 tail$/));
+
+};