Commit 08f52fff authored by Tristan Cavelier's avatar Tristan Cavelier

Unit tests updated

parent d7d986d1
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global window, define, module, test_util, jIO, local_storage, test, ok, /*global window, define, module, test_util, promy, jIO, local_storage, test, ok,
deepEqual, sinon, expect, stop, start, Blob */ deepEqual, sinon, expect, stop, start, Blob */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
...@@ -8,19 +8,67 @@ ...@@ -8,19 +8,67 @@
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
return define(dependencies, module); return define(dependencies, module);
} }
module(test_util, jIO, local_storage); module(test_util, promy, jIO, local_storage);
}([ }([
'test_util', 'test_util',
'promy',
'jio', 'jio',
'localstorage', 'localstorage',
'qunit' 'qunit'
], function (util, jIO, local_storage) { ], function (util, promy, jIO, local_storage) {
"use strict"; "use strict";
module("LocalStorage"); module("LocalStorage");
local_storage.clear(); local_storage.clear();
/**
* all(*promises): Promise
*
* Produces a promise that is resolved when all the given promises are
* fulfilled. The resolved value is an array of each of the answers of the
* given promises.
*
* @param {Promise} *[promises] The promises to use
* @return {Promise} A new promise
*/
function all() {
var promises, results = [], i, count = 0, deferred;
promises = Array.prototype.slice.call(arguments);
function cancel() {
var j;
for (j = 0; j < promises.length; j += 1) {
if (typeof promises[j].cancel === 'function') {
promises[j].cancel();
}
}
}
deferred = new promy.Deferred(cancel);
function succeed(j) {
return function (answer) {
results[j] = answer;
count += 1;
if (count !== promises.length) {
return;
}
deferred.resolve(results);
};
}
function notify(j) {
return function (answer) {
deferred.notify({
"promise": this,
"index": j,
"answer": answer
});
};
}
for (i = 0; i < promises.length; i += 1) {
promises[i].then(succeed(i), succeed(i), notify(i));
}
return deferred.promise;
}
test("Post & Get", function () { test("Post & Get", function () {
expect(6); expect(6);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
...@@ -33,14 +81,10 @@ ...@@ -33,14 +81,10 @@
stop(); stop();
jIO.Promise.all([ all(
jIO.Promise.execute(function () {
// get inexistent document // get inexistent document
return jio.get({"_id": "inexistent"}); jio.get({"_id": "inexistent"}).always(function (answer) {
}).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"error": "not_found", "error": "not_found",
...@@ -55,12 +99,9 @@ ...@@ -55,12 +99,9 @@
}), }),
jIO.Promise.execute(function () {
// post without id // post without id
return jio.post({}); jio.post({}).always(function (answer) {
}).always(function (answer) {
var uuid = answer.id; var uuid = answer.id;
delete answer.id; delete answer.id;
deepEqual(answer, { deepEqual(answer, {
...@@ -126,7 +167,7 @@ ...@@ -126,7 +167,7 @@
}) })
]).always(start); ).always(start);
}); });
...@@ -142,12 +183,8 @@ ...@@ -142,12 +183,8 @@
stop(); stop();
jIO.Promise.execute(function () {
// put non empty document // put non empty document
return jio.put({"_id": "put1", "title": "myPut1"}); jio.put({"_id": "put1", "title": "myPut1"}).always(function (answer) {
}).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"id": "put1", "id": "put1",
...@@ -224,13 +261,12 @@ ...@@ -224,13 +261,12 @@
stop(); stop();
jIO.Promise.all([ all(
jIO.Promise.execute(function () {
// get an attachment from an inexistent document // get an attachment from an inexistent document
return jio.getAttachment({"_id": "inexistent", "_attachment": "a"}); jio.getAttachment({
"_id": "inexistent",
"_attachment": "a"
}).always(function (answer) { }).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
...@@ -247,12 +283,8 @@ ...@@ -247,12 +283,8 @@
}), }),
jIO.Promise.execute(function () {
// put a document then get an attachment from the empty document // put a document then get an attachment from the empty document
return jio.put({"_id": "b"}); jio.put({"_id": "b"}).then(function () {
}).then(function () {
return jio.getAttachment({"_id": "b", "_attachment": "inexistent"}); return jio.getAttachment({"_id": "b", "_attachment": "inexistent"});
...@@ -272,15 +304,11 @@ ...@@ -272,15 +304,11 @@
}), }),
jIO.Promise.execute(function () {
// put an attachment to an inexistent document // put an attachment to an inexistent document
return jio.putAttachment({ jio.putAttachment({
"_id": "inexistent", "_id": "inexistent",
"_attachment": "putattmt2", "_attachment": "putattmt2",
"_data": "" "_data": ""
});
}).always(function (answer) { }).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
...@@ -297,13 +325,9 @@ ...@@ -297,13 +325,9 @@
}), }),
jIO.Promise.execute(function () {
// add a document to the storage // add a document to the storage
// don't need to be tested // don't need to be tested
return jio.put({"_id": "putattmt1", "title": "myPutAttmt1"}); jio.put({"_id": "putattmt1", "title": "myPutAttmt1"}).then(function () {
}).then(function () {
return jio.putAttachment({ return jio.putAttachment({
"_id": "putattmt1", "_id": "putattmt1",
...@@ -327,10 +351,10 @@ ...@@ -327,10 +351,10 @@
}).then(function () { }).then(function () {
// check document and attachment // check document and attachment
return jIO.Promise.all([ return all(
jio.get({"_id": "putattmt1"}), jio.get({"_id": "putattmt1"}),
jio.getAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}) jio.getAttachment({"_id": "putattmt1", "_attachment": "putattmt2"})
]); );
// XXX check attachment with a getAttachment // XXX check attachment with a getAttachment
...@@ -372,7 +396,7 @@ ...@@ -372,7 +396,7 @@
}) })
]).always(start); ).always(start);
}); });
...@@ -388,11 +412,7 @@ ...@@ -388,11 +412,7 @@
stop(); stop();
jIO.Promise.execute(function () { jio.put({"_id": "a"}).then(function () {
return jio.put({"_id": "a"});
}).then(function () {
return jio.putAttachment({"_id": "a", "_attachment": "b", "_data": "c"}); return jio.putAttachment({"_id": "a", "_attachment": "b", "_data": "c"});
...@@ -414,10 +434,10 @@ ...@@ -414,10 +434,10 @@
}).then(function () { }).then(function () {
// Promise.all always return success // Promise.all always return success
return jIO.Promise.all([jio.removeAttachment({ return all(jio.removeAttachment({
"_id": "a", "_id": "a",
"_attachment": "b" "_attachment": "b"
})]); }));
}).always(function (answers) { }).always(function (answers) {
...@@ -480,13 +500,11 @@ ...@@ -480,13 +500,11 @@
stop(); stop();
jIO.Promise.execute(function () {
o.date_a = new Date(0); o.date_a = new Date(0);
o.date_b = new Date(); o.date_b = new Date();
// put some document before list them // put some document before list them
return jIO.Promise.all([ all(
jio.put({ jio.put({
"_id": "a", "_id": "a",
"title": "one", "title": "one",
...@@ -501,9 +519,7 @@ ...@@ -501,9 +519,7 @@
jio.put({"_id": "b", "title": "two", "date": o.date_a}), jio.put({"_id": "b", "title": "two", "date": o.date_a}),
jio.put({"_id": "c", "title": "one", "date": o.date_b}), jio.put({"_id": "c", "title": "one", "date": o.date_b}),
jio.put({"_id": "d", "title": "two", "date": o.date_b}) jio.put({"_id": "d", "title": "two", "date": o.date_b})
]); ).then(function () {
}).then(function () {
// get a list of documents // get a list of documents
return jio.allDocs(); return jio.allDocs();
...@@ -656,8 +672,6 @@ ...@@ -656,8 +672,6 @@
stop(); stop();
jIO.Promise.execute(function () {
o.putCorruptedDocuments = function () { o.putCorruptedDocuments = function () {
// put a document with a wrong attachment reference // put a document with a wrong attachment reference
util.json_local_storage.setItem( util.json_local_storage.setItem(
...@@ -685,14 +699,12 @@ ...@@ -685,14 +699,12 @@
); );
o.putCorruptedDocuments(); o.putCorruptedDocuments();
return jIO.Promise.all([ all(
jio.check({"_id": "war"}), jio.check({"_id": "war"}),
jio.check({"_id": "meta"}), jio.check({"_id": "meta"}),
jio.check({"_id": "cor"}), jio.check({"_id": "cor"}),
jio.check({"_id": "inexistent"}) jio.check({"_id": "inexistent"})
]); ).always(function (answers) {
}).always(function (answers) {
deepEqual(answers[0], { deepEqual(answers[0], {
"error": "conflict", "error": "conflict",
...@@ -737,12 +749,12 @@ ...@@ -737,12 +749,12 @@
}).then(function () { }).then(function () {
return jIO.Promise.all([ return all(
jio.repair({"_id": "war"}), jio.repair({"_id": "war"}),
jio.repair({"_id": "meta"}), jio.repair({"_id": "meta"}),
jio.repair({"_id": "cor"}), jio.repair({"_id": "cor"}),
jio.repair({"_id": "inexistent"}) jio.repair({"_id": "inexistent"})
]); );
}).always(function (answers) { }).always(function (answers) {
...@@ -781,12 +793,12 @@ ...@@ -781,12 +793,12 @@
}).then(function () { }).then(function () {
o.getCorruptedDocuments = function () { o.getCorruptedDocuments = function () {
return jIO.Promise.all([ return all(
jio.get({"_id": "war"}), jio.get({"_id": "war"}),
jio.get({"_id": "meta"}), jio.get({"_id": "meta"}),
jio.get({"_id": "cor"}), jio.get({"_id": "cor"}),
jio.get({"_id": "inexistent"}) jio.get({"_id": "inexistent"})
]); );
}; };
return o.getCorruptedDocuments(); return o.getCorruptedDocuments();
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<script src="../lib/qunit/qunit.js"></script> <script src="../lib/qunit/qunit.js"></script>
<script src="../lib/sinon/sinon.js"></script> <script src="../lib/sinon/sinon.js"></script>
<script src="../lib/sinon/sinon-qunit.js"></script> <script src="../lib/sinon/sinon-qunit.js"></script>
<script src="../src/promy/promy.js"></script>
<script src="../src/sha256.amd.js"></script> <script src="../src/sha256.amd.js"></script>
<script src="../jio.js"></script> <script src="../jio.js"></script>
<script src="jio/util.js"></script> <script src="jio/util.js"></script>
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
require.config({ require.config({
"paths": { "paths": {
"promy": "../src/promy/promy",
"sha256": "../src/sha256.amd", "sha256": "../src/sha256.amd",
"jio": "../jio", "jio": "../jio",
"jio_tests": "jio/tests", "jio_tests": "jio/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