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 @@ ...@@ -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) { function sendBlobPart(db, id, name, blob, nbSlice) {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -332,7 +323,41 @@ ...@@ -332,7 +323,41 @@
if (result.rowsAffected === 0) { if (result.rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404); 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 @@ ...@@ -14,7 +14,7 @@
db = openDatabase('jio:qunit', '1.0', '', 2 * 1024 * 1024), db = openDatabase('jio:qunit', '1.0', '', 2 * 1024 * 1024),
j; j;
function exec(db, transac, args) { function exec(transac, args) {
return new RSVP.Promise(function (resolve, reject) { return new RSVP.Promise(function (resolve, reject) {
db.transaction(function (tx) { db.transaction(function (tx) {
/*jslint unparam: true*/ /*jslint unparam: true*/
...@@ -33,16 +33,16 @@ ...@@ -33,16 +33,16 @@
function deleteWebsql() { function deleteWebsql() {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return exec(db, "DELETE FROM documents", []); return exec("DELETE FROM documents", []);
}) })
.push(function () { .push(function () {
return exec(db, "DELETE FROM metadata", []); return exec("DELETE FROM metadata", []);
}) })
.push(function () { .push(function () {
return exec(db, "DELETE FROM attachment", []); return exec("DELETE FROM attachment", []);
}) })
.push(function () { .push(function () {
return exec(db, "DELETE FROM blob", []); return exec("DELETE FROM blob", []);
}); });
} }
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
// websqlStorage.constructor // websqlStorage.constructor
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("websqlStorage.constructor"); module("websqlStorage.constructor");
test("default unite value", function () { test("creation of the storage", function () {
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "websql", type: "websql",
database: "qunit" database: "qunit"
...@@ -64,6 +64,18 @@ ...@@ -64,6 +64,18 @@
equal(jio.__type, "websql"); 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 // websqlStorage.buildQuery
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -86,7 +98,13 @@ ...@@ -86,7 +98,13 @@
return context.jio.allDocs(); return context.jio.allDocs();
}) })
.then(function (result) { .then(function (result) {
deepEqual(result, {}); deepEqual(result, {
"data": {
"rows": [
],
"total_rows": 0
}
});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -173,9 +191,7 @@ ...@@ -173,9 +191,7 @@
}); });
}); });
/////////////////////////////////////////////////////////////////
// websqlStorage.get
/////////////////////////////////////////////////////////////////
module("websqlStorage.get", { module("websqlStorage.get", {
setup: function () { setup: function () {
this.jio = jIO.createJIO({ this.jio = jIO.createJIO({
...@@ -185,7 +201,6 @@ ...@@ -185,7 +201,6 @@
} }
}); });
test("get inexistent document", function () { test("get inexistent document", function () {
var context = this; var context = this;
stop(); stop();
...@@ -398,7 +413,39 @@ ...@@ -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 // websqlStorage.getAttachment
...@@ -492,7 +539,7 @@ ...@@ -492,7 +539,7 @@
}); });
} }
}); });
/*
test("remove attachment", function () { test("remove attachment", function () {
var context = this, var context = this,
attachment = "attachment"; attachment = "attachment";
...@@ -507,14 +554,30 @@ ...@@ -507,14 +554,30 @@
return context.jio.putAttachment("foo", attachment, big_string); return context.jio.putAttachment("foo", attachment, big_string);
}) })
.then(function () { .then(function () {
exec("SELECT * FROM attachment ") return exec("SELECT id, attachment FROM attachment UNION ALL" +
return context.jio.getAttachment("foo", attachment, " SELECT id, attachment FROM blob", []);
{"start": 15, "end": 25});
}) })
*/ .then(function (selectResult) {
equal(selectResult.rows.length, 2, "putAttachment done");
})
//bientot ici: de beaux tests. .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 // websqlStorage.putAttachment
...@@ -528,5 +591,41 @@ ...@@ -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)); }(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