Commit 1edc8e48 authored by Sven Franck's avatar Sven Franck

localStorage: ALLDOCs new API and qunit tests

parent 30160fbb
......@@ -320,16 +320,42 @@ jIO.addStorageType('local', function (spec, my) {
* @method allDocs
* @param {object} command The JIO command
*/
that.allDocs = function () {
setTimeout(function () {
that.allDocs = function (command) {
var i, s, j, file, items = 0, all_doc_response = {};
all_doc_response.rows = [];
for (i in localStorage) {
if (localStorage.hasOwnProperty(i)) {
// filter non-documents
s = new RegExp(priv.localpath + '\\/.*$');
if (s.test(i)) {
items += 1;
j = i.split('/').slice(-1)[0];
file = { value: {} };
file.id = j;
file.key = j;
if (command.getOption('include_docs')) {
file.doc = JSON.parse(localStorage.getItem(i));
}
all_doc_response.rows.push(file);
}
}
}
all_doc_response.total_rows = items;
if (items > 0) {
that.success(all_doc_response);
} else {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Your are not allowed to use this command",
"reason": "LocalStorage forbids AllDocs command executions"
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "No documents found",
"reason": "No documents found"
});
});
}
};
return that;
......
......@@ -1071,20 +1071,76 @@ test ("Remove", function(){
test ("AllDocs", function(){
var o = generateTools(this);
var o = generateTools(this), i, m = 15;
o.jio = JIO.newJio({
"type": "local",
"username": "ualldocs",
"application_name": "aalldocs"
});
o.localpath = "jio/localstorage/ualldocs/aalldocs";
// sample data
o.titles = ["Shawshank Redemption", "Godfather", "Godfather 2",
"Pulp Fiction", "The Good, The Bad and The Ugly", "12 Angry Men",
"The Dark Knight", "Schindlers List",
"Lord of the Rings - Return of the King", "Fight Club",
"Star Wars Episode V", "Lord Of the Rings - Fellowship of the Ring",
"One flew over the Cuckoo's Nest", "Inception", "Godfellas"
];
o.years = [1994,1972,1974,1994,1966,1957,2008,1993,2003,1999,1980,2001,
1975,2010,1990
];
o.director = ["Frank Darabont", "Francis Ford Coppola",
"Francis Ford Coppola", "Quentin Tarantino", "Sergio Leone",
"Sidney Lumet", "Christopher Nolan", "Steven Spielberg",
"Peter Jackson", "David Fincher", "Irvin Kershner", "Peter Jackson",
"Milos Forman", "Christopher Nolan", " Martin Scorsese"
]
// set documents
for (i = 0; i < m; i += 1) {
o.fakeDoc = {};
o.fakeDoc._id = "doc_"+i;
o.fakeDoc.title = o.titles[i];
o.fakeDoc.year = o.years[i];
o.fakeDoc.author = o.director[i];
localstorage.setItem(o.localpath+"/doc_"+i, o.fakeDoc);
}
// response
o.allDocsResponse = {};
o.allDocsResponse.rows = [];
o.allDocsResponse.total_rows = 15;
for (i = 0; i < m; i += 1) {
o.allDocsResponse.rows.push({
"id": "doc_"+i,
"key": "doc_"+i,
"value": {}
});
};
// alldocs
// error 405 -> method not allowed
o.spy(o, "status", 405, "Method not allowed");
o.spy(o, "value", o.allDocsResponse, "All docs");
o.jio.allDocs(o.f);
o.tick(o);
// include docs
o.allDocsResponse = {};
o.allDocsResponse.rows = [];
o.allDocsResponse.total_rows = 15;
for (i = 0; i < m; i += 1) {
o.allDocsResponse.rows.push({
"id": "doc_"+i,
"key": "doc_"+i,
"value": {},
"doc": localstorage.getItem(o.localpath+"/doc_"+i)
});
};
// alldocs
o.spy(o, "value", o.allDocsResponse, "All docs (include docs)");
o.jio.allDocs({"include_docs":true}, o.f);
o.tick(o);
o.jio.stop();
});
......
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