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 @@ ...@@ -390,6 +390,8 @@
}); });
}; };
declareMethod(JioProxyStorage, "allAttachments", checkId);
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Storage builder // Storage builder
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
...@@ -83,9 +83,8 @@ ...@@ -83,9 +83,8 @@
DavStorage.prototype.put = function (id, param) { DavStorage.prototype.put = function (id, param) {
id = restrictDocumentId(id); id = restrictDocumentId(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 some properties
throw new jIO.util.jIOError("Can not store properties: " + throw new jIO.util.jIOError("Can not store properties: " +
Object.getOwnPropertyNames(param), 400); Object.getOwnPropertyNames(param), 400);
} }
...@@ -104,6 +103,33 @@ ...@@ -104,6 +103,33 @@
}; };
DavStorage.prototype.get = function (id) { 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; var context = this;
id = restrictDocumentId(id); id = restrictDocumentId(id);
...@@ -126,7 +152,6 @@ ...@@ -126,7 +152,6 @@
// Extract all meta informations and return them to JSON // Extract all meta informations and return them to JSON
var i, var i,
result = {},
attachment = {}, attachment = {},
id, id,
attachment_list = new DOMParser().parseFromString( attachment_list = new DOMParser().parseFromString(
...@@ -146,10 +171,7 @@ ...@@ -146,10 +171,7 @@
attachment[id] = {}; attachment[id] = {};
} }
} }
if (Object.getOwnPropertyNames(attachment).length > 0) { return attachment;
result._attachments = attachment;
}
return result;
}, function (error) { }, function (error) {
if ((error.target !== undefined) && if ((error.target !== undefined) &&
......
...@@ -27,9 +27,6 @@ ...@@ -27,9 +27,6 @@
} }
DocumentStorage.prototype.get = function (id) { DocumentStorage.prototype.get = function (id) {
var result,
context = this;
return this._sub_storage.getAttachment( return this._sub_storage.getAttachment(
this._document_id, this._document_id,
getSubAttachmentIdFromParam(id) getSubAttachmentIdFromParam(id)
...@@ -39,17 +36,17 @@ ...@@ -39,17 +36,17 @@
}) })
.push(function (text) { .push(function (text) {
return JSON.parse(text.target.result); return JSON.parse(text.target.result);
}) });
.push(function (json) { };
result = json;
return context._sub_storage.get(context._document_id); DocumentStorage.prototype.allAttachments = function (id) {
}) return this._sub_storage.allAttachments(this._document_id)
.push(function (document) { .push(function (result) {
var attachments = {}, var attachments = {},
exec, exec,
key; key;
for (key in document._attachments) { for (key in result) {
if (document._attachments.hasOwnProperty(key)) { if (result.hasOwnProperty(key)) {
if (ATTACHMENT_REGEXP.test(key)) { if (ATTACHMENT_REGEXP.test(key)) {
exec = ATTACHMENT_REGEXP.exec(key); exec = ATTACHMENT_REGEXP.exec(key);
try { try {
...@@ -65,10 +62,7 @@ ...@@ -65,10 +62,7 @@
} }
} }
} }
if (Object.getOwnPropertyNames(attachments).length > 0) { return attachments;
result._attachments = attachments;
}
return result;
}); });
}; };
...@@ -99,12 +93,12 @@ ...@@ -99,12 +93,12 @@
}; };
DocumentStorage.prototype.buildQuery = function () { DocumentStorage.prototype.buildQuery = function () {
return this._sub_storage.get(this._document_id) return this._sub_storage.allAttachments(this._document_id)
.push(function (document) { .push(function (attachment_dict) {
var result = [], var result = [],
key; key;
for (key in document._attachments) { for (key in attachment_dict) {
if (document._attachments.hasOwnProperty(key)) { if (attachment_dict.hasOwnProperty(key)) {
if (DOCUMENT_REGEXP.test(key)) { if (DOCUMENT_REGEXP.test(key)) {
try { try {
result.push({ result.push({
......
...@@ -19,9 +19,7 @@ ...@@ -19,9 +19,7 @@
ROOT = "/"; ROOT = "/";
FileSystemBridgeStorage.prototype.get = function (id) { FileSystemBridgeStorage.prototype.get = function (id) {
var context = this, var context = this;
json_document,
explicit_document = false;
return new RSVP.Queue() return new RSVP.Queue()
// First, try to get explicit reference to the document // First, try to get explicit reference to the document
...@@ -39,38 +37,50 @@ ...@@ -39,38 +37,50 @@
return jIO.util.readBlobAsText(blob); return jIO.util.readBlobAsText(blob);
}) })
.push(function (text) { .push(function (text) {
explicit_document = true;
return JSON.parse(text.target.result); return JSON.parse(text.target.result);
}); });
}, function (error) { }, function (error) {
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
// Second, try to get default attachment
return context._sub_storage.allAttachments(ROOT)
.push(function (attachment_dict) {
if (attachment_dict.hasOwnProperty(id)) {
return {}; return {};
} }
throw new jIO.util.jIOError("Cannot find document " + id,
404);
});
}
throw error; throw error;
}) });
};
// Second, try to get default attachment
.push(function (result) {
json_document = result;
return context._sub_storage.get(ROOT);
})
.push(function (directory_document) { FileSystemBridgeStorage.prototype.allAttachments = function (id) {
if ((directory_document.hasOwnProperty("_attachments")) && var context = this;
(directory_document._attachments.hasOwnProperty(id))) { return context._sub_storage.allAttachments(ROOT)
json_document._attachments = { .push(function (attachment_dict) {
if (attachment_dict.hasOwnProperty(id)) {
return {
enclosure: {} enclosure: {}
}; };
} else { }
if (!explicit_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, throw new jIO.util.jIOError("Cannot find document " + id,
404); 404);
} }
} throw error;
return json_document; });
}); });
}; };
...@@ -157,12 +167,12 @@ ...@@ -157,12 +167,12 @@
// First, get list of explicit documents // First, get list of explicit documents
.push(function () { .push(function () {
return context._sub_storage.get(DOCUMENT_KEY); return context._sub_storage.allAttachments(DOCUMENT_KEY);
}) })
.push(function (result) { .push(function (result) {
var key; var key;
for (key in result._attachments) { for (key in result) {
if (result._attachments.hasOwnProperty(key)) { if (result.hasOwnProperty(key)) {
if (DOCUMENT_REGEXP.test(key)) { if (DOCUMENT_REGEXP.test(key)) {
result_dict[DOCUMENT_REGEXP.exec(key)[1]] = null; result_dict[DOCUMENT_REGEXP.exec(key)[1]] = null;
} }
...@@ -179,12 +189,12 @@ ...@@ -179,12 +189,12 @@
// Second, get list of enclosure // Second, get list of enclosure
.push(function () { .push(function () {
return context._sub_storage.get(ROOT); return context._sub_storage.allAttachments(ROOT);
}) })
.push(function (result) { .push(function (result) {
var key; var key;
for (key in result._attachments) { for (key in result) {
if (result._attachments.hasOwnProperty(key)) { if (result.hasOwnProperty(key)) {
result_dict[key] = null; result_dict[key] = null;
} }
} }
......
...@@ -66,10 +66,6 @@ ...@@ -66,10 +66,6 @@
return getDocumentAndHateoas(this, id) 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 = {
view: {},
links: {}
},
key; key;
// action_type; // action_type;
result.portal_type = result._links.type.name; result.portal_type = result._links.type.name;
...@@ -83,12 +79,20 @@ ...@@ -83,12 +79,20 @@
} }
} }
result._attachments = attachments;
return result; return result;
}); });
}; };
ERP5Storage.prototype.allAttachments = function (id) {
return getDocumentAndHateoas(this, id)
.push(function () {
return {
view: {},
links: {}
};
});
};
ERP5Storage.prototype.getAttachment = function (id, action) { ERP5Storage.prototype.getAttachment = function (id, action) {
if (action === "view") { if (action === "view") {
......
...@@ -206,6 +206,18 @@ ...@@ -206,6 +206,18 @@
} }
IndexedDBStorage.prototype.get = function (id) { 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 = {}; var attachment_dict = {};
function addEntry(cursor) { function addEntry(cursor) {
...@@ -222,12 +234,8 @@ ...@@ -222,12 +234,8 @@
.openCursor(IDBKeyRange.only(id)), addEntry) .openCursor(IDBKeyRange.only(id)), addEntry)
]); ]);
}) })
.push(function (result_list) { .push(function () {
var result = result_list[0].doc; return attachment_dict;
if (Object.getOwnPropertyNames(attachment_dict).length > 0) {
result._attachments = attachment_dict;
}
return result;
}); });
}; };
......
...@@ -43,22 +43,21 @@ ...@@ -43,22 +43,21 @@
LocalStorage.prototype.get = function (id) { LocalStorage.prototype.get = function (id) {
restrictDocumentId(id); restrictDocumentId(id);
return {};
};
LocalStorage.prototype.allAttachments = function (id) {
restrictDocumentId(id);
var doc = {}, var attachments = {},
attachments = {},
found = false,
key; key;
for (key in this._storage) { for (key in this._storage) {
if (this._storage.hasOwnProperty(key)) { if (this._storage.hasOwnProperty(key)) {
attachments[key] = {}; attachments[key] = {};
found = true;
}
} }
if (found) {
doc._attachments = attachments;
} }
return doc; return attachments;
}; };
// https://gist.github.com/davoclavo/4424731 // https://gist.github.com/davoclavo/4424731
......
...@@ -44,13 +44,8 @@ ...@@ -44,13 +44,8 @@
}; };
MemoryStorage.prototype.get = function (id) { MemoryStorage.prototype.get = function (id) {
var doc,
key,
found = false,
attachments = {};
try { try {
doc = JSON.parse(this._database[id].doc); return 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(
...@@ -60,18 +55,27 @@ ...@@ -60,18 +55,27 @@
} }
throw error; throw error;
} }
};
// XXX NotImplemented: list all attachments MemoryStorage.prototype.allAttachments = function (id) {
var key,
attachments = {};
try {
for (key in this._database[id].attachments) { for (key in this._database[id].attachments) {
if (this._database[id].attachments.hasOwnProperty(key)) { if (this._database[id].attachments.hasOwnProperty(key)) {
found = true;
attachments[key] = {}; attachments[key] = {};
} }
} }
if (found) { } catch (error) {
doc._attachments = attachments; if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + id,
404
);
}
throw error;
} }
return doc; return attachments;
}; };
MemoryStorage.prototype.remove = function (id) { MemoryStorage.prototype.remove = function (id) {
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
QueryStorage.prototype.get = function () { QueryStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments); 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 () { QueryStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments); return this._sub_storage.post.apply(this._sub_storage, arguments);
}; };
......
...@@ -86,6 +86,20 @@ ...@@ -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 * Post a document
* Simply store on the first substorage * Simply store on the first substorage
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
UUIDStorage.prototype.get = function () { UUIDStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments); 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) { UUIDStorage.prototype.post = function (param) {
function S4() { function S4() {
......
...@@ -318,7 +318,7 @@ ...@@ -318,7 +318,7 @@
}); });
}); });
test("get document without attachment", function () { test("get document", function () {
var id = "/id1/"; var id = "/id1/";
this.server.respondWith("PROPFIND", domain + id, [200, { this.server.respondWith("PROPFIND", domain + id, [200, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
...@@ -350,6 +350,60 @@ ...@@ -350,6 +350,60 @@
'<D:status>HTTP/1.1 200 OK</D:status>' + '<D:status>HTTP/1.1 200 OK</D:status>' +
'</D:propstat>' + '</D:propstat>' +
'</D:response>' + '</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>' '</D:multistatus>'
]); ]);
stop(); stop();
...@@ -357,8 +411,137 @@ ...@@ -357,8 +411,137 @@
this.jio.get(id) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {}, "Check document");
}, "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) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -459,13 +642,11 @@ ...@@ -459,13 +642,11 @@
stop(); stop();
expect(1); expect(1);
this.jio.get(id) this.jio.allAttachments(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_attachments": {
attachment1: {}, attachment1: {},
attachment2: {} attachment2: {}
}
}, "Check document"); }, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
......
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
test("document without attachment", function () { test("document without attachment", function () {
stop(); stop();
expect(4); expect(3);
function StorageGetNoAttachment() { function StorageGetNoAttachment() {
return this; return this;
...@@ -58,14 +58,6 @@ ...@@ -58,14 +58,6 @@
"another": "property" "another": "property"
})]); })]);
}; };
StorageGetNoAttachment.prototype.get = function (id) {
equal(id, "foo", "Get foo");
return {
title: id,
id: "ID " + id,
"another": "property"
};
};
jIO.addStorage('documentstoragegetnoattachment', StorageGetNoAttachment); jIO.addStorage('documentstoragegetnoattachment', StorageGetNoAttachment);
...@@ -93,28 +85,58 @@ ...@@ -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 () { test("document with attachment", function () {
stop(); stop();
expect(4); expect(2);
function StorageGetWithAttachment() { function StorageGetWithAttachment() {
return this; return this;
} }
StorageGetWithAttachment.prototype.getAttachment = function (id, name) { StorageGetWithAttachment.prototype.allAttachments = function (id) {
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) {
equal(id, "foo", "Get foo"); equal(id, "foo", "Get foo");
var result = { var result = {
title: id,
id: "ID " + id,
"another": "property",
"_attachments": { "_attachments": {
"foo1": {} "foo1": {}
} }
...@@ -130,29 +152,24 @@ ...@@ -130,29 +152,24 @@
result._attachments['jio_attachment/ERROR/' + btoa("bar4")] = {}; result._attachments['jio_attachment/ERROR/' + btoa("bar4")] = {};
result._attachments['jio_attachment/' + btoa("bar") + "/ERROR"] = {}; result._attachments['jio_attachment/' + btoa("bar") + "/ERROR"] = {};
result._attachments['jio_document/' + btoa("bar") + '.json'] = {}; result._attachments['jio_document/' + btoa("bar") + '.json'] = {};
return result; return result._attachments;
}; };
jIO.addStorage('documentstoragegetwithattachment', jIO.addStorage('documentstorageallattswithattachment',
StorageGetWithAttachment); StorageGetWithAttachment);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "document", type: "document",
document_id: "foo", document_id: "foo",
sub_storage: { sub_storage: {
type: "documentstoragegetwithattachment" type: "documentstorageallattswithattachment"
} }
}); });
jio.get("bar") jio.allAttachments("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
title: "jio_document/YmFy.json",
id: "ID jio_document/YmFy.json",
"another": "property",
"_attachments": {
bar1: {} bar1: {}
}
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -372,10 +389,9 @@ ...@@ -372,10 +389,9 @@
function StorageAllDocsNoAttachment() { function StorageAllDocsNoAttachment() {
return this; return this;
} }
StorageAllDocsNoAttachment.prototype.get = function (id) { StorageAllDocsNoAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo"); equal(id, "foo", "Get foo");
return {title: id, id: "ID " + id, return {};
"another": "property"};
}; };
jIO.addStorage('documentstoragealldocsnoattachment', jIO.addStorage('documentstoragealldocsnoattachment',
...@@ -413,12 +429,9 @@ ...@@ -413,12 +429,9 @@
function StorageAllDocsWithAttachment() { function StorageAllDocsWithAttachment() {
return this; return this;
} }
StorageAllDocsWithAttachment.prototype.get = function (id) { StorageAllDocsWithAttachment.prototype.allAttachments = function (id) {
equal(id, "foo", "Get foo"); equal(id, "foo", "Get foo");
var result = { var result = {
title: id,
id: "ID " + id,
"another": "property",
"_attachments": { "_attachments": {
"foo1": {} "foo1": {}
} }
...@@ -431,7 +444,7 @@ ...@@ -431,7 +444,7 @@
result._attachments['jio_document/ERROR.json'] = {}; result._attachments['jio_document/ERROR.json'] = {};
result._attachments['jio_attachment/' + btoa("foo5") + "/" + result._attachments['jio_attachment/' + btoa("foo5") + "/" +
btoa("bar5")] = {}; btoa("bar5")] = {};
return result; return result._attachments;
}; };
jIO.addStorage('documentstoragealldocswithattachment', jIO.addStorage('documentstoragealldocswithattachment',
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
equal(name, "bar.json", "getAttachment"); equal(name, "bar.json", "getAttachment");
throw new jIO.util.jIOError("Cannot find subattachment", 404); throw new jIO.util.jIOError("Cannot find subattachment", 404);
}; };
StorageGetNoDocument.prototype.get = function (id) { StorageGetNoDocument.prototype.allAttachments = function (id) {
equal(id, "/", "Get document"); equal(id, "/", "Get document");
return {}; return {};
}; };
...@@ -93,12 +93,10 @@ ...@@ -93,12 +93,10 @@
equal(name, "bar.json", "getAttachment"); equal(name, "bar.json", "getAttachment");
throw new jIO.util.jIOError("Cannot find subattachment", 404); throw new jIO.util.jIOError("Cannot find subattachment", 404);
}; };
StorageGetOnlyAttachment.prototype.get = function (id) { StorageGetOnlyAttachment.prototype.allAttachments = function (id) {
equal(id, "/", "Get document"); equal(id, "/", "Get document");
return { return {
"_attachments": {
"bar": {} "bar": {}
}
}; };
}; };
...@@ -114,11 +112,7 @@ ...@@ -114,11 +112,7 @@
jio.get("bar") jio.get("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {});
"_attachments": {
enclosure: {}
}
});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -130,7 +124,7 @@ ...@@ -130,7 +124,7 @@
test("get document with only one document", function () { test("get document with only one document", function () {
stop(); stop();
expect(4); expect(3);
function StorageGetOnlyDocument() { function StorageGetOnlyDocument() {
return this; return this;
...@@ -140,10 +134,6 @@ ...@@ -140,10 +134,6 @@
equal(name, "bar.json", "getAttachment"); equal(name, "bar.json", "getAttachment");
return new Blob([JSON.stringify({title: "foo"})]); return new Blob([JSON.stringify({title: "foo"})]);
}; };
StorageGetOnlyDocument.prototype.get = function (id) {
deepEqual(id, "/", "Get document");
return {};
};
jIO.addStorage('drivetojiomappinggetonlydocument', StorageGetOnlyDocument); jIO.addStorage('drivetojiomappinggetonlydocument', StorageGetOnlyDocument);
...@@ -168,44 +158,121 @@ ...@@ -168,44 +158,121 @@
}); });
}); });
test("get document with one document and one attachment", function () { /////////////////////////////////////////////////////////////////
// driveToJioMapping.allAttachments
/////////////////////////////////////////////////////////////////
module("driveToJioMapping.allAttachments");
test("get non existent document", function () {
stop(); stop();
expect(4); expect(6);
function StorageGetBoth() { function StorageGetNoDocument() {
return this; return this;
} }
StorageGetBoth.prototype.getAttachment = function (id, name) { StorageGetNoDocument.prototype.getAttachment =
function (id, name) {
equal(id, "/.jio_documents/", "getAttachment"); equal(id, "/.jio_documents/", "getAttachment");
equal(name, "bar.json", "getAttachment"); equal(name, "bar.json", "getAttachment");
return new Blob([JSON.stringify({title: "foo"})]); throw new jIO.util.jIOError("Cannot find subattachment", 404);
}; };
StorageGetBoth.prototype.get = function (id) { StorageGetNoDocument.prototype.allAttachments = function (id) {
equal(id, "/", "Get document");
return {};
};
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"); equal(id, "/", "Get document");
return { return {
"_attachments": {
"bar": {} "bar": {}
}
}; };
}; };
jIO.addStorage('drivetojiomappinggetboth', StorageGetBoth); jIO.addStorage('drivetojiomappingallattsonlyattachment',
StorageGetOnlyAttachment);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "drivetojiomapping", type: "drivetojiomapping",
sub_storage: { sub_storage: {
type: "drivetojiomappinggetboth" type: "drivetojiomappingallattsonlyattachment"
} }
}); });
jio.get("bar") jio.allAttachments("bar")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "foo",
"_attachments": {
enclosure: {} enclosure: {}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
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) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -716,7 +783,7 @@ ...@@ -716,7 +783,7 @@
return this; return this;
} }
StorageAllDocsNoDirectory.prototype.get = function (id) { StorageAllDocsNoDirectory.prototype.allAttachments = function (id) {
call_count += 1; call_count += 1;
if (call_count === 1) { if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called"); equal(id, "/.jio_documents/", "get documents called");
...@@ -765,7 +832,7 @@ ...@@ -765,7 +832,7 @@
return this; return this;
} }
StorageAllDocsEmpty.prototype.get = function (id) { StorageAllDocsEmpty.prototype.allAttachments = function (id) {
call_count += 1; call_count += 1;
if (call_count === 1) { if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called"); equal(id, "/.jio_documents/", "get documents called");
...@@ -815,7 +882,7 @@ ...@@ -815,7 +882,7 @@
return this; return this;
} }
StorageAllDocsAttachmentsOnly.prototype.get = function (id) { StorageAllDocsAttachmentsOnly.prototype.allAttachments = function (id) {
call_count += 1; call_count += 1;
if (call_count === 1) { if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called"); equal(id, "/.jio_documents/", "get documents called");
...@@ -824,10 +891,8 @@ ...@@ -824,10 +891,8 @@
equal(id, "/", "get attachments called"); equal(id, "/", "get attachments called");
return { return {
"_attachments": {
foo: {}, foo: {},
bar: {} bar: {}
}
}; };
}; };
...@@ -876,16 +941,14 @@ ...@@ -876,16 +941,14 @@
return this; return this;
} }
StorageAllDocsFilterDocument.prototype.get = function (id) { StorageAllDocsFilterDocument.prototype.allAttachments = function (id) {
call_count += 1; call_count += 1;
if (call_count === 1) { if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called"); equal(id, "/.jio_documents/", "get documents called");
return { return {
"_attachments": {
"foo.json": {}, "foo.json": {},
"bar.json": {}, "bar.json": {},
"foobar.pasjson": {} "foobar.pasjson": {}
}
}; };
} }
...@@ -937,24 +1000,20 @@ ...@@ -937,24 +1000,20 @@
return this; return this;
} }
StorageAllDocsBoth.prototype.get = function (id) { StorageAllDocsBoth.prototype.allAttachments = function (id) {
call_count += 1; call_count += 1;
if (call_count === 1) { if (call_count === 1) {
equal(id, "/.jio_documents/", "get documents called"); equal(id, "/.jio_documents/", "get documents called");
return { return {
"_attachments": {
"foo.json": {}, "foo.json": {},
"bar.json": {} "bar.json": {}
}
}; };
} }
equal(id, "/", "get attachments called"); equal(id, "/", "get attachments called");
return { return {
"_attachments": {
"bar": {}, "bar": {},
"foobar": {} "foobar": {}
}
}; };
}; };
......
...@@ -95,11 +95,79 @@ ...@@ -95,11 +95,79 @@
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
portal_type: "Person", portal_type: "Person",
title: "foo", title: "foo"
"_attachments": { }, "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: {}, links: {},
view: {} view: {}
}
}, "Check document"); }, "Check document");
equal(server.requests.length, 2); equal(server.requests.length, 2);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
......
...@@ -271,6 +271,163 @@ ...@@ -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 () { test("spy indexedDB usage", function () {
var context = this; var context = this;
stop(); stop();
...@@ -294,7 +451,7 @@ ...@@ -294,7 +451,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("foo"); return context.jio.allAttachments("foo");
}) })
.then(function () { .then(function () {
...@@ -374,7 +531,7 @@ ...@@ -374,7 +531,7 @@
deleteIndexedDB(context.jio) deleteIndexedDB(context.jio)
.then(function () { .then(function () {
return context.jio.get("inexistent"); return context.jio.allAttachments("inexistent");
}) })
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
...@@ -400,12 +557,10 @@ ...@@ -400,12 +557,10 @@
return context.jio.put(id, {"title": "bar"}); return context.jio.put(id, {"title": "bar"});
}) })
.then(function () { .then(function () {
return context.jio.get(id); return context.jio.allAttachments(id);
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {}, "Check document");
"title": "bar"
}, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -430,14 +585,11 @@ ...@@ -430,14 +585,11 @@
return context.jio.putAttachment(id, attachment, "bar"); return context.jio.putAttachment(id, attachment, "bar");
}) })
.then(function () { .then(function () {
return context.jio.get(id); return context.jio.allAttachments(id);
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "bar",
"_attachments": {
"foo": {} "foo": {}
}
}, "Check document"); }, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
......
...@@ -92,11 +92,76 @@ ...@@ -92,11 +92,76 @@
localStorage.setItem(attachment, "bar"); localStorage.setItem(attachment, "bar");
this.jio.get(id) 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) { .then(function (result) {
deepEqual(result, { deepEqual(result, {
"_attachments": {
"foo": {} "foo": {}
}
}, "Check document"); }, "Check document");
}) })
.fail(function (error) { .fail(function (error) {
......
...@@ -167,9 +167,81 @@ ...@@ -167,9 +167,81 @@
this.jio.get(id) this.jio.get(id)
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {}, "Check document");
"_attachments": {putattmt2: {}} })
}, "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) { .fail(function (error) {
ok(false, error); ok(false, error);
......
...@@ -71,6 +71,40 @@ ...@@ -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 // queryStorage.post
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
equal(id, "bar", "get 200 called"); equal(id, "bar", "get 200 called");
return {title: "foo"}; return {title: "foo"};
}; };
Storage200.prototype.allAttachments = function (id) {
equal(id, "bar", "allAttachments 200 called");
return {attachmentname: {}};
};
Storage200.prototype.put = function (id, param) { Storage200.prototype.put = function (id, param) {
equal(id, "bar", "put 200 called"); equal(id, "bar", "put 200 called");
deepEqual(param, {"title": "foo"}, "put 200 called"); deepEqual(param, {"title": "foo"}, "put 200 called");
...@@ -257,6 +261,145 @@ ...@@ -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 // unionStorage.post
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
...@@ -71,6 +71,40 @@ ...@@ -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 // 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