Commit 1e991dc6 authored by Romain Courteaud's avatar Romain Courteaud

Add QueryStorage tests.

parent 41b69b80
/*jslint nomen: true, maxlen: 200*/
/*jslint nomen: true*/
/*global RSVP*/
(function (jIO) {
(function (jIO, RSVP) {
"use strict";
/**
......@@ -33,18 +33,10 @@
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments);
};
QueryStorage.prototype.removeAttachment = function () {
return this._sub_storage.removeAttachment.apply(this._sub_storage, arguments);
return this._sub_storage.removeAttachment.apply(this._sub_storage,
arguments);
};
/**
* Retrieve documents.
* This method performs an .allDocs() call on the substorage,
* retrieving everything, then runs a query on the result.
*
* @method allDocs
* @param {Object} command The given parameters
* @param {Object} options The command options
*/
QueryStorage.prototype.hasCapacity = function (name) {
if (name === "list") {
return this._sub_storage.hasCapacity(name);
......@@ -54,7 +46,6 @@
QueryStorage.prototype.buildQuery = function (options) {
var substorage = this._sub_storage,
context = this,
// sub_query_result,
sub_options = {},
is_manual_query_needed = false,
is_manual_include_needed = false;
......@@ -63,17 +54,22 @@
// Can substorage handle the queries if needed?
try {
if (((options.query === undefined) || (substorage.hasCapacity("query"))) &&
((options.sort_on === undefined) || (substorage.hasCapacity("sort"))) &&
((options.select_list === undefined) || (substorage.hasCapacity("select"))) &&
((options.limit === undefined) || (substorage.hasCapacity("limit")))) {
if (((options.query === undefined) ||
(substorage.hasCapacity("query"))) &&
((options.sort_on === undefined) ||
(substorage.hasCapacity("sort"))) &&
((options.select_list === undefined) ||
(substorage.hasCapacity("select"))) &&
((options.limit === undefined) ||
(substorage.hasCapacity("limit")))) {
sub_options.query = options.query;
sub_options.sort_on = options.sort_on;
sub_options.select_list = options.select_list;
sub_options.limit = options.limit;
}
} catch (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 501)) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 501)) {
is_manual_query_needed = true;
} else {
throw error;
......@@ -82,18 +78,20 @@
// Can substorage include the docs if needed?
try {
if ((is_manual_query_needed || (options.include_docs === true)) && (!substorage.hasCapacity("include"))) {
sub_options.include_docs = options.include_docs;
if ((is_manual_query_needed ||
(options.include_docs === true)) &&
(substorage.hasCapacity("include"))) {
sub_options.include_docs = true;
}
} catch (error) {
if ((error instanceof jIO.util.jIOError) && (error.status_code === 501)) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 501)) {
is_manual_include_needed = true;
} else {
throw error;
}
}
return substorage.buildQuery(sub_options)
// Include docs if needed
......@@ -106,7 +104,8 @@
return substorage.get({"_id": result[j].id})
.push(undefined, function (error) {
// Document may have been dropped after listing
if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return;
}
throw error;
......@@ -135,6 +134,7 @@
result = original_result;
}
return result;
})
// Manual query if needed
......@@ -143,7 +143,6 @@
len,
i;
if (is_manual_query_needed) {
// sub_query_result = result;
len = result.length;
for (i = 0; i < len; i += 1) {
data_rows.push(result[i].doc);
......@@ -151,7 +150,8 @@
if (options.select_list) {
options.select_list.push("_id");
}
result = jIO.QueryFactory.create(options.query || "", context._key_schema).
result = jIO.QueryFactory.create(options.query || "",
context._key_schema).
exec(data_rows, options);
}
return result;
......@@ -186,137 +186,9 @@
return result;
});
// if (options.include_docs) {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "doc": docs[filtered_docs[i]._id],
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// } else {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// }
// response.data.rows = filtered_docs;
// response.data.total_rows = filtered_docs.length;
// return response;
// });
// return jIO.QueryFactory.create(options.query || "", that._key_schema).
// exec(data_rows, options).
// then(function (filtered_docs) {
// // reconstruct filtered rows, preserving the order from docs
// if (options.include_docs) {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "doc": docs[filtered_docs[i]._id],
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// } else {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// }
// response.data.rows = filtered_docs;
// response.data.total_rows = filtered_docs.length;
// return response;
// });
}
// }).then(function (response) {
//
// ((options.include_docs === undefined) || context.hasCapacity("include")) &&
// }
//
// return context.buildQuery.apply(context, arguments);
// }
//
// // // we need the full documents in order to perform the query, will
// // // remove them later if they were not required.
// // include_docs = (options.include_docs || options.query) ? true : false;
//
// console.log("QueryStorage: calling substorage buildQuery");
// return substorage.buildQuery.apply(substorage, arguments);
// return substorage.buildQuery.apply(substorage, arguments)
// .push(function (result) {
// });
// substorage.allDocs({
// "include_docs": include_docs
// }).then(function (response) {
//
// var data_rows = response.data.rows, docs = {}, row, i, l;
//
// if (!include_docs) {
// return response;
// }
//
// if (options.include_docs) {
// for (i = 0, l = data_rows.length; i < l; i += 1) {
// row = data_rows[i];
// docs[row.id] = JSON.parse(JSON.stringify(row.doc));
// row.doc._id = row.id;
// data_rows[i] = row.doc;
// }
// } else {
// for (i = 0, l = data_rows.length; i < l; i += 1) {
// row = data_rows[i];
// row.doc._id = row.id;
// data_rows[i] = row.doc;
// }
// }
//
// if (options.select_list) {
// options.select_list.push("_id");
// }
//
// return jIO.QueryFactory.create(options.query || "", that._key_schema).
// exec(data_rows, options).
// then(function (filtered_docs) {
// // reconstruct filtered rows, preserving the order from docs
// if (options.include_docs) {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "doc": docs[filtered_docs[i]._id],
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// } else {
// for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// filtered_docs[i] = {
// "id": filtered_docs[i]._id,
// "value": options.select_list ? filtered_docs[i] : {}
// };
// delete filtered_docs[i].value._id;
// }
// }
// response.data.rows = filtered_docs;
// response.data.total_rows = filtered_docs.length;
// return response;
// });
//
// }).then(command.success, command.error, command.notify);
};
jIO.addStorage('query', QueryStorage);
}(jIO));
}(jIO, RSVP));
/*jslint nomen: true, maxlen: 200 */
/*global Blob, test_util, console*/
(function (jIO, QUnit) {
/*jslint nomen: true*/
/*global Blob*/
(function (jIO, QUnit, Blob) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
......@@ -9,7 +9,8 @@
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module;
module = QUnit.module,
throws = QUnit.throws;
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
......@@ -17,31 +18,25 @@
function Storage200() {
return this;
}
Storage200.prototype.get = function (param) {
equal(param._id, "bar", "get 200 called");
return {title: "foo"};
};
Storage200.prototype.put = function (param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called");
return param._id;
};
Storage200.prototype.remove = function (param) {
deepEqual(param, {"_id": "bar"}, "remove 200 called");
return param._id;
};
Storage200.prototype.post = function (param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "post 200 called");
return param._id;
};
Storage200.prototype.buildQuery = function (options) {
console.log("Storage200: buildQuery");
deepEqual(options, {include_docs: true, query: 'title: "two"'},
"buildQuery 200 called");
console.log("Storage200: return");
return "taboulet";
};
jIO.addStorage('querystorage200', Storage200);
/////////////////////////////////////////////////////////////////
// queryStorage.constructor
/////////////////////////////////////////////////////////////////
module("queryStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
});
ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "querystorage200");
});
/////////////////////////////////////////////////////////////////
// queryStorage.get
/////////////////////////////////////////////////////////////////
......@@ -57,9 +52,15 @@
}
});
Storage200.prototype.get = function (param) {
equal(param._id, "bar", "get 200 called");
return {title: "foo"};
};
jio.get({"_id": "bar"})
.then(function (result) {
deepEqual(result, {
"_id": "bar",
"title": "foo"
}, "Check document");
})
......@@ -86,9 +87,14 @@
}
});
jio.post({"_id": "bar", "title": "foo"})
Storage200.prototype.post = function (param) {
deepEqual(param, {"title": "foo"}, "post 200 called");
return "youhou";
};
jio.post({"title": "foo"})
.then(function (result) {
equal(result, "bar");
equal(result, "youhou");
})
.fail(function (error) {
ok(false, error);
......@@ -112,6 +118,10 @@
type: "querystorage200"
}
});
Storage200.prototype.put = function (param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called");
return param._id;
};
jio.put({"_id": "bar", "title": "foo"})
.then(function (result) {
......@@ -139,6 +149,10 @@
type: "querystorage200"
}
});
Storage200.prototype.remove = function (param) {
deepEqual(param, {"_id": "bar"}, "remove 200 called");
return param._id;
};
jio.remove({"_id": "bar"})
.then(function (result) {
......@@ -153,10 +167,78 @@
});
/////////////////////////////////////////////////////////////////
// queryStorage.allDocs
// queryStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("queryStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
}),
blob = new Blob([""]);
Storage200.prototype.getAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"},
"getAttachment 200 called");
return {data: blob};
};
jio.getAttachment({"_id": "bar", "_attachment": "foo"})
.then(function (result) {
equal(result.data, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// queryStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("queryStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
}),
blob = new Blob([""]);
Storage200.prototype.putAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo", "_blob": blob},
"putAttachment 200 called");
return "OK";
};
jio.putAttachment({"_id": "bar", "_attachment": "foo", "_blob": blob})
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// queryStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("queryStorage.allDocs");
test("allDocs called substorage allDocs", function () {
module("queryStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () {
stop();
expect(2);
......@@ -167,16 +249,303 @@
}
});
Storage200.prototype.removeAttachment = function (param) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"},
"removeAttachment 200 called");
return "Removed";
};
jio.removeAttachment({"_id": "bar", "_attachment": "foo"})
.then(function (result) {
equal(result, "Removed");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// queryStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("queryStorage.hasCapacity");
test("hasCapacity is true by default", function () {
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
});
equal(jio.hasCapacity("foo"), true);
});
test("hasCapacity list return substorage value", function () {
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
});
throws(
function () {
jio.hasCapacity("list");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'list' is not implemented on 'querystorage200'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// queryStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("queryStorage.buildQuery");
test("substorage should have 'list' capacity", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
});
jio.allDocs({
include_docs: true,
query: 'title: "two"'
})
.then(function () {
ok(false);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'list' is not implemented on 'querystorage200'");
})
.always(function () {
start();
});
});
test("no manual query if substorage handle everything", function () {
stop();
expect(2);
function StorageAllDocsNoGet() {
return this;
}
StorageAllDocsNoGet.prototype.get = function () {
throw new Error("Unexpected get call");
};
StorageAllDocsNoGet.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "sort") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "query")) {
return true;
}
throw new Error("Unexpected " + capacity + " capacity check");
};
StorageAllDocsNoGet.prototype.buildQuery = function (options) {
deepEqual(options, {
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
query: 'title: "two"'
},
"buildQuery called");
return "taboulet";
};
jIO.addStorage('querystoragealldocsnoget', StorageAllDocsNoGet);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystoragealldocsnoget"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
query: 'title: "two"'
})
.then(function (result) {
equal(result, "bar");
deepEqual(result, {
data: {
rows: "taboulet",
total_rows: 8
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual query used if substorage does not handle sort", function () {
stop();
expect(4);
function StorageNoSortCapacity() {
return this;
}
StorageNoSortCapacity.prototype.get = function (options) {
if (options._id === "foo") {
equal(options._id, "foo", "Get foo");
} else {
equal(options._id, "bar", "Get bar");
}
return {title: options._id, id: "ID " + options._id,
"another": "property"};
};
StorageNoSortCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "query")) {
return true;
}
return false;
};
StorageNoSortCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {}, "No query parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('querystoragenosortcapacity', StorageNoSortCapacity);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystoragenosortcapacity"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
query: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual query used if substorage does not handle select", function () {
stop();
expect(4);
function StorageNoSelectCapacity() {
return this;
}
StorageNoSelectCapacity.prototype.get = function (options) {
if (options._id === "foo") {
equal(options._id, "foo", "Get foo");
} else {
equal(options._id, "bar", "Get bar");
}
return {title: options._id, id: "ID " + options._id,
"another": "property"};
};
StorageNoSelectCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "sort") ||
(capacity === "limit") ||
(capacity === "query")) {
return true;
}
return false;
};
StorageNoSelectCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {}, "No query parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('querystoragenoselectcapacity', StorageNoSelectCapacity);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystoragenoselectcapacity"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
query: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
console.error(error);
console.error(error.stack);
ok(false, error);
})
.always(function () {
......@@ -184,737 +553,235 @@
});
});
// function createQueryStorage(name, key_schema) {
// // var local_description = local_storage.createDescription(name,
// // name,
// // 'memory');
// return jIO.createJIO({
// type: 'query',
// sub_storage: {
// type: name
// },
// key_schema: key_schema
// }, {
// workspace: {}
// });
// }
//
//
//
// /*
// * What follows is almost a replica of the local storage tests,
// * plus a couple of schema queries.
// * This is redundant, but guarantees that the storage is working
// * under all circumstances.
// */
//
//
// function success(promise) {
// return new RSVP.Promise(function (resolve, reject, notify) {
// /*jslint unparam: true*/
// promise.then(resolve, resolve, notify);
// }, function () {
// promise.cancel();
// });
// }
//
//
// function unexpectedError(error) {
// if (error instanceof Error) {
// deepEqual([
// error.name + ": " + error.message,
// error
// ], "UNEXPECTED ERROR", "Unexpected error");
// } else {
// deepEqual(error, "UNEXPECTED ERROR", "Unexpected error");
// }
// }
//
//
// test("post & get", 6, function () {
// var jio = createQueryStorage('post-get');
//
// stop();
//
// function getMissingDocument() {
// return success(jio.get({_id: 'inexistent'}));
// }
//
// function getMissingDocumentTest(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");
// }
//
// function postWithoutID() {
// return jio.post({});
// }
//
// function postWithoutIDTest(answer) {
// var uuid = answer.id;
//
// delete answer.id;
// deepEqual(answer, {
// "method": "post",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// });
// ok(test_util.isUuid(uuid), "Uuid should look like " +
// "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + uuid);
// }
//
// function postNonEmptyDocument() {
// return jio.post({"_id": "post1", "title": "myPost1"});
// }
//
// function postNonEmptyDocumentTest(answer) {
// deepEqual(answer, {
// "id": "post1",
// "method": "post",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// });
// }
//
// function getNonEmptyDocument() {
// return jio.get({"_id": "post1"});
// }
//
// function getNonEmptyDocumentTest(answer) {
// deepEqual(answer, {
// "data": {
// "_id": "post1",
// "title": "myPost1"
// },
// "id": "post1",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
// function postExistingDocument() {
// return success(jio.post({"_id": "post1", "title": "myPost2"}));
// }
//
// function postExistingDocumentTest(answer) {
// deepEqual(answer, {
// "error": "conflict",
// "id": "post1",
// "message": "Cannot create a new document",
// "method": "post",
// "reason": "document exists",
// "result": "error",
// "status": 409,
// "statusText": "Conflict"
// });
// }
//
// getMissingDocument().then(getMissingDocumentTest).
// then(postWithoutID).then(postWithoutIDTest).
// then(postNonEmptyDocument).then(postNonEmptyDocumentTest).
// then(getNonEmptyDocument).then(getNonEmptyDocumentTest).
// then(postExistingDocument).then(postExistingDocumentTest).
// fail(unexpectedError).
// always(start);
// });
//
//
//
// test("put & get", 4, function () {
// var jio = createQueryStorage('put-get');
//
// stop();
//
// function putNonEmptyDocument() {
// return jio.put({"_id": "put1", "title": "myPut1"});
// }
//
// function putNonEmptyDocumentTest(answer) {
// deepEqual(answer, {
// "id": "put1",
// "method": "put",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// });
// }
//
// function getNonEmptyDocument() {
// return jio.get({"_id": "put1"});
// }
//
// function getNonEmptyDocumentTest(answer) {
// deepEqual(answer, {
// "data": {
// "_id": "put1",
// "title": "myPut1"
// },
// "id": "put1",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
// function putExistingDocument() {
// return success(jio.put({"_id": "put1", "title": "myPut2"}));
// }
//
// function putExistingDocumentTest(answer) {
// deepEqual(answer, {
// "id": "put1",
// "method": "put",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// });
// }
//
// function getNonEmptyDocument2() {
// return jio.get({"_id": "put1"});
// }
//
// function getNonEmptyDocument2Test(answer) {
// deepEqual(answer, {
// "data": {
// "_id": "put1",
// "title": "myPut2"
// },
// "id": "put1",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
// putNonEmptyDocument().then(putNonEmptyDocumentTest).
// then(getNonEmptyDocument).then(getNonEmptyDocumentTest).
// then(putExistingDocument).then(putExistingDocumentTest).
// then(getNonEmptyDocument2).then(getNonEmptyDocument2Test).
// fail(unexpectedError).
// always(start);
// });
//
//
// test("putAttachment & get & getAttachment", 9, function () {
// var jio = createQueryStorage('putattachment-get-getattachment');
//
// stop();
//
// function getAttachmentMissingDocument() {
// return success(jio.getAttachment({
// "_id": "inexistent",
// "_attachment": "a"
// }));
// }
//
// function getAttachmentMissingDocumentTest(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"
// });
// }
//
// function getAttachmentFromEmptyDocument() {
// var promise = jio.put({"_id": "b"}).
// then(function () {
// return jio.getAttachment({"_id": "b", "_attachment": "inexistent"});
// });
// return success(promise);
// }
//
// function getAttachmentFromEmptyDocumentTest(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"
// });
// }
//
// function putAttachmentMissingDocument() {
// return success(jio.putAttachment({
// "_id": "inexistent",
// "_attachment": "putattmt2",
// "_data": ""
// }));
// }
//
// function putAttachmentMissingDocumentTest(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"
// });
// }
//
// function addAttachment() {
// var promise = jio.put({"_id": "putattmt1", "title": "myPutAttmt1"}).
// then(function () {
// return jio.putAttachment({
// "_id": "putattmt1",
// "_attachment": "putattmt2",
// "_data": ""
// });
// });
// return success(promise);
// }
//
// function addAttachmentTest(answer) {
// deepEqual(answer, {
// "attachment": "putattmt2",
// "digest": "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b93" +
// "4ca495991b7852b855",
// "id": "putattmt1",
// "method": "putAttachment",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// });
// }
//
// function checkDocumentAndAttachment() {
// return RSVP.all([
// jio.get({"_id": "putattmt1"}),
// jio.getAttachment({"_id": "putattmt1", "_attachment": "putattmt2"})
// ]);
// }
//
// function checkDocumentAndAttachmentTest(answers) {
// deepEqual(answers[0], {
// "data": {
// "_attachments": {
// "putattmt2": {
// "content_type": "",
// "digest": "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4" +
// "649b934ca495991b7852b855",
// "length": 0
// }
// },
// "_id": "putattmt1",
// "title": "myPutAttmt1"
// },
// "id": "putattmt1",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
//
// 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",
// "digest": "sha256-e3b0c44298fc1c149afbf4c8996fb9242" +
// "7ae41e4649b934ca495991b7852b855",
// "method": "getAttachment",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "Get Attachment, Check Response");
// }
//
// getAttachmentMissingDocument().then(getAttachmentMissingDocumentTest).
// then(getAttachmentFromEmptyDocument).
// then(getAttachmentFromEmptyDocumentTest).
// then(putAttachmentMissingDocument).
// then(putAttachmentMissingDocumentTest).
// then(addAttachment).then(addAttachmentTest).
// then(checkDocumentAndAttachment).then(checkDocumentAndAttachmentTest).
// fail(unexpectedError).
// always(start);
// });
//
//
// test("remove & removeAttachment", 5, function () {
// var jio = createQueryStorage('remove-removeattachment');
//
// stop();
//
// function putAttachment() {
// var promise = jio.put({"_id": "a"}).
// then(function () {
// return jio.putAttachment({
// "_id": "a",
// "_attachment": "b",
// "_data": "c"
// });
// });
//
// return promise;
// }
//
// function putAttachmentTest(answer) {
// deepEqual(answer, {
// "attachment": "b",
// "digest": "sha256-2e7d2c03a9507ae265ecf5b5356885a53" +
// "393a2029d241394997265a1a25aefc6",
// "id": "a",
// "method": "putAttachment",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// });
// }
//
// function removeAttachment() {
// return success(jio.removeAttachment({"_id": "a", "_attachment": "b"}));
// }
//
// function removeAttachmentTest(answer) {
// deepEqual(answer, {
// "attachment": "b",
// "id": "a",
// "method": "removeAttachment",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// });
// }
//
// function removeAttachmentAgainTest(answer) {
// deepEqual(answer, {
// "attachment": "b",
// "error": "not_found",
// "id": "a",
// "message": "Attachment not found",
// "method": "removeAttachment",
// "reason": "missing attachment",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// });
// }
//
// function removeDocument() {
// return success(jio.remove({"_id": "a"}));
// }
//
// function removeDocumentTest(answer) {
// deepEqual(answer, {
// "id": "a",
// "method": "remove",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// });
// }
//
// function removeDocumentAgainTest(answer) {
// deepEqual(answer, {
// "error": "not_found",
// "id": "a",
// "message": "Document not found",
// "method": "remove",
// "reason": "missing",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// });
// }
//
// putAttachment().then(putAttachmentTest).
// then(removeAttachment).then(removeAttachmentTest).
// then(removeAttachment).then(removeAttachmentAgainTest).
// then(removeDocument).then(removeDocumentTest).
// then(removeDocument).then(removeDocumentAgainTest).
// fail(unexpectedError).
// always(start);
// });
//
//
// test("allDocs", 5, function () {
// var jio = createQueryStorage('alldocs'),
// key_schema = {
// key_set: {
// case_insensitive_title: {
// read_from: 'title',
// equal_match: function (object_value, value) {
// return (object_value.toLowerCase() === value.toLowerCase());
// }
// }
// }
// };
//
// stop();
//
//
// function putDocuments() {
// var date_a = new Date(0),
// date_b = new Date(1234567890000);
//
// return RSVP.all([
// jio.put({
// "_id": "a",
// "title": "one",
// "date": date_a
// }).then(function () {
// return jio.putAttachment({
// "_id": "a",
// "_attachment": "aa",
// "_data": "aaa"
// });
// }),
// jio.put({"_id": "b", "title": "two", "date": date_a}),
// jio.put({"_id": "c", "title": "one", "date": date_b}),
// jio.put({"_id": "d", "title": "two", "date": date_b})
// ]);
// }
//
// function putDocumentsTest(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, [
// {
// "attachment": "aa",
// "digest": "sha256-9834876dcfb05cb167a5c24953eba58" +
// "c4ac89b1adf57f28f2f9d09af107ee8f0",
// "id": "a",
// "method": "putAttachment",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// },
// {
// "id": "b",
// "method": "put",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// },
// {
// "id": "c",
// "method": "put",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// },
// {
// "id": "d",
// "method": "put",
// "result": "success",
// "status": 201,
// "statusText": "Created"
// }
// ]);
//
// }
//
//
// function listDocuments() {
// return jio.allDocs();
// }
//
// function listDocumentsTest(answer) {
// 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"
// });
// }
//
// function listDocumentsQuery() {
// return jio.allDocs({
// "include_docs": true,
// "query": "title: \"two\""
// });
// }
//
//
// function listDocumentsQueryTest(answer) {
// deepEqual(answer, {
// "data": {
// "rows": [
// {
// "doc": {
// "_id": "b",
// "date": "1970-01-01T00:00:00.000Z",
// "title": "two"
// },
// "id": "b",
// "value": {}
// },
// {
// "doc": {
// "_id": "d",
// "date": "2009-02-13T23:31:30.000Z",
// "title": "two"
// },
// "id": "d",
// "value": {}
// }
// ],
// "total_rows": 2
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
// function listDocumentSchemaQueryNoDocs() {
// var jio_schema = createQueryStorage('alldocs', key_schema);
// return jio_schema.allDocs({
// "include_docs": false,
// "query": "case_insensitive_title: \"oNe\""
// });
// }
//
// function listDocumentSchemaQueryNoDocsTest(answer) {
// deepEqual(answer, {
// "data": {
// "rows": [
// {
// "id": "a",
// "value": {}
// },
// {
// "id": "c",
// "value": {}
// }
// ],
// "total_rows": 2
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
//
// function listDocumentSchemaQueryWithDocs() {
// var jio_schema = createQueryStorage('alldocs', key_schema);
// return jio_schema.allDocs({
// "include_docs": true,
// "query": "case_insensitive_title: \"oNe\""
// });
// }
//
// function listDocumentSchemaQueryWithDocsTest(answer) {
// deepEqual(answer, {
// "data": {
// "rows": [
// {
// "doc": {
// "_attachments": {
// "aa": {
// "content_type": "",
// "digest": "sha256-9834876dcfb05cb167a5c24953eba58c" +
// "4ac89b1adf57f28f2f9d09af107ee8f0",
// "length": 3
// }
// },
// "_id": "a",
// "date": "1970-01-01T00:00:00.000Z",
// "title": "one"
// },
// "id": "a",
// "value": {}
// },
// {
// "doc": {
// "_id": "c",
// "date": "2009-02-13T23:31:30.000Z",
// "title": "one"
// },
// "id": "c",
// "value": {}
// }
// ],
// "total_rows": 2
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// });
// }
//
//
//
// putDocuments().then(putDocumentsTest).
// then(listDocuments).then(listDocumentsTest).
// then(listDocumentsQuery).then(listDocumentsQueryTest).
// then(listDocumentSchemaQueryNoDocs).
// then(listDocumentSchemaQueryNoDocsTest).
// then(listDocumentSchemaQueryWithDocs).
// then(listDocumentSchemaQueryWithDocsTest).
// fail(unexpectedError).
// always(start);
// });
//
//
//
// // XXX check/repair not tested yet, may change soon btw
// // test("check & repair", 18, function () {
// // })
}(jIO, QUnit));
test("manual query used if substorage does not handle limit", function () {
stop();
expect(4);
function StorageNoLimitCapacity() {
return this;
}
StorageNoLimitCapacity.prototype.get = function (options) {
if (options._id === "foo") {
equal(options._id, "foo", "Get foo");
} else {
equal(options._id, "bar", "Get bar");
}
return {title: options._id, id: "ID " + options._id,
"another": "property"};
};
StorageNoLimitCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "sort") ||
(capacity === "query")) {
return true;
}
return false;
};
StorageNoLimitCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {}, "No query parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('querystoragenolimitcapacity', StorageNoLimitCapacity);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystoragenolimitcapacity"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
query: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual query used if substorage does not handle query", function () {
stop();
expect(4);
function StorageNoQueryCapacity() {
return this;
}
StorageNoQueryCapacity.prototype.get = function (options) {
if (options._id === "foo") {
equal(options._id, "foo", "Get foo");
} else {
equal(options._id, "bar", "Get bar");
}
return {title: options._id, id: "ID " + options._id,
"another": "property"};
};
StorageNoQueryCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "sort")) {
return true;
}
return false;
};
StorageNoQueryCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {}, "No query parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('querystoragenoquerycapacity', StorageNoQueryCapacity);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystoragenoquerycapacity"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
query: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("does not fetch doc one by one if substorage handle include_docs",
function () {
stop();
expect(2);
function StorageIncludeDocsCapacity() {
return this;
}
StorageIncludeDocsCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "include")) {
return true;
}
return false;
};
StorageIncludeDocsCapacity.prototype.buildQuery = function (options) {
deepEqual(options, {include_docs: true}, "Include docs parameter");
var result2 = [{
id: "foo",
value: {},
doc: {
title: "foo",
id: "ID foo",
_id: "foo",
another: "property"
}
}, {
id: "bar",
value: {},
doc: {
title: "bar",
id: "ID bar",
_id: "bar",
another: "property"
}
}];
return result2;
};
jIO.addStorage('querystorageincludedocscapacity',
StorageIncludeDocsCapacity);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorageincludedocscapacity"
}
});
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
query: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
......@@ -3,13 +3,14 @@
<head>
<meta charset="utf-8" />
<title>JIO Qunit/Sinon Unit Tests</title>
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js"></script>
<script src="../dist/jio-latest.js"></script>
<link rel="stylesheet" href="../node_modules/grunt-contrib-qunit/test/libs/qunit.css" type="text/css" media="screen"/>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js"></script>
<script src="../node_modules/sinon/pkg/sinon.js" type="text/javascript"></script>
<script src="../dist/jio-latest.js"></script>
<script src="html5.js"></script>
<!--script src="html5.js"></script-->
<!--script src="jio/util.js"></script-->
<!--script src="jio/fakestorage.js"></script>
<script src="jio/tests.js"></script-->
......@@ -22,24 +23,27 @@
<script src="queries/jiodate.tests.js"></script>
<script src="queries/key-jiodate.tests.js"></script>
<script src="queries/key-localstorage.tests.js"></script>
<!--script src="queries/key-localstorage.tests.js"></script-->
<script src="jio.storage/localstorage.tests.js"></script>
<script src="jio.storage/memorystorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script>
<!--script src="jio.storage/localstorage.tests.js"></script>
<script src="jio.storage/davstorage.tests.js"></script>
<script src="jio.storage/indexeddbstorage.tests.js"></script>
<script src="jio.storage/unionstorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script-->
<!--script src="jio.storage/indexeddbstorage.tests.js"></script-->
<!--script src="jio.storage/indexstorage.tests.js"></script-->
<!--script src="jio.storage/dropboxstorage.tests.js"></script-->
<!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script-->
<!--script src="../src/jio.storage/indexstorage.js"></script>
<script src="jio.storage/indexstorage.tests.js"></script>
<script src="../src/jio.storage/gidstorage.js"></script>
<!--script src="../src/jio.storage/gidstorage.js"></script>
<script src="../test/jio.storage/gidstorage.tests.js"></script>
<script src="../src/jio.storage/revisionstorage.js"></script>
......
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