Commit 5e8b8a5a authored by lucas.parsy's avatar lucas.parsy

added allDocs method in websqlStorage

added tests for Websqlstorage and refactored some tests.
parent f5382029
......@@ -195,15 +195,6 @@
});
};
websqlStorage.prototype.allDocs = function () {
var db = this._database;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "SELECT id FROM documents");
});
};
function sendBlobPart(db, id, name, blob, nbSlice) {
return new RSVP.Queue()
.push(function () {
......@@ -332,7 +323,41 @@
if (result.rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return id;
return name;
});
};
websqlStorage.prototype.hasCapacity = function (name) {
return (name === "list" || (name === "include"));
};
websqlStorage.prototype.buildQuery = function (options) {
var db = this._database,
query = "SELECT id";
if (options === undefined) { options = {}; }
if (options.include_docs === true) {
query += ", data AS doc";
}
query += " FROM documents ORDER BY id";
return new RSVP.Queue()
.push(function () {
return sqlExec(db, query, []);
})
.push(function (result) {
var array = [],
len = result.rows.length,
i;
for (i = 0; i < len; i += 1) {
array.push(result.rows[i]);
array[i].value = {};
if (array[i].doc !== undefined) {
array[i].doc = JSON.parse(array[i].doc);
}
}
return array;
});
};
......
......@@ -14,7 +14,7 @@
db = openDatabase('jio:qunit', '1.0', '', 2 * 1024 * 1024),
j;
function exec(db, transac, args) {
function exec(transac, args) {
return new RSVP.Promise(function (resolve, reject) {
db.transaction(function (tx) {
/*jslint unparam: true*/
......@@ -33,16 +33,16 @@
function deleteWebsql() {
return new RSVP.Queue()
.push(function () {
return exec(db, "DELETE FROM documents", []);
return exec("DELETE FROM documents", []);
})
.push(function () {
return exec(db, "DELETE FROM metadata", []);
return exec("DELETE FROM metadata", []);
})
.push(function () {
return exec(db, "DELETE FROM attachment", []);
return exec("DELETE FROM attachment", []);
})
.push(function () {
return exec(db, "DELETE FROM blob", []);
return exec("DELETE FROM blob", []);
});
}
......@@ -55,7 +55,7 @@
// websqlStorage.constructor
/////////////////////////////////////////////////////////////////
module("websqlStorage.constructor");
test("default unite value", function () {
test("creation of the storage", function () {
var jio = jIO.createJIO({
type: "websql",
database: "qunit"
......@@ -64,6 +64,18 @@
equal(jio.__type, "websql");
});
/////////////////////////////////////////////////////////////////
// websqlStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("websqlStorage.hasCapacity");
test("can list documents", function () {
var jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
ok(jio.hasCapacity("list"));
});
/////////////////////////////////////////////////////////////////
// websqlStorage.buildQuery
/////////////////////////////////////////////////////////////////
......@@ -86,7 +98,13 @@
return context.jio.allDocs();
})
.then(function (result) {
deepEqual(result, {});
deepEqual(result, {
"data": {
"rows": [
],
"total_rows": 0
}
});
})
.fail(function (error) {
ok(false, error);
......@@ -173,9 +191,7 @@
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.get
/////////////////////////////////////////////////////////////////
module("websqlStorage.get", {
setup: function () {
this.jio = jIO.createJIO({
......@@ -185,7 +201,6 @@
}
});
test("get inexistent document", function () {
var context = this;
stop();
......@@ -398,7 +413,39 @@
}
});
//bientot ici: de beaux tests.
test("remove document", function () {
var context = this;
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.put("foo", {});
})
.then(function () {
return exec("SELECT id FROM documents", []);
})
.then(function (selectResult) {
equal(selectResult.rows.length, 1, "putAttachment done");
})
.then(function () {
return context.jio.remove("foo");
})
.then(function (result) {
equal(result, "foo");
return exec("SELECT id FROM documents", []);
})
.then(function (selectResult) {
equal(selectResult.rows.length, 0, "remove done");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.getAttachment
......@@ -492,7 +539,7 @@
});
}
});
/*
test("remove attachment", function () {
var context = this,
attachment = "attachment";
......@@ -507,14 +554,30 @@
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
exec("SELECT * FROM attachment ")
return context.jio.getAttachment("foo", attachment,
{"start": 15, "end": 25});
return exec("SELECT id, attachment FROM attachment UNION ALL" +
" SELECT id, attachment FROM blob", []);
})
*/
//bientot ici: de beaux tests.
.then(function (selectResult) {
equal(selectResult.rows.length, 2, "putAttachment done");
})
.then(function () {
return context.jio.removeAttachment("foo", attachment);
})
.then(function (result) {
equal(result, attachment);
return exec("SELECT id, attachment FROM attachment UNION ALL" +
" SELECT id, attachment FROM blob", []);
})
.then(function (selectResult) {
equal(selectResult.rows.length, 0, "removeAttachment done");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.putAttachment
......@@ -528,5 +591,41 @@
}
});
//bientot ici: de beaux tests.
test("put attachment", function () {
var context = this,
attachment = "attachment";
stop();
expect(2);
deleteWebsql()
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
return exec("SELECT id, attachment FROM attachment UNION ALL" +
" SELECT id, attachment FROM blob", []);
})
.then(function (selectResult) {
equal(selectResult.rows.length, 2, "putAttachment done");
})
.then(function () {
return context.jio.getAttachment("foo", attachment);
})
.then(function (result) {
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
equal(result.target.result, big_string, "attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, openDatabase, Blob));
\ No newline at end of file
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