Commit 9beab765 authored by Tristan Cavelier's avatar Tristan Cavelier

splitstorage unit tests added

parent 1a6a0a32
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, jIO, jio_tests, test, ok, deepEqual, sinon, module */ /*global define, jIO, test_util, RSVP, test, ok, deepEqual, module, stop,
start, hex_sha256 */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -7,226 +8,422 @@ ...@@ -7,226 +8,422 @@
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(jIO, test_util, RSVP);
}(['jio', 'jio_tests', 'localstorage', 'splitstorage'], function (jIO, util) { }([
'jio',
'test_util',
'rsvp',
'localstorage',
'splitstorage'
], function (jIO, util, RSVP) {
"use strict"; "use strict";
function generateTools() { var tool = {
return { "readBlobAsBinaryString": jIO.util.readBlobAsBinaryString
clock: sinon.useFakeTimers(),
spy: util.ospy,
tick: util.otick
}; };
function reverse(promise) {
return new RSVP.Promise(function (resolve, reject, notify) {
promise.then(reject, resolve, notify);
}, function () {
promise.cancel();
});
}
/**
* sequence(thens): Promise
*
* Executes a sequence of *then* callbacks. It acts like
* `smth().then(callback).then(callback)...`. The first callback is called
* with no parameter.
*
* Elements of `thens` array can be a function or an array contaning at most
* three *then* callbacks: *onFulfilled*, *onRejected*, *onNotified*.
*
* When `cancel()` is executed, each then promises are cancelled at the same
* time.
*
* @param {Array} thens An array of *then* callbacks
* @return {Promise} A new promise
*/
function sequence(thens) {
var promises = [];
return new RSVP.Promise(function (resolve, reject, notify) {
var i;
promises[0] = new RSVP.Promise(function (resolve) {
resolve();
});
for (i = 0; i < thens.length; i += 1) {
if (Array.isArray(thens[i])) {
promises[i + 1] = promises[i].
then(thens[i][0], thens[i][1], thens[i][2]);
} else {
promises[i + 1] = promises[i].then(thens[i]);
}
}
promises[i].then(resolve, reject, notify);
}, function () {
var i;
for (i = 0; i < promises.length; i += 1) {
promises[i].cancel();
}
});
}
function unexpectedError(error) {
if (error instanceof Error) {
deepEqual([
error.name + ": " + error.message,
error
], "UNEXPECTED ERROR", "Unexpected error");
} else {
deepEqual(error, "UNEXPECTED ERROR", "Unexpected error");
}
} }
module("SplitStorage + LocalStorage"); module("SplitStorage + LocalStorage");
test("Post", function () { test("Post", function () {
var o = generateTools(); var shared = {}, jio, jio_local_list = [];
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "post1" "application_name": "post1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "post2" "application_name": "post2"
}] };
jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
jio_local_list[0] = jIO.createJIO(shared.local_storage_description1, {
"workspace": shared.workspace
}); });
jio_local_list[1] = jIO.createJIO(shared.local_storage_description2, {
"workspace": shared.workspace
});
jio_local_list.run = function (method, argument) {
var i, promises = [];
for (i = 0; i < this.length; i += 1) {
promises[i] = this[i][method].apply(this[i], argument);
}
return RSVP.all(promises);
};
jio_local_list.get = function () {
return this.run("get", arguments);
};
stop();
// post without id // post without id
o.spy(o, "jobstatus", "done", "Post document without id"); jio.post({
o.jio.post({
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data" "meta": "data"
}, function (err, response) { }).then(function (answer) {
o.f(err, response); shared.uuid = answer.id;
o.uuid = (err || response).id; answer.id = "<uuid>";
ok(util.isUuid(o.uuid), "Uuid should look like " + ok(util.isUuid(shared.uuid), "Uuid should look like " +
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + o.uuid); "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + shared.uuid);
}); deepEqual(answer, {
o.tick(o); "id": "<uuid>",
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Post document without id");
// check uploaded documents // check uploaded documents
deepEqual(util.jsonlocalstorage.getItem( return jio_local_list.get({"_id": shared.uuid});
'jio/localstorage/splitstorage/post1/' + o.uuid
), {
"_id": o.uuid,
"_underscored_meta": "uvalue",
"data": "{\"meta\""
}, "Check uploaded document in sub storage 1");
deepEqual(util.jsonlocalstorage.getItem( }).then(function (answers) {
'jio/localstorage/splitstorage/post2/' + o.uuid var i;
), { for (i = 0; i < answers.length; i += 1) {
"_id": o.uuid, deepEqual(answers[i].data, {
"_id": shared.uuid,
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"data": ":\"data\"}" "data": i === 0 ? "{\"meta\"" : ":\"data\"}"
}, "Check uploaded document in sub storage 2"); }, "Check uploaded document in sub storage " + (i + 1));
}
// post with id // post with id
o.spy(o, "value", {"ok": true, "id": "one"}, "Post document with id"); return jio.post({
o.jio.post({
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data", "meta": "data",
"hello": "world" "hello": "world"
}, o.f); });
o.tick(o);
}).then(function (answer) {
deepEqual(answer, {
"id": "one",
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, "Post document with id");
// check uploaded documents // check uploaded documents
deepEqual(util.jsonlocalstorage.getItem( return jio_local_list.get({"_id": "one"});
'jio/localstorage/splitstorage/post1/one'
), { }).then(function (answers) {
deepEqual(answers[0].data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"data": "{\"meta\":\"data\"," "data": "{\"meta\":\"data\","
}, "Check uploaded document in sub storage 1"); }, "Check uploaded document in sub storage 1");
deepEqual(util.jsonlocalstorage.getItem( deepEqual(answers[1].data, {
'jio/localstorage/splitstorage/post2/one'
), {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"data": "\"hello\":\"world\"}" "data": "\"hello\":\"world\"}"
}, "Check uploaded document in sub storage 2"); }, "Check uploaded document in sub storage 2");
// post with id // post with id
o.spy(o, "status", 409, "Post document with same id"); return reverse(jio.post({
o.jio.post({
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data", "meta": "data",
"hello": "world" "hello": "world"
}, o.f); }));
o.tick(o);
}).then(function (answer) {
deepEqual(answer, {
"error": "conflict",
"id": "one",
"message": "Unable to post document",
"method": "post",
"reason": "document exists",
"result": "error",
"status": 409,
"statusText": "Conflict"
}, "Post document with same id -> 409 Conflict");
}).fail(unexpectedError).always(start);
util.closeAndcleanUpJio(o.jio);
}); });
test("PutAttachment", function () { test("PutAttachment", function () {
var o = generateTools(); var shared = {}, jio, jio_local_list = [];
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "putAttachment1" "application_name": "putAttachment1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "putAttachment2" "application_name": "putAttachment2"
}] };
jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
jio_local_list[0] = jIO.createJIO(shared.local_storage_description1, {
"workspace": shared.workspace
});
jio_local_list[1] = jIO.createJIO(shared.local_storage_description2, {
"workspace": shared.workspace
});
jio_local_list.run = function (method, argument) {
var i, promises = [];
for (i = 0; i < this.length; i += 1) {
promises[i] = this[i][method].apply(this[i], argument);
}
return RSVP.all(promises);
};
jio_local_list.get = function () {
return this.run("get", arguments);
};
jio_local_list.getAttachmentAsBinaryString = function () {
return this.run("getAttachment", arguments).then(function (answers) {
var i, promises = [];
for (i = 0; i < answers.length; i += 1) {
promises[i] = tool.readBlobAsBinaryString(answers[i].data);
}
return RSVP.all(promises);
}); });
};
o.spy(o, "status", 404, "Put attachment on a inexistent document"); stop();
o.jio.putAttachment({
return reverse(jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_content_type": "text/plain"
}, o.f); })).then(function (answer) {
o.tick(o);
o.jio.post({"_id": "one", "_underscored_meta": "uvalue", "meta": "data"}); deepEqual(answer, {
o.clock.tick(1000); "attachment": "my_attachment",
"error": "not_found",
o.spy(o, "value", {
"ok": true,
"id": "one", "id": "one",
"attachment": "my_attachment" "message": "Unable to put attachment",
}, "Put attachment"); "method": "putAttachment",
o.jio.putAttachment({ "reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Put attachment on a inexistent document -> 404 Not Found");
return jio.post({
"_id": "one",
"_underscored_meta": "uvalue",
"meta": "data"
});
}).then(function () {
return jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_mimetype": "text/plain"
}, o.f); });
o.tick(o);
}).then(function (answer) {
deepEqual(answer, {
"attachment": "my_attachment",
"id": "one",
"method": "putAttachment",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Put attachment on a document");
// check uploaded documents // check uploaded documents
deepEqual(util.jsonlocalstorage.getItem( return jio_local_list.get({"_id": "one"});
'jio/localstorage/splitstorage/putAttachment1/one'
), { }).then(function (answers) {
"_id": "one",
"_underscored_meta": "uvalue", deepEqual(answers[0].data, {
"data": "{\"meta\"",
"_attachments": { "_attachments": {
"my_attachment": { "my_attachment": {
"length": 3, "content_type": "text/plain",
"digest": "md5-1b4686bc8ca15befdccb1da1dcb8c271", // md5("My ") "digest": "sha256-ebf2d770a6a2dfa135f6c81431f22fc3cbcde9ae" +
"content_type": "text/plain" "e52759ca9e520d4d964c1322", // sha256("My ")
} "length": 3
} }
}, "Check uploaded document in sub storage 1"); },
deepEqual(util.jsonlocalstorage.getItem(
'jio/localstorage/splitstorage/putAttachment2/one'
), {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"data": ":\"data\"}", "data": "{\"meta\""
}, "Check uploaded document in sub storage 1");
deepEqual(answers[1].data, {
"_attachments": { "_attachments": {
"my_attachment": { "my_attachment": {
"length": 4, "content_type": "text/plain",
"digest": "md5-f6068daa29dbb05a7ead1e3b5a48bbee", // md5("Data") "digest": "sha256-cec3a9b89b2e391393d0f68e4bc12a9fa6cf358b" +
"content_type": "text/plain" "3cdf79496dc442d52b8dd528", // sha256("Data")
} "length": 4
} }
},
"_id": "one",
"_underscored_meta": "uvalue",
"data": ":\"data\"}"
}, "Check uploaded document in sub storage 2"); }, "Check uploaded document in sub storage 2");
deepEqual(util.jsonlocalstorage.getItem( return jio_local_list.getAttachmentAsBinaryString({
'jio/localstorage/splitstorage/putAttachment1/one/my_attachment' "_id": "one",
), "My ", "Check uploaded document in sub storage 1"); "_attachment": "my_attachment"
});
}).then(function (events) {
deepEqual(util.jsonlocalstorage.getItem( deepEqual(events[0].target.result, "My ",
'jio/localstorage/splitstorage/putAttachment2/one/my_attachment' "Check uploaded document in sub storage 1");
), "Data", "Check uploaded document in sub storage 2"); deepEqual(events[1].target.result, "Data",
"Check uploaded document in sub storage 1");
}).fail(unexpectedError).always(start);
util.closeAndcleanUpJio(o.jio);
}); });
test("Get", function () { test("Get", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "get1" "application_name": "get1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "get2" "application_name": "get2"
}] };
jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
stop();
reverse(jio.get({"_id": "one"})).then(function (answer) {
deepEqual(answer, {
"error": "not_found",
"id": "one",
"message": "Unable to get document",
"method": "get",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get missing document");
return jio.post({
"_id": "one",
"_underscored_meta": "uvalue",
"meta": "data"
}); });
o.spy(o, "status", 404, "Get missing document"); }).then(function () {
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
o.jio.post({"_id": "one", "_underscored_meta": "uvalue", "meta": "data"}); return jio.get({"_id": "one"});
o.clock.tick(1000);
o.spy(o, "value", { }).then(function (answer) {
deepEqual(answer.data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data" "meta": "data"
}, "Get posted document"); }, "Get posted document");
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
o.jio.putAttachment({ return jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_content_type": "text/plain"
}); });
o.clock.tick(1000);
o.spy(o, "value", { }).then(function () {
return jio.get({"_id": "one"});
}).then(function (answer) {
deepEqual(answer.data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data", "meta": "data",
...@@ -237,241 +434,469 @@ ...@@ -237,241 +434,469 @@
} }
} }
}, "Get document with attachment informations"); }, "Get document with attachment informations");
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio); }).fail(unexpectedError).always(start);
}); });
test("GetAttachment", function () { test("GetAttachment", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "getAttachment1" "application_name": "getAttachment1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "getAttachment2" "application_name": "getAttachment2"
}] };
jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
stop();
reverse(jio.getAttachment({
"_id": "one",
"_attachment": "my_attachment"
})).then(function (answer) {
deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to get attachment",
"method": "getAttachment",
"reason": "missing document",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get attachment from missing document -> 404 Not Found");
return jio.post({
"_id": "one",
"_underscored_meta": "uvalue",
"meta": "data"
}); });
o.spy(o, "status", 404, "Get attachment from missing document"); }).then(function () {
o.jio.getAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o);
o.jio.post({"_id": "one", "_underscored_meta": "uvalue", "meta": "data"}); return reverse(jio.getAttachment({
o.clock.tick(1000); "_id": "one",
"_attachment": "my_attachment"
}));
o.spy(o, "status", 404, "Get missing attachment from document"); }).then(function (answer) {
o.jio.getAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o);
o.jio.putAttachment({ deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to get attachment",
"method": "getAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Get missing attachment from document");
return jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_mimetype": "text/plain"
}); });
o.clock.tick(1000);
o.spy(o, "value", "My Data", "Get attachment"); }).then(function () {
o.jio.getAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o); return jio.getAttachment({"_id": "one", "_attachment": "my_attachment"});
}).then(function (answer) {
return tool.readBlobAsBinaryString(answer.data);
}).then(function (event) {
deepEqual(event.target.result, "My Data", "Get attachment");
}).fail(unexpectedError).always(start);
util.closeAndcleanUpJio(o.jio);
}); });
test("removeAttachment", function () { test("RemoveAttachment", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({ shared.workspace = {};
"type": "split", shared.local_storage_description1 = {
"storage_list": [{
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "removeAttachment1" "application_name": "removeAttachment1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "removeAttachment2" "application_name": "removeAttachment2"
}] };
jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
stop();
reverse(jio.removeAttachment({
"_id": "one",
"_attachment": "my_attachment"
})).then(function (answer) {
deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to remove attachment",
"method": "removeAttachment",
"reason": "missing document",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove attachment from inexistent document -> 404 Not Found");
return jio.post({
"_id": "one",
"_underscored_meta": "uvalue",
"meta": "data"
}); });
o.spy(o, "status", 404, "Remove attachment from inexistent document"); }).then(function () {
o.jio.removeAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o);
o.jio.post({"_id": "one", "_underscored_meta": "uvalue", "meta": "data"}); return reverse(jio.removeAttachment({
o.clock.tick(1000); "_id": "one",
"_attachment": "my_attachment"
}));
o.spy(o, "status", 404, "Remove inexistent attachment"); }).then(function (answer) {
o.jio.removeAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o);
o.jio.putAttachment({ deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to remove attachment",
"method": "removeAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove inexistent attachment -> 404 Not Found");
return jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_mimetype": "text/plain"
}); });
o.clock.tick(1000);
o.spy(o, "value", { }).then(function () {
"ok": true,
return jio.removeAttachment({
"_id": "one",
"_attachment": "my_attachment"
});
}).then(function (answer) {
deepEqual(answer, {
"attachment": "my_attachment",
"id": "one", "id": "one",
"attachment": "my_attachment" "method": "removeAttachment",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Remove attachment"); }, "Remove attachment");
o.jio.removeAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f);
o.tick(o);
o.spy(o, "value", { return jio.get({"_id": "one"});
}).then(function (answer) {
deepEqual(answer.data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data" "meta": "data"
}, "Get document for check"); }, "Check document");
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
o.spy(o, "status", 404, "Get attachment for check"); return reverse(jio.getAttachment({
o.jio.getAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f); "_id": "one",
o.tick(o); "_attachment": "my_attachment"
}));
}).then(function (answer) {
deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to get attachment",
"method": "getAttachment",
"reason": "missing attachment",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Check attachment -> 404 Not Found");
}).fail(unexpectedError).always(start);
util.closeAndcleanUpJio(o.jio);
}); });
test("remove", function () { test("Remove", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "remove1" "application_name": "remove1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "remove2" "application_name": "remove2"
}] };
}); jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
stop();
reverse(jio.remove({"_id": "one"})).then(function (answer) {
o.spy(o, "status", 404, "Remove missing document"); deepEqual(answer, {
o.jio.remove({"_id": "one"}, o.f); "error": "not_found",
o.tick(o); "id": "one",
"message": "Unable to remove document",
"method": "remove",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Remove missing document -> 404 Not Found");
return jio.post({
"_id": "one",
"_underscored_meta": "uvalue",
"meta": "data"
});
o.jio.post({"_id": "one", "_underscored_meta": "uvalue", "meta": "data"}); }).then(function () {
o.clock.tick(1000);
o.jio.putAttachment({ return jio.putAttachment({
"_id": "one", "_id": "one",
"_attachment": "my_attachment", "_attachment": "my_attachment",
"_data": "My Data", "_data": "My Data",
"_mimetype": "text/plain" "_mimetype": "text/plain"
}); });
o.clock.tick(1000);
o.spy(o, "value", {"ok": true, "id": "one"}, "Remove document"); }).then(function () {
o.jio.remove({"_id": "one"}, o.f);
o.tick(o); return jio.remove({"_id": "one"});
}).then(function (answer) {
o.spy(o, "status", 404, "Get attachment for check"); deepEqual(answer, {
o.jio.getAttachment({"_id": "one", "_attachment": "my_attachment"}, o.f); "id": "one",
o.tick(o); "method": "remove",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Remove document");
return reverse(jio.getAttachment({
"_id": "one",
"_attachment": "my_attachment"
}));
o.spy(o, "status", 404, "Get document for check"); }).then(function (answer) {
o.jio.get({"_id": "one"}, o.f);
o.tick(o); deepEqual(answer, {
"attachment": "my_attachment",
"error": "not_found",
"id": "one",
"message": "Unable to get attachment",
"method": "getAttachment",
"reason": "missing document",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Check attachment -> 404 Not Found");
return reverse(jio.get({"_id": "one"}));
}).then(function (answer) {
deepEqual(answer, {
"error": "not_found",
"id": "one",
"message": "Unable to get document",
"method": "get",
"reason": "missing",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "Check document -> 404 Not Found");
}).fail(unexpectedError).always(start);
util.closeAndcleanUpJio(o.jio);
}); });
test("Put", function () { test("Put", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "put1" "application_name": "put1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "put2" "application_name": "put2"
}] };
}); jio = jIO.createJIO({
"type": "split",
"storage_list": [
shared.local_storage_description1,
shared.local_storage_description2
]
}, {"workspace": shared.workspace});
o.spy(o, "value", {"ok": true, "id": "one"}, "Put document"); stop();
o.jio.put({
jio.put({
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data" "meta": "data"
}, o.f); }).then(function (answer) {
o.tick(o);
deepEqual(answer, {
"id": "one",
"method": "put",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Put new document");
return jio.get({"_id": "one"});
o.spy(o, "value", { }).then(function (answer) {
deepEqual(answer.data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meta": "data" "meta": "data"
}, "Get document for check"); }, "Check document");
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
o.spy(o, "value", {"ok": true, "id": "one"}, "Put document again"); return jio.put({
o.jio.put({
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meow": "dog" "meow": "dog"
}, o.f); });
o.tick(o);
o.spy(o, "value", { }).then(function (answer) {
deepEqual(answer, {
"id": "one",
"method": "put",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Put same document again");
return jio.get({"_id": "one"});
}).then(function (answer) {
deepEqual(answer.data, {
"_id": "one", "_id": "one",
"_underscored_meta": "uvalue", "_underscored_meta": "uvalue",
"meow": "dog" "meow": "dog"
}, "Get document for check"); }, "Get document for check");
o.jio.get({"_id": "one"}, o.f);
o.tick(o);
util.closeAndcleanUpJio(o.jio); }).fail(unexpectedError).always(start);
}); });
test("AllDocs", function () { test("AllDocs", function () {
var o = generateTools(); var shared = {}, jio;
o.jio = jIO.newJio({
"type": "split", shared.workspace = {};
"storage_list": [{ shared.local_storage_description1 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "alldocs1" "application_name": "alldocs1"
}, { };
shared.local_storage_description2 = {
"type": "local", "type": "local",
"username": "splitstorage", "username": "splitstorage",
"application_name": "alldocs2" "application_name": "alldocs2"
}] };
}); jio = jIO.createJIO({
"type": "split",
for (o.i = 0; o.i < 5; o.i += 1) { "storage_list": [
o.jio.post({ shared.local_storage_description1,
"_id": "doc" + o.i, shared.local_storage_description2
"_underscored_meta": "uvalue" + o.i, ]
"meta": "data" + o.i }, {"workspace": shared.workspace});
stop();
function prepareDatabase() {
var i, do_list = [];
function post(i) {
return function () {
return jio.post({
"_id": "doc" + i,
"_underscored_meta": "uvalue" + i,
"meta": "data" + i
}); });
o.clock.tick(1000); };
} }
function putAttachment(i) {
for (o.i = 0; o.i < 2; o.i += 1) { return function () {
o.jio.putAttachment({ return jio.putAttachment({
"_id": "doc" + o.i, "_id": "doc" + i,
"_attachment": "my_attachment" + o.i, "_attachment": "my_attachment" + i,
"_data": "My Data" + o.i, "_data": "My Data" + i,
"_mimetype": "text/plain" "_content_type": "text/plain"
}); });
o.clock.tick(1000); };
}
for (i = 0; i < 5; i += 1) {
do_list.push(post(i));
}
for (i = 0; i < 2; i += 1) {
do_list.push(putAttachment(i));
}
return sequence(do_list);
} }
o.spy(o, "value", { prepareDatabase().then(function () {
return jio.get({"_id": "doc1"});
}).then(function (answer) {
deepEqual(answer.data, {
"_id": "doc1", "_id": "doc1",
"_underscored_meta": "uvalue1", "_underscored_meta": "uvalue1",
"meta": "data1", "meta": "data1",
...@@ -481,11 +906,17 @@ ...@@ -481,11 +906,17 @@
"content_type": "text/plain" "content_type": "text/plain"
} }
} }
}, "Get document for check"); }, "Check document");
o.jio.get({"_id": "doc1"}, o.f);
o.tick(o); return jio.allDocs();
}).then(function (answer) {
answer.data.rows.sort(function (a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
});
o.spy(o, "value", { deepEqual(answer.data, {
"total_rows": 5, "total_rows": 5,
"rows": [{ "rows": [{
"id": "doc0", "id": "doc0",
...@@ -509,17 +940,9 @@ ...@@ -509,17 +940,9 @@
"value": {} "value": {}
}] }]
}, "AllDocs with document ids only"); }, "AllDocs with document ids only");
o.jio.allDocs(function (err, response) {
if (response && Array.isArray(response.rows)) {
response.rows.sort(function (a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
});
}
o.f(err, response);
});
o.tick(o);
util.closeAndcleanUpJio(o.jio); }).fail(unexpectedError).always(start);
}); });
})); }));
...@@ -38,5 +38,8 @@ ...@@ -38,5 +38,8 @@
<script src="../src/jio.storage/replicaterevisionstorage.js"></script> <script src="../src/jio.storage/replicaterevisionstorage.js"></script>
<script src="../test/jio.storage/replicaterevisionstorage.tests.js"></script> <script src="../test/jio.storage/replicaterevisionstorage.tests.js"></script>
<script src="../src/jio.storage/splitstorage.js"></script>
<script src="../test/jio.storage/splitstorage.tests.js"></script>
</body> </body>
</html> </html>
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
"replicaterevisionstorage_tests": "replicaterevisionstorage_tests":
"jio.storage/replicaterevisionstorage.tests", "jio.storage/replicaterevisionstorage.tests",
"splitstorage": "../src/jio.storage/splitstorage",
"splitstorage_tests": "jio.storage/splitstorage.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"
...@@ -56,6 +59,7 @@ ...@@ -56,6 +59,7 @@
"indexstorage_tests", "indexstorage_tests",
"gidstorage_tests", "gidstorage_tests",
"revisionstorage_tests", "revisionstorage_tests",
"replicaterevisionstorage_tests" "replicaterevisionstorage_tests",
"splitstorage_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