Commit 7a527b42 authored by Romain Courteaud's avatar Romain Courteaud

Add jIO.allAttachments method.

Do not return the list of attachments in jIO.get.
Use a specific "allAttachments" method to fetch this information.

jIO.get now really returns the document as saved by jIO.put/post.
parent 0370fa97
......@@ -390,6 +390,8 @@
});
};
declareMethod(JioProxyStorage, "allAttachments", checkId);
/////////////////////////////////////////////////////////////////
// Storage builder
/////////////////////////////////////////////////////////////////
......
......@@ -83,9 +83,8 @@
DavStorage.prototype.put = function (id, param) {
id = restrictDocumentId(id);
delete param._id;
if (Object.getOwnPropertyNames(param).length > 0) {
// Reject if param has other properties than _id
// Reject if param has some properties
throw new jIO.util.jIOError("Can not store properties: " +
Object.getOwnPropertyNames(param), 400);
}
......@@ -104,6 +103,33 @@
};
DavStorage.prototype.get = function (id) {
var context = this;
id = restrictDocumentId(id);
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "PROPFIND",
url: context._url + id,
dataType: "text",
headers: {
// Increasing this value is a performance killer
Depth: "1"
}
});
})
.push(function () {
return {};
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
};
DavStorage.prototype.allAttachments = function (id) {
var context = this;
id = restrictDocumentId(id);
......@@ -126,7 +152,6 @@
// Extract all meta informations and return them to JSON
var i,
result = {},
attachment = {},
id,
attachment_list = new DOMParser().parseFromString(
......@@ -146,10 +171,7 @@
attachment[id] = {};
}
}
if (Object.getOwnPropertyNames(attachment).length > 0) {
result._attachments = attachment;
}
return result;
return attachment;
}, function (error) {
if ((error.target !== undefined) &&
......
......@@ -27,9 +27,6 @@
}
DocumentStorage.prototype.get = function (id) {
var result,
context = this;
return this._sub_storage.getAttachment(
this._document_id,
getSubAttachmentIdFromParam(id)
......@@ -39,17 +36,17 @@
})
.push(function (text) {
return JSON.parse(text.target.result);
})
.push(function (json) {
result = json;
return context._sub_storage.get(context._document_id);
})
.push(function (document) {
});
};
DocumentStorage.prototype.allAttachments = function (id) {
return this._sub_storage.allAttachments(this._document_id)
.push(function (result) {
var attachments = {},
exec,
key;
for (key in document._attachments) {
if (document._attachments.hasOwnProperty(key)) {
for (key in result) {
if (result.hasOwnProperty(key)) {
if (ATTACHMENT_REGEXP.test(key)) {
exec = ATTACHMENT_REGEXP.exec(key);
try {
......@@ -65,10 +62,7 @@
}
}
}
if (Object.getOwnPropertyNames(attachments).length > 0) {
result._attachments = attachments;
}
return result;
return attachments;
});
};
......@@ -99,12 +93,12 @@
};
DocumentStorage.prototype.buildQuery = function () {
return this._sub_storage.get(this._document_id)
.push(function (document) {
return this._sub_storage.allAttachments(this._document_id)
.push(function (attachment_dict) {
var result = [],
key;
for (key in document._attachments) {
if (document._attachments.hasOwnProperty(key)) {
for (key in attachment_dict) {
if (attachment_dict.hasOwnProperty(key)) {
if (DOCUMENT_REGEXP.test(key)) {
try {
result.push({
......
......@@ -19,9 +19,7 @@
ROOT = "/";
FileSystemBridgeStorage.prototype.get = function (id) {
var context = this,
json_document,
explicit_document = false;
var context = this;
return new RSVP.Queue()
// First, try to get explicit reference to the document
......@@ -39,38 +37,50 @@
return jIO.util.readBlobAsText(blob);
})
.push(function (text) {
explicit_document = true;
return JSON.parse(text.target.result);
});
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return {};
// Second, try to get default attachment
return context._sub_storage.allAttachments(ROOT)
.push(function (attachment_dict) {
if (attachment_dict.hasOwnProperty(id)) {
return {};
}
throw new jIO.util.jIOError("Cannot find document " + id,
404);
});
}
throw error;
})
// Second, try to get default attachment
.push(function (result) {
json_document = result;
return context._sub_storage.get(ROOT);
})
});
};
.push(function (directory_document) {
if ((directory_document.hasOwnProperty("_attachments")) &&
(directory_document._attachments.hasOwnProperty(id))) {
json_document._attachments = {
FileSystemBridgeStorage.prototype.allAttachments = function (id) {
var context = this;
return context._sub_storage.allAttachments(ROOT)
.push(function (attachment_dict) {
if (attachment_dict.hasOwnProperty(id)) {
return {
enclosure: {}
};
} else {
if (!explicit_document) {
throw new jIO.util.jIOError("Cannot find document " + id,
404);
}
}
return json_document;
// Second get the document itself if it exists
return context._sub_storage.getAttachment(
DOCUMENT_KEY,
id + DOCUMENT_EXTENSION
)
.push(function () {
return {};
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
throw new jIO.util.jIOError("Cannot find document " + id,
404);
}
throw error;
});
});
};
......@@ -157,12 +167,12 @@
// First, get list of explicit documents
.push(function () {
return context._sub_storage.get(DOCUMENT_KEY);
return context._sub_storage.allAttachments(DOCUMENT_KEY);
})
.push(function (result) {
var key;
for (key in result._attachments) {
if (result._attachments.hasOwnProperty(key)) {
for (key in result) {
if (result.hasOwnProperty(key)) {
if (DOCUMENT_REGEXP.test(key)) {
result_dict[DOCUMENT_REGEXP.exec(key)[1]] = null;
}
......@@ -179,12 +189,12 @@
// Second, get list of enclosure
.push(function () {
return context._sub_storage.get(ROOT);
return context._sub_storage.allAttachments(ROOT);
})
.push(function (result) {
var key;
for (key in result._attachments) {
if (result._attachments.hasOwnProperty(key)) {
for (key in result) {
if (result.hasOwnProperty(key)) {
result_dict[key] = null;
}
}
......
......@@ -66,10 +66,6 @@
return getDocumentAndHateoas(this, id)
.push(function (response) {
var result = JSON.parse(response.target.responseText),
attachments = {
view: {},
links: {}
},
key;
// action_type;
result.portal_type = result._links.type.name;
......@@ -83,12 +79,20 @@
}
}
result._attachments = attachments;
return result;
});
};
ERP5Storage.prototype.allAttachments = function (id) {
return getDocumentAndHateoas(this, id)
.push(function () {
return {
view: {},
links: {}
};
});
};
ERP5Storage.prototype.getAttachment = function (id, action) {
if (action === "view") {
......
......@@ -206,6 +206,18 @@
}
IndexedDBStorage.prototype.get = function (id) {
return openIndexedDB(this)
.push(function (db) {
var transaction = openTransaction(db, ["metadata"],
"readonly");
return handleGet(transaction.objectStore("metadata").get(id));
})
.push(function (result) {
return result.doc;
});
};
IndexedDBStorage.prototype.allAttachments = function (id) {
var attachment_dict = {};
function addEntry(cursor) {
......@@ -222,12 +234,8 @@
.openCursor(IDBKeyRange.only(id)), addEntry)
]);
})
.push(function (result_list) {
var result = result_list[0].doc;
if (Object.getOwnPropertyNames(attachment_dict).length > 0) {
result._attachments = attachment_dict;
}
return result;
.push(function () {
return attachment_dict;
});
};
......
......@@ -43,22 +43,21 @@
LocalStorage.prototype.get = function (id) {
restrictDocumentId(id);
return {};
};
LocalStorage.prototype.allAttachments = function (id) {
restrictDocumentId(id);
var doc = {},
attachments = {},
found = false,
var attachments = {},
key;
for (key in this._storage) {
if (this._storage.hasOwnProperty(key)) {
attachments[key] = {};
found = true;
}
}
if (found) {
doc._attachments = attachments;
}
return doc;
return attachments;
};
// https://gist.github.com/davoclavo/4424731
......
......@@ -44,13 +44,8 @@
};
MemoryStorage.prototype.get = function (id) {
var doc,
key,
found = false,
attachments = {};
try {
doc = JSON.parse(this._database[id].doc);
return JSON.parse(this._database[id].doc);
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
......@@ -60,18 +55,27 @@
}
throw error;
}
};
// XXX NotImplemented: list all attachments
for (key in this._database[id].attachments) {
if (this._database[id].attachments.hasOwnProperty(key)) {
found = true;
attachments[key] = {};
MemoryStorage.prototype.allAttachments = function (id) {
var key,
attachments = {};
try {
for (key in this._database[id].attachments) {
if (this._database[id].attachments.hasOwnProperty(key)) {
attachments[key] = {};
}
}
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + id,
404
);
}
throw error;
}
if (found) {
doc._attachments = attachments;
}
return doc;
return attachments;
};
MemoryStorage.prototype.remove = function (id) {
......
......@@ -17,6 +17,9 @@
QueryStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
QueryStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
QueryStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
};
......
......@@ -86,6 +86,20 @@
});
};
/*
* Get attachments list
* Try on each substorage on after the other
*/
UnionStorage.prototype.allAttachments = function () {
var argument_list = arguments,
context = this;
return this._getWithStorageIndex.apply(this, arguments)
.push(function (result) {
var sub_storage = context._storage_list[result[0]];
return sub_storage.allAttachments.apply(sub_storage, argument_list);
});
};
/*
* Post a document
* Simply store on the first substorage
......
......@@ -15,6 +15,9 @@
UUIDStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
UUIDStorage.prototype.post = function (param) {
function S4() {
......
......@@ -318,7 +318,7 @@
});
});
test("get document without attachment", function () {
test("get document", function () {
var id = "/id1/";
this.server.respondWith("PROPFIND", domain + id, [200, {
"Content-Type": "text/xml"
......@@ -350,6 +350,60 @@
'<D:status>HTTP/1.1 200 OK</D:status>' +
'</D:propstat>' +
'</D:response>' +
'<D:response xmlns:lp1="DAV:" ' +
'xmlns:lp2="http://apache.org/dav/props/">' +
'<D:href>/uploads/attachment1</D:href>' +
'<D:propstat>' +
'<D:prop>' +
'<lp1:resourcetype/>' +
'<lp1:creationdate>2013-10-30T17:19:46Z</lp1:creationdate>' +
'<lp1:getcontentlength>66</lp1:getcontentlength>' +
'<lp1:getlastmodified>Wed, 30 Oct 2013 17:19:46 GMT' +
'</lp1:getlastmodified>' +
'<lp1:getetag>"20568-42-4e9f88a2ea198"</lp1:getetag>' +
'<lp2:executable>F</lp2:executable>' +
'<D:supportedlock>' +
'<D:lockentry>' +
'<D:lockscope><D:exclusive/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'<D:lockentry>' +
'<D:lockscope><D:shared/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'</D:supportedlock>' +
'<D:lockdiscovery/>' +
'</D:prop>' +
'<D:status>HTTP/1.1 200 OK</D:status>' +
'</D:propstat>' +
'</D:response>' +
'<D:response xmlns:lp1="DAV:" ' +
'xmlns:lp2="http://apache.org/dav/props/">' +
'<D:href>/uploads/attachment2</D:href>' +
'<D:propstat>' +
'<D:prop>' +
'<lp1:resourcetype/>' +
'<lp1:creationdate>2013-10-30T17:19:46Z</lp1:creationdate>' +
'<lp1:getcontentlength>25</lp1:getcontentlength>' +
'<lp1:getlastmodified>Wed, 30 Oct 2013 17:19:46 GMT' +
'</lp1:getlastmodified>' +
'<lp1:getetag>"21226-19-4e9f88a305c4e"</lp1:getetag>' +
'<lp2:executable>F</lp2:executable>' +
'<D:supportedlock>' +
'<D:lockentry>' +
'<D:lockscope><D:exclusive/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'<D:lockentry>' +
'<D:lockscope><D:shared/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'</D:supportedlock>' +
'<D:lockdiscovery/>' +
'</D:prop>' +
'<D:status>HTTP/1.1 200 OK</D:status>' +
'</D:propstat>' +
'</D:response>' +
'</D:multistatus>'
]);
stop();
......@@ -357,8 +411,137 @@
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
}, "Check document");
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// davStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("davStorage.allAttachments", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "dav",
url: domain,
basic_login: basic_login
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("reject ID not starting with /", function () {
stop();
expect(3);
this.jio.allAttachments("get1/")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "id get1/ is forbidden (no begin /)");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("reject ID not ending with /", function () {
stop();
expect(3);
this.jio.allAttachments("/get1")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "id /get1 is forbidden (no end /)");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get inexistent document", function () {
var url = domain + "/inexistent/";
this.server.respondWith("PROPFIND", url, [404, {
"Content-Type": "text/html"
}, "foo"]);
stop();
expect(3);
this.jio.allAttachments("/inexistent/")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document without attachment", function () {
var id = "/id1/";
this.server.respondWith("PROPFIND", domain + id, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="utf-8"?>' +
'<D:multistatus xmlns:D="DAV:">' +
'<D:response xmlns:lp1="DAV:" ' +
'xmlns:lp2="http://apache.org/dav/props/">' +
'<D:href>/uploads/</D:href>' +
'<D:propstat>' +
'<D:prop>' +
'<lp1:resourcetype><D:collection/></lp1:resourcetype>' +
'<lp1:creationdate>2013-10-30T17:19:46Z</lp1:creationdate>' +
'<lp1:getlastmodified>Wed, 30 Oct 2013 17:19:46 GMT' +
'</lp1:getlastmodified>' +
'<lp1:getetag>"240be-1000-4e9f88a305c4e"</lp1:getetag>' +
'<D:supportedlock>' +
'<D:lockentry>' +
'<D:lockscope><D:exclusive/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'<D:lockentry>' +
'<D:lockscope><D:shared/></D:lockscope>' +
'<D:locktype><D:write/></D:locktype>' +
'</D:lockentry>' +
'</D:supportedlock>' +
'<D:lockdiscovery/>' +
'<D:getcontenttype>httpd/unix-directory</D:getcontenttype>' +
'</D:prop>' +
'<D:status>HTTP/1.1 200 OK</D:status>' +
'</D:propstat>' +
'</D:response>' +
'</D:multistatus>'
]);
stop();
expect(1);
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
......@@ -459,13 +642,11 @@
stop();
expect(1);
this.jio.get(id)
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {
"_attachments": {
attachment1: {},
attachment2: {}
}
attachment1: {},
attachment2: {}
}, "Check document");
})
.fail(function (error) {
......
......@@ -44,7 +44,7 @@
test("document without attachment", function () {
stop();
expect(4);
expect(3);
function StorageGetNoAttachment() {
return this;
......@@ -58,14 +58,6 @@
"another": "property"
})]);
};
StorageGetNoAttachment.prototype.get = function (id) {
equal(id, "foo", "Get foo");
return {
title: id,
id: "ID " + id,
"another": "property"
};
};
jIO.addStorage('documentstoragegetnoattachment', StorageGetNoAttachment);
......@@ -93,28 +85,58 @@
});
});
/////////////////////////////////////////////////////////////////
// documentStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("documentStorage.allAttachments");
test("document without attachment", function () {
stop();
expect(2);
function StorageGetNoAttachment() {
return this;
}
StorageGetNoAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo");
return {};
};
jIO.addStorage(
'documentstorageallattsnoattachment',
StorageGetNoAttachment
);
var jio = jIO.createJIO({
type: "document",
document_id: "foo",
sub_storage: {
type: "documentstorageallattsnoattachment"
}
});
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("document with attachment", function () {
stop();
expect(4);
expect(2);
function StorageGetWithAttachment() {
return this;
}
StorageGetWithAttachment.prototype.getAttachment = function (id, name) {
equal(id, "foo", "getAttachment bar");
equal(name, "jio_document/YmFy.json", "getAttachment bar");
return new Blob([JSON.stringify({
title: name,
id: "ID " + name,
"another": "property"
})]);
};
StorageGetWithAttachment.prototype.get = function (id) {
StorageGetWithAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo");
var result = {
title: id,
id: "ID " + id,
"another": "property",
"_attachments": {
"foo1": {}
}
......@@ -130,29 +152,24 @@
result._attachments['jio_attachment/ERROR/' + btoa("bar4")] = {};
result._attachments['jio_attachment/' + btoa("bar") + "/ERROR"] = {};
result._attachments['jio_document/' + btoa("bar") + '.json'] = {};
return result;
return result._attachments;
};
jIO.addStorage('documentstoragegetwithattachment',
jIO.addStorage('documentstorageallattswithattachment',
StorageGetWithAttachment);
var jio = jIO.createJIO({
type: "document",
document_id: "foo",
sub_storage: {
type: "documentstoragegetwithattachment"
type: "documentstorageallattswithattachment"
}
});
jio.get("bar")
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
title: "jio_document/YmFy.json",
id: "ID jio_document/YmFy.json",
"another": "property",
"_attachments": {
bar1: {}
}
bar1: {}
});
})
.fail(function (error) {
......@@ -372,10 +389,9 @@
function StorageAllDocsNoAttachment() {
return this;
}
StorageAllDocsNoAttachment.prototype.get = function (id) {
StorageAllDocsNoAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo");
return {title: id, id: "ID " + id,
"another": "property"};
return {};
};
jIO.addStorage('documentstoragealldocsnoattachment',
......@@ -413,12 +429,9 @@
function StorageAllDocsWithAttachment() {
return this;
}
StorageAllDocsWithAttachment.prototype.get = function (id) {
StorageAllDocsWithAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo");
var result = {
title: id,
id: "ID " + id,
"another": "property",
"_attachments": {
"foo1": {}
}
......@@ -431,7 +444,7 @@
result._attachments['jio_document/ERROR.json'] = {};
result._attachments['jio_attachment/' + btoa("foo5") + "/" +
btoa("bar5")] = {};
return result;
return result._attachments;
};
jIO.addStorage('documentstoragealldocswithattachment',
......
......@@ -53,7 +53,7 @@
equal(name, "bar.json", "getAttachment");
throw new jIO.util.jIOError("Cannot find subattachment", 404);
};
StorageGetNoDocument.prototype.get = function (id) {
StorageGetNoDocument.prototype.allAttachments = function (id) {
equal(id, "/", "Get document");
return {};
};
......@@ -93,12 +93,10 @@
equal(name, "bar.json", "getAttachment");
throw new jIO.util.jIOError("Cannot find subattachment", 404);
};
StorageGetOnlyAttachment.prototype.get = function (id) {
StorageGetOnlyAttachment.prototype.allAttachments = function (id) {
equal(id, "/", "Get document");
return {
"_attachments": {
"bar": {}
}
"bar": {}
};
};
......@@ -114,11 +112,7 @@
jio.get("bar")
.then(function (result) {
deepEqual(result, {
"_attachments": {
enclosure: {}
}
});
deepEqual(result, {});
})
.fail(function (error) {
ok(false, error);
......@@ -130,7 +124,7 @@
test("get document with only one document", function () {
stop();
expect(4);
expect(3);
function StorageGetOnlyDocument() {
return this;
......@@ -140,10 +134,6 @@
equal(name, "bar.json", "getAttachment");
return new Blob([JSON.stringify({title: "foo"})]);
};
StorageGetOnlyDocument.prototype.get = function (id) {
deepEqual(id, "/", "Get document");
return {};
};
jIO.addStorage('drivetojiomappinggetonlydocument', StorageGetOnlyDocument);
......@@ -168,43 +158,80 @@
});
});
test("get document with one document and one attachment", function () {
/////////////////////////////////////////////////////////////////
// driveToJioMapping.allAttachments
/////////////////////////////////////////////////////////////////
module("driveToJioMapping.allAttachments");
test("get non existent document", function () {
stop();
expect(4);
expect(6);
function StorageGetBoth() {
function StorageGetNoDocument() {
return this;
}
StorageGetBoth.prototype.getAttachment = function (id, name) {
equal(id, "/.jio_documents/", "getAttachment");
equal(name, "bar.json", "getAttachment");
return new Blob([JSON.stringify({title: "foo"})]);
StorageGetNoDocument.prototype.getAttachment =
function (id, name) {
equal(id, "/.jio_documents/", "getAttachment");
equal(name, "bar.json", "getAttachment");
throw new jIO.util.jIOError("Cannot find subattachment", 404);
};
StorageGetNoDocument.prototype.allAttachments = function (id) {
equal(id, "/", "Get document");
return {};
};
StorageGetBoth.prototype.get = function (id) {
jIO.addStorage('drivetojiomappingallattsnodocument', StorageGetNoDocument);
var jio = jIO.createJIO({
type: "drivetojiomapping",
sub_storage: {
type: "drivetojiomappingallattsnodocument"
}
});
jio.allAttachments("bar")
.then(function () {
ok(false);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document bar");
equal(error.status_code, 404);
})
.always(function () {
start();
});
});
test("get document with only one attachment", function () {
stop();
expect(2);
function StorageGetOnlyAttachment() {
return this;
}
StorageGetOnlyAttachment.prototype.allAttachments = function (id) {
equal(id, "/", "Get document");
return {
"_attachments": {
"bar": {}
}
"bar": {}
};
};
jIO.addStorage('drivetojiomappinggetboth', StorageGetBoth);
jIO.addStorage('drivetojiomappingallattsonlyattachment',
StorageGetOnlyAttachment);
var jio = jIO.createJIO({
type: "drivetojiomapping",
sub_storage: {
type: "drivetojiomappinggetboth"
type: "drivetojiomappingallattsonlyattachment"
}
});
jio.get("bar")
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
"title": "foo",
"_attachments": {
enclosure: {}
}
enclosure: {}
});
})
.fail(function (error) {
......@@ -215,6 +242,46 @@
});
});
test("get document with only one document", function () {
stop();
expect(4);
function StorageGetOnlyDocument() {
return this;
}
StorageGetOnlyDocument.prototype.getAttachment =
function (id, name) {
equal(id, "/.jio_documents/", "getAttachment");
equal(name, "bar.json", "getAttachment");
return new Blob([JSON.stringify({title: "foo"})]);
};
StorageGetOnlyDocument.prototype.allAttachments = function (id) {
deepEqual(id, "/", "Get document");
return {};
};
jIO.addStorage('drivetojiomappingallattsonlydocument',
StorageGetOnlyDocument);
var jio = jIO.createJIO({
type: "drivetojiomapping",
sub_storage: {
type: "drivetojiomappingallattsonlydocument"
}
});
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// driveToJioMapping.put
/////////////////////////////////////////////////////////////////
......@@ -716,7 +783,7 @@
return this;
}
StorageAllDocsNoDirectory.prototype.get = function (id) {
StorageAllDocsNoDirectory.prototype.allAttachments = function (id) {
call_count += 1;
if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called");
......@@ -765,7 +832,7 @@
return this;
}
StorageAllDocsEmpty.prototype.get = function (id) {
StorageAllDocsEmpty.prototype.allAttachments = function (id) {
call_count += 1;
if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called");
......@@ -815,7 +882,7 @@
return this;
}
StorageAllDocsAttachmentsOnly.prototype.get = function (id) {
StorageAllDocsAttachmentsOnly.prototype.allAttachments = function (id) {
call_count += 1;
if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called");
......@@ -824,10 +891,8 @@
equal(id, "/", "get attachments called");
return {
"_attachments": {
foo: {},
bar: {}
}
foo: {},
bar: {}
};
};
......@@ -876,16 +941,14 @@
return this;
}
StorageAllDocsFilterDocument.prototype.get = function (id) {
StorageAllDocsFilterDocument.prototype.allAttachments = function (id) {
call_count += 1;
if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called");
return {
"_attachments": {
"foo.json": {},
"bar.json": {},
"foobar.pasjson": {}
}
"foo.json": {},
"bar.json": {},
"foobar.pasjson": {}
};
}
......@@ -937,24 +1000,20 @@
return this;
}
StorageAllDocsBoth.prototype.get = function (id) {
StorageAllDocsBoth.prototype.allAttachments = function (id) {
call_count += 1;
if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called");
return {
"_attachments": {
"foo.json": {},
"bar.json": {}
}
"foo.json": {},
"bar.json": {}
};
}
equal(id, "/", "get attachments called");
return {
"_attachments": {
"bar": {},
"foobar": {}
}
"bar": {},
"foobar": {}
};
};
......
......@@ -95,11 +95,79 @@
.then(function (result) {
deepEqual(result, {
portal_type: "Person",
title: "foo",
"_attachments": {
links: {},
view: {}
title: "foo"
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// erp5Storage.allAttachments
/////////////////////////////////////////////////////////////////
module("erp5Storage.allAttachments", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "erp5",
url: domain
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("allAttachments ERP5 document", function () {
var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id),
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
// Remove all _ properties
"_bar": "john doo",
"_links": {
type: {
name: "Person"
}
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", traverse_url, [200, {
"Content-Type": "application/hal+json"
}, document_hateoas]);
stop();
expect(10);
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {
links: {},
view: {}
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
......
......@@ -271,6 +271,163 @@
}
});
test("spy indexedDB usage", function () {
var context = this;
stop();
expect(10);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
context.spy_open = sinon.spy(indexedDB, "open");
context.spy_create_store = sinon.spy(IDBDatabase.prototype,
"createObjectStore");
context.spy_transaction = sinon.spy(IDBDatabase.prototype,
"transaction");
context.spy_store = sinon.spy(IDBTransaction.prototype, "objectStore");
context.spy_get = sinon.spy(IDBObjectStore.prototype, "get");
return context.jio.get("foo");
})
.then(function () {
ok(context.spy_open.calledOnce, "open count " +
context.spy_open.callCount);
equal(context.spy_open.firstCall.args[0], "jio:qunit",
"open first argument");
equal(context.spy_create_store.callCount, 0,
"createObjectStore count");
ok(context.spy_transaction.calledOnce, "transaction count " +
context.spy_transaction.callCount);
deepEqual(context.spy_transaction.firstCall.args[0],
["metadata"], "transaction first argument");
equal(context.spy_transaction.firstCall.args[1], "readonly",
"transaction second argument");
ok(context.spy_store.calledOnce, "store count " +
context.spy_store.callCount);
deepEqual(context.spy_store.firstCall.args[0], "metadata",
"store first argument");
ok(context.spy_get.calledOnce, "index count " +
context.spy_get.callCount);
deepEqual(context.spy_get.firstCall.args[0], "foo",
"get first argument");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
context.spy_open.restore();
delete context.spy_open;
context.spy_create_store.restore();
delete context.spy_create_store;
context.spy_transaction.restore();
delete context.spy_transaction;
context.spy_store.restore();
delete context.spy_store;
context.spy_get.restore();
delete context.spy_get;
})
.always(function () {
start();
});
});
test("get inexistent document", function () {
var context = this;
stop();
expect(3);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.get("inexistent");
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document without attachment", function () {
var id = "/",
context = this;
stop();
expect(1);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document with attachment", function () {
var id = "/",
attachment = "foo",
context = this;
stop();
expect(1);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment(id, attachment, "bar");
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// indexeddbStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("indexeddbStorage.allAttachments", {
setup: function () {
this.jio = jIO.createJIO({
type: "indexeddb",
database: "qunit"
});
}
});
test("spy indexedDB usage", function () {
var context = this;
stop();
......@@ -294,7 +451,7 @@
context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor");
context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.get("foo");
return context.jio.allAttachments("foo");
})
.then(function () {
......@@ -374,7 +531,7 @@
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.get("inexistent");
return context.jio.allAttachments("inexistent");
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
......@@ -400,12 +557,10 @@
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.get(id);
return context.jio.allAttachments(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar"
}, "Check document");
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
......@@ -430,14 +585,11 @@
return context.jio.putAttachment(id, attachment, "bar");
})
.then(function () {
return context.jio.get(id);
return context.jio.allAttachments(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar",
"_attachments": {
"foo": {}
}
"foo": {}
}, "Check document");
})
.fail(function (error) {
......
......@@ -92,11 +92,76 @@
localStorage.setItem(attachment, "bar");
this.jio.get(id)
.then(function (result) {
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// localStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("localStorage.allAttachments", {
setup: function () {
localStorage.clear();
this.jio = jIO.createJIO({
"type": "local"
});
}
});
test("get non valid document ID", function () {
stop();
expect(3);
this.jio.allAttachments("inexistent")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "id inexistent is forbidden (!== /)");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document without attachment", function () {
var id = "/";
stop();
expect(1);
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document with attachment", function () {
var id = "/",
attachment = "foo";
stop();
expect(1);
localStorage.setItem(attachment, "bar");
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {
"_attachments": {
"foo": {}
}
"foo": {}
}, "Check document");
})
.fail(function (error) {
......
......@@ -167,9 +167,81 @@
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
"_attachments": {putattmt2: {}}
}, "Check document");
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("memoryStorage.allAttachments", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("get inexistent document", function () {
stop();
expect(3);
this.jio.allAttachments("inexistent")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("document without attachment", function () {
var id = "post1";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({title: "myPost1"})
};
stop();
expect(1);
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {}, "Attachments");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("document with attachment", function () {
var id = "putattmt1";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({}),
"attachments": {
putattmt2: undefined
}
};
stop();
expect(1);
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {putattmt2: {}}, "Check document");
})
.fail(function (error) {
ok(false, error);
......
......@@ -71,6 +71,40 @@
});
});
/////////////////////////////////////////////////////////////////
// queryStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("queryStorage.allAttachments");
test("allAttachments called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "query",
sub_storage: {
type: "querystorage200"
}
});
Storage200.prototype.allAttachments = function (id) {
equal(id, "bar", "allAttachments, 200 called");
return {attachmentname: {}};
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
attachmentname: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// queryStorage.post
/////////////////////////////////////////////////////////////////
......
......@@ -31,6 +31,10 @@
equal(id, "bar", "get 200 called");
return {title: "foo"};
};
Storage200.prototype.allAttachments = function (id) {
equal(id, "bar", "allAttachments 200 called");
return {attachmentname: {}};
};
Storage200.prototype.put = function (id, param) {
equal(id, "bar", "put 200 called");
deepEqual(param, {"title": "foo"}, "put 200 called");
......@@ -257,6 +261,145 @@
});
});
/////////////////////////////////////////////////////////////////
// unionStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("unionStorage.allAttachments");
test("allAttachments inexistent document", function () {
stop();
expect(5);
var jio = jIO.createJIO({
type: "union",
storage_list: [{
type: "unionstorage404"
}, {
type: "unionstorage404"
}]
});
jio.allAttachments("bar")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments document on first storage", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "union",
storage_list: [{
type: "unionstorage200"
}, {
type: "unionstorage404"
}]
});
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
"attachmentname": {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments document on second storage", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "union",
storage_list: [{
type: "unionstorage404"
}, {
type: "unionstorage200"
}]
});
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
"attachmentname": {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments error on first storage", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "union",
storage_list: [{
type: "unionstorage500"
}, {
type: "unionstorage200"
}]
});
jio.get("bar")
.fail(function (error) {
ok(error instanceof Error);
equal(error.message, "manually triggered error");
equal(error.status_code, undefined);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments error on second storage", function () {
stop();
expect(5);
var jio = jIO.createJIO({
type: "union",
storage_list: [{
type: "unionstorage404"
}, {
type: "unionstorage500"
}]
});
jio.get("bar")
.fail(function (error) {
ok(error instanceof Error);
equal(error.message, "manually triggered error");
equal(error.status_code, undefined);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// unionStorage.post
/////////////////////////////////////////////////////////////////
......
......@@ -71,6 +71,40 @@
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("uuidStorage.allAttachments");
test("get called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "uuidstorage200"
}
});
Storage200.prototype.allAttachments = function (param) {
equal(param, "bar", "allAttachments 200 called");
return {attachmentname: {}};
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
attachmentname: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.post
/////////////////////////////////////////////////////////////////
......
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