Commit 12bb5c55 authored by Romain Courteaud's avatar Romain Courteaud

Stop mixing API parameters and user data.

Modify jIO API to keep user data not modified.
    jIO.get({"_id": "foo"} -> jIO.get("foo")
    jIO.remove({"_id": "foo"} -> jIO.remove("foo")
    jIO.post({"title": "bar"}) -> jIO.post({"title": "bar"})
    jIO.put({"_id": "foo", "title": "bar"}) -> jIO.put("foo", {"title": "bar"})
    jIO.getAttachment({"_id": "foo", "_attachment": "enclosure"} -> jIO.get("foo", "enclosure")
    jIO.removeAttachment({"_id": "foo", "_attachment": "enclosure"} -> jIO.remove("foo", "enclosure")
    jIO.putAttachment({"_id": "foo", "_attachment": "enclosure", "_blob": blob} -> jIO.remove("foo", "enclosure", blob)

jIO.get does not return an _id attribute anymore.
parent f4a45ae1
...@@ -174,8 +174,8 @@ ...@@ -174,8 +174,8 @@
util.readBlobAsDataURL = readBlobAsDataURL; util.readBlobAsDataURL = readBlobAsDataURL;
// tools // tools
function checkId(param, storage, method_name) { function checkId(argument_list, storage, method_name) {
if (typeof param._id !== 'string' || param._id === '') { if (typeof argument_list[0] !== 'string' || argument_list[0] === '') {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Document id must be a non empty string on '" + storage.__type + "Document id must be a non empty string on '" + storage.__type +
"." + method_name + "'.", "." + method_name + "'.",
...@@ -184,8 +184,8 @@ ...@@ -184,8 +184,8 @@
} }
} }
function checkAttachmentId(param, storage, method_name) { function checkAttachmentId(argument_list, storage, method_name) {
if (typeof param._attachment !== 'string' || param._attachment === '') { if (typeof argument_list[1] !== 'string' || argument_list[1] === '') {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Attachment id must be a non empty string on '" + storage.__type + "Attachment id must be a non empty string on '" + storage.__type +
"." + method_name + "'.", "." + method_name + "'.",
...@@ -204,7 +204,7 @@ ...@@ -204,7 +204,7 @@
if (precondition_function !== undefined) { if (precondition_function !== undefined) {
return precondition_function.apply( return precondition_function.apply(
context.__storage, context.__storage,
[argument_list[0], context, name] [argument_list, context, name]
); );
} }
}) })
...@@ -252,78 +252,59 @@ ...@@ -252,78 +252,59 @@
} }
declareMethod(JioProxyStorage, "put", checkId, function (argument_list) { declareMethod(JioProxyStorage, "put", checkId, function (argument_list) {
return argument_list[0]._id; return argument_list[0];
}); });
declareMethod(JioProxyStorage, "get", checkId, declareMethod(JioProxyStorage, "get", checkId);
function (argument_list, result) {
// XXX Drop all _ properties
// Put _id properties to the result
result._id = argument_list[0]._id;
return result;
});
declareMethod(JioProxyStorage, "remove", checkId, function (argument_list) { declareMethod(JioProxyStorage, "remove", checkId, function (argument_list) {
return argument_list[0]._id; return argument_list[0];
}); });
JioProxyStorage.prototype.post = function (param) { JioProxyStorage.prototype.post = function () {
var context = this; var context = this,
argument_list = arguments;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
if (param._id === undefined) { var storage_method = context.__storage.post;
var storage_method = context.__storage.post; if (storage_method === undefined) {
if (storage_method === undefined) { throw new jIO.util.jIOError(
throw new jIO.util.jIOError( "Capacity 'post' is not implemented on '" + context.__type + "'",
"Capacity 'post' is not implemented on '" + context.__type + "'", 501
501 );
);
}
return context.__storage.post(param);
} }
return context.put(param); return context.__storage.post.apply(context.__storage, argument_list);
}); });
}; };
declareMethod(JioProxyStorage, 'putAttachment', function (param, storage, declareMethod(JioProxyStorage, 'putAttachment', function (argument_list,
storage,
method_name) { method_name) {
checkId(param, storage, method_name); checkId(argument_list, storage, method_name);
checkAttachmentId(param, storage, method_name); checkAttachmentId(argument_list, storage, method_name);
var options = argument_list[3] || {};
if (!(param._blob instanceof Blob) && if (typeof argument_list[2] === 'string') {
typeof param._data === 'string') { argument_list[2] = new Blob([argument_list[2]], {
param._blob = new Blob([param._data], { "type": options._content_type || options._mimetype ||
"type": param._content_type || param._mimetype ||
"text/plain;charset=utf-8" "text/plain;charset=utf-8"
}); });
delete param._data; } else if (!(argument_list[2] instanceof Blob)) {
delete param._mimetype;
delete param._content_type;
} else if (param._blob instanceof Blob) {
delete param._data;
delete param._mimetype;
delete param._content_type;
} else if (param._data instanceof Blob) {
param._blob = param._data;
delete param._data;
delete param._mimetype;
delete param._content_type;
} else {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
'Attachment information must be like {"_id": document id, ' + 'Attachment content is not a blob',
'"_attachment": attachment name, "_data": string, ["_mimetype": ' +
'content type]} or {"_id": document id, "_attachment": ' +
'attachment name, "_blob": Blob}',
400 400
); );
} }
}); });
declareMethod(JioProxyStorage, 'removeAttachment', function (param, storage, declareMethod(JioProxyStorage, 'removeAttachment', function (argument_list,
storage,
method_name) { method_name) {
checkId(param, storage, method_name); checkId(argument_list, storage, method_name);
checkAttachmentId(param, storage, method_name); checkAttachmentId(argument_list, storage, method_name);
}); });
declareMethod(JioProxyStorage, 'getAttachment', function (param, storage, declareMethod(JioProxyStorage, 'getAttachment', function (argument_list,
storage,
method_name) { method_name) {
// if (param.storage_spec.type !== "indexeddb" && // if (param.storage_spec.type !== "indexeddb" &&
// param.storage_spec.type !== "dav" && // param.storage_spec.type !== "dav" &&
...@@ -336,8 +317,8 @@ ...@@ -336,8 +317,8 @@
// ]); // ]);
// return false; // return false;
// } // }
checkId(param, storage, method_name); checkId(argument_list, storage, method_name);
checkAttachmentId(param, storage, method_name); checkAttachmentId(argument_list, storage, method_name);
}, function (argument_list, result) { }, function (argument_list, result) {
if (!(result instanceof Blob)) { if (!(result instanceof Blob)) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
......
...@@ -81,8 +81,8 @@ ...@@ -81,8 +81,8 @@
} }
DavStorage.prototype.put = function (param) { DavStorage.prototype.put = function (id, param) {
var id = restrictDocumentId(param._id); id = restrictDocumentId(id);
delete param._id; delete param._id;
if (Object.getOwnPropertyNames(param).length > 0) { if (Object.getOwnPropertyNames(param).length > 0) {
// Reject if param has other properties than _id // Reject if param has other properties than _id
...@@ -95,18 +95,18 @@ ...@@ -95,18 +95,18 @@
}); });
}; };
DavStorage.prototype.remove = function (param) { DavStorage.prototype.remove = function (id) {
var id = restrictDocumentId(param._id); id = restrictDocumentId(id);
return ajax(this, { return ajax(this, {
type: "DELETE", type: "DELETE",
url: this._url + id url: this._url + id
}); });
}; };
DavStorage.prototype.get = function (param) { DavStorage.prototype.get = function (id) {
var id = restrictDocumentId(param._id), var context = this;
context = this; id = restrictDocumentId(id);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -162,26 +162,26 @@ ...@@ -162,26 +162,26 @@
}; };
DavStorage.prototype.putAttachment = function (param) { DavStorage.prototype.putAttachment = function (id, name, blob) {
var id = restrictDocumentId(param._id); id = restrictDocumentId(id);
restrictAttachmentId(param._attachment); restrictAttachmentId(name);
return ajax(this, { return ajax(this, {
type: "PUT", type: "PUT",
url: this._url + id + param._attachment, url: this._url + id + name,
data: param._blob data: blob
}); });
}; };
DavStorage.prototype.getAttachment = function (param) { DavStorage.prototype.getAttachment = function (id, name) {
var id = restrictDocumentId(param._id), var context = this;
context = this; id = restrictDocumentId(id);
restrictAttachmentId(param._attachment); restrictAttachmentId(name);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return ajax(context, { return ajax(context, {
type: "GET", type: "GET",
url: context._url + id + param._attachment, url: context._url + id + name,
dataType: "blob" dataType: "blob"
}); });
}) })
...@@ -195,7 +195,7 @@ ...@@ -195,7 +195,7 @@
if ((error.target !== undefined) && if ((error.target !== undefined) &&
(error.target.status === 404)) { (error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find attachment: " throw new jIO.util.jIOError("Cannot find attachment: "
+ param._id + " , " + param._attachment, + id + " , " + name,
404); 404);
} }
throw error; throw error;
...@@ -203,23 +203,23 @@ ...@@ -203,23 +203,23 @@
}; };
DavStorage.prototype.removeAttachment = function (param) { DavStorage.prototype.removeAttachment = function (id, name) {
var id = restrictDocumentId(param._id), var context = this;
context = this; id = restrictDocumentId(id);
restrictAttachmentId(param._attachment); restrictAttachmentId(name);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return ajax(context, { return ajax(context, {
type: "DELETE", type: "DELETE",
url: context._url + id + param._attachment url: context._url + id + name
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error.target !== undefined) && if ((error.target !== undefined) &&
(error.target.status === 404)) { (error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find attachment: " throw new jIO.util.jIOError("Cannot find attachment: "
+ param._id + " , " + param._attachment, + id + " , " + name,
404); 404);
} }
throw error; throw error;
......
...@@ -19,21 +19,21 @@ ...@@ -19,21 +19,21 @@
DOCUMENT_EXTENSION + "$"), DOCUMENT_EXTENSION + "$"),
ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$"); ATTACHMENT_REGEXP = new RegExp("^jio_attachment/([\\w=]+)/([\\w=]+)$");
function getSubAttachmentIdFromParam(param) { function getSubAttachmentIdFromParam(id, name) {
if (param._attachment === undefined) { if (name === undefined) {
return 'jio_document/' + btoa(param._id) + DOCUMENT_EXTENSION; return 'jio_document/' + btoa(id) + DOCUMENT_EXTENSION;
} }
return 'jio_attachment/' + btoa(param._id) + "/" + btoa(param._attachment); return 'jio_attachment/' + btoa(id) + "/" + btoa(name);
} }
DocumentStorage.prototype.get = function (param) { DocumentStorage.prototype.get = function (id) {
var result, var result,
context = this; context = this;
return this._sub_storage.getAttachment({ return this._sub_storage.getAttachment(
"_id": this._document_id, this._document_id,
"_attachment": getSubAttachmentIdFromParam(param) getSubAttachmentIdFromParam(id)
}) )
.push(function (blob) { .push(function (blob) {
return jIO.util.readBlobAsText(blob); return jIO.util.readBlobAsText(blob);
}) })
...@@ -42,9 +42,7 @@ ...@@ -42,9 +42,7 @@
}) })
.push(function (json) { .push(function (json) {
result = json; result = json;
return context._sub_storage.get({ return context._sub_storage.get(context._document_id);
"_id": context._document_id
});
}) })
.push(function (document) { .push(function (document) {
var attachments = {}, var attachments = {},
...@@ -55,7 +53,7 @@ ...@@ -55,7 +53,7 @@
if (ATTACHMENT_REGEXP.test(key)) { if (ATTACHMENT_REGEXP.test(key)) {
exec = ATTACHMENT_REGEXP.exec(key); exec = ATTACHMENT_REGEXP.exec(key);
try { try {
if (atob(exec[1]) === param._id) { if (atob(exec[1]) === id) {
attachments[atob(exec[2])] = {}; attachments[atob(exec[2])] = {};
} }
} catch (error) { } catch (error) {
...@@ -74,27 +72,25 @@ ...@@ -74,27 +72,25 @@
}); });
}; };
DocumentStorage.prototype.put = function (param) { DocumentStorage.prototype.put = function (doc_id, param) {
var doc_id = param._id; return this._sub_storage.putAttachment(
this._document_id,
return this._sub_storage.putAttachment({ getSubAttachmentIdFromParam(doc_id),
"_id": this._document_id, new Blob([JSON.stringify(param)], {type: "application/json"})
"_attachment": getSubAttachmentIdFromParam(param), )
"_blob": new Blob([JSON.stringify(param)], {type: "application/json"})
})
.push(function () { .push(function () {
return doc_id; return doc_id;
}); });
}; };
DocumentStorage.prototype.remove = function (param) { DocumentStorage.prototype.remove = function (id) {
return this._sub_storage.removeAttachment({ return this._sub_storage.removeAttachment(
"_id": this._document_id, this._document_id,
"_attachment": getSubAttachmentIdFromParam(param) getSubAttachmentIdFromParam(id)
}) )
.push(function () { .push(function () {
return param._id; return id;
}); });
}; };
...@@ -103,9 +99,7 @@ ...@@ -103,9 +99,7 @@
}; };
DocumentStorage.prototype.buildQuery = function () { DocumentStorage.prototype.buildQuery = function () {
return this._sub_storage.get({ return this._sub_storage.get(this._document_id)
"_id": this._document_id
})
.push(function (document) { .push(function (document) {
var result = [], var result = [],
key; key;
...@@ -130,26 +124,26 @@ ...@@ -130,26 +124,26 @@
}); });
}; };
DocumentStorage.prototype.getAttachment = function (param) { DocumentStorage.prototype.getAttachment = function (id, name) {
return this._sub_storage.getAttachment({ return this._sub_storage.getAttachment(
"_id": this._document_id, this._document_id,
"_attachment": getSubAttachmentIdFromParam(param) getSubAttachmentIdFromParam(id, name)
}); );
}; };
DocumentStorage.prototype.putAttachment = function (param) { DocumentStorage.prototype.putAttachment = function (id, name, blob) {
return this._sub_storage.putAttachment({ return this._sub_storage.putAttachment(
"_id": this._document_id, this._document_id,
"_attachment": getSubAttachmentIdFromParam(param), getSubAttachmentIdFromParam(id, name),
"_blob": param._blob blob
}); );
}; };
DocumentStorage.prototype.removeAttachment = function (param) { DocumentStorage.prototype.removeAttachment = function (id, name) {
return this._sub_storage.removeAttachment({ return this._sub_storage.removeAttachment(
"_id": this._document_id, this._document_id,
"_attachment": getSubAttachmentIdFromParam(param) getSubAttachmentIdFromParam(id, name)
}); );
}; };
jIO.addStorage('document', DocumentStorage); jIO.addStorage('document', DocumentStorage);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
DOCUMENT_KEY = "/.jio_documents/", DOCUMENT_KEY = "/.jio_documents/",
ROOT = "/"; ROOT = "/";
FileSystemBridgeStorage.prototype.get = function (param) { FileSystemBridgeStorage.prototype.get = function (id) {
var context = this, var context = this,
json_document, json_document,
explicit_document = false; explicit_document = false;
...@@ -28,10 +28,10 @@ ...@@ -28,10 +28,10 @@
.push(function () { .push(function () {
// First get the document itself if it exists // First get the document itself if it exists
return context._sub_storage.getAttachment({ return context._sub_storage.getAttachment(
"_id": DOCUMENT_KEY, DOCUMENT_KEY,
"_attachment": param._id + DOCUMENT_EXTENSION id + DOCUMENT_EXTENSION
}); );
}) })
.push(function (blob) { .push(function (blob) {
return new RSVP.Queue() return new RSVP.Queue()
...@@ -55,20 +55,18 @@ ...@@ -55,20 +55,18 @@
.push(function (result) { .push(function (result) {
json_document = result; json_document = result;
return context._sub_storage.get({ return context._sub_storage.get(ROOT);
"_id": ROOT
});
}) })
.push(function (directory_document) { .push(function (directory_document) {
if ((directory_document.hasOwnProperty("_attachments")) && if ((directory_document.hasOwnProperty("_attachments")) &&
(directory_document._attachments.hasOwnProperty(param._id))) { (directory_document._attachments.hasOwnProperty(id))) {
json_document._attachments = { json_document._attachments = {
enclosure: {} enclosure: {}
}; };
} else { } else {
if (!explicit_document) { if (!explicit_document) {
throw new jIO.util.jIOError("Cannot find document " + param._id, throw new jIO.util.jIOError("Cannot find document " + id,
404); 404);
} }
} }
...@@ -77,29 +75,26 @@ ...@@ -77,29 +75,26 @@
}; };
FileSystemBridgeStorage.prototype.put = function (param) { FileSystemBridgeStorage.prototype.put = function (doc_id, param) {
var context = this, var context = this;
doc_id = param._id;
// XXX Handle conflict! // XXX Handle conflict!
return context._sub_storage.putAttachment({ return context._sub_storage.putAttachment(
"_id": DOCUMENT_KEY, DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION, doc_id + DOCUMENT_EXTENSION,
"_blob": new Blob([JSON.stringify(param)], {type: "application/json"}) new Blob([JSON.stringify(param)], {type: "application/json"})
}) )
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
return context._sub_storage.put({ return context._sub_storage.put(DOCUMENT_KEY, {})
"_id": DOCUMENT_KEY
})
.push(function () { .push(function () {
return context._sub_storage.putAttachment({ return context._sub_storage.putAttachment(
"_id": DOCUMENT_KEY, DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION, doc_id + DOCUMENT_EXTENSION,
"_blob": new Blob([JSON.stringify(param)], new Blob([JSON.stringify(param)],
{type: "application/json"}) {type: "application/json"})
}); );
}); });
} }
throw error; throw error;
...@@ -110,18 +105,17 @@ ...@@ -110,18 +105,17 @@
}; };
FileSystemBridgeStorage.prototype.remove = function (param) { FileSystemBridgeStorage.prototype.remove = function (doc_id) {
var context = this, var context = this,
got_error = false, got_error = false;
doc_id = param._id;
return new RSVP.Queue() return new RSVP.Queue()
// First, try to remove enclosure // First, try to remove enclosure
.push(function () { .push(function () {
return context._sub_storage.removeAttachment({ return context._sub_storage.removeAttachment(
"_id": ROOT, ROOT,
"_attachment": doc_id doc_id
}); );
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -135,10 +129,10 @@ ...@@ -135,10 +129,10 @@
// Second, try to remove explicit doc // Second, try to remove explicit doc
.push(function () { .push(function () {
return context._sub_storage.removeAttachment({ return context._sub_storage.removeAttachment(
"_id": DOCUMENT_KEY, DOCUMENT_KEY,
"_attachment": doc_id + DOCUMENT_EXTENSION doc_id + DOCUMENT_EXTENSION
}); );
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -163,9 +157,7 @@ ...@@ -163,9 +157,7 @@
// First, get list of explicit documents // First, get list of explicit documents
.push(function () { .push(function () {
return context._sub_storage.get({ return context._sub_storage.get(DOCUMENT_KEY);
"_id": DOCUMENT_KEY
});
}) })
.push(function (result) { .push(function (result) {
var key; var key;
...@@ -187,9 +179,7 @@ ...@@ -187,9 +179,7 @@
// Second, get list of enclosure // Second, get list of enclosure
.push(function () { .push(function () {
return context._sub_storage.get({ return context._sub_storage.get(ROOT);
"_id": ROOT
});
}) })
.push(function (result) { .push(function (result) {
var key; var key;
...@@ -218,41 +208,35 @@ ...@@ -218,41 +208,35 @@
}; };
FileSystemBridgeStorage.prototype.getAttachment = function (param) { FileSystemBridgeStorage.prototype.getAttachment = function (id, name) {
if (param._attachment !== "enclosure") { if (name !== "enclosure") {
throw new jIO.util.jIOError("Only support 'enclosure' attachment", throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400); 400);
} }
return this._sub_storage.getAttachment({ return this._sub_storage.getAttachment(ROOT, id);
"_id": ROOT,
"_attachment": param._id
});
}; };
FileSystemBridgeStorage.prototype.putAttachment = function (param) { FileSystemBridgeStorage.prototype.putAttachment = function (id, name, blob) {
if (param._attachment !== "enclosure") { if (name !== "enclosure") {
throw new jIO.util.jIOError("Only support 'enclosure' attachment", throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400); 400);
} }
return this._sub_storage.putAttachment({ return this._sub_storage.putAttachment(
"_id": ROOT, ROOT,
"_attachment": param._id, id,
"_blob": param._blob blob
}); );
}; };
FileSystemBridgeStorage.prototype.removeAttachment = function (param) { FileSystemBridgeStorage.prototype.removeAttachment = function (id, name) {
if (param._attachment !== "enclosure") { if (name !== "enclosure") {
throw new jIO.util.jIOError("Only support 'enclosure' attachment", throw new jIO.util.jIOError("Only support 'enclosure' attachment",
400); 400);
} }
return this._sub_storage.removeAttachment({ return this._sub_storage.removeAttachment(ROOT, id);
"_id": ROOT,
"_attachment": param._id
});
}; };
jIO.addStorage('drivetojiomapping', FileSystemBridgeStorage); jIO.addStorage('drivetojiomapping', FileSystemBridgeStorage);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
// url: {string} // url: {string}
// } // }
/*jslint nomen: true */ /*jslint nomen: true, unparam: true */
/*global jIO, UriTemplate, FormData, RSVP, URI, Blob*/ /*global jIO, UriTemplate, FormData, RSVP, URI, Blob*/
(function (jIO, UriTemplate, FormData, RSVP, URI, Blob) { (function (jIO, UriTemplate, FormData, RSVP, URI, Blob) {
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
}); });
} }
function getDocumentAndHateoas(storage, param, options) { function getDocumentAndHateoas(storage, id, options) {
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
} }
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
"type": "GET", "type": "GET",
"url": UriTemplate.parse(site_hal._links.traverse.href) "url": UriTemplate.parse(site_hal._links.traverse.href)
.expand({ .expand({
relative_url: param._id, relative_url: id,
view: options._view view: options._view
}), }),
"xhrFields": { "xhrFields": {
...@@ -62,8 +62,8 @@ ...@@ -62,8 +62,8 @@
this._default_view_reference = spec.default_view_reference; this._default_view_reference = spec.default_view_reference;
} }
ERP5Storage.prototype.get = function (param) { ERP5Storage.prototype.get = function (id) {
return getDocumentAndHateoas(this, param) return getDocumentAndHateoas(this, id)
.push(function (response) { .push(function (response) {
var result = JSON.parse(response.target.responseText), var result = JSON.parse(response.target.responseText),
attachments = { attachments = {
...@@ -72,7 +72,6 @@ ...@@ -72,7 +72,6 @@
}, },
key; key;
// action_type; // action_type;
result._id = param._id;
result.portal_type = result._links.type.name; result.portal_type = result._links.type.name;
// Remove all ERP5 hateoas links / convert them into jIO ID // Remove all ERP5 hateoas links / convert them into jIO ID
...@@ -90,15 +89,14 @@ ...@@ -90,15 +89,14 @@
}); });
}; };
ERP5Storage.prototype.getAttachment = function (param) { ERP5Storage.prototype.getAttachment = function (id, action) {
var action = param._attachment;
if (action === "view") { if (action === "view") {
return getDocumentAndHateoas(this, param, return getDocumentAndHateoas(this, id,
{"_view": this._default_view_reference}) {"_view": this._default_view_reference})
.push(function (response) { .push(function (response) {
var result = JSON.parse(response.target.responseText); var result = JSON.parse(response.target.responseText);
result._id = param._id; result._id = id;
result.portal_type = result._links.type.name; result.portal_type = result._links.type.name;
// Remove all ERP5 hateoas links / convert them into jIO ID // Remove all ERP5 hateoas links / convert them into jIO ID
...@@ -113,7 +111,7 @@ ...@@ -113,7 +111,7 @@
}); });
} }
if (action === "links") { if (action === "links") {
return getDocumentAndHateoas(this, param) return getDocumentAndHateoas(this, id)
.push(function (response) { .push(function (response) {
return new Blob( return new Blob(
[JSON.stringify(JSON.parse(response.target.responseText))], [JSON.stringify(JSON.parse(response.target.responseText))],
...@@ -134,7 +132,7 @@ ...@@ -134,7 +132,7 @@
}) })
.push(function (evt) { .push(function (evt) {
var result = JSON.parse(evt.target.responseText); var result = JSON.parse(evt.target.responseText);
result._id = param._id; result._id = id;
return new Blob( return new Blob(
[JSON.stringify(result)], [JSON.stringify(result)],
{"type": evt.target.getResponseHeader("Content-Type")} {"type": evt.target.getResponseHeader("Content-Type")}
...@@ -145,16 +143,16 @@ ...@@ -145,16 +143,16 @@
400); 400);
}; };
ERP5Storage.prototype.putAttachment = function (metadata) { ERP5Storage.prototype.putAttachment = function (id, name, blob) {
// Assert we use a callable on a document from the ERP5 site // Assert we use a callable on a document from the ERP5 site
if (metadata._attachment.indexOf(this._url) !== 0) { if (name.indexOf(this._url) !== 0) {
throw new jIO.util.jIOError("Can not store outside ERP5: " + throw new jIO.util.jIOError("Can not store outside ERP5: " +
metadata._attachment, 400); name, 400);
} }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.readBlobAsText(metadata._blob); return jIO.util.readBlobAsText(blob);
}) })
.push(function (evt) { .push(function (evt) {
var form_data = JSON.parse(evt.target.result), var form_data = JSON.parse(evt.target.result),
...@@ -167,7 +165,7 @@ ...@@ -167,7 +165,7 @@
} }
return jIO.util.ajax({ return jIO.util.ajax({
"type": "POST", "type": "POST",
"url": metadata._attachment, "url": name,
"data": data, "data": data,
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: true
......
...@@ -205,7 +205,7 @@ ...@@ -205,7 +205,7 @@
return new RSVP.Promise(resolver); return new RSVP.Promise(resolver);
} }
IndexedDBStorage.prototype.get = function (param) { IndexedDBStorage.prototype.get = function (id) {
var attachment_dict = {}; var attachment_dict = {};
function addEntry(cursor) { function addEntry(cursor) {
...@@ -217,13 +217,13 @@ ...@@ -217,13 +217,13 @@
var transaction = openTransaction(db, ["metadata", "attachment"], var transaction = openTransaction(db, ["metadata", "attachment"],
"readonly"); "readonly");
return RSVP.all([ return RSVP.all([
handleGet(transaction.objectStore("metadata").get(param._id)), handleGet(transaction.objectStore("metadata").get(id)),
handleCursor(transaction.objectStore("attachment").index("_id") handleCursor(transaction.objectStore("attachment").index("_id")
.openCursor(IDBKeyRange.only(param._id)), addEntry) .openCursor(IDBKeyRange.only(id)), addEntry)
]); ]);
}) })
.push(function (result_list) { .push(function (result_list) {
var result = result_list[0]; var result = result_list[0].doc;
if (Object.getOwnPropertyNames(attachment_dict).length > 0) { if (Object.getOwnPropertyNames(attachment_dict).length > 0) {
result._attachments = attachment_dict; result._attachments = attachment_dict;
} }
...@@ -241,11 +241,14 @@ ...@@ -241,11 +241,14 @@
return new RSVP.Promise(resolver); return new RSVP.Promise(resolver);
} }
IndexedDBStorage.prototype.put = function (metadata) { IndexedDBStorage.prototype.put = function (id, metadata) {
return openIndexedDB(this) return openIndexedDB(this)
.push(function (db) { .push(function (db) {
var transaction = openTransaction(db, ["metadata"], "readwrite"); var transaction = openTransaction(db, ["metadata"], "readwrite");
return handleRequest(transaction.objectStore("metadata").put(metadata)); return handleRequest(transaction.objectStore("metadata").put({
"_id": id,
"doc": metadata
}));
}); });
}; };
...@@ -253,33 +256,36 @@ ...@@ -253,33 +256,36 @@
cursor["delete"](); cursor["delete"]();
} }
IndexedDBStorage.prototype.remove = function (param) { IndexedDBStorage.prototype.remove = function (id) {
return openIndexedDB(this) return openIndexedDB(this)
.push(function (db) { .push(function (db) {
var transaction = openTransaction(db, ["metadata", "attachment", var transaction = openTransaction(db, ["metadata", "attachment",
"blob"], "readwrite"); "blob"], "readwrite");
return RSVP.all([ return RSVP.all([
handleRequest(transaction handleRequest(transaction
.objectStore("metadata")["delete"](param._id)), .objectStore("metadata")["delete"](id)),
// XXX Why not possible to delete with KeyCursor? // XXX Why not possible to delete with KeyCursor?
handleCursor(transaction.objectStore("attachment").index("_id") handleCursor(transaction.objectStore("attachment").index("_id")
.openCursor(IDBKeyRange.only(param._id)), deleteEntry), .openCursor(IDBKeyRange.only(id)), deleteEntry),
handleCursor(transaction.objectStore("blob").index("_id") handleCursor(transaction.objectStore("blob").index("_id")
.openCursor(IDBKeyRange.only(param._id)), deleteEntry) .openCursor(IDBKeyRange.only(id)), deleteEntry)
]); ]);
}); });
}; };
IndexedDBStorage.prototype.getAttachment = function (param) { IndexedDBStorage.prototype.getAttachment = function (id, name, options) {
var transaction, var transaction,
start, start,
end; end;
if (options === undefined) {
options = {};
}
return openIndexedDB(this) return openIndexedDB(this)
.push(function (db) { .push(function (db) {
transaction = openTransaction(db, ["attachment", "blob"], "readonly"); transaction = openTransaction(db, ["attachment", "blob"], "readonly");
// XXX Should raise if key is not good // XXX Should raise if key is not good
return handleGet(transaction.objectStore("attachment") return handleGet(transaction.objectStore("attachment")
.get(buildKeyPath([param._id, param._attachment]))); .get(buildKeyPath([id, name])));
}) })
.push(function (attachment) { .push(function (attachment) {
var total_length = attachment.info.length, var total_length = attachment.info.length,
...@@ -289,8 +295,8 @@ ...@@ -289,8 +295,8 @@
start_index, start_index,
end_index; end_index;
start = param._start || 0; start = options.start || 0;
end = param._end || total_length; end = options.end || total_length;
if (end > total_length) { if (end > total_length) {
end = total_length; end = total_length;
} }
...@@ -312,8 +318,8 @@ ...@@ -312,8 +318,8 @@
for (i = start_index; i <= end_index; i += 1) { for (i = start_index; i <= end_index; i += 1) {
promise_list.push( promise_list.push(
handleGet(store.get(buildKeyPath([param._id, handleGet(store.get(buildKeyPath([id,
param._attachment, i]))) name, i])))
); );
} }
return RSVP.all(promise_list); return RSVP.all(promise_list);
...@@ -331,22 +337,22 @@ ...@@ -331,22 +337,22 @@
}); });
}; };
function removeAttachment(transaction, param) { function removeAttachment(transaction, id, name) {
return RSVP.all([ return RSVP.all([
// XXX How to get the right attachment // XXX How to get the right attachment
handleRequest(transaction.objectStore("attachment")["delete"]( handleRequest(transaction.objectStore("attachment")["delete"](
buildKeyPath([param._id, param._attachment]) buildKeyPath([id, name])
)), )),
handleCursor(transaction.objectStore("blob").index("_id_attachment") handleCursor(transaction.objectStore("blob").index("_id_attachment")
.openCursor(IDBKeyRange.only( .openCursor(IDBKeyRange.only(
[param._id, param._attachment] [id, name]
)), )),
deleteEntry deleteEntry
) )
]); ]);
} }
IndexedDBStorage.prototype.putAttachment = function (metadata) { IndexedDBStorage.prototype.putAttachment = function (id, name, blob) {
var blob_part = [], var blob_part = [],
transaction, transaction,
db; db;
...@@ -356,11 +362,11 @@ ...@@ -356,11 +362,11 @@
db = database; db = database;
// Split the blob first // Split the blob first
return jIO.util.readBlobAsArrayBuffer(metadata._blob); return jIO.util.readBlobAsArrayBuffer(blob);
}) })
.push(function (event) { .push(function (event) {
var array_buffer = event.target.result, var array_buffer = event.target.result,
total_size = metadata._blob.size, total_size = blob.size,
handled_size = 0; handled_size = 0;
while (handled_size < total_size) { while (handled_size < total_size) {
...@@ -371,18 +377,18 @@ ...@@ -371,18 +377,18 @@
// Remove previous attachment // Remove previous attachment
transaction = openTransaction(db, ["attachment", "blob"], "readwrite"); transaction = openTransaction(db, ["attachment", "blob"], "readwrite");
return removeAttachment(transaction, metadata); return removeAttachment(transaction, id, name);
}) })
.push(function () { .push(function () {
var promise_list = [ var promise_list = [
handleRequest(transaction.objectStore("attachment").put({ handleRequest(transaction.objectStore("attachment").put({
"_key_path": buildKeyPath([metadata._id, metadata._attachment]), "_key_path": buildKeyPath([id, name]),
"_id": metadata._id, "_id": id,
"_attachment": metadata._attachment, "_attachment": name,
"info": { "info": {
"content_type": metadata._blob.type, "content_type": blob.type,
"length": metadata._blob.size "length": blob.size
} }
})) }))
], ],
...@@ -392,10 +398,10 @@ ...@@ -392,10 +398,10 @@
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
promise_list.push( promise_list.push(
handleRequest(blob_store.put({ handleRequest(blob_store.put({
"_key_path": buildKeyPath([metadata._id, metadata._attachment, "_key_path": buildKeyPath([id, name,
i]), i]),
"_id" : metadata._id, "_id" : id,
"_attachment" : metadata._attachment, "_attachment" : name,
"_part" : i, "_part" : i,
"blob": blob_part[i] "blob": blob_part[i]
})) }))
...@@ -406,12 +412,12 @@ ...@@ -406,12 +412,12 @@
}); });
}; };
IndexedDBStorage.prototype.removeAttachment = function (param) { IndexedDBStorage.prototype.removeAttachment = function (id, name) {
return openIndexedDB(this) return openIndexedDB(this)
.push(function (db) { .push(function (db) {
var transaction = openTransaction(db, ["attachment", "blob"], var transaction = openTransaction(db, ["attachment", "blob"],
"readwrite"); "readwrite");
return removeAttachment(transaction, param); return removeAttachment(transaction, id, name);
}); });
}; };
......
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
} }
} }
LocalStorage.prototype.get = function (param) { LocalStorage.prototype.get = function (id) {
restrictDocumentId(param._id); restrictDocumentId(id);
var doc = {}, var doc = {},
attachments = {}, attachments = {},
...@@ -78,38 +78,38 @@ ...@@ -78,38 +78,38 @@
return new Blob([arrayBuffer], {type: mimeString}); return new Blob([arrayBuffer], {type: mimeString});
} }
LocalStorage.prototype.getAttachment = function (param) { LocalStorage.prototype.getAttachment = function (id, name) {
restrictDocumentId(param._id); restrictDocumentId(id);
var textstring = this._storage.getItem(param._attachment); var textstring = this._storage.getItem(name);
if (textstring === null) { if (textstring === null) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Cannot find attachment " + param._attachment, "Cannot find attachment " + name,
404 404
); );
} }
return dataURItoBlob(textstring); return dataURItoBlob(textstring);
}; };
LocalStorage.prototype.putAttachment = function (param) { LocalStorage.prototype.putAttachment = function (id, name, blob) {
var context = this; var context = this;
restrictDocumentId(param._id); restrictDocumentId(id);
// the document already exists // the document already exists
// download data // download data
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.readBlobAsDataURL(param._blob); return jIO.util.readBlobAsDataURL(blob);
}) })
.push(function (e) { .push(function (e) {
context._storage.setItem(param._attachment, e.target.result); context._storage.setItem(name, e.target.result);
}); });
}; };
LocalStorage.prototype.removeAttachment = function (param) { LocalStorage.prototype.removeAttachment = function (id, name) {
restrictDocumentId(param._id); restrictDocumentId(id);
return this._storage.removeItem(param._attachment); return this._storage.removeItem(name);
}; };
......
...@@ -33,28 +33,28 @@ ...@@ -33,28 +33,28 @@
this._database = {}; this._database = {};
} }
MemoryStorage.prototype.put = function (metadata) { MemoryStorage.prototype.put = function (id, metadata) {
if (!this._database.hasOwnProperty(metadata._id)) { if (!this._database.hasOwnProperty(id)) {
this._database[metadata._id] = { this._database[id] = {
attachments: {} attachments: {}
}; };
} }
this._database[metadata._id].doc = JSON.stringify(metadata); this._database[id].doc = JSON.stringify(metadata);
return metadata._id; return id;
}; };
MemoryStorage.prototype.get = function (param) { MemoryStorage.prototype.get = function (id) {
var doc, var doc,
key, key,
found = false, found = false,
attachments = {}; attachments = {};
try { try {
doc = JSON.parse(this._database[param._id].doc); doc = JSON.parse(this._database[id].doc);
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Cannot find document: " + param._id, "Cannot find document: " + id,
404 404
); );
} }
...@@ -62,8 +62,8 @@ ...@@ -62,8 +62,8 @@
} }
// XXX NotImplemented: list all attachments // XXX NotImplemented: list all attachments
for (key in this._database[param._id].attachments) { for (key in this._database[id].attachments) {
if (this._database[param._id].attachments.hasOwnProperty(key)) { if (this._database[id].attachments.hasOwnProperty(key)) {
found = true; found = true;
attachments[key] = {}; attachments[key] = {};
} }
...@@ -74,17 +74,17 @@ ...@@ -74,17 +74,17 @@
return doc; return doc;
}; };
MemoryStorage.prototype.remove = function (param) { MemoryStorage.prototype.remove = function (id) {
delete this._database[param._id]; delete this._database[id];
return param._id; return id;
}; };
MemoryStorage.prototype.getAttachment = function (param) { MemoryStorage.prototype.getAttachment = function (id, name) {
try { try {
var result = this._database[param._id].attachments[param._attachment]; var result = this._database[id].attachments[name];
if (result === undefined) { if (result === undefined) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Cannot find attachment: " + param._id + " , " + param._attachment, "Cannot find attachment: " + id + " , " + name,
404 404
); );
} }
...@@ -92,7 +92,7 @@ ...@@ -92,7 +92,7 @@
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Cannot find attachment: " + param._id + " , " + param._attachment, "Cannot find attachment: " + id + " , " + name,
404 404
); );
} }
...@@ -100,26 +100,26 @@ ...@@ -100,26 +100,26 @@
} }
}; };
MemoryStorage.prototype.putAttachment = function (param) { MemoryStorage.prototype.putAttachment = function (id, name, blob) {
var attachment_dict; var attachment_dict;
try { try {
attachment_dict = this._database[param._id].attachments; attachment_dict = this._database[id].attachments;
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError("Cannot find document: " + param._id, 404); throw new jIO.util.jIOError("Cannot find document: " + id, 404);
} }
throw error; throw error;
} }
attachment_dict[param._attachment] = param._blob; attachment_dict[name] = blob;
}; };
MemoryStorage.prototype.removeAttachment = function (param) { MemoryStorage.prototype.removeAttachment = function (id, name) {
try { try {
delete this._database[param._id].attachments[param._attachment]; delete this._database[id].attachments[name];
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Cannot find document: " + param._id, "Cannot find document: " + id,
404 404
); );
} }
......
...@@ -101,8 +101,13 @@ ...@@ -101,8 +101,13 @@
i; i;
function safeGet(j) { function safeGet(j) {
return substorage.get({"_id": result[j].id}) var id = result[j].id;
.push(undefined, function (error) { return substorage.get(id)
.push(function (doc) {
// XXX Can delete user data!
doc._id = id;
return doc;
}, function (error) {
// Document may have been dropped after listing // Document may have been dropped after listing
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
......
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
UnionStorage.prototype.put = function () { UnionStorage.prototype.put = function () {
var arg = arguments, var arg = arguments,
context = this; context = this;
return this._getWithStorageIndex({"_id": arg[0]._id}) return this._getWithStorageIndex(arg[0])
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
...@@ -124,7 +124,7 @@ ...@@ -124,7 +124,7 @@
UnionStorage.prototype.remove = function () { UnionStorage.prototype.remove = function () {
var arg = arguments, var arg = arguments,
context = this; context = this;
return this._getWithStorageIndex({"_id": arg[0]._id}) return this._getWithStorageIndex(arg[0])
.push(function (result) { .push(function (result) {
// Storage found, remove from it directly // Storage found, remove from it directly
var sub_storage = context._storage_list[result[0]]; var sub_storage = context._storage_list[result[0]];
......
...@@ -23,13 +23,13 @@ ...@@ -23,13 +23,13 @@
).toString(16)).slice(-4); ).toString(16)).slice(-4);
} }
param._id = S4() + S4() + "-" + var id = S4() + S4() + "-" +
S4() + "-" + S4() + "-" +
S4() + "-" + S4() + "-" +
S4() + "-" + S4() + "-" +
S4() + S4() + S4(); S4() + S4() + S4();
return this.put(param); return this.put(id, param);
}; };
UUIDStorage.prototype.put = function () { UUIDStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments); return this._sub_storage.put.apply(this._sub_storage, arguments);
......
...@@ -73,7 +73,7 @@ ...@@ -73,7 +73,7 @@
stop(); stop();
expect(7); expect(7);
this.jio.put({"_id": "/put1/"}) this.jio.put("/put1/", {})
.then(function () { .then(function () {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "MKCOL"); equal(server.requests[0].method, "MKCOL");
...@@ -98,7 +98,7 @@ ...@@ -98,7 +98,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.put({"_id": "put1/"}) this.jio.put("put1/", {})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id put1/ is forbidden (no begin /)"); equal(error.message, "id put1/ is forbidden (no begin /)");
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.put({"_id": "/put1"}) this.jio.put("/put1", {})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /put1 is forbidden (no end /)"); equal(error.message, "id /put1 is forbidden (no end /)");
...@@ -134,7 +134,7 @@ ...@@ -134,7 +134,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.put({"_id": "/put1/", title: "foo"}) this.jio.put("/put1/", {title: "foo"})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Can not store properties: title"); equal(error.message, "Can not store properties: title");
...@@ -180,7 +180,7 @@ ...@@ -180,7 +180,7 @@
stop(); stop();
expect(7); expect(7);
this.jio.remove({"_id": "/remove1/"}) this.jio.remove("/remove1/")
.then(function () { .then(function () {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "DELETE"); equal(server.requests[0].method, "DELETE");
...@@ -205,7 +205,7 @@ ...@@ -205,7 +205,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.remove({"_id": "remove1/"}) this.jio.remove("remove1/")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id remove1/ is forbidden (no begin /)"); equal(error.message, "id remove1/ is forbidden (no begin /)");
...@@ -223,7 +223,7 @@ ...@@ -223,7 +223,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.remove({"_id": "/remove1"}) this.jio.remove("/remove1")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /remove1 is forbidden (no end /)"); equal(error.message, "id /remove1 is forbidden (no end /)");
...@@ -263,7 +263,7 @@ ...@@ -263,7 +263,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.get({"_id": "get1/"}) this.jio.get("get1/")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id get1/ is forbidden (no begin /)"); equal(error.message, "id get1/ is forbidden (no begin /)");
...@@ -281,7 +281,7 @@ ...@@ -281,7 +281,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.get({"_id": "/get1"}) this.jio.get("/get1")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /get1 is forbidden (no end /)"); equal(error.message, "id /get1 is forbidden (no end /)");
...@@ -304,7 +304,7 @@ ...@@ -304,7 +304,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.get({"_id": "/inexistent/"}) this.jio.get("/inexistent/")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document"); equal(error.message, "Cannot find document");
...@@ -355,10 +355,9 @@ ...@@ -355,10 +355,9 @@
stop(); stop();
expect(1); expect(1);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id
}, "Check document"); }, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
...@@ -460,10 +459,9 @@ ...@@ -460,10 +459,9 @@
stop(); stop();
expect(1); expect(1);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id,
"_attachments": { "_attachments": {
attachment1: {}, attachment1: {},
attachment2: {} attachment2: {}
...@@ -504,11 +502,11 @@ ...@@ -504,11 +502,11 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": "putAttachment1/", "putAttachment1/",
"_attachment": "attachment1", "attachment1",
"_blob": new Blob([""]) new Blob([""])
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id putAttachment1/ is forbidden (no begin /)"); equal(error.message, "id putAttachment1/ is forbidden (no begin /)");
...@@ -526,11 +524,11 @@ ...@@ -526,11 +524,11 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": "/putAttachment1", "/putAttachment1",
"_attachment": "attachment1", "attachment1",
"_blob": new Blob([""]) new Blob([""])
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /putAttachment1 is forbidden (no end /)"); equal(error.message, "id /putAttachment1 is forbidden (no end /)");
...@@ -548,11 +546,11 @@ ...@@ -548,11 +546,11 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": "/putAttachment1/", "/putAttachment1/",
"_attachment": "attach/ment1", "attach/ment1",
"_blob": new Blob([""]) new Blob([""])
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "attachment attach/ment1 is forbidden"); equal(error.message, "attachment attach/ment1 is forbidden");
...@@ -577,11 +575,11 @@ ...@@ -577,11 +575,11 @@
stop(); stop();
expect(7); expect(7);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": "/putAttachment1/", "/putAttachment1/",
"_attachment": "attachment1", "attachment1",
"_blob": blob blob
}) )
.then(function () { .then(function () {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "PUT"); equal(server.requests[0].method, "PUT");
...@@ -628,10 +626,10 @@ ...@@ -628,10 +626,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment(
"_id": "removeAttachment1/", "removeAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id removeAttachment1/ is forbidden (no begin /)"); equal(error.message, "id removeAttachment1/ is forbidden (no begin /)");
...@@ -649,10 +647,10 @@ ...@@ -649,10 +647,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment(
"_id": "/removeAttachment1", "/removeAttachment1",
"_attachment": "attachment1" "attachment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /removeAttachment1 is forbidden (no end /)"); equal(error.message, "id /removeAttachment1 is forbidden (no end /)");
...@@ -670,10 +668,10 @@ ...@@ -670,10 +668,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment(
"_id": "/removeAttachment1/", "/removeAttachment1/",
"_attachment": "attach/ment1" "attach/ment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "attachment attach/ment1 is forbidden"); equal(error.message, "attachment attach/ment1 is forbidden");
...@@ -697,10 +695,10 @@ ...@@ -697,10 +695,10 @@
stop(); stop();
expect(7); expect(7);
this.jio.removeAttachment({ this.jio.removeAttachment(
"_id": "/removeAttachment1/", "/removeAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.then(function () { .then(function () {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "DELETE"); equal(server.requests[0].method, "DELETE");
...@@ -730,10 +728,10 @@ ...@@ -730,10 +728,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment(
"_id": "/removeAttachment1/", "/removeAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.then(function () { .then(function () {
ok(false); ok(false);
}) })
...@@ -774,10 +772,10 @@ ...@@ -774,10 +772,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment(
"_id": "getAttachment1/", "getAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id getAttachment1/ is forbidden (no begin /)"); equal(error.message, "id getAttachment1/ is forbidden (no begin /)");
...@@ -795,10 +793,10 @@ ...@@ -795,10 +793,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment(
"_id": "/getAttachment1", "/getAttachment1",
"_attachment": "attachment1" "attachment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /getAttachment1 is forbidden (no end /)"); equal(error.message, "id /getAttachment1 is forbidden (no end /)");
...@@ -816,10 +814,10 @@ ...@@ -816,10 +814,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment(
"_id": "/getAttachment1/", "/getAttachment1/",
"_attachment": "attach/ment1" "attach/ment1"
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "attachment attach/ment1 is forbidden"); equal(error.message, "attachment attach/ment1 is forbidden");
...@@ -843,10 +841,10 @@ ...@@ -843,10 +841,10 @@
stop(); stop();
expect(10); expect(10);
this.jio.getAttachment({ this.jio.getAttachment(
"_id": "/getAttachment1/", "/getAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.then(function (result) { .then(function (result) {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
...@@ -883,10 +881,10 @@ ...@@ -883,10 +881,10 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment(
"_id": "/getAttachment1/", "/getAttachment1/",
"_attachment": "attachment1" "attachment1"
}) )
.then(function () { .then(function () {
ok(false); ok(false);
}) })
......
...@@ -44,26 +44,25 @@ ...@@ -44,26 +44,25 @@
test("document without attachment", function () { test("document without attachment", function () {
stop(); stop();
expect(3); expect(4);
function StorageGetNoAttachment() { function StorageGetNoAttachment() {
return this; return this;
} }
StorageGetNoAttachment.prototype.getAttachment = function (options) { StorageGetNoAttachment.prototype.getAttachment = function (id, name) {
deepEqual(options, {"_id": "foo", equal(id, "foo", "getAttachment bar");
"_attachment": "jio_document/YmFy.json"}, equal(name, "jio_document/YmFy.json", "getAttachment bar");
"getAttachment bar");
return new Blob([JSON.stringify({ return new Blob([JSON.stringify({
title: options._attachment, title: name,
id: "ID " + options._attachment, id: "ID " + name,
"another": "property" "another": "property"
})]); })]);
}; };
StorageGetNoAttachment.prototype.get = function (options) { StorageGetNoAttachment.prototype.get = function (id) {
deepEqual(options, {"_id": "foo"}, "Get foo"); equal(id, "foo", "Get foo");
return { return {
title: options._id, title: id,
id: "ID " + options._id, id: "ID " + id,
"another": "property" "another": "property"
}; };
}; };
...@@ -78,10 +77,9 @@ ...@@ -78,10 +77,9 @@
} }
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
title: "jio_document/YmFy.json", title: "jio_document/YmFy.json",
id: "ID jio_document/YmFy.json", id: "ID jio_document/YmFy.json",
"another": "property" "another": "property"
...@@ -97,26 +95,25 @@ ...@@ -97,26 +95,25 @@
test("document with attachment", function () { test("document with attachment", function () {
stop(); stop();
expect(3); expect(4);
function StorageGetWithAttachment() { function StorageGetWithAttachment() {
return this; return this;
} }
StorageGetWithAttachment.prototype.getAttachment = function (options) { StorageGetWithAttachment.prototype.getAttachment = function (id, name) {
deepEqual(options, {"_id": "foo", equal(id, "foo", "getAttachment bar");
"_attachment": "jio_document/YmFy.json"}, equal(name, "jio_document/YmFy.json", "getAttachment bar");
"getAttachment bar");
return new Blob([JSON.stringify({ return new Blob([JSON.stringify({
title: options._attachment, title: name,
id: "ID " + options._attachment, id: "ID " + name,
"another": "property" "another": "property"
})]); })]);
}; };
StorageGetWithAttachment.prototype.get = function (options) { StorageGetWithAttachment.prototype.get = function (id) {
deepEqual(options, {"_id": "foo"}, "Get foo"); equal(id, "foo", "Get foo");
var result = { var result = {
title: options._id, title: id,
id: "ID " + options._id, id: "ID " + id,
"another": "property", "another": "property",
"_attachments": { "_attachments": {
"foo1": {} "foo1": {}
...@@ -147,10 +144,9 @@ ...@@ -147,10 +144,9 @@
} }
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
title: "jio_document/YmFy.json", title: "jio_document/YmFy.json",
id: "ID jio_document/YmFy.json", id: "ID jio_document/YmFy.json",
"another": "property", "another": "property",
...@@ -173,7 +169,7 @@ ...@@ -173,7 +169,7 @@
module("documentStorage.put"); module("documentStorage.put");
test("put called substorage put", function () { test("put called substorage put", function () {
stop(); stop();
expect(4); expect(5);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
...@@ -182,26 +178,22 @@ ...@@ -182,26 +178,22 @@
type: "documentstorage200" type: "documentstorage200"
} }
}); });
Storage200.prototype.putAttachment = function (param) { Storage200.prototype.putAttachment = function (id, name, blob) {
var blob = param._blob;
delete param._blob;
equal(blob.type, "application/json", "Blob type is OK"); equal(blob.type, "application/json", "Blob type is OK");
deepEqual(param, { equal(id, "foo", "putAttachment 200 called");
"_id": "foo", equal(name, "jio_document/YmFy.json", "putAttachment 200 called");
"_attachment": "jio_document/YmFy.json"
}, "putAttachment 200 called");
return jIO.util.readBlobAsText(blob) return jIO.util.readBlobAsText(blob)
.then(function (result) { .then(function (result) {
deepEqual(JSON.parse(result.target.result), deepEqual(JSON.parse(result.target.result),
{"_id": "bar", "title": "bartitle"}, {"title": "bartitle"},
"JSON is in blob"); "JSON is in blob");
return param._id; return id;
}); });
}; };
jio.put({"_id": "bar", "title": "bartitle"}) jio.put("bar", {"title": "bartitle"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -219,7 +211,7 @@ ...@@ -219,7 +211,7 @@
module("documentStorage.remove"); module("documentStorage.remove");
test("remove called substorage removeAttachment", function () { test("remove called substorage removeAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
...@@ -228,14 +220,13 @@ ...@@ -228,14 +220,13 @@
type: "documentstorage200" type: "documentstorage200"
} }
}); });
Storage200.prototype.removeAttachment = function (param) { Storage200.prototype.removeAttachment = function (id, name) {
deepEqual(param, {"_id": "foo", equal(id, "foo", "removeAttachment 200 called");
"_attachment": "jio_document/YmFy.json"}, equal(name, "jio_document/YmFy.json", "removeAttachment 200 called");
"removeAttachment 200 called"); return id;
return param._id;
}; };
jio.remove({"_id": "bar"}) jio.remove("bar")
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -254,7 +245,7 @@ ...@@ -254,7 +245,7 @@
module("documentStorage.getAttachment"); module("documentStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () { test("getAttachment called substorage getAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
...@@ -265,14 +256,13 @@ ...@@ -265,14 +256,13 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.getAttachment = function (param) { Storage200.prototype.getAttachment = function (id, name) {
deepEqual(param, {"_id": "foo", equal(id, "foo", "getAttachment 200 called");
"_attachment": "jio_attachment/YmFy/YmFyMg=="}, equal(name, "jio_attachment/YmFy/YmFyMg==", "getAttachment 200 called");
"getAttachment 200 called");
return blob; return blob;
}; };
jio.getAttachment({"_id": "bar", "_attachment": "bar2"}) jio.getAttachment("bar", "bar2")
.then(function (result) { .then(function (result) {
equal(result, blob); equal(result, blob);
}) })
...@@ -290,7 +280,7 @@ ...@@ -290,7 +280,7 @@
module("documentStorage.putAttachment"); module("documentStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () { test("putAttachment called substorage putAttachment", function () {
stop(); stop();
expect(2); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
...@@ -301,15 +291,14 @@ ...@@ -301,15 +291,14 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.putAttachment = function (param) { Storage200.prototype.putAttachment = function (id, name, blob2) {
deepEqual(param, {"_id": "foo", equal(id, "foo", "putAttachment 200 called");
"_attachment": "jio_attachment/YmFy/YmFyMg==", equal(name, "jio_attachment/YmFy/YmFyMg==", "putAttachment 200 called");
"_blob": blob}, deepEqual(blob2, blob, "putAttachment 200 called");
"putAttachment 200 called");
return "OK"; return "OK";
}; };
jio.putAttachment({"_id": "bar", "_attachment": "bar2", "_blob": blob}) jio.putAttachment("bar", "bar2", blob)
.then(function (result) { .then(function (result) {
equal(result, "OK"); equal(result, "OK");
}) })
...@@ -327,7 +316,7 @@ ...@@ -327,7 +316,7 @@
module("documentStorage.removeAttachment"); module("documentStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () { test("removeAttachment called substorage removeAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
...@@ -337,14 +326,14 @@ ...@@ -337,14 +326,14 @@
} }
}); });
Storage200.prototype.removeAttachment = function (param) { Storage200.prototype.removeAttachment = function (id, name) {
deepEqual(param, {"_id": "foo", equal(id, "foo", "removeAttachment 200 called");
"_attachment": "jio_attachment/YmFy/YmFyMg=="}, equal(name, "jio_attachment/YmFy/YmFyMg==",
"removeAttachment 200 called"); "removeAttachment 200 called");
return "Removed"; return "Removed";
}; };
jio.removeAttachment({"_id": "bar", "_attachment": "bar2"}) jio.removeAttachment("bar", "bar2")
.then(function (result) { .then(function (result) {
equal(result, "Removed"); equal(result, "Removed");
}) })
...@@ -383,9 +372,9 @@ ...@@ -383,9 +372,9 @@
function StorageAllDocsNoAttachment() { function StorageAllDocsNoAttachment() {
return this; return this;
} }
StorageAllDocsNoAttachment.prototype.get = function (options) { StorageAllDocsNoAttachment.prototype.get = function (id) {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
return {title: options._id, id: "ID " + options._id, return {title: id, id: "ID " + id,
"another": "property"}; "another": "property"};
}; };
...@@ -424,11 +413,11 @@ ...@@ -424,11 +413,11 @@
function StorageAllDocsWithAttachment() { function StorageAllDocsWithAttachment() {
return this; return this;
} }
StorageAllDocsWithAttachment.prototype.get = function (options) { StorageAllDocsWithAttachment.prototype.get = function (id) {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
var result = { var result = {
title: options._id, title: id,
id: "ID " + options._id, id: "ID " + id,
"another": "property", "another": "property",
"_attachments": { "_attachments": {
"foo1": {} "foo1": {}
......
This diff is collapsed.
...@@ -91,12 +91,11 @@ ...@@ -91,12 +91,11 @@
stop(); stop();
expect(10); expect(10);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
portal_type: "Person", portal_type: "Person",
title: "foo", title: "foo",
"_id": id,
"_attachments": { "_attachments": {
links: {}, links: {},
view: {} view: {}
...@@ -149,11 +148,11 @@ ...@@ -149,11 +148,11 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": "putAttachment1/", "putAttachment1/",
"_attachment": "attachment1", "attachment1",
"_blob": new Blob(["foo"]) new Blob(["foo"])
}) )
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Can not store outside ERP5: attachment1"); equal(error.message, "Can not store outside ERP5: attachment1");
...@@ -185,11 +184,11 @@ ...@@ -185,11 +184,11 @@
stop(); stop();
expect(11); expect(11);
this.jio.putAttachment({ this.jio.putAttachment(
"_id": id, id,
"_attachment": submit_url, submit_url,
"_blob": new Blob([JSON.stringify(form_json)]) new Blob([JSON.stringify(form_json)])
}) )
.then(function () { .then(function () {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "POST"); equal(server.requests[0].method, "POST");
...@@ -241,10 +240,7 @@ ...@@ -241,10 +240,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment("getAttachment1/", "attachment1")
"_id": "getAttachment1/",
"_attachment": "attachment1"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "ERP5: not support get attachment: attachment1"); equal(error.message, "ERP5: not support get attachment: attachment1");
...@@ -284,10 +280,7 @@ ...@@ -284,10 +280,7 @@
stop(); stop();
expect(12); expect(12);
this.jio.getAttachment({ this.jio.getAttachment(id, "view")
"_id": id,
"_attachment": "view"
})
.then(function (result) { .then(function (result) {
equal(server.requests.length, 2); equal(server.requests.length, 2);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
...@@ -344,10 +337,7 @@ ...@@ -344,10 +337,7 @@
stop(); stop();
expect(12); expect(12);
this.jio.getAttachment({ this.jio.getAttachment(id, "links")
"_id": id,
"_attachment": "links"
})
.then(function (result) { .then(function (result) {
equal(server.requests.length, 2); equal(server.requests.length, 2);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
...@@ -398,10 +388,7 @@ ...@@ -398,10 +388,7 @@
stop(); stop();
expect(8); expect(8);
this.jio.getAttachment({ this.jio.getAttachment(id, callable_url)
"_id": id,
"_attachment": callable_url
})
.then(function (result) { .then(function (result) {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
......
...@@ -230,8 +230,8 @@ ...@@ -230,8 +230,8 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return RSVP.all([ return RSVP.all([
context.jio.put({"_id": "2", "title": "title2"}), context.jio.put("2", {"title": "title2"}),
context.jio.put({"_id": "1", "title": "title1"}) context.jio.put("1", {"title": "title1"})
]); ]);
}) })
.then(function () { .then(function () {
...@@ -278,7 +278,7 @@ ...@@ -278,7 +278,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
context.spy_open = sinon.spy(indexedDB, "open"); context.spy_open = sinon.spy(indexedDB, "open");
...@@ -294,7 +294,7 @@ ...@@ -294,7 +294,7 @@
context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor"); context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.get({"_id": "foo"}); return context.jio.get("foo");
}) })
.then(function () { .then(function () {
...@@ -374,7 +374,7 @@ ...@@ -374,7 +374,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.get({"_id": "inexistent"}); return context.jio.get("inexistent");
}) })
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
...@@ -397,14 +397,13 @@ ...@@ -397,14 +397,13 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": id, "title": "bar"}); return context.jio.put(id, {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.get({"_id": id}); return context.jio.get(id);
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "/",
"title": "bar" "title": "bar"
}, "Check document"); }, "Check document");
}) })
...@@ -425,18 +424,16 @@ ...@@ -425,18 +424,16 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": id, "title": "bar"}); return context.jio.put(id, {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.putAttachment({"_id": id, "_attachment": attachment, return context.jio.putAttachment(id, attachment, "bar");
"_data": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.get({"_id": id}); return context.jio.get(id);
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id,
"title": "bar", "title": "bar",
"_attachments": { "_attachments": {
"foo": {} "foo": {}
...@@ -483,7 +480,7 @@ ...@@ -483,7 +480,7 @@
context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor"); context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
...@@ -560,7 +557,7 @@ ...@@ -560,7 +557,7 @@
ok(context.spy_put.calledOnce, "put count " + ok(context.spy_put.calledOnce, "put count " +
context.spy_put.callCount); context.spy_put.callCount);
deepEqual(context.spy_put.firstCall.args[0], deepEqual(context.spy_put.firstCall.args[0],
{"_id": "foo", title: "bar"}, {"_id": "foo", doc: {title: "bar"}},
"put first argument"); "put first argument");
ok(!context.spy_index.called, "index count " + ok(!context.spy_index.called, "index count " +
...@@ -608,7 +605,7 @@ ...@@ -608,7 +605,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "inexistent"}); return context.jio.put("inexistent", {});
}) })
.then(function (result) { .then(function (result) {
equal(result, "inexistent"); equal(result, "inexistent");
...@@ -640,7 +637,7 @@ ...@@ -640,7 +637,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
context.spy_open = sinon.spy(indexedDB, "open"); context.spy_open = sinon.spy(indexedDB, "open");
...@@ -657,7 +654,7 @@ ...@@ -657,7 +654,7 @@
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete"); context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.remove({"_id": "foo"}); return context.jio.remove("foo");
}) })
.then(function () { .then(function () {
...@@ -749,16 +746,12 @@ ...@@ -749,16 +746,12 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
return RSVP.all([ return RSVP.all([
context.jio.putAttachment({"_id": "foo", context.jio.putAttachment("foo", "attachment1", "bar"),
"_attachment": "attachment1", context.jio.putAttachment("foo", "attachment2", "bar2")
"_data": "bar"}),
context.jio.putAttachment({"_id": "foo",
"_attachment": "attachment2",
"_data": "bar2"})
]); ]);
}) })
.then(function () { .then(function () {
...@@ -776,7 +769,7 @@ ...@@ -776,7 +769,7 @@
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete"); context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.remove({"_id": "foo"}); return context.jio.remove("foo");
}) })
.then(function () { .then(function () {
...@@ -882,12 +875,10 @@ ...@@ -882,12 +875,10 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.putAttachment({"_id": "foo", return context.jio.putAttachment("foo", attachment, big_string);
"_attachment": attachment,
"_data": big_string});
}) })
.then(function () { .then(function () {
context.spy_open = sinon.spy(indexedDB, "open"); context.spy_open = sinon.spy(indexedDB, "open");
...@@ -901,8 +892,7 @@ ...@@ -901,8 +892,7 @@
context.spy_create_index = sinon.spy(IDBObjectStore.prototype, context.spy_create_index = sinon.spy(IDBObjectStore.prototype,
"createIndex"); "createIndex");
return context.jio.getAttachment({"_id": "foo", return context.jio.getAttachment("foo", attachment);
"_attachment": attachment});
}) })
.then(function () { .then(function () {
...@@ -974,16 +964,13 @@ ...@@ -974,16 +964,13 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.putAttachment({"_id": "foo", return context.jio.putAttachment("foo", attachment, big_string);
"_attachment": attachment,
"_data": big_string});
}) })
.then(function () { .then(function () {
return context.jio.getAttachment({"_id": "foo", return context.jio.getAttachment("foo", attachment);
"_attachment": attachment});
}) })
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
...@@ -1009,17 +996,14 @@ ...@@ -1009,17 +996,14 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.putAttachment({"_id": "foo", return context.jio.putAttachment("foo", attachment, big_string);
"_attachment": attachment,
"_data": big_string});
}) })
.then(function () { .then(function () {
return context.jio.getAttachment({"_id": "foo", return context.jio.getAttachment("foo", attachment,
"_attachment": attachment, {"start": 1999995, "end": 2000005});
"_start": 1999995, "_end": 2000005});
}) })
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
...@@ -1057,12 +1041,10 @@ ...@@ -1057,12 +1041,10 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.putAttachment({"_id": "foo", return context.jio.putAttachment("foo", attachment, big_string);
"_attachment": attachment,
"_data": big_string});
}) })
.then(function () { .then(function () {
context.spy_open = sinon.spy(indexedDB, "open"); context.spy_open = sinon.spy(indexedDB, "open");
...@@ -1080,8 +1062,7 @@ ...@@ -1080,8 +1062,7 @@
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete"); context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.removeAttachment({"_id": "foo", return context.jio.removeAttachment("foo", attachment);
"_attachment": attachment});
}) })
.then(function () { .then(function () {
...@@ -1179,7 +1160,7 @@ ...@@ -1179,7 +1160,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.put({"_id": "foo", "title": "bar"}); return context.jio.put("foo", {"title": "bar"});
}) })
.then(function () { .then(function () {
context.spy_open = sinon.spy(indexedDB, "open"); context.spy_open = sinon.spy(indexedDB, "open");
...@@ -1197,9 +1178,7 @@ ...@@ -1197,9 +1178,7 @@
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete"); context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only"); context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.putAttachment({"_id": "foo", return context.jio.putAttachment("foo", attachment, big_string);
"_attachment": attachment,
"_data": big_string});
}) })
.then(function () { .then(function () {
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.get({"_id": "inexistent"}) this.jio.get("inexistent")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id inexistent is forbidden (!== /)"); equal(error.message, "id inexistent is forbidden (!== /)");
...@@ -71,11 +71,9 @@ ...@@ -71,11 +71,9 @@
stop(); stop();
expect(1); expect(1);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {}, "Check document");
"_id": "/"
}, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -93,10 +91,9 @@ ...@@ -93,10 +91,9 @@
localStorage.setItem(attachment, "bar"); localStorage.setItem(attachment, "bar");
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id,
"_attachments": { "_attachments": {
"foo": {} "foo": {}
} }
...@@ -126,10 +123,7 @@ ...@@ -126,10 +123,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment("inexistent", "a")
"_id": "inexistent",
"_attachment": "a"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id inexistent is forbidden (!== /)"); equal(error.message, "id inexistent is forbidden (!== /)");
...@@ -148,10 +142,7 @@ ...@@ -148,10 +142,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment(id, "inexistent")
"_id": id,
"_attachment": "inexistent"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment inexistent"); equal(error.message, "Cannot find attachment inexistent");
...@@ -175,10 +166,7 @@ ...@@ -175,10 +166,7 @@
localStorage.setItem(attachment, "data:text/plain;charset=utf-8;base64," + localStorage.setItem(attachment, "data:text/plain;charset=utf-8;base64," +
btoa(unescape(encodeURIComponent(value)))); btoa(unescape(encodeURIComponent(value))));
this.jio.getAttachment({ this.jio.getAttachment(id, attachment)
"_id": id,
"_attachment": attachment
})
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/plain;charset=utf-8", deepEqual(result.type, "text/plain;charset=utf-8",
...@@ -217,10 +205,7 @@ ...@@ -217,10 +205,7 @@
data_url = imgCanvas.toDataURL("image/png"); data_url = imgCanvas.toDataURL("image/png");
localStorage.setItem(attachment, data_url); localStorage.setItem(attachment, data_url);
return context.jio.getAttachment({ return context.jio.getAttachment(id, attachment)
"_id": id,
"_attachment": attachment
})
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
return jIO.util.readBlobAsDataURL(result); return jIO.util.readBlobAsDataURL(result);
...@@ -252,11 +237,7 @@ ...@@ -252,11 +237,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment("inexistent", "putattmt2", "")
"_id": "inexistent",
"_attachment": "putattmt2",
"_data": ""
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "id inexistent is forbidden (!== /)"); equal(error.message, "id inexistent is forbidden (!== /)");
...@@ -278,11 +259,7 @@ ...@@ -278,11 +259,7 @@
expect(1); expect(1);
this.jio.putAttachment({ this.jio.putAttachment(id, attachment, value)
"_id": id,
"_attachment": attachment,
"_data": value
})
.then(function () { .then(function () {
equal( equal(
localStorage.getItem(attachment), localStorage.getItem(attachment),
...@@ -336,11 +313,7 @@ ...@@ -336,11 +313,7 @@
} }
imgCanvas.toBlob(function (blob) { imgCanvas.toBlob(function (blob) {
return context.jio.putAttachment({ return context.jio.putAttachment(id, attachment, blob)
"_id": id,
"_attachment": attachment,
"_blob": blob
})
.then(function () { .then(function () {
equal(localStorage.getItem(attachment), data_url); equal(localStorage.getItem(attachment), data_url);
}) })
...@@ -368,10 +341,7 @@ ...@@ -368,10 +341,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment("inexistent", "removeattmt2")
"_id": "inexistent",
"_attachment": "removeattmt2"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError, error); ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "id inexistent is forbidden (!== /)"); equal(error.message, "id inexistent is forbidden (!== /)");
...@@ -394,10 +364,7 @@ ...@@ -394,10 +364,7 @@
stop(); stop();
expect(1); expect(1);
this.jio.removeAttachment({ this.jio.removeAttachment(id, attachment)
"_id": id,
"_attachment": attachment
})
.then(function () { .then(function () {
ok(!localStorage.hasOwnProperty(attachment)); ok(!localStorage.hasOwnProperty(attachment));
}) })
......
...@@ -53,13 +53,12 @@ ...@@ -53,13 +53,12 @@
var test = this; var test = this;
this.jio.put({"_id": "put1", "title": "myPut1"}) this.jio.put("put1", {"title": "myPut1"})
.then(function (uuid) { .then(function (uuid) {
equal(uuid, "put1"); equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
attachments: {}, attachments: {},
doc: JSON.stringify({ doc: JSON.stringify({
"_id": "put1",
"title": "myPut1" "title": "myPut1"
}) })
}); });
...@@ -83,14 +82,13 @@ ...@@ -83,14 +82,13 @@
expect(2); expect(2);
stop(); stop();
this.jio.put({"_id": id, "title": "myPut2"}) this.jio.put(id, {"title": "myPut2"})
.then(function (uuid) { .then(function (uuid) {
equal(uuid, "put1"); equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
"foo": "bar", "foo": "bar",
"attachments": {"foo": "bar"}, "attachments": {"foo": "bar"},
doc: JSON.stringify({ doc: JSON.stringify({
"_id": "put1",
"title": "myPut2" "title": "myPut2"
}) })
}); });
...@@ -118,7 +116,7 @@ ...@@ -118,7 +116,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.get({"_id": "inexistent"}) this.jio.get("inexistent")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError, error); ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent"); equal(error.message, "Cannot find document: inexistent");
...@@ -140,10 +138,9 @@ ...@@ -140,10 +138,9 @@
stop(); stop();
expect(1); expect(1);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id,
"title": "myPost1" "title": "myPost1"
}, "Check document"); }, "Check document");
}) })
...@@ -168,10 +165,9 @@ ...@@ -168,10 +165,9 @@
stop(); stop();
expect(1); expect(1);
this.jio.get({"_id": id}) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": id,
"_attachments": {putattmt2: {}} "_attachments": {putattmt2: {}}
}, "Check document"); }, "Check document");
}) })
...@@ -204,7 +200,7 @@ ...@@ -204,7 +200,7 @@
stop(); stop();
expect(1); expect(1);
this.jio.remove({"_id": "foo"}) this.jio.remove("foo")
.then(function (result) { .then(function (result) {
equal(result, "foo"); equal(result, "foo");
}) })
...@@ -230,10 +226,7 @@ ...@@ -230,10 +226,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.getAttachment({ this.jio.getAttachment("inexistent", "a")
"_id": "inexistent",
"_attachment": "a"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment: inexistent , a"); equal(error.message, "Cannot find attachment: inexistent , a");
...@@ -256,10 +249,7 @@ ...@@ -256,10 +249,7 @@
"doc": JSON.stringify({}) "doc": JSON.stringify({})
}; };
this.jio.getAttachment({ this.jio.getAttachment(id, "inexistent")
"_id": id,
"_attachment": "inexistent"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment: b , inexistent"); equal(error.message, "Cannot find attachment: b , inexistent");
...@@ -284,10 +274,7 @@ ...@@ -284,10 +274,7 @@
attachments: {"foo": "bar"} attachments: {"foo": "bar"}
}; };
this.jio.getAttachment({ this.jio.getAttachment(id, "inexistent")
"_id": id,
"_attachment": "inexistent"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment: b , inexistent"); equal(error.message, "Cannot find attachment: b , inexistent");
...@@ -315,10 +302,7 @@ ...@@ -315,10 +302,7 @@
} }
}; };
this.jio.getAttachment({ this.jio.getAttachment(id, attachment)
"_id": id,
"_attachment": attachment
})
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
equal(result, blob); equal(result, blob);
...@@ -346,11 +330,7 @@ ...@@ -346,11 +330,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.putAttachment({ this.jio.putAttachment("inexistent", "putattmt2", "")
"_id": "inexistent",
"_attachment": "putattmt2",
"_data": ""
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError, error); ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent"); equal(error.message, "Cannot find document: inexistent");
...@@ -377,11 +357,7 @@ ...@@ -377,11 +357,7 @@
stop(); stop();
expect(1); expect(1);
jio.putAttachment({ jio.putAttachment(id, "putattmt2", blob)
"_id": id,
"_attachment": "putattmt2",
"_blob": blob
})
.then(function () { .then(function () {
equal(jio.__storage._database[id].attachments.putattmt2, blob); equal(jio.__storage._database[id].attachments.putattmt2, blob);
}) })
...@@ -408,10 +384,7 @@ ...@@ -408,10 +384,7 @@
stop(); stop();
expect(3); expect(3);
this.jio.removeAttachment({ this.jio.removeAttachment("inexistent", "removeattmt2")
"_id": "inexistent",
"_attachment": "removeattmt2"
})
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError, error); ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent"); equal(error.message, "Cannot find document: inexistent");
...@@ -437,10 +410,7 @@ ...@@ -437,10 +410,7 @@
stop(); stop();
expect(1); expect(1);
jio.removeAttachment({ jio.removeAttachment(id, "removeattmt2")
"_id": id,
"_attachment": "removeattmt2"
})
.then(function () { .then(function () {
deepEqual(jio.__storage._database[id].attachments, {}); deepEqual(jio.__storage._database[id].attachments, {});
}) })
......
...@@ -52,15 +52,14 @@ ...@@ -52,15 +52,14 @@
} }
}); });
Storage200.prototype.get = function (param) { Storage200.prototype.get = function (id) {
equal(param._id, "bar", "get 200 called"); equal(id, "bar", "get 200 called");
return {title: "foo"}; return {title: "foo"};
}; };
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
"title": "foo" "title": "foo"
}, "Check document"); }, "Check document");
}) })
...@@ -110,7 +109,7 @@ ...@@ -110,7 +109,7 @@
module("queryStorage.put"); module("queryStorage.put");
test("put called substorage put", function () { test("put called substorage put", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "query", type: "query",
...@@ -118,12 +117,13 @@ ...@@ -118,12 +117,13 @@
type: "querystorage200" type: "querystorage200"
} }
}); });
Storage200.prototype.put = function (param) { Storage200.prototype.put = function (id, param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called"); equal(id, "bar", "put 200 called");
return param._id; deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
}; };
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -149,12 +149,12 @@ ...@@ -149,12 +149,12 @@
type: "querystorage200" type: "querystorage200"
} }
}); });
Storage200.prototype.remove = function (param) { Storage200.prototype.remove = function (id) {
deepEqual(param, {"_id": "bar"}, "remove 200 called"); deepEqual(id, "bar", "remove 200 called");
return param._id; return id;
}; };
jio.remove({"_id": "bar"}) jio.remove("bar")
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -172,7 +172,7 @@ ...@@ -172,7 +172,7 @@
module("queryStorage.getAttachment"); module("queryStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () { test("getAttachment called substorage getAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "query", type: "query",
...@@ -182,13 +182,13 @@ ...@@ -182,13 +182,13 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.getAttachment = function (param) { Storage200.prototype.getAttachment = function (id, name) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"}, equal(id, "bar", "getAttachment 200 called");
"getAttachment 200 called"); equal(name, "foo", "getAttachment 200 called");
return blob; return blob;
}; };
jio.getAttachment({"_id": "bar", "_attachment": "foo"}) jio.getAttachment("bar", "foo")
.then(function (result) { .then(function (result) {
equal(result, blob); equal(result, blob);
}) })
...@@ -206,7 +206,7 @@ ...@@ -206,7 +206,7 @@
module("queryStorage.putAttachment"); module("queryStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () { test("putAttachment called substorage putAttachment", function () {
stop(); stop();
expect(2); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "query", type: "query",
...@@ -216,13 +216,15 @@ ...@@ -216,13 +216,15 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.putAttachment = function (param) { Storage200.prototype.putAttachment = function (id, name, blob2) {
deepEqual(param, {"_id": "bar", "_attachment": "foo", "_blob": blob}, equal(id, "bar", "putAttachment 200 called");
equal(name, "foo", "putAttachment 200 called");
deepEqual(blob2, blob,
"putAttachment 200 called"); "putAttachment 200 called");
return "OK"; return "OK";
}; };
jio.putAttachment({"_id": "bar", "_attachment": "foo", "_blob": blob}) jio.putAttachment("bar", "foo", blob)
.then(function (result) { .then(function (result) {
equal(result, "OK"); equal(result, "OK");
}) })
...@@ -240,7 +242,7 @@ ...@@ -240,7 +242,7 @@
module("queryStorage.removeAttachment"); module("queryStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () { test("removeAttachment called substorage removeAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "query", type: "query",
...@@ -249,13 +251,13 @@ ...@@ -249,13 +251,13 @@
} }
}); });
Storage200.prototype.removeAttachment = function (param) { Storage200.prototype.removeAttachment = function (id, name) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"}, equal(id, "bar", "removeAttachment 200 called");
"removeAttachment 200 called"); equal(name, "foo", "removeAttachment 200 called");
return "Removed"; return "Removed";
}; };
jio.removeAttachment({"_id": "bar", "_attachment": "foo"}) jio.removeAttachment("bar", "foo")
.then(function (result) { .then(function (result) {
equal(result, "Removed"); equal(result, "Removed");
}) })
...@@ -408,13 +410,13 @@ ...@@ -408,13 +410,13 @@
function StorageNoSortCapacity() { function StorageNoSortCapacity() {
return this; return this;
} }
StorageNoSortCapacity.prototype.get = function (options) { StorageNoSortCapacity.prototype.get = function (id) {
if (options._id === "foo") { if (id === "foo") {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
} else { } else {
equal(options._id, "bar", "Get bar"); equal(id, "bar", "Get bar");
} }
return {title: options._id, id: "ID " + options._id, return {title: id, id: "ID " + id,
"another": "property"}; "another": "property"};
}; };
StorageNoSortCapacity.prototype.hasCapacity = function (capacity) { StorageNoSortCapacity.prototype.hasCapacity = function (capacity) {
...@@ -484,13 +486,13 @@ ...@@ -484,13 +486,13 @@
function StorageNoSelectCapacity() { function StorageNoSelectCapacity() {
return this; return this;
} }
StorageNoSelectCapacity.prototype.get = function (options) { StorageNoSelectCapacity.prototype.get = function (id) {
if (options._id === "foo") { if (id === "foo") {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
} else { } else {
equal(options._id, "bar", "Get bar"); equal(id, "bar", "Get bar");
} }
return {title: options._id, id: "ID " + options._id, return {title: id, id: "ID " + id,
"another": "property"}; "another": "property"};
}; };
StorageNoSelectCapacity.prototype.hasCapacity = function (capacity) { StorageNoSelectCapacity.prototype.hasCapacity = function (capacity) {
...@@ -560,13 +562,13 @@ ...@@ -560,13 +562,13 @@
function StorageNoLimitCapacity() { function StorageNoLimitCapacity() {
return this; return this;
} }
StorageNoLimitCapacity.prototype.get = function (options) { StorageNoLimitCapacity.prototype.get = function (id) {
if (options._id === "foo") { if (id === "foo") {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
} else { } else {
equal(options._id, "bar", "Get bar"); equal(id, "bar", "Get bar");
} }
return {title: options._id, id: "ID " + options._id, return {title: id, id: "ID " + id,
"another": "property"}; "another": "property"};
}; };
StorageNoLimitCapacity.prototype.hasCapacity = function (capacity) { StorageNoLimitCapacity.prototype.hasCapacity = function (capacity) {
...@@ -636,13 +638,13 @@ ...@@ -636,13 +638,13 @@
function StorageNoQueryCapacity() { function StorageNoQueryCapacity() {
return this; return this;
} }
StorageNoQueryCapacity.prototype.get = function (options) { StorageNoQueryCapacity.prototype.get = function (id) {
if (options._id === "foo") { if (id === "foo") {
equal(options._id, "foo", "Get foo"); equal(id, "foo", "Get foo");
} else { } else {
equal(options._id, "bar", "Get bar"); equal(id, "bar", "Get bar");
} }
return {title: options._id, id: "ID " + options._id, return {title: id, id: "ID " + id,
"another": "property"}; "another": "property"};
}; };
StorageNoQueryCapacity.prototype.hasCapacity = function (capacity) { StorageNoQueryCapacity.prototype.hasCapacity = function (capacity) {
......
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
function Storage404() { function Storage404() {
return this; return this;
} }
function generate404Error(param) { function generate404Error(id) {
equal(param._id, "bar", "get 404 called"); equal(id, "bar", "get 404 called");
throw new jIO.util.jIOError("Cannot find document", 404); throw new jIO.util.jIOError("Cannot find document", 404);
} }
Storage404.prototype.get = generate404Error; Storage404.prototype.get = generate404Error;
...@@ -27,17 +27,18 @@ ...@@ -27,17 +27,18 @@
function Storage200() { function Storage200() {
return this; return this;
} }
Storage200.prototype.get = function (param) { Storage200.prototype.get = function (id) {
equal(param._id, "bar", "get 200 called"); equal(id, "bar", "get 200 called");
return {title: "foo"}; return {title: "foo"};
}; };
Storage200.prototype.put = function (param) { Storage200.prototype.put = function (id, param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called"); equal(id, "bar", "put 200 called");
return param._id; deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
}; };
Storage200.prototype.remove = function (param) { Storage200.prototype.remove = function (id) {
deepEqual(param, {"_id": "bar"}, "remove 200 called"); equal(id, "bar", "remove 200 called");
return param._id; return id;
}; };
Storage200.prototype.post = function (param) { Storage200.prototype.post = function (param) {
deepEqual(param, {"title": "foo"}, "post 200 called"); deepEqual(param, {"title": "foo"}, "post 200 called");
...@@ -134,7 +135,7 @@ ...@@ -134,7 +135,7 @@
}] }]
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document"); equal(error.message, "Cannot find document");
...@@ -161,10 +162,9 @@ ...@@ -161,10 +162,9 @@
}] }]
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
"title": "foo" "title": "foo"
}, "Check document"); }, "Check document");
}) })
...@@ -189,10 +189,9 @@ ...@@ -189,10 +189,9 @@
}] }]
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
"title": "foo" "title": "foo"
}, "Check document"); }, "Check document");
}) })
...@@ -217,7 +216,7 @@ ...@@ -217,7 +216,7 @@
}] }]
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.fail(function (error) { .fail(function (error) {
ok(error instanceof Error); ok(error instanceof Error);
equal(error.message, "manually triggered error"); equal(error.message, "manually triggered error");
...@@ -244,7 +243,7 @@ ...@@ -244,7 +243,7 @@
}] }]
}); });
jio.get({"_id": "bar"}) jio.get("bar")
.fail(function (error) { .fail(function (error) {
ok(error instanceof Error); ok(error instanceof Error);
equal(error.message, "manually triggered error"); equal(error.message, "manually triggered error");
...@@ -275,7 +274,7 @@ ...@@ -275,7 +274,7 @@
}] }]
}); });
jio.post({"_id": "bar", "title": "foo"}) jio.post({"title": "foo"})
.fail(function (error) { .fail(function (error) {
ok(error instanceof Error); ok(error instanceof Error);
equal(error.message, "manually triggered error"); equal(error.message, "manually triggered error");
...@@ -331,7 +330,7 @@ ...@@ -331,7 +330,7 @@
}] }]
}); });
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.fail(function (error) { .fail(function (error) {
ok(error instanceof Error); ok(error instanceof Error);
equal(error.message, "manually triggered error"); equal(error.message, "manually triggered error");
...@@ -347,7 +346,7 @@ ...@@ -347,7 +346,7 @@
test("put on first storage", function () { test("put on first storage", function () {
stop(); stop();
expect(3); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "union", type: "union",
...@@ -358,7 +357,7 @@ ...@@ -358,7 +357,7 @@
}] }]
}); });
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -372,7 +371,7 @@ ...@@ -372,7 +371,7 @@
test("put on second storage", function () { test("put on second storage", function () {
stop(); stop();
expect(4); expect(5);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "union", type: "union",
...@@ -383,7 +382,7 @@ ...@@ -383,7 +382,7 @@
}] }]
}); });
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -397,19 +396,20 @@ ...@@ -397,19 +396,20 @@
test("put create on first storage", function () { test("put create on first storage", function () {
stop(); stop();
expect(4); expect(5);
function StoragePut404() { function StoragePut404() {
return this; return this;
} }
function generatePut404Error(param) { function generatePut404Error(id) {
equal(param._id, "bar", "get Put404 called"); equal(id, "bar", "get Put404 called");
throw new jIO.util.jIOError("Cannot find document", 404); throw new jIO.util.jIOError("Cannot find document", 404);
} }
StoragePut404.prototype.get = generatePut404Error; StoragePut404.prototype.get = generatePut404Error;
StoragePut404.prototype.put = function (param) { StoragePut404.prototype.put = function (id, param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 404 called"); equal(id, "bar", "put 404 called");
return param._id; deepEqual(param, {"title": "foo"}, "put 404 called");
return id;
}; };
jIO.addStorage('unionstorageput404', StoragePut404); jIO.addStorage('unionstorageput404', StoragePut404);
...@@ -422,7 +422,7 @@ ...@@ -422,7 +422,7 @@
}] }]
}); });
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -451,7 +451,7 @@ ...@@ -451,7 +451,7 @@
}] }]
}); });
jio.remove({"_id": "bar"}) jio.remove("bar")
.fail(function (error) { .fail(function (error) {
ok(error instanceof Error); ok(error instanceof Error);
equal(error.message, "manually triggered error"); equal(error.message, "manually triggered error");
...@@ -478,7 +478,7 @@ ...@@ -478,7 +478,7 @@
}] }]
}); });
jio.remove({"_id": "bar"}) jio.remove("bar")
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -503,7 +503,7 @@ ...@@ -503,7 +503,7 @@
}] }]
}); });
jio.remove({"_id": "bar"}) jio.remove("bar")
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
......
...@@ -53,14 +53,13 @@ ...@@ -53,14 +53,13 @@
}); });
Storage200.prototype.get = function (param) { Storage200.prototype.get = function (param) {
equal(param._id, "bar", "get 200 called"); equal(param, "bar", "get 200 called");
return {title: "foo"}; return {title: "foo"};
}; };
jio.get({"_id": "bar"}) jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_id": "bar",
"title": "foo" "title": "foo"
}, "Check document"); }, "Check document");
}) })
...@@ -99,9 +98,9 @@ ...@@ -99,9 +98,9 @@
) === null ? false : true); ) === null ? false : true);
} }
Storage200.prototype.put = function (param) { Storage200.prototype.put = function (id, param) {
uuid = param._id; uuid = id;
deepEqual(param, {"_id": uuid, "title": "foo"}, "post 200 called"); deepEqual(param, {"title": "foo"}, "post 200 called");
return "bar"; return "bar";
}; };
...@@ -124,7 +123,7 @@ ...@@ -124,7 +123,7 @@
module("uuidStorage.put"); module("uuidStorage.put");
test("put called substorage put", function () { test("put called substorage put", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "uuid", type: "uuid",
...@@ -132,12 +131,13 @@ ...@@ -132,12 +131,13 @@
type: "uuidstorage200" type: "uuidstorage200"
} }
}); });
Storage200.prototype.put = function (param) { Storage200.prototype.put = function (id, param) {
deepEqual(param, {"_id": "bar", "title": "foo"}, "put 200 called"); equal(id, "bar", "put 200 called");
return param._id; deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
}; };
jio.put({"_id": "bar", "title": "foo"}) jio.put("bar", {"title": "foo"})
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -164,11 +164,11 @@ ...@@ -164,11 +164,11 @@
} }
}); });
Storage200.prototype.remove = function (param) { Storage200.prototype.remove = function (param) {
deepEqual(param, {"_id": "bar"}, "remove 200 called"); equal(param, "bar", "remove 200 called");
return param._id; return param._id;
}; };
jio.remove({"_id": "bar"}) jio.remove("bar")
.then(function (result) { .then(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
...@@ -186,7 +186,7 @@ ...@@ -186,7 +186,7 @@
module("uuidStorage.getAttachment"); module("uuidStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () { test("getAttachment called substorage getAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "uuid", type: "uuid",
...@@ -196,13 +196,13 @@ ...@@ -196,13 +196,13 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.getAttachment = function (param) { Storage200.prototype.getAttachment = function (id, name) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"}, equal(id, "bar", "getAttachment 200 called");
"getAttachment 200 called"); equal(name, "foo", "getAttachment 200 called");
return blob; return blob;
}; };
jio.getAttachment({"_id": "bar", "_attachment": "foo"}) jio.getAttachment("bar", "foo")
.then(function (result) { .then(function (result) {
equal(result, blob); equal(result, blob);
}) })
...@@ -220,7 +220,7 @@ ...@@ -220,7 +220,7 @@
module("uuidStorage.putAttachment"); module("uuidStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () { test("putAttachment called substorage putAttachment", function () {
stop(); stop();
expect(2); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "uuid", type: "uuid",
...@@ -230,13 +230,14 @@ ...@@ -230,13 +230,14 @@
}), }),
blob = new Blob([""]); blob = new Blob([""]);
Storage200.prototype.putAttachment = function (param) { Storage200.prototype.putAttachment = function (id, name, blob2) {
deepEqual(param, {"_id": "bar", "_attachment": "foo", "_blob": blob}, equal(id, "bar", "putAttachment 200 called");
"putAttachment 200 called"); equal(name, "foo", "putAttachment 200 called");
deepEqual(blob2, blob, "putAttachment 200 called");
return "OK"; return "OK";
}; };
jio.putAttachment({"_id": "bar", "_attachment": "foo", "_blob": blob}) jio.putAttachment("bar", "foo", blob)
.then(function (result) { .then(function (result) {
equal(result, "OK"); equal(result, "OK");
}) })
...@@ -254,7 +255,7 @@ ...@@ -254,7 +255,7 @@
module("uuidStorage.removeAttachment"); module("uuidStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () { test("removeAttachment called substorage removeAttachment", function () {
stop(); stop();
expect(2); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "uuid", type: "uuid",
...@@ -263,13 +264,13 @@ ...@@ -263,13 +264,13 @@
} }
}); });
Storage200.prototype.removeAttachment = function (param) { Storage200.prototype.removeAttachment = function (id, name) {
deepEqual(param, {"_id": "bar", "_attachment": "foo"}, equal(id, "bar", "removeAttachment 200 called");
"removeAttachment 200 called"); equal(name, "foo", "removeAttachment 200 called");
return "Removed"; return "Removed";
}; };
jio.removeAttachment({"_id": "bar", "_attachment": "foo"}) jio.removeAttachment("bar", "foo")
.then(function (result) { .then(function (result) {
equal(result, "Removed"); equal(result, "Removed");
}) })
......
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