Commit f3294a72 authored by Tristan Cavelier's avatar Tristan Cavelier

gidstorage.tests.js added

parent f4aa93ab
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, jIO, jio_tests, test, ok, deepEqual, sinon */ /*global define, jIO, test_util, RSVP, test, ok, deepEqual, sinon, module, stop,
start */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -7,699 +8,670 @@ ...@@ -7,699 +8,670 @@
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
return define(dependencies, module); return define(dependencies, module);
} }
module(jIO, jio_tests); module(RSVP, jIO);
}(['jio', 'jio_tests', 'localstorage', 'gidstorage'], function (jIO, util) { }([
'rsvp',
'jio',
'localstorage',
'gidstorage'
], function (RSVP, jIO) {
"use strict"; "use strict";
function generateTools() {
return {
clock: sinon.useFakeTimers(),
spy: util.ospy,
tick: util.otick
};
}
module("GID Storage"); module("GID Storage");
test("Post", function () { function success(promise) {
var o = generateTools(this); return new RSVP.Promise(function (resolve, reject, notify) {
/*jslint unparam: true*/
o.localstorage_spec = { promise.then(resolve, resolve, notify);
"type": "local", }, function () {
"username": "one", promise.cancel();
"application_name": "gid storage post test" });
}; }
// local jio is going to help us to prepare localstorage for gid tests /**
o.local_jio = jIO.newJio(o.localstorage_spec); * Test with a memory storage, the responses of gid storage should not be
* related to memory storage ones.
*/
test("Scenario", 29, function () {
o.jio = jIO.newJio({ var jio = jIO.createJIO({
"type": "gid", "type": "gid",
"sub_storage": o.localstorage_spec,
"constraints": { "constraints": {
"default": { "default": {
"creator": "list" "created": "date",
"title": "string",
"type": "DCMIType"
} }
},
"sub_storage": {
"type": "local",
"username": "gidtest",
"application_name": "jiotests",
"mode": "memory"
} }
}, {
"workspace": {},
"max_retry": 2
}); });
// preparing localstorage with documents function postNewDocument() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.post({
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); "created": "2013-10-10",
o.clock.tick(2000); "title": "Unique ID",
"type": "Text"
util.closeAndcleanUpJio(o.local_jio);
// Fail to post a document because metadata doesn't respect constraints
// XXX check reason
o.spy(o, 'status', 400, 'Post document without respecting constraints ' +
'-> bad request');
o.jio.post({}, o.f);
o.tick(o);
// Fail to post a document but a document already exists
o.spy(o, 'status', 409, 'Post existent document -> conflict');
o.jio.post({"creator": "a", "title": "water"}, o.f);
o.tick(o);
// Succeed to post because no document with the same gid has been found
o.spy(o, 'value', {
"id": "{\"creator\":[\"a%\"]}",
"ok": true
}, 'Post respecting constraints');
o.jio.post({"creator": "a%", "title": "fire"}, o.f);
o.tick(o);
// Fail to post because this document has been uploaded right before
o.spy(o, 'status', 409, 'Post same document respecting constraints ' +
'-> conflicts');
o.jio.post({"creator": "a%", "title": "space"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
}); });
test("Get", function () {
var o = generateTools(this);
o.localstorage_spec = {
"type": "local",
"username": "one",
"application_name": "gid storage get test"
};
// local jio is going to help us to prepare localstorage for gid tests
o.local_jio = jIO.newJio(o.localstorage_spec);
o.jio = jIO.newJio({
"type": "gid",
"sub_storage": o.localstorage_spec,
"constraints": {
"default": {
"creator": "list"
} }
function postNewDocumentTest(answer) {
deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}',
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Post a new document");
} }
});
// preparing localstorage with documents function getCreatedDocument() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.get({
o.local_jio.put({"_id": "red", "creator": ["ac", "b"], "title": "wind"}); "_id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}'
o.clock.tick(2000);
util.closeAndcleanUpJio(o.local_jio);
// Fail to get document because _id doesn't respect constraints
o.spy(o, 'status', 400, 'Get document without respecting constraints ' +
'-> bad request');
o.jio.get({"_id": "a"}, o.f);
o.tick(o);
// Fail to get because no document with the same gid has been found
o.spy(o, 'status', 404, 'Get inexistent document');
o.jio.get({"_id": "{\"creator\":[\"c\"]}"}, o.f);
o.tick(o);
// Succeed to get, gid is good, document found
o.spy(o, 'value', {
"_id": "{\"creator\":[\"b\"]}",
"creator": ["ac", "b"],
"title": "wind"
}, 'Get document');
o.jio.get({"_id": "{\"creator\":[\"b\"]}"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
}); });
test("AllDocs", function () {
var o = generateTools(this);
o.localstorage_spec = {
"type": "local",
"username": "one",
"application_name": "gid storage allDocs test"
};
// local jio is going to help us to prepare localstorage for gid tests
o.local_jio = jIO.newJio(o.localstorage_spec);
o.jio = jIO.newJio({
"type": "gid",
"sub_storage": o.localstorage_spec,
"constraints": {
"default": {
"creator": "list"
} }
function getCreatedDocumentTest(answer) {
deepEqual(answer, {
"data": {
"_id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}',
"created": "2013-10-10",
"title": "Unique ID",
"type": "Text"
},
"id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}',
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get new document");
} }
function postSpecificDocument() {
return jio.post({
"_id": '{"created":"2013-10-10","title":"Bee","type":"Text"}',
"created": "2013-10-10",
"title": "Bee",
"type": "Text"
}); });
}
// preparing localstorage with documents function postSpecificDocumentTest(answer) {
o.local_jio.put({"_id": "green", "creator": ["a"], "title": "earth"}); deepEqual(answer, {
o.local_jio.put({"_id": "red", "creator": ["a", "b"], "title": "water"}); "id": '{"created":"2013-10-10","title":"Bee","type":"Text"}',
o.local_jio.put({"_id": "yellow", "creator": ["c", "d"], "title": "wind"}); "method": "post",
o.local_jio.put({"_id": "purple", "creator": ["s", "d"], "title": "fire"}); "result": "success",
o.local_jio.put({"_id": "blue", "title": "space"}); "status": 201,
o.clock.tick(3000); "statusText": "Created"
}, "Post specific document");
}
util.closeAndcleanUpJio(o.local_jio); function listDocuments() {
return jio.allDocs();
}
// Get all document and sort to make comparison easier function list2DocumentsTest(answer) {
o.spy(o, 'value', { if (answer && answer.data && Array.isArray(answer.data.rows)) {
answer.data.rows.sort(function (a) {
return a.id === "b" ? 1 : 0;
});
}
deepEqual(answer, {
"data": {
"total_rows": 2,
"rows": [{ "rows": [{
"id": "{\"creator\":[\"a\"]}", "id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}',
"value": {} "value": {}
}, { }, {
"id": "{\"creator\":[\"a\",\"b\"]}", "id": '{"created":"2013-10-10","title":"Bee","type":"Text"}',
"value": {} "value": {}
}, { }]
"id": "{\"creator\":[\"c\",\"d\"]}", },
"value": {} "method": "allDocs",
}, { "result": "success",
"id": "{\"creator\":[\"s\",\"d\"]}", "status": 200,
"value": {} "statusText": "Ok"
}], }, "List 2 documents");
"total_rows": 4 }
}, 'Get all docs');
o.jio.allDocs({ function removeCreatedDocument() {
"sort_on": [["creator", "ascending"]] return jio.remove({
}, o.f); "_id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}'
o.tick(o);
// Get all document with complex queries
o.spy(o, 'value', {
"rows": [{
"id": "{\"creator\":[\"s\",\"d\"]}",
"value": {"creator": ["s", "d"]}
}],
"total_rows": 1
}, 'Get all docs with complex query');
o.jio.allDocs({
"query": 'creator: "d"',
"select_list": ["creator"],
"limit": [1, 1],
"sort_on": [["creator", "ascending"]]
}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
}); });
}
test("Put", function () { function removeCreatedDocumentTest(answer) {
var o = generateTools(this); deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Unique ID","type":"Text"}',
"method": "remove",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Remove first document.");
}
o.localstorage_spec = { function removeSpecificDocument() {
"type": "local", return jio.remove({
"username": "one", "_id": '{"created":"2013-10-10","title":"Bee","type":"Text"}'
"application_name": "gid storage put test" });
}; }
// local jio is going to help us to prepare localstorage for gid tests function removeSpecificDocumentTest(answer) {
o.local_jio = jIO.newJio(o.localstorage_spec); deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Bee","type":"Text"}',
"method": "remove",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Remove second document.");
}
o.jio = jIO.newJio({ function listEmptyStorage() {
"type": "gid", return jio.allDocs();
"sub_storage": o.localstorage_spec,
"constraints": {
"default": {
"creator": "list"
} }
function listEmptyStorageTest(answer) {
deepEqual(answer, {
"data": {
"total_rows": 0,
"rows": []
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "List empty storage");
} }
});
// preparing localstorage with documents function putNewDocument() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.put({
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
o.clock.tick(2000); "created": "2013-10-10",
"title": "Hey",
// Fail to put document because id does not respect constraints "type": "Text"
o.spy(o, 'status', 400, 'Put document without respecting constraints ' +
'-> bad request');
o.jio.put({"_id": "a", "creator": "a", "title": "fire"}, o.f);
o.tick(o);
// Fail to put because gid given != gid generated by the constraints
o.spy(o, 'status', 400, 'Put document without respecting constraints ' +
'-> bad request');
o.jio.put({
"_id": "{\"creator\":[\"a\"]}",
"creator": "b",
"title": "water"
}, o.f);
o.tick(o);
// Succeed to update a document with its gid
o.spy(o, 'value', {
"ok": true,
"id": "{\"creator\":[\"a\"]}"
}, 'Update document');
o.jio.put({
"_id": "{\"creator\":[\"a\"]}",
"creator": "a",
"title": "space"
}, o.f);
o.tick(o);
// Succeed to create a document, the gid given is good
o.spy(o, 'value', {
"ok": true,
"id": "{\"creator\":[\"c\"]}"
}, 'Create document');
o.jio.put({
"_id": "{\"creator\":[\"c\"]}",
"creator": "c",
"title": "magma"
}, o.f);
o.tick(o);
// Check if the local storage document is well updated to make sure the
// second put did not update the wrong document.
o.spy(o, 'value', {
"_id": "blue",
"creator": "a",
"title": "space"
}, "Check sub documents");
o.local_jio.get({"_id": "blue"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.local_jio);
util.closeAndcleanUpJio(o.jio);
}); });
}
test("Remove", function () { function putNewDocumentTest(answer) {
var o = generateTools(this); deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "put",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Put new document");
}
o.localstorage_spec = { function getCreatedDocument2() {
"type": "local", return jio.get({
"username": "one", "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
"application_name": "gid storage remove test" });
}; }
// local jio is going to help us to prepare localstorage for gid tests function getCreatedDocument2Test(answer) {
o.local_jio = jIO.newJio(o.localstorage_spec); deepEqual(answer, {
"data": {
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"created": "2013-10-10",
"title": "Hey",
"type": "Text"
},
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get new document");
}
o.jio = jIO.newJio({ function postSameDocument() {
"type": "gid", return success(jio.post({
"sub_storage": o.localstorage_spec, "created": "2013-10-10",
"constraints": { "title": "Hey",
"default": { "type": "Text"
"creator": "list" }));
} }
function postSameDocumentTest(answer) {
deepEqual(answer, {
"error": "conflict",
"message": "Cannot post document",
"method": "post",
"reason": "Document already exists",
"result": "error",
"status": 409,
"statusText": "Conflict"
}, "Unable to post the same document (conflict)");
} }
function createAttachment() {
return jio.putAttachment({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_attachment": "aa",
"_data": "aaa",
"_content_type": "text/plain"
}); });
}
function createAttachmentTest(answer) {
deepEqual(answer, {
"attachment": "aa",
"digest": "sha256-9834876dcfb05cb167a5c24953eba58c4" +
"ac89b1adf57f28f2f9d09af107ee8f0",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "putAttachment",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Create new attachment");
}
// preparing localstorage with documents function updateAttachment() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.putAttachment({
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
o.clock.tick(2000); "_attachment": "aa",
"_data": "aab",
util.closeAndcleanUpJio(o.local_jio); "_content_type": "text/plain"
// Fail to remove document because given gid does not respect constraints
o.spy(o, 'status', 400, 'Remove document without respecting constraints ' +
'-> bad request');
o.jio.remove({"_id": "a"}, o.f);
o.tick(o);
// Succeed to remove
o.spy(o, 'value', {
"ok": true,
"id": "{\"creator\":[\"b\"]}"
}, 'Remove document');
o.jio.remove({
"_id": "{\"creator\":[\"b\"]}"
}, o.f);
o.tick(o);
// Fail to remove the same document. This test checks also that only one
// document matches the gid constraints
o.spy(o, 'status', 404, 'Remove inexistent document');
o.jio.remove({
"_id": "{\"creator\":[\"b\"]}"
}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio);
}); });
}
test("putAttachment", function () { function updateAttachmentTest(answer) {
var o = generateTools(this); deepEqual(answer, {
"attachment": "aa",
"digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
"0728e095ff24218119d51bd22475363",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "putAttachment",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Update last attachment");
}
o.localstorage_spec = { function createAnotherAttachment() {
"type": "local", return jio.putAttachment({
"username": "one", "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"application_name": "gid storage put attachment test" "_attachment": "ab",
}; "_data": "aba",
"_content_type": "text/plain"
});
}
// local jio is going to help us to prepare localstorage for gid tests function createAnotherAttachmentTest(answer) {
o.local_jio = jIO.newJio(o.localstorage_spec); deepEqual(answer, {
"attachment": "ab",
"digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
"ed343e6c739e54131fcb3a56e4bc1bd",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "putAttachment",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Create another attachment");
}
o.jio = jIO.newJio({ function updateLastDocument() {
"type": "gid", return jio.put({
"sub_storage": o.localstorage_spec, "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"constraints": { "created": "2013-10-10",
"default": { "title": "Hey",
"creator": "list" "type": "Text",
"modified": "2013-10-11"
});
} }
function updateLastDocumentTest(answer) {
deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "put",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Update document metadata");
} }
function getFirstAttachment() {
return jio.getAttachment({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_attachment": "aa"
}); });
}
// preparing localstorage with documents function getFirstAttachmentTest(answer) {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); var blob = answer.data;
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); answer.data = "<blob>";
o.clock.tick(2000); deepEqual(answer, {
"attachment": "aa",
// Fail to put attachment because given gid doesn't respect constraints "data": "<blob>",
o.spy(o, 'status', 400, 'put attachment without respecting constraints ' + "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
'-> bad request'); "0728e095ff24218119d51bd22475363",
o.jio.putAttachment({ "id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_id": "a", "method": "getAttachment",
"_attachment": "body", "result": "success",
"_data": "abc", "status": 200,
"_mimetype": "text/plain" "statusText": "Ok"
}, o.f); }, "Get first attachment");
o.tick(o); return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
// Succeed to put an attachment to a document deepEqual(e.target.result, "aab", "Check blob text content");
o.spy(o, 'value', { }, function (err) {
"ok": true, deepEqual(err, "no error", "Check blob text content");
"id": "{\"creator\":[\"b\"]}",
"attachment": "body"
}, 'put attachment');
o.jio.putAttachment({
"_id": "{\"creator\":[\"b\"]}",
"_attachment": "body",
"_data": "abc",
"_mimetype": "text/plain"
}, o.f);
o.tick(o);
// Check if the local storage document really have the new attachment
o.spy(o, 'value', "abc", "Check attachment");
o.local_jio.getAttachment({"_id": "green", "_attachment": "body"}, o.f);
o.tick(o);
// Succeed to update an attachment
o.spy(o, 'value', {
"ok": true,
"id": "{\"creator\":[\"b\"]}",
"attachment": "body"
}, 'put attachment');
o.jio.putAttachment({
"_id": "{\"creator\":[\"b\"]}",
"_attachment": "body",
"_data": "def",
"_mimetype": "text/plain"
}, o.f);
o.tick(o);
// Check if the local storage attachment really changed
o.spy(o, 'value', "def", "Check attachment");
o.local_jio.getAttachment({"_id": "green", "_attachment": "body"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.local_jio);
util.closeAndcleanUpJio(o.jio);
}); });
}
test("getAttachment", function () { function getSecondAttachment() {
var o = generateTools(this); return jio.getAttachment({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_attachment": "ab"
});
}
o.localstorage_spec = { function getSecondAttachmentTest(answer) {
"type": "local", var blob = answer.data;
"username": "one", answer.data = "<blob>";
"application_name": "gid storage get attachment test" deepEqual(answer, {
}; "attachment": "ab",
"data": "<blob>",
"digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
"ed343e6c739e54131fcb3a56e4bc1bd",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get first attachment");
return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
deepEqual(e.target.result, "aba", "Check blob text content");
}, function (err) {
deepEqual(err, "no error", "Check blob text content");
});
}
// local jio is going to help us to prepare localstorage for gid tests function getLastDocument() {
o.local_jio = jIO.newJio(o.localstorage_spec); return jio.get({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
});
}
o.jio = jIO.newJio({ function getLastDocumentTest(answer) {
"type": "gid", deepEqual(answer, {
"sub_storage": o.localstorage_spec, "data": {
"constraints": { "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"default": { "created": "2013-10-10",
"creator": "list" "modified": "2013-10-11",
"title": "Hey",
"type": "Text",
"_attachments": {
"aa": {
"content_type": "text/plain",
"digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
"0728e095ff24218119d51bd22475363",
"length": 3
},
"ab": {
"content_type": "text/plain",
"digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
"ed343e6c739e54131fcb3a56e4bc1bd",
"length": 3
} }
} }
}); },
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get last document metadata");
}
// preparing localstorage with documents function removeSecondAttachment() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.removeAttachment({
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
o.clock.tick(2000); "_attachment": "ab"
// Fail to get attachment because given gid doesn't respect constraints
o.spy(o, 'status', 400, 'get attachment without respecting constraints ' +
'-> bad request');
o.jio.getAttachment({
"_id": "a",
"_attachment": "body"
}, o.f);
o.tick(o);
// Fail to get an inexistent attachment from a document
o.spy(o, 'status', 404, 'Get inexistent attachment');
o.jio.getAttachment({
"_id": "{\"creator\":[\"a\"]}",
"_attachment": "body"
}, o.f);
o.tick(o);
// Add an attachment manually to the document 'blue'
o.local_jio.putAttachment({
"_id": "blue",
"_attachment": "body",
"_data": "lol",
"_mimetype": "text/plain"
}); });
o.clock.tick(2000); }
util.closeAndcleanUpJio(o.local_jio);
function removeSecondAttachmentTest(answer) {
// Succeed to get the previous attachment deepEqual(answer, {
o.spy(o, 'value', "lol", 'Get attachment'); "attachment": "ab",
o.jio.getAttachment({ "id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_id": "{\"creator\":[\"a\"]}", "method": "removeAttachment",
"_attachment": "body" "result": "success",
}, o.f); "status": 204,
o.tick(o); "statusText": "No Content"
}, "Remove second document");
util.closeAndcleanUpJio(o.jio); }
function getInexistentSecondAttachment() {
return success(jio.getAttachment({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_attachment": "ab"
}));
}
function getInexistentSecondAttachmentTest(answer) {
deepEqual(answer, {
"attachment": "ab",
"error": "not_found",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"message": "Cannot get attachment",
"method": "getAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get inexistent second attachment");
}
function getOneAttachmentDocument() {
return jio.get({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
}); });
}
test("removeAttachment", function () { function getOneAttachmentDocumentTest(answer) {
var o = generateTools(this); deepEqual(answer, {
"data": {
"_attachments": {
"aa": {
"content_type": "text/plain",
"digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
"0728e095ff24218119d51bd22475363",
"length": 3
}
},
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"created": "2013-10-10",
"modified": "2013-10-11",
"title": "Hey",
"type": "Text"
},
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get document metadata");
}
o.localstorage_spec = { function removeSecondAttachmentAgain() {
"type": "local", return success(jio.removeAttachment({
"username": "one", "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"application_name": "gid storage remove attachment test" "_attachment": "ab"
}; }));
}
// local jio is going to help us to prepare localstorage for gid tests function removeSecondAttachmentAgainTest(answer) {
o.local_jio = jIO.newJio(o.localstorage_spec); deepEqual(answer, {
"attachment": "ab",
"error": "not_found",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"message": "Cannot remove attachment",
"method": "removeAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove inexistent attachment");
}
o.jio = jIO.newJio({ function getInexistentDocument() {
"type": "gid", return success(jio.get({
"sub_storage": o.localstorage_spec, "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
"constraints": { }));
"default": {
"creator": "list"
} }
function getInexistentDocumentTest(answer) {
deepEqual(answer, {
"error": "not_found",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"message": "Cannot get document",
"method": "get",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get inexistent document");
} }
});
// preparing localstorage with documents function removeDocument() {
o.local_jio.put({"_id": "blue", "creator": "a", "title": "earth"}); return jio.remove({
o.local_jio.put({"_id": "green", "creator": ["ac", "b"], "title": "wind"}); "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
o.clock.tick(2000);
o.local_jio.putAttachment({
"_id": "blue",
"_attachment": "body",
"_data": "lol",
"_mimetype": "text/plain"
});
o.clock.tick(2000);
// Fail to remove attachment because given gid doesn't respect constraints
o.spy(o, 'status', 400, 'Remove attachment without respecting ' +
'constraints -> bad request');
o.jio.removeAttachment({
"_id": "a",
"_attachment": "body",
"_data": "abc",
"_mimetype": "text/plain"
}, o.f);
o.tick(o);
// Succeed to remove an attachment from a document
o.spy(o, 'value', {
"ok": true,
"id": "{\"creator\":[\"a\"]}",
"attachment": "body"
}, 'Remove attachment');
o.jio.removeAttachment({
"_id": "{\"creator\":[\"a\"]}",
"_attachment": "body"
}, o.f);
o.tick(o);
// Check if the local storage document doesn't have attachment anymore
o.spy(o, 'status', 404, "Check attachment");
o.local_jio.getAttachment({"_id": "green", "_attachment": "body"}, o.f);
o.tick(o);
// Fail to remove the same attachment because it's already removed
o.spy(o, 'status', 404, 'Remove attachment');
o.jio.removeAttachment({
"_id": "{\"creator\":[\"b\"]}",
"_attachment": "body",
"_data": "def",
"_mimetype": "text/plain"
}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.local_jio);
util.closeAndcleanUpJio(o.jio);
}); });
}
test("More Constraints", function () { function removeDocumentTest(answer) {
// This test will use gid storage in a 'real case' deepEqual(answer, {
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"method": "remove",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Remove document and its attachments");
}
var o = generateTools(this); function getInexistentFirstAttachment() {
return success(jio.getAttachment({
"_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"_attachment": "aa"
}));
}
o.localstorage_spec = { function getInexistentFirstAttachmentTest(answer) {
"type": "local", deepEqual(answer, {
"username": "one", "attachment": "aa",
"application_name": "gid storage more constraints test" "error": "not_found",
}; "id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"message": "Cannot get attachment",
"method": "getAttachment",
"reason": "Document already exists",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get inexistent first attachment");
}
o.jio = jIO.newJio({ function removeInexistentDocument() {
"type": "gid", return success(jio.remove({
"sub_storage": o.localstorage_spec, "_id": '{"created":"2013-10-10","title":"Hey","type":"Text"}'
"constraints": { }));
"default": {
"type": "DCMIType",
"title": "string"
},
"Text": {
"date": "date",
"language": "string"
},
"Image": {
"format": "contentType"
} }
function removeInexistentDocumentTest(answer) {
deepEqual(answer, {
"error": "not_found",
"id": '{"created":"2013-10-10","title":"Hey","type":"Text"}',
"message": "Cannot remove document",
"method": "remove",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove already removed document");
} }
});
// Post a text document. This test also checks if the gid is well function unexpectedError(error) {
// created. Indeed, the json string "id" of the response is a dict with keys if (error instanceof Error) {
// inserted in alphabetic order, so that a gid is universal. It also checks deepEqual([
// document types list management. It checks 'string', 'DCMIType' and 'date' error.name + ": " + error.message,
// metadata types. error
o.spy(o, 'value', { ], "UNEXPECTED ERROR", "Unexpected error");
"ok": true, } else {
"id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," + deepEqual(error, "UNEXPECTED ERROR", "Unexpected error");
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}"
}, 'Post a text document');
o.jio.post({
"type": ["Text", "web page"],
"title": {"lang": "fr", "content": "Texte pour ce test"},
"date": "2012-12-12",
"modified": "2012-12-12",
"format": "text/html",
"language": "fr"
}, o.f);
o.tick(o);
// Put the associated attachment
o.spy(o, 'value', {
"ok": true,
"id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," +
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}",
"attachment": "body"
}, 'Put text content as body');
o.jio.putAttachment({
"_id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," +
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}",
"_attachment": "body",
"_data": "<h1>Mon document html.</h1>",
"_mimetype": "text/html"
}, o.f);
o.tick(o);
// Post an image. It checks 'string', 'DCMIType' and 'contentType' metadata
// types.
o.spy(o, 'value', {
"ok": true,
"id": "{\"format\":\"text/svg+xml\"," +
"\"title\":\"My image title\",\"type\":\"Image\"}"
}, 'Post an image document');
o.jio.post({
"type": "Image",
"title": "My image title",
"date": "2012-12-13",
"modified": "2012-12-13",
"format": "text/svg+xml"
}, o.f);
o.tick(o);
// Put the associated attachment
o.spy(o, 'value', {
"ok": true,
"id": "{\"format\":\"text/svg+xml\"," +
"\"title\":\"My image title\",\"type\":\"Image\"}",
"attachment": "body"
}, 'Put text content as body');
o.jio.putAttachment({
"_id": "{\"format\":\"text/svg+xml\"," +
"\"title\":\"My image title\",\"type\":\"Image\"}",
"_attachment": "body",
"_data": "<svg/>",
"_mimetype": "text/svg+xml"
}, o.f);
o.tick(o);
// Get the html document
o.spy(o, 'value', {
"_id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," +
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}",
"type": ["Text", "web page"],
"title": {"lang": "fr", "content": "Texte pour ce test"},
"date": "2012-12-12",
"modified": "2012-12-12",
"format": "text/html",
"language": "fr",
"_attachments": {
"body": {
"length": 27,
"digest": "md5-6f40c762ca7a8fac52567f12ce5441ef",
"content_type": "text/html"
} }
} }
}, "Get html metadata");
o.jio.get({
"_id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," +
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}",
}, o.f);
o.tick(o);
// Get a list of documents stop();
o.spy(o, 'value', {
"rows": [{ // # Post new documents, list them and remove them
"id": "{\"format\":\"text/svg+xml\"," + // post a 201
"\"title\":\"My image title\",\"type\":\"Image\"}", postNewDocument().then(postNewDocumentTest).
"value": {} // get 200
}, { then(getCreatedDocument).then(getCreatedDocumentTest).
"id": "{\"date\":\"2012-12-12\",\"language\":\"fr\"," + // post b 201
"\"title\":\"Texte pour ce test\",\"type\":\"Text\"}", then(postSpecificDocument).then(postSpecificDocumentTest).
"value": {} // // check b 204
}], // then(checkDocument).then(checkDocumentTest).
"total_rows": 2 // // check storage 204
}, 'Get a document list'); // then(checkStorage).then(checkStorageTest).
o.jio.allDocs({"sort_on": [["title", "ascending"]]}, o.f); // allD 200 2 documents
o.tick(o); then(listDocuments).then(list2DocumentsTest).
// remove a 204
then(removeCreatedDocument).then(removeCreatedDocumentTest).
// remove b 204
then(removeSpecificDocument).then(removeSpecificDocumentTest).
// allD 200 empty storage
then(listEmptyStorage).then(listEmptyStorageTest).
// # Create and update documents, and some attachment and remove them
// put 201
then(putNewDocument).then(putNewDocumentTest).
// get 200
then(getCreatedDocument2).then(getCreatedDocument2Test).
// post 409
then(postSameDocument).then(postSameDocumentTest).
// putA a 204
then(createAttachment).then(createAttachmentTest).
// putA a 204
then(updateAttachment).then(updateAttachmentTest).
// putA b 204
then(createAnotherAttachment).then(createAnotherAttachmentTest).
// put 204
then(updateLastDocument).then(updateLastDocumentTest).
// getA a 200
then(getFirstAttachment).then(getFirstAttachmentTest).
// getA b 200
then(getSecondAttachment).then(getSecondAttachmentTest).
// get 200
then(getLastDocument).then(getLastDocumentTest).
// removeA b 204
then(removeSecondAttachment).then(removeSecondAttachmentTest).
// getA b 404
then(getInexistentSecondAttachment).
then(getInexistentSecondAttachmentTest).
// get 200
then(getOneAttachmentDocument).then(getOneAttachmentDocumentTest).
// removeA b 404
then(removeSecondAttachmentAgain).then(removeSecondAttachmentAgainTest).
// remove 204
then(removeDocument).then(removeDocumentTest).
// getA a 404
then(getInexistentFirstAttachment).then(getInexistentFirstAttachmentTest).
// get 404
then(getInexistentDocument).then(getInexistentDocumentTest).
// remove 404
then(removeInexistentDocument).then(removeInexistentDocumentTest).
// end
fail(unexpectedError).
always(start);
util.closeAndcleanUpJio(o.jio);
}); });
})); }));
...@@ -29,5 +29,8 @@ ...@@ -29,5 +29,8 @@
<script src="../src/jio.storage/indexstorage.js"></script> <script src="../src/jio.storage/indexstorage.js"></script>
<script src="jio.storage/indexstorage.tests.js"></script> <script src="jio.storage/indexstorage.tests.js"></script>
<script src="../src/jio.storage/gidstorage.js"></script>
<script src="../test/jio.storage/gidstorage.tests.js"></script>
</body> </body>
</html> </html>
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
"indexstorage": "../src/jio.storage/indexstorage", "indexstorage": "../src/jio.storage/indexstorage",
"indexstorage_tests": "jio.storage/indexstorage.tests", "indexstorage_tests": "jio.storage/indexstorage.tests",
"gidstorage": "../src/jio.storage/gidstorage",
"gidstorage_tests": "jio.storage/gidstorage.tests",
"qunit": "../lib/qunit/qunit", "qunit": "../lib/qunit/qunit",
"sinon": "../lib/sinon/sinon", "sinon": "../lib/sinon/sinon",
"sinon_qunit": "../lib/sinon/sinon-qunit" "sinon_qunit": "../lib/sinon/sinon-qunit"
...@@ -42,6 +45,7 @@ ...@@ -42,6 +45,7 @@
"complex_queries_tests", "complex_queries_tests",
"localstorage_tests", "localstorage_tests",
"davstorage_tests", "davstorage_tests",
"indexstorage_tests" "indexstorage_tests",
"gidstorage_tests"
]); ]);
}()); }());
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