Commit 891be93a authored by Tristan Cavelier's avatar Tristan Cavelier

jiotests updated

parent a42b4948
(function () { var thisfun = function(loader) { (function () { var thisfun = function(loader) {
var JIO = loader.JIO, var JIO = loader.JIO,
LocalOrCookieStorage = loader.LocalOrCookieStorage,
sjcl = loader.sjcl, sjcl = loader.sjcl,
Base64 = loader.Base64, Base64 = loader.Base64,
$ = loader.jQuery; $ = loader.jQuery;
...@@ -19,12 +18,28 @@ contains = function (array,content) { ...@@ -19,12 +18,28 @@ contains = function (array,content) {
} }
return false; return false;
}, },
clean_up_local_storage_function = function(){ // localStorage wrapper
var k, storageObject = LocalOrCookieStorage.getAll(); localstorage = {
clear: function () {
return localStorage.clear();
},
getItem: function (item) {
var value = localStorage.getItem(item);
return value === null? null: JSON.parse(value);
},
setItem: function (item,value) {
return localStorage.setItem(item,JSON.stringify (value));
},
removeItem: function (item) {
return localStorage.removeItem(item);
}
},
cleanUpLocalStorage = function(){
var k, storageObject = localstorage.getAll();
for (k in storageObject) { for (k in storageObject) {
var splitk = k.split('/'); var splitk = k.split('/');
if ( splitk[0] === 'jio' ) { if ( splitk[0] === 'jio' ) {
LocalOrCookieStorage.deleteItem(k); localstorage.removeItem(k);
} }
} }
var d = document.createElement ('div'); var d = document.createElement ('div');
...@@ -34,11 +49,10 @@ clean_up_local_storage_function = function(){ ...@@ -34,11 +49,10 @@ clean_up_local_storage_function = function(){
localStorage.clear(); localStorage.clear();
}, },
base_tick = 30000, base_tick = 30000,
basic_test_function_generator = function(o,res,value,message) { basicTestFunctionGenerator = function(o,res,value,message) {
return function(err,val) { return function(err,val) {
var jobstatus = (err?'fail':'done'), var jobstatus = (err?'fail':'done');
val = ( isEmptyObject(value) && isUUID(val._id) ) ? {} : val;
switch (res) { switch (res) {
case 'status': case 'status':
...@@ -56,15 +70,15 @@ basic_test_function_generator = function(o,res,value,message) { ...@@ -56,15 +70,15 @@ basic_test_function_generator = function(o,res,value,message) {
deepEqual (val,value,message); deepEqual (val,value,message);
}; };
}, },
basic_spy_function = function(o,res,value,message,fun) { basicSpyFunction = function(o,res,value,message,fun) {
fun = fun || 'f'; fun = fun || 'f';
o[fun] = basic_test_function_generator(o,res,value,message); o[fun] = basicTestFunctionGenerator(o,res,value,message);
o.t.spy(o,fun); o.t.spy(o,fun);
}, },
basic_tick_function = function (o) { basicTickFunction = function (o) {
var tick, fun, i = 1; var tick, fun, i = 1;
tick = 1000; tick = 10000;
fun = fun || 'f'; fun = fun || 'f';
if (typeof arguments[i] === 'number') { if (typeof arguments[i] === 'number') {
...@@ -83,7 +97,7 @@ basic_tick_function = function (o) { ...@@ -83,7 +97,7 @@ basic_tick_function = function (o) {
} }
}, },
// debug function to show custumized log at the bottom of the page // debug function to show custumized log at the bottom of the page
my_log = function (html_string) { myLog = function (html_string) {
document.querySelector ('div#log').innerHTML += html_string + '<hr/>'; document.querySelector ('div#log').innerHTML += html_string + '<hr/>';
}, },
getXML = function (url) { getXML = function (url) {
...@@ -95,181 +109,88 @@ getXML = function (url) { ...@@ -95,181 +109,88 @@ getXML = function (url) {
objectifyDocumentArray = function (array) { objectifyDocumentArray = function (array) {
var obj = {}, k; var obj = {}, k;
for (k = 0; k < array.length; k += 1) { for (k = 0; k < array.length; k += 1) {
obj[array[k].id] = array[k]; obj[array[k]._id] = array[k];
} }
return obj; return obj;
}, },
addFileToLocalStorage = function (user,appid,file) { getLastJob = function (id) {
var i, l, found = false, filenamearray, return (localstorage.getItem("jio/job_array/"+id) || [undefined]).pop();
userarray = LocalOrCookieStorage.getItem('jio/local_user_array') || [];
for (i = 0, l = userarray.length; i < l; i+= 1) {
if (userarray[i] === user) { found = true; }
}
if (!found) {
userarray.push(user);
LocalOrCookieStorage.setItem('jio/local_user_array',userarray);
LocalOrCookieStorage.setItem(
'jio/local_file_name_array/'+user+'/'+appid,[file._id]);
} else {
filenamearray =
LocalOrCookieStorage.getItem(
'jio/local_file_name_array/'+user+'/'+appid) || [];
filenamearray.push(file._id);
LocalOrCookieStorage.setItem(
'jio/local_file_name_array/'+user+'/'+appid,
filenamearray);
LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file._id,
file);
}
LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file._id,
file);
}, },
removeFileFromLocalStorage = function (user,appid,file) { generateTools = function (sinon) {
var i, l, newarray = [], var o = {};
filenamearray = o.t = sinon;
LocalOrCookieStorage.getItem( o.clock = o.t.sandbox.useFakeTimers();
'jio/local_file_name_array/'+user+'/'+appid) || []; o.clock.tick(base_tick);
for (i = 0, l = filenamearray.length; i < l; i+= 1) { o.spy = basicSpyFunction;
if (filenamearray[i] !== file._id) { o.tick = basicTickFunction;
newarray.push(filenamearray[i]); o.testLastJobLabel = function (label, mess) {
} deepEqual(
} getLastJob(o.jio.getId()).command.label,
LocalOrCookieStorage.setItem('jio/local_file_name_array/'+user+'/'+appid, label,
newarray); mess
LocalOrCookieStorage.deleteItem( );
'jio/local/'+user+'/'+appid+'/'+file._id); };
}, o.testLastJobId = function (id, mess) {
makeRevsAccordingToRevsInfo = function (revs,revs_info) { deepEqual(
var i, j; getLastJob(o.jio.getId()).id,
for (i = 0; i < revs.start; i+= 1) { id,
for (j = 0; j < revs_info.length; j+= 1) { mess
var id = revs_info[j].rev.split('-'); id.shift(); id = id.join('-'); );
if (revs.ids[i] === id) { };
revs.ids[i] = revs_info[j].rev.split('-')[0]; o.testLastJobWaitForTime = function (mess) {
ok(getLastJob(o.jio.getId()).status.waitfortime > 0, mess);
};
o.testLastJobWaitForJob = function (job_id_array, mess) {
deepEqual(
getLastJob(o.jio.getId()).status.waitforjob,
job_id_array,
mess
);
};
o.waitUntilAJobExists = function (timeout) {
var cpt = 0
while (true) {
if (getLastJob(o.jio.getId()) !== undefined) {
break; break;
} }
if (timeout >= cpt) {
ok(false, "no job added to the queue");
break;
}
o.clock.tick(25);
cpt += 25;
} }
} };
}, o.waitUntilLastJobIs = function (state) {
checkRev = function (rev) { while (true) {
if (typeof rev === 'string') { if (getLastJob(o.jio.getId()) === undefined) {
if (rev.split('-').length > 1 && ok(false, "a job is never called");
parseInt(rev.split('-')[0],10) > 0) { break;
return rev; }
} if (getLastJob(o.jio.getId()).status.label === state) {
} break;
return 'ERROR: not a good revision!'; }
}, o.clock.tick(25);
checkConflictRow = function (row) {
var fun;
if (typeof row === 'object') {
if (row.value && typeof row.value._solveConflict === 'function') {
fun = row.value._solveConflict;
row.value._solveConflict = 'function';
}
}
return fun;
},
getHashFromRev = function (rev) {
var id = rev;
if (typeof id === 'string') {
id = id.split('-');
id.shift(); id = id.join('-');
}
return id;
},
revs_infoContains = function (revs_info, rev) {
var i;
if (typeof revs_info !== 'object') {
return undefined;
}
for (i = 0; i < revs_info.length || 0; i+= 1) {
if (revs_info[i].rev && revs_info[i].rev === rev) {
return true;
}
}
return false;
},
isUUID = function( _id ){
var re = /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/;
if ( re.test( _id ) ){
return true;
} else {
return false;
}
},
isEmptyObject = function( obj) {
var key;
if (obj.length && obj.length > 0){
return false;
}
if (obj.length && obj.length === 0){
return true;
}
for (key in obj) {
if (hasOwnProperty.call(obj, key)){
return false;
} }
} };
return true; return o;
}, };
//// end tools //// end tools
//// test methods //// //// test methods ////
checkReply = function(o,tick,fun){
basic_tick_function(o,tick,fun);
},
checkFile = function (response, o, tick, value, fun) {
o.tmp = localstorage.getItem('jio/local/'+o.username+'/jiotests/'+
response.id+'/'+response.rev );
// remove everything not needed for basic response
o.tmp.ok = true;
delete o.tmp._revisions;
delete o.tmp._revs_info;
delete o.tmp.content;
if (o.tmp) {
deepEqual (o.tmp,{
"ok":response.ok,
"_id":response.id,
"_rev":response.rev,
},'document was created or updated');
} else {
ok (false, 'document was not created or updated');
}
},
checkTreeNode = function (response,o,tick,value,fun) {
o.tmp = localstorage.getItem('jio/local/'+o.username+'/jiotests/'+
response.id+'/revision_tree' );
if (o.tmp) {
deepEqual (o.tmp,o.buildTestTree,'tree node was created');
} else {
ok (false, 'tree node was not created');
}
};
//// QUnit Tests //// //// QUnit Tests ////
module ('Jio Global tests'); module ('Jio Global tests');
test ( "Jio simple methods", function () { test ( "Jio simple methods", function () {
var clock = this.sandbox.useFakeTimers(); clock.tick(base_tick);
// Test Jio simple methods // Test Jio simple methods
// It checks if we can create several instance of jio at the same // It checks if we can create several instance of jio at the same
// time. Checks if they don't overlap informations, if they are // time. Checks if they don't overlap informations, if they are
// started and stopped correctly and if they are ready when they // started and stopped correctly and if they are ready when they
// have to be ready. // have to be ready.
var o = {}; var o = generateTools(this);
o.jio = JIO.newJio(); o.jio = JIO.newJio();
ok ( o.jio, 'a new jio -> 1'); ok ( o.jio, 'a new jio -> 1');
...@@ -312,917 +233,776 @@ test ( "Jio simple methods", function () { ...@@ -312,917 +233,776 @@ test ( "Jio simple methods", function () {
// o.jio.stop(); // o.jio.stop();
// }); // });
module ( 'Jio Dummy Storages' ); module ( "Jio Dummy Storages" );
test ('All tests', function () { test ("All requests ok", function () {
// Tests all dummy storages from jio.dummystorages.js // Tests the request methods and the response with dummy storages
// It is simple tests, but they will be used by replicate storage later
// for sync operation.
var o = {}; o.t = this; o.clock = o.t.sandbox.useFakeTimers(); var o = generateTools(this);
o.clock.tick(base_tick);
o.spy = basic_spy_function;
o.tick = basic_tick_function;
// All Ok Dummy Storage // All Ok Dummy Storage
o.jio = JIO.newJio({'type':'dummyallok'}); o.jio = JIO.newJio({"type": "dummyallok"});
// post empty // post empty document, some storage can create there own id (like couchdb
o.jio.post({}, // generates uuid). In this case, the dummy storage write an undefined id.
function(err, response) { o.spy(o, "value", {"ok": true, "id": undefined},
o.spy(o,'value',{"ok":true, "id":response.id, "rev":response.rev}, "Post document with empty id");
'dummyallok post/create empty object'); o.jio.post({}, o.f);
o.f(response);
});
o.tick(o); o.tick(o);
// post // post non empty document
o.jio.post({"content":"basic_content"}, o.spy(o, "value", {"ok": true, "id": "file"}, "Post non empty document");
function(err, response) { o.jio.post({"_id": "file", "title": "myFile"}, o.f);
o.spy(o,'value',{"ok":true, "id":response.id, "rev":response.rev},
'dummyallok post/create object');
o.f(response);
});
o.tick(o); o.tick(o);
// put // put without id
o.jio.put({"_id":"file","content":"basic_content"}, // error 20 -> document id required
function(err, response) { o.spy(o, "status", 20, "Put document with empty id");
o.spy(o,'value',{"ok":true, "id":"file", "rev":response.rev}, o.jio.put({}, o.f);
'dummyallok put create object');
o.f(response);
});
o.tick(o); o.tick(o);
/* // put non empty document
o.spy(o, "value", {"ok": true, "id": "file"}, "Put non empty document");
o.jio.put({"_id": "file", "title": "myFile"}, o.f);
o.tick(o);
// load // put an attachment without attachment id
o.spy(o,'value',{_id:'file',content:'content',_last_modified:15000, // error 22 -> attachment id required
_creation_date:10000},'dummyallok loading'); o.spy(o, "status", 22,
o.jio.get('file',o.f); "Put attachment without id");
o.jio.putAttachment({
"id": "file",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o); o.tick(o);
// remove // put an attachment
o.spy(o,'value',{ok:true,id:"file"},'dummyallok removing'); o.spy(o, "value", {"ok": true, "id": "file/attmt"},
o.jio.remove({_id:'file'},o.f); "Put attachment");
o.jio.putAttachment({
"id": "file/attmt",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o); o.tick(o);
// get list // get document
o.spy (o,'value',{ o.spy(o, "value", {"_id": "file", "title": "get_title"}, "Get document");
total_rows:2, o.jio.get("file", o.f);
rows:[{ o.tick(o);
id:'file',key:'file',
value:{ // get attachment
content:'filecontent', o.spy(o, "value", "0123456789", "Get attachment");
_last_modified:15000, o.jio.get("file/attmt", o.f);
_creation_date:10000
}
},{
id:'memo',key:'memo',
value:{
content:'memocontent',
_last_modified:25000,
_creation_date:20000
}
}]
},'dummyallok getting list');
o.jio.allDocs({metadata_only:false},o.f);
o.tick(o); o.tick(o);
o.jio.stop();
// remove document
o.spy(o, "value", {"ok": true, "id": "file"}, "Remove document");
o.jio.remove({"_id": "file"}, o.f);
o.tick(o);
// remove attachment
o.spy(o, "value", {"ok": true, "id": "file/attmt"}, "Remove attachment");
o.jio.remove({"_id": "file/attmt"}, o.f);
o.tick(o);
// alldocs
// error 405 -> Method not allowed
o.spy(o, "status", 405, "AllDocs fail");
o.jio.allDocs(o.f);
o.tick(o);
o.jio = JIO.newJio({'type':'dummyallok'});
// save
o.spy(o,'value',{ok:true,id:'file'},'dummyallok saving1','f');
o.spy(o,'value',{ok:true,id:'file2'},'dummyallok saving2','f2');
o.spy(o,'value',{ok:true,id:'file3'},'dummyallok saving3','f3');
o.jio.put({_id:'file',content:'content'},o.f);
o.jio.put({_id:'file2',content:'content2'},o.f2);
o.jio.put({_id:'file3',content:'content3'},o.f3);
o.tick(o, 1000, 'f');
o.tick(o, 'f2');
o.tick(o, 'f3');
o.jio.stop(); o.jio.stop();
});
test ("All requests fail", function () {
// Tests the request methods and the err object with dummy storages
var o = generateTools(this);
// All Ok Dummy Storage
o.jio = JIO.newJio({"type": "dummyallfail"});
// post empty document
// error 0 -> unknown
o.spy(o, "status", 0, "Post document with empty id");
o.jio.post({}, o.f);
o.tick(o);
// test if the job still exists
if (getLastJob(o.jio.getId()) !== undefined) {
ok(false, "The job is not removed from the job queue");
}
// All Fail Dummy Storage // post non empty document
o.jio = JIO.newJio({'type':'dummyallfail'}); o.spy(o, "status", 0, "Post non empty document");
// save o.jio.post({"_id": "file", "title": "myFile"}, o.f);
o.spy (o,'status',0,'dummyallfail saving');
o.jio.put({_id:'file',content:'content'},o.f);
o.tick(o); o.tick(o);
// load
o.spy (o,'status',0,'dummyallfail loading'); // put without id
o.jio.get('file',o.f); // error 20 -> document id required
o.spy(o, "status", 20, "Put document with empty id");
o.jio.put({}, o.f);
o.tick(o); o.tick(o);
// remove
o.spy (o,'status',0,'dummyallfail removing'); // put non empty document
o.jio.remove({_id:'file'},o.f); o.spy(o, "status", 0, "Put non empty document");
o.jio.put({"_id": "file", "title": "myFile"}, o.f);
o.tick(o); o.tick(o);
// get list
o.spy (o,'status',0,'dummyallfail getting list'); // put an attachment without attachment id
// error 22 -> attachment id required
o.spy(o, "status", 22,
"Put attachment without id");
o.jio.putAttachment({
"id": "file",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o);
// put an attachment
o.spy(o, "status", 0,
"Put attachment");
o.jio.putAttachment({
"id": "file/attmt",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o);
// get document
o.spy(o, "status", 0, "Get document");
o.jio.get("file", o.f);
o.tick(o);
// get attachment
o.spy(o, "status", 0, "Get attachment");
o.jio.get("file/attmt", o.f);
o.tick(o);
// remove document
o.spy(o, "status", 0, "Remove document");
o.jio.remove({"_id": "file"}, o.f);
o.tick(o);
// remove attachment
o.spy(o, "status", 0, "Remove attachment");
o.jio.remove({"_id": "file/attmt"}, o.f);
o.tick(o);
// alldocs
// error 405 -> Method not allowed
o.spy(o, "status", 405, "AllDocs fail");
o.jio.allDocs(o.f); o.jio.allDocs(o.f);
o.tick(o); o.tick(o);
o.jio.stop(); o.jio.stop();
});
test ("All document not found", function () {
// Tests the request methods without document
var o = generateTools(this);
// All Ok Dummy Storage
o.jio = JIO.newJio({"type": "dummyallnotfound"});
// All Not Found Dummy Storage // post document
o.jio = JIO.newJio({'type':'dummyallnotfound'}); o.spy(o, "value", {"ok": true, "id": "file"}, "Post document");
// save o.jio.post({"_id": "file", "title": "myFile"}, o.f);
o.spy(o,'value',{ok:true,id:'file'},'dummyallnotfound saving');
o.jio.put({_id:'file',content:'content'},o.f);
o.tick(o); o.tick(o);
// load
o.spy(o,'status',404,'dummyallnotfound loading') // put document
o.jio.get('file',o.f); o.spy(o, "value", {"ok": true, "id": "file"}, "Put document");
o.jio.put({"_id": "file", "title": "myFile"}, o.f);
o.tick(o); o.tick(o);
// remove
o.spy(o,'value',{ok:true,id:'file'},'dummyallnotfound removing'); // put an attachment without attachment id
o.jio.remove({_id:'file'},o.f); // error 22 -> attachment id required
o.spy(o, "status", 22,
"Put attachment without id");
o.jio.putAttachment({
"id": "file",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o); o.tick(o);
// get list
o.spy(o,'status',404,'dummyallnotfound getting list'); // put an attachment
o.jio.allDocs (o.f); o.spy(o, "value", {"ok": true, "id": "file/attmt"},
"Put attachment");
o.jio.putAttachment({
"id": "file/attmt",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o); o.tick(o);
o.jio.stop();
*/ // get document
o.spy(o, "status", 404, "Get document");
o.jio.get("file", o.f);
o.tick(o);
// get attachment
o.spy(o, "status", 404, "Get attachment");
o.jio.get("file/attmt", o.f);
o.tick(o);
// remove document
o.spy(o, "status", 404, "Remove document");
o.jio.remove({"_id": "file"}, o.f);
o.tick(o);
// remove attachment
o.spy(o, "status", 404, "Remove attachment");
o.jio.remove({"_id": "file/attmt"}, o.f);
o.tick(o);
o.jio.stop();
}); });
/*
module ( 'Jio Job Managing' ); test ("All document found", function () {
// Tests the request methods with document
test ('Simple Job Elimination', function () {
var clock = this.sandbox.useFakeTimers(); clock.tick(base_tick); var o = generateTools(this);
var o = {}, id = 0;
o.f1 = this.spy(); o.f2 = this.spy(); // All Ok Dummy Storage
o.jio = JIO.newJio({"type": "dummyallfound"});
o.jio = JIO.newJio({type:'dummyallok',applicationname:'jiotests'});
id = o.jio.getId(); // post non empty document
o.jio.put({_id:'file',content:'content'}, o.spy(o, "status", 409, "Post document");
{max_retry:1},o.f1); o.jio.post({"_id": "file", "title": "myFile"}, o.f);
ok(LocalOrCookieStorage.getItem('jio/job_array/'+id)[0], o.tick(o);
'job creation');
o.jio.remove({_id:'file'},{max_retry:1},o.f2); // put non empty document
o.tmp = LocalOrCookieStorage.getItem('jio/job_array/'+id)[0]; o.spy(o, "value", {"ok": true, "id": "file"}, "Put non empty document");
deepEqual(o.tmp.command.label,'remove','job elimination'); o.jio.put({"_id": "file", "title": "myFile"}, o.f);
o.tick(o);
// put an attachment without attachment id
// error 22 -> attachment id required
o.spy(o, "status", 22,
"Put attachment without id");
o.jio.putAttachment({
"id": "file",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o);
// put an attachment
o.spy(o, "value", {"ok": true, "id": "file/attmt"},
"Put attachment");
o.jio.putAttachment({
"id": "file/attmt",
"data": "0123456789",
"mimetype": "text/plain"
}, o.f);
o.tick(o);
// get document
o.spy(o, "value", {"_id": "file", "title": "get_title"}, "Get document");
o.jio.get("file", o.f);
o.tick(o);
// get attachment
o.spy(o, "value", "0123456789", "Get attachment");
o.jio.get("file/attmt", o.f);
o.tick(o);
// remove document
o.spy(o, "value", {"ok": true, "id": "file"}, "Remove document");
o.jio.remove({"_id": "file"}, o.f);
o.tick(o);
// remove attachment
o.spy(o, "value", {"ok": true, "id": "file/attmt"}, "Remove attachment");
o.jio.remove({"_id": "file/attmt"}, o.f);
o.tick(o);
o.jio.stop(); o.jio.stop();
}); });
test ('Simple Job Replacement', function () { module ( "Jio Job Managing" );
// Test if the second job write over the first one
var o = {}; test ("Several Jobs at the same time", function () {
o.clock = this.sandbox.useFakeTimers();
o.clock.tick(base_tick); var o = generateTools(this);
o.id = 0;
o.f1 = function (err,val) { o.jio = JIO.newJio({"type":"dummyallok"});
if (err) { o.spy(o, "value", {"ok": true, "id": "file"}, "job1", "f");
o.err = err; o.spy(o, "value", {"ok": true, "id": "file2"}, "job2", "f2");
} else { o.spy(o, "value", {"ok": true, "id": "file3"}, "job3", "f3");
o.err = {status:'done'}; o.jio.put({"_id": "file", "content": "content"}, o.f);
} o.jio.put({"_id": "file2", "content": "content2"}, o.f2);
}; o.jio.put({"_id": "file3", "content": "content3"}, o.f3);
this.spy(o,'f1'); o.tick(o, 1000, "f");
o.f2 = this.spy(); o.tick(o, "f2");
o.tick(o, "f3");
o.jio = JIO.newJio({type:'dummyallok',applicationname:'jiotests'});
o.id = o.jio.getId();
o.jio.put({_id:'file',content:'content'},o.f1);
o.clock.tick(10);
o.jio.put({_id:'file',content:'content'},o.f2);
deepEqual(LocalOrCookieStorage.getItem(
'jio/job_array/'+o.id)[0].date,base_tick + 10,
'The first job date have to be equal to the second job date.');
o.clock.tick(1000);
deepEqual([o.f1.calledOnce,o.err.status],[true,12],
'callback for the first save request -> result fail');
ok(o.f2.calledOnce,'second callback is called once');
o.jio.stop(); o.jio.stop();
o.jio = JIO.newJio({type:'dummyallok',applicationname:'jiotests'}); });
o.ok1 = 0;
o.jio.get('file1',function (err,val) { test ("Similar Jobs at the same time (Replace)", function () {
deepEqual (err || val,
{_id:'file1',content:'content', var o = generateTools(this);
_creation_date:10000,_last_modified:15000},
'First load'); o.jio = JIO.newJio({"type":"dummyallok"});
o.ok1 ++; o.spy(o, "status", 12, "job1 replaced", "f");
}); o.spy(o, "status", 12, "job2 replaced", "f2");
o.ok2 = 0; o.spy(o, "value", {"ok": true, "id": "file"}, "job3 ok", "f3");
o.jio.get('file2',function (err,val) { o.jio.put({"_id": "file", "content": "content"}, o.f);
deepEqual (err || val, o.jio.put({"_id": "file", "content": "content"}, o.f2);
{_id:'file2',content:'content', o.jio.put({"_id": "file", "content": "content"}, o.f3);
_creation_date:10000,_last_modified:15000}, o.tick(o, 1000, "f");
'Second load must not replace the first one'); o.tick(o, "f2");
o.ok2 ++; o.tick(o, "f3");
});
o.clock.tick(1000);
if (o.ok1 !== 1) {
ok (false,'no response / too much response');
}
if (o.ok2 !== 1) {
ok (false,'no response / too much response');
}
o.jio.stop(); o.jio.stop();
}); });
test ('Simple Job Waiting', function () { test ("One document aim jobs at the same time (Wait for job(s))" , function () {
// Test if the second job doesn't erase the first on going one
var o = {}; var o = generateTools(this);
o.clock = this.sandbox.useFakeTimers();
o.clock.tick(base_tick);
o.id = 0;
o.f = function (err,val) {
deepEqual(err || val,{ok:true,id:'file'},'job 1 result');
};
o.f3 = o.f; this.spy(o,'f3');
o.f4 = o.f; this.spy(o,'f4');
o.checkCallback = function (fun_name,message) {
if (!o[fun_name].calledOnce) {
if (o[fun_name].called) {
ok(false, 'too much response');
} else {
ok(false, 'no response');
}
} else {
ok(true,message);
}
};
o.jio = JIO.newJio({type:'dummyallok',applicationname:'jiotests'}); o.jio = JIO.newJio({"type":"dummyallok"});
o.id = o.jio.getId(); o.spy(o, "value", {"ok": true, "id": "file"}, "job1", "f");
o.jio.put({_id:'file',content:'content'},o.f3); o.spy(o, "value", {"ok": true, "id": "file"}, "job2", "f2");
o.clock.tick(200); o.spy(o, "value", {"_id": "file", "title": "get_title"}, "job3", "f3");
o.jio.put({_id:'file',content:'content1'},o.f4);
o.tmp0 = LocalOrCookieStorage.getItem('jio/job_array/'+o.id)[0]; o.jio.post({"_id": "file", "content": "content"}, o.f);
o.tmp1 = LocalOrCookieStorage.getItem('jio/job_array/'+o.id)[1]; o.testLastJobWaitForJob(undefined, "job1 is not waiting for someone");
ok(o.tmp0 && o.tmp0.id === 1,'job 1 exists'); o.jio.put({"_id": "file", "content": "content"}, o.f2);
deepEqual(o.tmp0.status.label,'on going','job 1 is on going'); o.testLastJobWaitForJob([1], "job2 is waiting");
ok(o.tmp1 && o.tmp1.id === 2,'job 2 exists');
deepEqual(o.tmp1.status.label,'wait','job 2 waiting');
deepEqual(o.tmp1.status.waitforjob,[1],
'job 2 must wait for the first to end');
o.clock.tick(1000); o.jio.get("file", o.f3);
o.checkCallback('f3','first request passed'); o.testLastJobWaitForJob([1, 2], "job3 is waiting");
o.checkCallback('f4','restore waiting job');
o.tick(o, 1000, "f");
o.tick(o, "f2");
o.tick(o, "f3");
o.jio.stop(); o.jio.stop();
}); });
test ('Simple Time Waiting' , function () { test ("One document aim jobs at the same time (Elimination)" , function () {
// Test if the job that have fail wait until a certain moment to restart.
// It will use the dummyall3tries, which will work after the 3rd try.
var o = {}, clock = this.sandbox.useFakeTimers(), id = 0; var o = generateTools(this);
clock.tick(base_tick);
o.f = function (err,val) { o.jio = JIO.newJio({"type":"dummyallok"});
if (err) { o.spy(o, "status", 10, "job1 stopped", "f");
o.res = err; o.spy(o, "value", {"ok": true, "id": "file"}, "job2", "f2");
} else {
o.res = val; o.jio.post({"_id": "file", "content": "content"}, o.f);
} o.testLastJobLabel("post", "job1 exists");
};
this.spy(o,'f'); o.jio.remove({"_id": "file"}, o.f2);
o.jio = JIO.newJio({type:'dummyall3tries',applicationname:'jiotests'}); o.testLastJobLabel("remove", "job1 does not exist anymore");
o.jio.put({_id:'file',content:'content'},{max_retry:3},o.f);
clock.tick(10000); o.tick(o, 1000, "f");
if (!o.f.calledOnce) { o.tick(o, "f2");
if (o.f.called) {
ok(false,'callback called too much times.');
} else {
ok(false,'no response.');
}
}
deepEqual(o.res,{ok:true,id:'file'},'job done.');
o.jio.stop(); o.jio.stop();
}); });
module ( 'Jio Restore'); test ("One document aim jobs at the same time (Not Acceptable)" , function () {
test ('Restore old Jio', function() { var o = generateTools(this);
var o = {};
o.clock = this.sandbox.useFakeTimers(); o.jio = JIO.newJio({"type":"dummyallok"});
o.f = function() { o.spy(o, "value", {"_id": "file", "title": "get_title"}, "job1", "f");
ok(false,'must never be called!'); o.spy(o, "status", 11, "job2 is not acceptable", "f2");
};
this.spy(o,'f'); o.jio.get("file", o.f);
o.jio = JIO.newJio({type:'dummyall3tries',applicationname:'jiotests'}); o.testLastJobId(1, "job1 added to queue");
o.id = o.jio.getId(); o.waitUntilLastJobIs("on going");
ok(true,'create jio, id = ' + o.id);
o.jio.put({_id:'file',content:'content'},{max_retry:3},o.f); o.jio.get("file", o.f2);
o.clock.tick(1000); o.testLastJobId(1, "job2 not added");
o.jio.close();
o.jio = JIO.newJio({type:'dummyallok',applicationname:'jiotests'}); o.tick(o, 1000, "f");
o.clock.tick(11000); // 10 sec o.tick(o, "f2");
deepEqual(LocalOrCookieStorage.getItem('jio/job_array/'+o.id),null,
'job array list must be empty');
o.tmp1 = LocalOrCookieStorage.getItem('jio/job_array/'+o.jio.getId());
if (o.tmp1.length > 0) {
deepEqual([o.tmp1[0].command.label,o.tmp1[0].command.doc._id,
o.tmp1[0].command.doc.content],
['put','file','content'],
'job which id is id = ' +o.jio.getId()+', restored the jio');
} else {
ok (false, 'The recovered job must exists');
}
o.jio.stop(); o.jio.stop();
}); });
*/ test ("Server will be available soon (Wait for time)" , function () {
module ( 'Jio LocalStorage' ); var o = generateTools(this);
o.max_retry = 3;
// ============================== POST ========================== o.jio = JIO.newJio({"type":"dummyall3tries"});
test ('Post', function(){ o.spy(o, "value", {"ok": true, "id": "file"}, "job1", "f");
// runs following assertions o.jio.put({"_id": "file", "content": "content"},
// 1) POST with id - should be an error {"max_retry": o.max_retry}, o.f);
// 2) POST with attachment - should be an error for (o.i = 0; o.i < o.max_retry - 1; o.i += 1) {
// 3) POST CREATE with content o.waitUntilLastJobIs("on going");
// 4) check that document is created with UUID.revision o.waitUntilLastJobIs("wait");
// 5) check that document revision tree is created o.testLastJobWaitForTime("job1 is waiting for time");
// 6) POST UPDATE }
var o = {}; o.tick(o, 1000, "f");
o.t = this; o.jio.stop();
o.clock = o.t.sandbox.useFakeTimers(),
localstorage = {
getItem: function (item) {
return JSON.parse (localStorage.getItem(item));
},
setItem: function (item,value) {
return localStorage.setItem(item,JSON.stringify(value));
},
deleteItem: function (item) {
delete localStorage[item];
}
};
o.clock.tick(base_tick); });
o.spy = basic_spy_function;
o.clean = clean_up_local_storage_function(); module ( "Jio Restore");
o.username = 'MrPost';
o.testRevisionStorage = []; test ("Restore old Jio", function() {
// let's go var o = generateTools(this);
o.jio = JIO.newJio({ type:'local', username:o.username,
applicationname:'jiotests' }); o.jio = JIO.newJio({
// ======================================== "type": "dummyall3tries",
// 1) POST with id "applicationname": "jiotests"
o.jio.post({"_id":'file',"content":'content'},function(err, response){
o.spy (o,'value',{
"error": 'forbidden',
"message": 'Forbidden',
"reason": 'ID cannot be supplied with a POST request. Please use PUT',
"status": 403,
"statusText": 'Forbidden'
},'POST with id = 403 forbidden');
o.f(err);
}); });
checkReply(o,null,true);
o.clock.tick(base_tick); o.jio_id = o.jio.getId();
// ========================================
// 2) POST attachment o.jio.put({"_id": "file", "title": "myFile"}, {"max_retry":3}, o.f);
o.jio.post({"_id":'file/ABC', "mimetype":'text/html', o.waitUntilLastJobIs("on going");
"content":'<b>hello</b>'},function(err, response){ o.jio.close();
o.spy (o,'value',{
"error": 'forbidden', o.jio = JIO.newJio({
"message": 'Forbidden', "type": "dummyallok",
"reason": 'Attachment cannot be added with a POST request', "applicationname": "jiotests"
"status": 403,
"statusText": 'Forbidden'
},'POST attachment = 403 forbidden');
o.f(err);
}); });
checkReply(o,null,true); o.waitUntilAJobExists(30000); // timeout 30 sec
o.clock.tick(base_tick); o.testLastJobLabel("put", "Job restored");
// ======================================== o.clock.tick(1000);
// 3) POST content ok(getLastJob(o.jio.getId()) === undefined,
o.jio.post({"content":'content'}, "Job executed");
function(err, response) {
o.spy(o,'value',{"ok":true,"id":response.id,"rev":response.rev},
'POST content = ok');
o.f(response);
// store falseRevision
o.falseRevision = response.rev;
// build tree manually
o.testRevisionStorage.push(response.rev);
o.buildTestTree = {"kids":[],"rev":o.testRevisionStorage[0],
"status":'available',"type":'leaf'};
// 4) check if document is created and correct
checkFile(response, o, null, true);
// 5) check if document tree is created and correct
checkTreeNode(response, o, null, true);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// END POST
o.jio.stop(); o.jio.stop();
o.clean;
}); });
// ============================== PUT ========================== module ( "Jio LocalStorage" );
test ('Put', function(){
// runs following assertions
// 1) PUT without ID = 409
// 2) PUT with wrong ID/rev = 404
// 3) PUT CREATE response
// 4) check file was created
// 5) check tree was created
// 6) PUT UPDATE response
// 7) check file was replaced
// 8) check tree was updated
// 9) PUT UPDATE 2 response
// 10) check file was replaced
// 11) check tree was updated
// 12) PUT UPDATE false revision = 409
// 13) SYNC-PUT no revs_info = 409
// 14) SYNC-PUT revs_info response
// 15) check if file created
// 16) check if tree was merged
// 17) SYNC-PUT revs_info dead leaf response
// 18) check that file was NOT created
// 19) check that tree was updated
var fake_rev_0,
fake_rev_1,
fake_rev_2,
fake_id_0,
fake_id_1,
fake_id_2,
o = {};
o.t = this;
o.clock = o.t.sandbox.useFakeTimers();
o.falseRevision;
localstorage = {
getItem: function (item) {
return JSON.parse (localStorage.getItem(item));
},
setItem: function (item,value) {
return localStorage.setItem(item,JSON.stringify (value));
},
deleteItem: function (item) {
delete localStorage[item];
}
};
o.clock.tick(base_tick); test ("Post", function(){
o.spy = basic_spy_function;
o.clean = clean_up_local_storage_function();
o.username = 'MrPutt';
o.testRevisionStorage = [];
// let's go
o.jio = JIO.newJio({ type:'local', username:o.username,
applicationname:'jiotests' });
// ========================================
// 1) PUT without ID
o.jio.put({"content":'content'},function(err, response){
o.spy (o,'value',{
"error": 'conflict',
"message": 'Document update conflict.',
"reason": 'Missing Document ID and or Revision',
"status": 409,
"statusText": 'Conflict'
},'PUT without id = 409 Conflict');
o.f(err);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// ========================================
// 2) PUT wrong id/rev
o.jio.put({"content":'content', "_id":'myDoc',
"_rev":'1-ABCDEFG'}, function(err, response){
o.spy (o,'value',{
"error": 'not found',
"message": 'Document not found.',
"reason": 'Document not found, please check revision and/or ID',
"status": 404,
"statusText": 'Not found'
},'PUT with wrong id/revision = 404 Not found');
o.f(err);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// ========================================
// 3) PUT content
o.jio.put({"content":'content',"_id":'myDoc'},
function(err, response) {
o.spy(o,'value',{"ok":true,"id":response.id,"rev":response.rev},
'PUT content = ok');
o.f(response);
o.falseRevision = response.rev;
o.testRevisionStorage.unshift(response.rev);
o.buildTestTree = {"kids":[],"rev":o.testRevisionStorage[0],
"status":'available',"type":'leaf'};
// ========================================
// 4) check file was created
checkFile(response, o, null, true);
// ========================================
// 5) check tree was created
checkTreeNode(response, o, null, true);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// ========================================
// 6) PUT UPDATE (modify content)
o.jio.put({"content":'content_modified',"_id":'myDoc',
"_rev":o.testRevisionStorage[0]},
function(err, response) {
o.spy(o,'value',{"ok":true,"id":response.id,"rev":response.rev},
'PUT content = ok');
o.f(response);
o.testRevisionStorage.unshift(response.rev);
o.buildTestTree = {"kids":[{"kids":[],"rev":
o.testRevisionStorage[0],"status":'available',
"type":'leaf'}],"rev":o.testRevisionStorage[1],
"status":'deleted',"type":'branch'};
// ========================================
// 7) check document was replaced
checkFile(response, o, null, true);
// ========================================
// 8) check tree was updated
checkTreeNode(response, o, null, true);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// ========================================
// 9. update document (modify again)
o.jio.put({"content":'content_modified_again',
"_id":'myDoc', "_rev":o.testRevisionStorage[0]},
function(err, response) {
o.spy(o,'value',{"ok":true,"id":response.id,
"rev":response.rev}, 'PUT content = ok');
o.f(response);
o.testRevisionStorage.unshift(response.rev);
o.buildTestTree = {"kids":[{"kids":[{"kids":[],
"rev":o.testRevisionStorage[0],"status":'available',
"type":'leaf'}],"rev":o.testRevisionStorage[1],
"status":'deleted',"type":'branch'}],
"rev":o.testRevisionStorage[2],"status":'deleted',
"type":'branch'};
// 10) check document was replaced
checkFile(response, o, null, true);
// 11) check tree was updated
checkTreeNode(response, o, null, true);
}); var o = generateTools(this);
checkReply(o,null,true);
o.clock.tick(base_tick); o.jio = JIO.newJio({
// ======================================== "type": "local",
// 12) PUT false revision "username": "upost",
o.jio.put({"content":'content_modified_false', "applicationname": "apost"
"_id":'myDoc',
"_rev":o.falseRevision},function(err, response){
o.spy (o,'value',{
"error": 'conflict',
"message": 'Document update conflict.',
"reason":
'Revision supplied is not the latest revision',
"status": 409,
"statusText": 'Conflict'
},'PUT false revision = 409 Conflict');
o.f(err);
}); });
checkReply(o,null,true);
o.clock.tick(base_tick); // post without id
// ======================================== o.spy (o, "status", 405, "Post without id");
// 13) SYNC-PUT no revs_info o.jio.post({}, o.f);
o.jio.put({"content":'content_modified_false', o.tick(o);
"_id":'myDoc',
"_rev":'1-abcdefg'},function(err, response){ // post non empty document
o.spy (o,'value',{ o.spy (o, "value", {"ok": true, "id": "post1"}, "Post");
"error": 'conflict', o.jio.post({"_id": "post1", "title": "myPost1"}, o.f);
"message": 'Document update conflict.', o.tick(o);
"reason":
'Missing revs_info required for sync-put', deepEqual(
"status": 409, localstorage.getItem("jio/localstorage/upost/apost/post1"),
"statusText": 'Conflict' {
},'PUT no sync info = 409 Conflict'); "_id": "post1",
o.f(err); "title": "myPost1"
},
"Check document"
);
// post but document already exists
o.spy (o, "status", 409, "Post but document already exists");
o.jio.post({"_id": "post1", "title": "myPost2"}, o.f);
o.tick(o);
o.jio.stop();
});
test ("Put", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type": "local",
"username": "uput",
"applicationname": "aput"
}); });
checkReply(o,null,true);
o.clock.tick(base_tick);
// add a new document version with fake revs_info // put without id
// the new document has the same origin and first edit, o.spy (o, "status", 20, "Put without id");
// then it was changed to a new version (3-a9d...), o.jio.put({}, o.f);
// which was changed to a fourth version (4-b5bb...), o.tick(o);
// the tree must merge on o.testRevisionStorage[1]
// and add the two new dummy revisions into the final // put non empty document
// tree. Also the new document should be stored o.spy (o, "value", {"ok": true, "id": "put1"}, "Creates a document");
// in local storage. o.jio.put({"_id": "put1", "title": "myPut1"}, o.f);
fake_rev_2 = o.testRevisionStorage[2]; o.tick(o);
fake_rev_1 = o.testRevisionStorage[1];
fake_rev_0 = o.testRevisionStorage[0]; // check document
fake_id_2 = o.testRevisionStorage[2].split('-')[1]; deepEqual(
fake_id_1 = o.testRevisionStorage[1].split('-')[1]; localstorage.getItem("jio/localstorage/uput/aput/put1"),
fake_id_0 = o.testRevisionStorage[0].split('-')[1]; {
// ======================================== "_id": "put1",
// 14) PUT UPDATE A TREE using revs_info "title": "myPut1"
o.jio.put({
"content":'a_new_version',
"_id":'myDoc',
"_rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89",
"_revs_info":[
{"rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89","status":"available"},
{"rev":"3-a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123","status":"deleted"},
{"rev":fake_rev_1,"status":"deleted"},
{"rev":fake_rev_0,"status":"deleted"}
],
"_revisions":{
"start":4,
"ids":[
"b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89",
"a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123",
fake_id_1,
fake_id_0
]}
}, },
function(err, response) { "Check document"
o.buildTestTree = { );
"kids":[
{ // put but document already exists
"kids":[ o.spy (o, "value", {"ok": true, "id": "put1"}, "Update the document");
{"kids":[],"rev":o.testRevisionStorage[0],"status":'available',"type":'leaf'}, o.jio.put({"_id": "put1", "title": "myPut2"}, o.f);
{"kids":[{ o.tick(o);
"kids":[],
"rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89", // check document
"status":'available', "type":'leaf' deepEqual(
}], localstorage.getItem("jio/localstorage/uput/aput/put1"),
"rev":"3-a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123", {
"status":'deleted',"type":'branch' "_id": "put1",
}], "title": "myPut2"
"rev":o.testRevisionStorage[1],"status":'deleted',"type":'branch'}],
"rev":o.testRevisionStorage[2],"status":'deleted',"type":'branch'
};
o.spy(o,'value',{"ok":true,"id":response.id,
"rev":response.rev}, 'PUT SYNC = ok');
o.f(response);
// 15) check document was stored
checkFile(response, o, null, true);
// 16) check tree was updated
checkTreeNode(response, o, null, true);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// ========================================
// 17) PUT UPDATE (deleted tree)
o.jio.put({
"content":'a_deleted_version',
"_id":'myDoc',
"_rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61",
"_revs_info":[
{"rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61","status":"deleted"},
{"rev":"2-67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844","status":"deleted"},
{"rev":fake_rev_2,"status":"deleted"}
],
"_revisions":{
"start":3,
"ids":[
"05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61",
"67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844",
fake_id_2
]}
}, },
function(err, response) { "Check document"
);
o.buildTestTree = {
"kids":[{
"kids":[
{"kids":[],
"rev":o.testRevisionStorage[0],
"status":'available',
"type":'leaf'
},
{"kids":[{
"kids":[],
"rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89",
"status":'available', "type":'leaf'
}],
"rev":"3-a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123",
"status":'deleted',
"type":'branch'
}],
"rev":o.testRevisionStorage[1],
"status":'deleted',
"type":'branch'
},{
"kids":[
{
"kids":[],
"rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61",
"status":'deleted',
"type":'leaf'
}],
"rev":"2-67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844",
"status":'deleted',
"typ":'branch'
}],
"rev":o.testRevisionStorage[2],
"status":'deleted',
"type":'branch'
};
o.spy(o,'value',{"ok":true,"id":response.id,
"rev":response.rev}, 'PUT SYNC dead leaf = ok');
o.f(response);
// 18) check document was stored
checkFile(response, o, null, true);
// 19) check tree was updated
checkTreeNode(response, o, null, true);
});
checkReply(o,null,true);
o.clock.tick(base_tick);
// END PUT
o.jio.stop(); o.jio.stop();
o.clean;
}); });
test ("PutAttachment", function(){
// ====================== PUTATTACHMENT ========================== var o = generateTools(this);
test ('PutAttachment', function(){
// runs following assertions
// 1) PUTATTACHMENT with wrong id/rev1
// 2) PUTATTACHMENT without id/rev1
var o = {};
o.t = this;
o.clock = o.t.sandbox.useFakeTimers();
o.falseRevision;
localstorage = {
getItem: function (item) {
return JSON.parse (localStorage.getItem(item));
},
setItem: function (item,value) {
return localStorage.setItem(item,JSON.stringify (value));
},
deleteItem: function (item) {
delete localStorage[item];
}
};
o.clock.tick(base_tick); o.jio = JIO.newJio({
o.spy = basic_spy_function; "type": "local",
o.clean = clean_up_local_storage_function(); "username": "uputattmt",
o.username = 'MrPuttAttachment'; "applicationname": "aputattmt"
o.testRevisionStorage = [];
// let's go
o.jio = JIO.newJio({ type:'local', username:o.username,
applicationname:'jiotests' });
// ========================================
// 1) PUTATTACHMENT with wrong id/rev
o.jio.putAttachment("ABC/DEF","A-1aaa","<b>hello</b>","text/html",function(err, response){
o.spy (o,'value',{
"error": 'not found',
"message": 'Document not found.',
"reason": 'Document not found, please check document ID',
"status": 404,
"statusText": 'Not found'
},'PUTATTACHMENT without id = 404 NOT FOUND');
o.f(err);
}); });
checkReply(o,null,true);
// ======================================== // putAttachment without doc id
// 2) PUTATTACHMENT with wrong id/rev // error 20 -> document id required
/* o.spy(o, "status", 20, "PutAttachment without doc id");
o.jio.putAttachment("ABC/DEF","text/html","<b>hello</b>",function(err, response){ o.jio.putAttachment({}, o.f);
o.spy (o,'value',{ o.tick(o);
"error": 'not found',
"message": 'Document not found.', // putAttachment without attmt id
"reason": 'Document not found, please check document ID', // error 22 -> attachment id required
"status": 404, o.spy(o, "status", 22, "PutAttachment without attmt id");
"statusText": 'Not found' o.jio.putAttachment({"id": "putattmt1"}, o.f);
},'PUTATTACHMENT without id = 404 NOT FOUND'); o.tick(o);
o.f(err);
// putAttachment without document
// error 404 -> not found
o.spy(o, "status", 404, "PutAttachment without document");
o.jio.putAttachment({"id": "putattmt1/putattmt2"}, o.f);
o.tick(o);
// adding a document
localstorage.setItem("jio/localstorage/uputattmt/aputattmt/putattmt1", {
"_id": "putattmt1",
"title": "myPutAttmt1"
}); });
checkReply(o,null,true);
*/ // putAttachment with document
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"PutAttachment with document, without data");
o.jio.putAttachment({"id": "putattmt1/putattmt2"}, o.f);
o.tick(o);
// check document
deepEqual(
localstorage.getItem("jio/localstorage/uputattmt/aputattmt/putattmt1"),
{
"_id": "putattmt1",
"title": "myPutAttmt1",
"_attachments": {
"putattmt2": {
"length": 0,
// md5("")
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
}
}
},
"Check document"
);
// check attachment
deepEqual(
localstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1/putattmt2"),
"", "Check attachment"
);
// update attachment
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"Update Attachment, with data");
o.jio.putAttachment({"id": "putattmt1/putattmt2", "data": "abc"}, o.f);
o.tick(o);
// check document
deepEqual(
localstorage.getItem("jio/localstorage/uputattmt/aputattmt/putattmt1"),
{
"_id": "putattmt1",
"title": "myPutAttmt1",
"_attachments": {
"putattmt2": {
"length": 3,
// md5("abc")
"digest": "md5-900150983cd24fb0d6963f7d28e17f72"
}
}
},
"Check document"
);
// check attachment
deepEqual(
localstorage.getItem(
"jio/localstorage/uputattmt/aputattmt/putattmt1/putattmt2"),
"abc", "Check attachment"
);
o.jio.stop();
}); });
/* test ("Get", function(){
test ('Document load', function () {
// Test if LocalStorage can load documents.
// We launch a loading from localstorage and we check if the file is
// realy loaded.
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = generateTools(this);
o.clock.tick(base_tick);
o.spy = basic_spy_function;
o.tick = basic_tick_function;
o.jio = JIO.newJio({type:'local',username:'MrLoadName', o.jio = JIO.newJio({
applicationname:'jiotests'}); "type": "local",
// save and check document existence "username": "uget",
o.doc = {_id:'file',content:'content', "applicationname": "aget"
_last_modified:1234,_creation_date:1000}; });
// get unexistant document
o.spy(o, "status", 404, "Get unexistant document");
o.jio.get("get1", o.f);
o.tick(o);
// get unexistant attachment
o.spy(o, "status", 404, "Get unexistant attachment");
o.jio.get("get1/get2", o.f);
o.tick(o);
// adding a document
o.doc_get1 = {
"_id": "get1",
"title": "myGet1"
};
localstorage.setItem("jio/localstorage/uget/aget/get1", o.doc_get1);
// get document
o.spy(o, "value", o.doc_get1, "Get document");
o.jio.get("get1", o.f);
o.tick(o);
o.spy(o,'status',404,'loading document failure'); // get unexistant attachment (document exists)
o.jio.get('file',o.f); o.spy(o, "status", 404, "Get unexistant attachment (document exists)");
o.jio.get("get1/get2", o.f);
o.tick(o); o.tick(o);
addFileToLocalStorage('MrLoadName','jiotests',o.doc); // adding an attachment
o.spy(o,'value',o.doc,'loading document success'); o.doc_get1["_attachments"] = {
o.jio.get('file',o.f); "get2": {
"length": 2,
// md5("de")
"digest": "md5-5f02f0889301fd7be1ac972c11bf3e7d"
}
};
localstorage.setItem("jio/localstorage/uget/aget/get1", o.doc_get1);
localstorage.setItem("jio/localstorage/uget/aget/get1/get2", "de");
// get attachment
o.spy(o, "value", "de", "Get attachment");
o.jio.get("get1/get2", o.f);
o.tick(o); o.tick(o);
o.jio.stop(); o.jio.stop();
}); });
*//*
test ('Get document list', function () {
// Test if LocalStorage can get a list of documents.
// We create 2 documents inside localStorage to check them.
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; test ("Remove", function(){
o.clock.tick(base_tick);
o.mytest = function (value){ var o = generateTools(this);
o.f = function (err,val) {
if (val) { o.jio = JIO.newJio({
deepEqual (objectifyDocumentArray(val.rows), "type": "local",
objectifyDocumentArray(value),'getting list'); "username": "uremove",
} else { "applicationname": "aremove"
deepEqual (err,value,'getting list'); });
}
}; // remove unexistant document
o.t.spy(o,'f'); o.spy(o, "status", 404, "Remove unexistant document");
o.jio.allDocs(o.f); o.jio.remove({"_id": "remove1"}, o.f);
o.clock.tick(1000); o.tick(o);
if (!o.f.calledOnce) {
if (o.f.called) { // remove unexistant document/attachment
ok(false, 'too much results'); o.spy(o, "status", 404, "Remove unexistant document/attachment");
} else { o.jio.remove({"_id": "remove1/remove2"}, o.f);
ok(false, 'no response'); o.tick(o);
// adding a document
localstorage.setItem("jio/localstorage/uremove/aremove/remove1", {
"_id": "remove1",
"title": "myRemove1"
});
// remove document
o.spy(o, "value", {"ok": true, "id": "remove1"}, "Remove document");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
// check document
ok(localstorage.getItem("jio/localstorage/uremove/aremove/remove1")===null,
"Check documuent");
// adding a document + attmt
localstorage.setItem("jio/localstorage/uremove/aremove/remove1", {
"_id": "remove1",
"title": "myRemove1",
"_attachments": {
"remove2": {
"length": 4,
"digest": "md5-blahblah"
} }
} }
}; });
o.jio = JIO.newJio({type:'local',username:'MrListName', localstorage.setItem(
applicationname:'jiotests'}); "jio/localstorage/uremove/aremove/remove1/remove2", "fghi");
o.doc1 = {_id:'file',content:'content',
_last_modified:1,_creation_date:0}; // remove attachment
o.doc2 = {_id:'memo',content:'test', o.spy(o, "value", {"ok": true, "id": "remove1"}, "Remove attachment");
_last_modified:5,_creation_date:2}; o.jio.remove({"_id": "remove1"}, o.f);
addFileToLocalStorage ('MrListName','jiotests',o.doc1); o.tick(o);
addFileToLocalStorage ('MrListName','jiotests',o.doc2);
o.mytest ([{
id:o.doc2._id,key:o.doc2._id,
value:{
_creation_date:o.doc2._creation_date,
_last_modified:o.doc2._last_modified
}
},{
id:o.doc1._id,key:o.doc1._id,
value:{
_last_modified:o.doc1._last_modified,
_creation_date:o.doc1._creation_date
}
}]);
o.jio.stop(); o.jio.stop();
}); });
*//*
test ('Document remove', function () {
// Test if LocalStorage can remove documents.
// We launch a remove from localstorage and we check if the file is
// realy removed.
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick);
o.spy = basic_spy_function;
o.tick = function () {
basic_tick_function.apply(basic_tick_function,arguments);
// check if the file is still there
o.tmp = LocalOrCookieStorage.getItem (
'jio/local/MrRemoveName/jiotests/file');
ok (!o.tmp, 'check no content');
};
o.jio = JIO.newJio({type:'local',username:'MrRemoveName', test ("AllDocs", function(){
applicationname:'jiotests'});
// test removing a file var o = generateTools(this);
o.spy (o,'value',{ok:true,id:'file'},'removing document');
addFileToLocalStorage ('MrRemoveName','jiotests',{_id:'file'}); o.jio = JIO.newJio({
o.jio.remove({_id:'file'},o.f); "type": "local",
o.tick (o); "username": "ualldocs",
"applicationname": "aalldocs"
});
// alldocs
// error 405 -> method not allowed
o.spy(o, "status", 405, "Method not allowed");
o.jio.allDocs(o.f);
o.tick(o);
o.jio.stop(); o.jio.stop();
}); });
*/
/* /*
module ('Jio DAVStorage'); module ('Jio DAVStorage');
...@@ -2818,7 +2598,6 @@ if (window.requirejs) { ...@@ -2818,7 +2598,6 @@ if (window.requirejs) {
paths: { paths: {
jiotestsloader: './jiotests.loader', jiotestsloader: './jiotests.loader',
LocalOrCookieStorage: './testlocalorcookiestorage',
jQueryAPI: '../lib/jquery/jquery', jQueryAPI: '../lib/jquery/jquery',
jQuery: '../js/jquery.requirejs_module', jQuery: '../js/jquery.requirejs_module',
JIO: '../src/jio', JIO: '../src/jio',
...@@ -2832,8 +2611,7 @@ if (window.requirejs) { ...@@ -2832,8 +2611,7 @@ if (window.requirejs) {
}); });
require(['jiotestsloader'],thisfun); require(['jiotestsloader'],thisfun);
} else { } else {
thisfun ({LocalOrCookieStorage:LocalOrCookieStorage, thisfun ({JIO:jIO,
JIO:jIO,
sjcl:sjcl, sjcl:sjcl,
Base64:Base64, Base64:Base64,
jQuery:jQuery}); jQuery:jQuery});
......
...@@ -11,8 +11,9 @@ ...@@ -11,8 +11,9 @@
<script type="text/javascript" src="../lib/sinon/sinon.js"></script> <script type="text/javascript" src="../lib/sinon/sinon.js"></script>
<script type="text/javascript" src="../lib/sinon/sinon-qunit.js"></script> <script type="text/javascript" src="../lib/sinon/sinon-qunit.js"></script>
<script type="text/javascript" src="../lib/jquery/jquery.min.js"></script> <script type="text/javascript" src="../lib/jquery/jquery.min.js"></script>
<script type="text/javascript" src="./testlocalorcookiestorage.js"></script>
<script type="text/javascript" src="./browserlocalstorage.stub.js"></script>
<script type="text/javascript" src="../lib/md5/md5.js"></script>
<script type="text/javascript" src="../jio.js"></script> <script type="text/javascript" src="../jio.js"></script>
<script type="text/javascript" src="../lib/base64/base64.js"></script> <script type="text/javascript" src="../lib/base64/base64.js"></script>
......
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