Commit 26848791 authored by Tristan Cavelier's avatar Tristan Cavelier

asynctests.html + localstorage asynctests added

parent fedeb5cc
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="../lib/qunit/qunit.css" />
<title>JIO Qunit Unit Tests</title>
</head>
<body>
<div id="qunit"></div>
<script src="../lib/qunit/qunit.js"></script>
<script src="../src/sha256.amd.js"></script>
<script src="../jio.js"></script>
<script src="../complex_queries.js"></script>
<script src="jio/util.js"></script>
<script src="../src/jio.storage/localstorage.js"></script>
<script src="jio.storage/localstorage.asynctests.js"></script>
</body>
</html>
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global window, define, module, test_util, jIO, local_storage, test, ok,
deepEqual, sinon, expect, stop, start, Blob */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
module(test_util, jIO, local_storage);
}([
'test_util',
'jio',
'localstorage',
'qunit'
], function (util, jIO, local_storage) {
"use strict";
module("LocalStorage");
local_storage.clear();
test("Post & Get", function () {
expect(6);
var jio = jIO.createJIO({
"type": "local",
"username": "upost",
"application_name": "apost"
}, {
"workspace": {}
});
stop();
jIO.Promise.all([
jIO.Promise.execute(function () {
// get inexistent document
return jio.get({"_id": "inexistent"});
}).always(function (answer) {
deepEqual(answer, {
"error": "not_found",
"id": "inexistent",
"message": "Cannot find document",
"method": "get",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get inexistent document");
}),
jIO.Promise.execute(function () {
// post without id
return jio.post({});
}).always(function (answer) {
var uuid = answer.id;
delete answer.id;
deepEqual(answer, {
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Post without id");
ok(util.isUuid(uuid), "Uuid should look like " +
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + uuid);
}).then(function () {
// post non empty document
return jio.post({"_id": "post1", "title": "myPost1"});
}).always(function (answer) {
deepEqual(answer, {
"id": "post1",
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Post");
}).then(function () {
return jio.get({"_id": "post1"});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"_id": "post1",
"title": "myPost1"
},
"id": "post1",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get, Check document");
}).then(function () {
// post but document already exists
return jio.post({"_id": "post1", "title": "myPost2"});
}).always(function (answer) {
deepEqual(answer, {
"error": "conflict",
"id": "post1",
"message": "Cannot create a new document",
"method": "post",
"reason": "document exists",
"result": "error",
"status": 409,
"statusText": "Conflict"
}, "Post but document already exists");
})
]).always(start);
});
test("Put & Get", function () {
expect(4);
var jio = jIO.createJIO({
"type": "local",
"username": "uput",
"application_name": "aput"
}, {
"workspace": {}
});
stop();
jIO.Promise.execute(function () {
// put non empty document
return jio.put({"_id": "put1", "title": "myPut1"});
}).always(function (answer) {
deepEqual(answer, {
"id": "put1",
"method": "put",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Creates a document");
}).then(function () {
return jio.get({"_id": "put1"});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"_id": "put1",
"title": "myPut1"
},
"id": "put1",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get, Check document");
}).then(function () {
// put but document already exists
return jio.put({"_id": "put1", "title": "myPut2"});
}).always(function (answer) {
deepEqual(answer, {
"id": "put1",
"method": "put",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Update the document");
}).then(function () {
return jio.get({"_id": "put1"});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"_id": "put1",
"title": "myPut2"
},
"id": "put1",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get, Check document");
}).always(start);
});
test("PutAttachment & Get & GetAttachment", function () {
expect(9);
var jio = jIO.createJIO({
"type": "local",
"username": "uputattmt",
"application_name": "aputattmt"
}, {
"workspace": {}
});
stop();
jIO.Promise.all([
jIO.Promise.execute(function () {
// get an attachment from an inexistent document
return jio.getAttachment({"_id": "inexistent", "_attachment": "a"});
}).always(function (answer) {
deepEqual(answer, {
"attachment": "a",
"error": "not_found",
"id": "inexistent",
"message": "Cannot find document",
"method": "getAttachment",
"reason": "missing document",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "GetAttachment from inexistent document")
}),
jIO.Promise.execute(function () {
// put a document then get an attachment from the empty document
return jio.put({"_id": "b"});
}).then(function () {
return jio.getAttachment({"_id": "b", "_attachment": "inexistent"});
}).always(function (answer) {
deepEqual(answer, {
"attachment": "inexistent",
"error": "not_found",
"id": "b",
"message": "Cannot find attachment",
"method": "getAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get inexistent attachment")
}),
jIO.Promise.execute(function () {
// put an attachment to an inexistent document
return jio.putAttachment({
"_id": "inexistent",
"_attachment": "putattmt2",
"_data": ""
});
}).always(function (answer) {
deepEqual(answer, {
"attachment": "putattmt2",
"error": "not_found",
"id": "inexistent",
"message": "Impossible to add attachment",
"method": "putAttachment",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "PutAttachment to inexistent document");
}),
jIO.Promise.execute(function () {
// add a document to the storage
// don't need to be tested
return jio.put({"_id": "putattmt1", "title": "myPutAttmt1"});
}).then(function () {
return jio.putAttachment({
"_id": "putattmt1",
"_attachment": "putattmt2",
"_data": ""
});
}).always(function (answer) {
deepEqual(answer, {
"attachment": "putattmt2",
"hash": "sha256-e3b0c44298fc1c149afbf4c8996fb9242" +
"7ae41e4649b934ca495991b7852b855", // hex_sha256("")
"id": "putattmt1",
"method": "putAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "PutAttachment to a document, without data");
}).then(function () {
// check document and attachment
return jIO.Promise.all([
jio.get({"_id": "putattmt1"}),
jio.getAttachment({"_id": "putattmt1", "_attachment": "putattmt2"})
]);
// XXX check attachment with a getAttachment
}).always(function (answers) {
deepEqual(answers[0], {
"data": {
"_attachments": {
"putattmt2": {
"content_type": "",
"digest": "sha256-e3b0c44298fc1c149afbf4c8996fb9242" +
"7ae41e4649b934ca495991b7852b855",
"length": 0
}
},
"_id": "putattmt1",
"title": "myPutAttmt1"
},
"id": "putattmt1",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get, Check document");
ok(answers[1].data instanceof Blob, "Data is Blob");
deepEqual(answers[1].data.type, "", "Check mimetype");
deepEqual(answers[1].data.size, 0, "Check size");
delete answers[1].data;
deepEqual(answers[1], {
"attachment": "putattmt2",
"id": "putattmt1",
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get Attachment, Check Response");
})
]).always(start);
});
test("Remove & RemoveAttachment", function () {
expect(4);
var jio = jIO.createJIO({
"type": "local",
"username": "uremove",
"application_name": "aremove"
}, {
"workspace": {}
});
stop();
jIO.Promise.execute(function () {
return jio.put({"_id": "a"});
}).then(function () {
return jio.putAttachment({"_id": "a", "_attachment": "b", "_data": "c"});
}).then(function () {
return jio.removeAttachment({"_id": "a", "_attachment": "b"});
}).always(function (answer) {
deepEqual(answer, {
"attachment": "b",
"id": "a",
"method": "removeAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Remove existent attachment");
}).then(function () {
// Promise.all always return success
return jIO.Promise.all([jio.removeAttachment({
"_id": "a",
"_attachment": "b"
})]);
}).always(function (answers) {
deepEqual(answers[0], {
"attachment": "b",
"error": "not_found",
"id": "a",
"message": "Attachment not found",
"method": "removeAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove removed attachment");
}).then(function () {
return jio.remove({"_id": "a"});
}).always(function (answer) {
deepEqual(answer, {
"id": "a",
"method": "remove",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Remove existent document");
}).then(function () {
return jio.remove({"_id": "a"});
}).always(function (answer) {
deepEqual(answer, {
"error": "not_found",
"id": "a",
"message": "Document not found",
"method": "remove",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove removed document");
}).always(start);
});
test("AllDocs", function () {
expect(3);
var o = {}, jio = jIO.createJIO({
"type": "local",
"username": "ualldocs",
"application_name": "aalldocs"
}, {
"workspace": {}
});
stop();
jIO.Promise.execute(function () {
o.date_a = new Date(0);
o.date_b = new Date();
// put some document before list them
return jIO.Promise.all([
jio.put({
"_id": "a",
"title": "one",
"date": o.date_a
}).then(function () {
return jio.putAttachment({
"_id": "a",
"_attachment": "aa",
"_data": "aaa"
});
}),
jio.put({"_id": "b", "title": "two", "date": o.date_a}),
jio.put({"_id": "c", "title": "one", "date": o.date_b}),
jio.put({"_id": "d", "title": "two", "date": o.date_b})
]);
}).then(function () {
// get a list of documents
return jio.allDocs();
}).always(function (answer) {
// sort answer rows for comparison
if (answer.data && answer.data.rows) {
answer.data.rows.sort(function (a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
});
}
deepEqual(answer, {
"data": {
"rows": [{
"id": "a",
"key": "a",
"value": {}
}, {
"id": "b",
"key": "b",
"value": {}
}, {
"id": "c",
"key": "c",
"value": {}
}, {
"id": "d",
"key": "d",
"value": {}
}],
"total_rows": 4
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs");
}).then(function () {
// get a list of documents
return jio.allDocs({
"include_docs": true,
"sort_on": [['title', 'ascending'], ['date', 'descending']],
"select_list": ['title', 'date'],
"limit": [1] // ==> equal [1, 3] in this case
});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"rows": [{
"doc": {
"_attachments": {
"aa": {
"content_type": "",
"digest": "sha256-9834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f0",
"length": 3
}
},
"_id": "a",
"date": o.date_a.toJSON(),
"title": "one"
},
"id": "a",
"key": "a",
"value": {
"date": o.date_a.toJSON(),
"title": "one"
}
}, {
"doc": {
"_id": "d",
"date": o.date_b.toJSON(),
"title": "two"
},
"id": "d",
"key": "d",
"value": {
"date": o.date_b.toJSON(),
"title": "two"
}
}, {
"doc": {
"_id": "b",
"date": o.date_a.toJSON(),
"title": "two"
},
"id": "b",
"key": "b",
"value": {
"date": o.date_a.toJSON(),
"title": "two"
}
}],
"total_rows": 3
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs include docs + sort on + limit + select_list");
}).then(function () {
// use a query
return jio.allDocs({
"query": "title: \"two\"",
"sort_on": [["title", "ascending"]]
});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"rows": [{
"id": "d",
"key": "d",
"value": {}
}, {
"id": "b",
"key": "b",
"value": {}
}],
"total_rows": 2
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs sort on + query");
}).always(start);
});
}));
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, module, jIO, jio_tests, window, test, ok, deepEqual, sinon,
expect */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
module(jIO, jio_tests);
}(['jio', 'jio_tests', 'localstorage'], function (jIO, util) {
"use strict";
function generateTools() {
return {
clock: sinon.useFakeTimers(),
spy: util.ospy,
tick: util.otick
};
}
module("LocalStorage");
test("Post", function () {
expect(5);
var clock = sinon.useFakeTimers(), jio = jIO.newJio({
"type": "local",
"username": "upost",
"application_name": "apost"
});
// post without id
jio.post({}, function (err, response) {
var uuid;
util.
spyJioCallback('jobstatus', 'done', "Post without id")(err, response);
uuid = (err || response).id;
ok(util.isUuid(uuid), "Uuid should look like " +
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + uuid);
});
clock.tick(1000);
// post non empty document
jio.post(
{"_id": "post1", "title": "myPost1"},
util.spyJioCallback("value", {"ok": true, "id": "post1"}, "Post")
);
clock.tick(1000);
deepEqual(
util.jsonlocalstorage.getItem("jio/localstorage/upost/apost/post1"),
{
"_id": "post1",
"title": "myPost1"
},
"Check document"
);
// post but document already exists
jio.post({"_id": "post1", "title": "myPost2"}, util.spyJioCallback(
"status",
409,
"Post but document already exists"
));
clock.tick(1000);
util.closeAndcleanUpJio(jio);
});
test("Put", function () {
expect(5);
var clock = sinon.useFakeTimers(), jio = jIO.newJio({
"type": "local",
"username": "uput",
"application_name": "aput"
});
// put without id
// error 20 -> document id required
jio.put({}, util.spyJioCallback("status", 20, "Put without id"));
clock.tick(1000);
// put non empty document
jio.put({"_id": "put1", "title": "myPut1"}, util.spyJioCallback("value", {
"ok": true,
"id": "put1"
}, "Creates a document"));
clock.tick(1000);
// check document
deepEqual(
util.jsonlocalstorage.getItem("jio/localstorage/uput/aput/put1"),
{
"_id": "put1",
"title": "myPut1"
},
"Check document"
);
// put but document already exists
jio.put({"_id": "put1", "title": "myPut2"}, util.spyJioCallback("value", {
"ok": true,
"id": "put1"
}, "Update the document"));
clock.tick(1000);
// check document
deepEqual(
util.jsonlocalstorage.getItem("jio/localstorage/uput/aput/put1"),
{
"_id": "put1",
"title": "myPut2"
},
"Check document"
);
util.closeAndcleanUpJio(jio);
});
test("PutAttachment", function () {
var o = generateTools(this);
o.jio = jIO.newJio({
"type": "local",
"username": "uputattmt",
"application_name": "aputattmt"
});
// putAttachment without doc id
// error 20 -> document id required
o.spy(o, "status", 20, "PutAttachment without doc id");
o.jio.putAttachment({}, o.f);
o.tick(o);
// putAttachment without attachment id
// error 22 -> attachment id required
o.spy(o, "status", 22, "PutAttachment without attachment id");
o.jio.putAttachment({"_id": "putattmt1"}, o.f);
o.tick(o);
// putAttachment without document
// error 404 -> not found
o.spy(o, "status", 404, "PutAttachment without document");
o.jio.putAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}, o.f);
o.tick(o);
// adding a document
util.jsonlocalstorage.setItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1",
{
"_id": "putattmt1",
"title": "myPutAttmt1"
}
);
// putAttachment with document
o.spy(o, "value",
{"ok": true, "id": "putattmt1", "attachment": "putattmt2"},
"PutAttachment with document, without data");
o.jio.putAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}, o.f);
o.tick(o);
// check document
deepEqual(
util.jsonlocalstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1"
),
{
"_id": "putattmt1",
"title": "myPutAttmt1",
"_attachments": {
"putattmt2": {
"length": 0,
// md5("")
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
}
}
},
"Check document"
);
// check attachment
deepEqual(util.jsonlocalstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1/putattmt2"
), "", "Check attachment");
// update attachment
o.spy(o, "value",
{"ok": true, "id": "putattmt1", "attachment": "putattmt2"},
"Update Attachment, with data");
o.jio.putAttachment({
"_id": "putattmt1",
"_attachment": "putattmt2",
"_data": "abc"
}, o.f);
o.tick(o);
// check document
deepEqual(
util.jsonlocalstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1"
),
{
"_id": "putattmt1",
"title": "myPutAttmt1",
"_attachments": {
"putattmt2": {
"length": 3,
// md5("abc")
"digest": "md5-900150983cd24fb0d6963f7d28e17f72"
}
}
},
"Check document"
);
// check attachment
deepEqual(util.jsonlocalstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1/putattmt2"
), "abc", "Check attachment");
util.closeAndcleanUpJio(o.jio);
});
test("Get", function () {
var o = generateTools(this);
o.jio = jIO.newJio({
"type": "local",
"username": "uget",
"application_name": "aget"
});
// get inexistent document
o.spy(o, "status", 404, "Get inexistent document");
o.jio.get({"_id": "get1"}, o.f);
o.tick(o);
// get inexistent attachment
o.spy(o, "status", 404, "Get inexistent attachment");
o.jio.getAttachment({"_id": "get1", "_attachment": "get2"}, o.f);
o.tick(o);
// adding a document
o.doc_get1 = {
"_id": "get1",
"title": "myGet1"
};
util.jsonlocalstorage.setItem(
"jio/localstorage/uget/aget/get1",
o.doc_get1
);
// get document
o.spy(o, "value", o.doc_get1, "Get document");
o.jio.get({"_id": "get1"}, o.f);
o.tick(o);
// get inexistent attachment (document exists)
o.spy(o, "status", 404, "Get inexistent attachment (document exists)");
o.jio.getAttachment({"_id": "get1", "_attachment": "get2"}, o.f);
o.tick(o);
// adding an attachment
o.doc_get1._attachments = {
"get2": {
"length": 2,
// md5("de")
"digest": "md5-5f02f0889301fd7be1ac972c11bf3e7d"
}
};
util.jsonlocalstorage.setItem(
"jio/localstorage/uget/aget/get1",
o.doc_get1
);
util.jsonlocalstorage.setItem("jio/localstorage/uget/aget/get1/get2", "de");
// get attachment
o.spy(o, "value", "de", "Get attachment");
o.jio.getAttachment({"_id": "get1", "_attachment": "get2"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
});
test("Remove", function () {
var o = generateTools(this);
o.jio = jIO.newJio({
"type": "local",
"username": "uremove",
"application_name": "aremove"
});
// remove inexistent document
o.spy(o, "status", 404, "Remove inexistent document");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
// remove inexistent attachment
o.spy(o, "status", 404, "Remove inexistent attachment");
o.jio.removeAttachment({"_id": "remove1", "_attachment": "remove2"}, o.f);
o.tick(o);
// adding a document + attmt
util.jsonlocalstorage.setItem("jio/localstorage/uremove/aremove/remove1", {
"_id": "remove1",
"title": "myRemove1",
"_attachments": {
"remove2": {
"length": 4,
"digest": "md5-blahblah"
}
}
});
util.jsonlocalstorage.setItem(
"jio/localstorage/uremove/aremove/remove1/remove2",
"fghi"
);
// remove attachment
o.spy(o, "value", {"ok": true, "id": "remove1", "attachment": "remove2"},
"Remove document");
o.jio.removeAttachment({"_id": "remove1", "_attachment": "remove2"}, o.f);
o.tick(o);
// remove document
o.spy(o, "value", {"ok": true, "id": "remove1"}, "Remove document");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
// check document
ok(util.jsonlocalstorage.getItem(
"jio/localstorage/uremove/aremove/remove1"
) === null, "Check document is removed");
// adding a document + attmt
util.jsonlocalstorage.setItem("jio/localstorage/uremove/aremove/remove1", {
"_id": "remove1",
"title": "myRemove1",
"_attachments": {
"remove2": {
"length": 4,
"digest": "md5-blahblah"
}
}
});
util.jsonlocalstorage.setItem(
"jio/localstorage/uremove/aremove/remove1/remove2",
"fghi"
);
// remove attachment
o.spy(o, "value", {"ok": true, "id": "remove1"},
"Remove document and attachment");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
ok(util.jsonlocalstorage.getItem(
"jio/localstorage/uremove/aremove/remove1"
) === null, "Check document is removed");
ok(util.jsonlocalstorage.getItem(
"jio/localstorage/uremove/aremove/remove1/remove2"
) === null, "Check attachment is removed");
util.closeAndcleanUpJio(o.jio);
});
test("AllDocs", function () {
var o = generateTools(this), i, m = 15;
o.jio = jIO.newJio({
"type": "local",
"username": "ualldocs",
"application_name": "aalldocs"
});
o.localpath = "jio/localstorage/ualldocs/aalldocs";
// sample data
o.titles = [
"Shawshank Redemption",
"Godfather",
"Godfather 2",
"Pulp Fiction",
"The Good, The Bad and The Ugly",
"12 Angry Men",
"The Dark Knight",
"Schindlers List",
"Lord of the Rings - Return of the King",
"Fight Club",
"Star Wars Episode V",
"Lord Of the Rings - Fellowship of the Ring",
"One flew over the Cuckoo's Nest",
"Inception",
"Godfellas"
];
o.years = [
1994,
1972,
1974,
1994,
1966,
1957,
2008,
1993,
2003,
1999,
1980,
2001,
1975,
2010,
1990
];
o.director = [
"Frank Darabont",
"Francis Ford Coppola",
"Francis Ford Coppola",
"Quentin Tarantino",
"Sergio Leone",
"Sidney Lumet",
"Christopher Nolan",
"Steven Spielberg",
"Peter Jackson",
"David Fincher",
"Irvin Kershner",
"Peter Jackson",
"Milos Forman",
"Christopher Nolan",
"Martin Scorsese"
];
for (i = 0; i < m; i += 1) {
o.fakeDoc = {};
o.fakeDoc._id = "doc_" + (i < 10 ? "0" + i : i);
o.fakeDoc.title = o.titles[i];
o.fakeDoc.year = o.years[i];
o.fakeDoc.author = o.director[i];
if (i === 5) {
o.fakeDoc._attachments = {
"att": {
"digest": "md5-dontcare",
"content_type": "text/plain",
"length": 3
}
};
util.jsonlocalstorage.setItem(o.localpath + "/doc_05/att", "abc");
}
util.jsonlocalstorage.setItem(
o.localpath + "/doc_" + (i < 10 ? "0" + i : i),
o.fakeDoc
);
}
// response
o.allDocsResponse = {};
o.allDocsResponse.rows = [];
o.allDocsResponse.total_rows = 15;
for (i = 0; i < m; i += 1) {
o.allDocsResponse.rows.push({
"id": "doc_" + (i < 10 ? "0" + i : i),
"key": "doc_" + (i < 10 ? "0" + i : i),
"value": {}
});
}
// alldocs
o.spy(o, "value", o.allDocsResponse, "All docs");
o.jio.allDocs(function (err, response) {
if (response && response.rows) {
response.rows.sort(function (a, b) {
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
});
}
o.f(err, response);
});
o.tick(o);
// include docs
o.allDocsResponse = {};
o.allDocsResponse.rows = [];
o.allDocsResponse.total_rows = m;
for (i = 0; i < m; i += 1) {
o.allDocsResponse.rows.push({
"id": "doc_" + (i < 10 ? "0" + i : i),
"key": "doc_" + (i < 10 ? "0" + i : i),
"value": {},
"doc": util.jsonlocalstorage.getItem(
o.localpath + "/doc_" + (i < 10 ? "0" + i : i)
)
});
}
// alldocs
o.spy(o, "value", o.allDocsResponse, "All docs (include docs)");
o.jio.allDocs({"include_docs": true}, function (err, response) {
if (response && response.rows) {
response.rows.sort(function (a, b) {
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
});
}
o.f(err, response);
});
o.tick(o);
// complex queries
o.thisShouldBeTheAnswer4 = {"total_rows": 0, "rows": []};
o.allDocsResponse.rows.forEach(function (row) {
var new_row;
if (row.doc.year >= 1980) {
new_row = JSON.parse(JSON.stringify(row));
new_row.value.title = row.doc.title;
new_row.value.year = row.doc.year;
delete new_row.doc;
o.thisShouldBeTheAnswer4.rows.push(new_row);
o.thisShouldBeTheAnswer4.total_rows += 1;
}
});
o.thisShouldBeTheAnswer4.rows.sort(function (a, b) {
return (a.value.year > b.value.year ? -1 :
a.value.year < b.value.year ? 1 : 0);
});
o.thisShouldBeTheAnswer4.total_rows = 5;
o.thisShouldBeTheAnswer4.rows.length = 5;
o.spy(o, "value", o.thisShouldBeTheAnswer4,
"allDocs (complex queries year >= 1980, all query options)");
o.jio.allDocs({
"query": '(year: >= "1980")',
"limit": [0, 5],
"sort_on": [["year", "descending"]],
"select_list": ["title", "year"]
}, o.f);
o.tick(o);
// empty query returns all
o.thisShouldBeTheAnswer5 = {"total_rows": 0, "rows": []};
o.allDocsResponse.rows.forEach(function (row) {
var new_row = JSON.parse(JSON.stringify(row));
new_row.value.title = row.doc.title;
o.thisShouldBeTheAnswer5.rows.push(new_row);
o.thisShouldBeTheAnswer5.total_rows += 1;
});
o.thisShouldBeTheAnswer5.rows.sort(function (a, b) {
return (a.value.title > b.value.title ? -1 :
a.value.title < b.value.title ? 1 : 0);
});
o.spy(o, "value", o.thisShouldBeTheAnswer5,
"allDocs (empty query in complex query)");
o.jio.allDocs({
"sort_on": [["title", "descending"]],
"select_list": ["title"],
"include_docs": true
}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
});
}));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment