Commit 23273a36 authored by lucas.parsy's avatar lucas.parsy

various fixes in websqlstorage.

added error callback on db.transaction.
refactored sqlExec to do multiples queries in one transaction.
refactored database initialisation.
removed index functionality from websql.
modified tests to follow above changes.
parent cf923fa7
......@@ -21,61 +21,54 @@
* @constructor
*/
/*jslint unparam: true*/
function sqlExec(db, transac, args) {
return new RSVP.Promise(function (resolve, reject) {
db.transaction(function (tx) {
/*jslint unparam: true*/
tx.executeSql(transac, args,
function (tx, results) {
resolve(results);
},
function (tx, error) {
var len = transac.length,
res = [],
i;
function transacSuccess(tx, results) {
res.push(results);
if (res.length === len) {
resolve(res);
}
}
function transacFailure(tx, error) {
reject(error);
return true;
}
for (i = 0; i < len; i += 1) {
tx.executeSql(transac[i], args[i], transacSuccess, transacFailure);
}
}, function (tx, error) {
reject(error);
});
});
/*jslint unparam: false*/
});
}
/*jslint unparam: false*/
function initDatabase(that) {
var db = that._database;
if (that._base_created === true) {return; }
that._base_created = true;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "CREATE TABLE IF NOT EXISTS documents" +
"(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)");
})
.push(function () {
return sqlExec(db, "CREATE TABLE IF NOT EXISTS metadata" +
"(doc VARCHAR, prop VARCHAR, stringValue " +
"TEXT, nbrValue INT)");
})
.push(function () {
return sqlExec(db, "CREATE TABLE IF NOT EXISTS attachment" +
function initDatabase(db) {
var queries;
queries = ["CREATE TABLE IF NOT EXISTS documents" +
"(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)",
"CREATE TABLE IF NOT EXISTS attachment" +
"(id VARCHAR, attachment VARCHAR, " +
"CONSTRAINT un_id_attachment UNIQUE (id, attachment))");
})
.push(function () {
return sqlExec(db, "CREATE TABLE IF NOT EXISTS blob" +
"(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)");
})
.push(function () {
return sqlExec(db, "CREATE TRIGGER IF NOT EXISTS jIOremove " +
"CONSTRAINT un_id_attachment UNIQUE (id, attachment))",
"CREATE TABLE IF NOT EXISTS blob" +
"(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)",
"CREATE TRIGGER IF NOT EXISTS jIOremove " +
"BEFORE DELETE ON documents FOR EACH ROW BEGIN DELETE " +
"FROM metadata WHERE doc = OLD.id; DELETE FROM " +
"attachment WHERE id = OLD.id;END;");
})
.push(function () {
return sqlExec(db, "CREATE TRIGGER IF NOT EXISTS jIOupdate " +
"BEFORE INSERT ON documents FOR EACH ROW BEGIN " +
"DELETE FROM metadata WHERE doc = NEW.id;END;");
})
.push(function () {
return sqlExec(db, "CREATE TRIGGER IF NOT EXISTS jIOremoveAttachment" +
"FROM attachment WHERE id = OLD.id;END;",
"CREATE TRIGGER IF NOT EXISTS jIOremoveAttachment " +
"BEFORE DELETE ON attachment FOR EACH ROW " +
"BEGIN DELETE from blob WHERE id = OLD.id " +
"AND attachment = OLD.attachment;END;");
"AND attachment = OLD.attachment;END;"];
return new RSVP.Queue()
.push(function () {
return sqlExec(db, queries, [[], [], [], [], []]);
});
}
......@@ -92,55 +85,24 @@
throw new TypeError("blob_len parameter must be a number >= 20");
}
this._blob_length = spec.blob_length || 2000000;
this._base_created = false;
}
function addProperty(db, id, prop, value) {
var type = typeof value;
if (type === "string") {
return (sqlExec(db, "INSERT INTO metadata(doc, prop, stringValue) " +
"VALUES(?, ?, ?)",
[id, prop, value]));
}
if (type === "number") {
return (sqlExec(db, "INSERT INTO metadata(doc, prop, nbrValue) " +
"VALUES(?, ?, ?)",
[id, prop, value]));
}
this._init_base = initDatabase(this._database);
}
websqlStorage.prototype.put = function (id, param) {
var db = this._database,
that = this,
dataString = JSON.stringify(param),
i,
arrayLen;
dataString = JSON.stringify(param);
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "INSERT OR REPLACE INTO " +
"documents(id, data) VALUES(?,?)",
[id, dataString]);
return sqlExec(db, ["INSERT OR REPLACE INTO " +
"documents(id, data) VALUES(?,?)"],
[[id, dataString]]);
})
.push(function () {
var prop;
for (prop in param) {
if (param.hasOwnProperty(prop)) {
if (param[prop] instanceof Array) {
arrayLen = param[prop].length;
for (i = 0; i < arrayLen; i += 1) {
addProperty(db, id, prop, param[prop][i]);
}
} else {
addProperty(db, id, prop, param[prop]);
}
}
}
return id;
});
};
......@@ -151,13 +113,13 @@
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "DELETE FROM documents WHERE id = ?", [id]);
return sqlExec(db, ["DELETE FROM documents WHERE id = ?"], [[id]]);
})
.push(function (result) {
if (result.rowsAffected === 0) {
if (result[0].rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return id;
......@@ -171,16 +133,16 @@
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "SELECT data FROM documents WHERE id = ?", [id]);
return sqlExec(db, ["SELECT data FROM documents WHERE id = ?"], [[id]]);
})
.push(function (result) {
if (result.rows.length === 0) {
if (result[0].rows.length === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return JSON.parse(result.rows[0].data);
return JSON.parse(result[0].rows[0].data);
});
};
......@@ -190,38 +152,37 @@
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]);
return sqlExec(db, ["SELECT id FROM documents WHERE id = ?"], [[id]]);
})
.push(function (result) {
if (result.rows[0]["COUNT(*)"] === 0) {
if (result[0].rows.length === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return sqlExec(db, "SELECT attachment FROM attachment WHERE id = ?",
[id]);
return sqlExec(db, ["SELECT attachment FROM attachment WHERE id = ?"],
[[id]]);
})
.push(function (result) {
var len = result.rows.length,
var len = result[0].rows.length,
obj = {},
i;
for (i = 0; i < len; i += 1) {
obj[result.rows[i].attachment] = {};
obj[result[0].rows[i].attachment] = {};
}
return obj;
});
};
function sendBlobPart(db, id, name, blob, nbSlice, queue) {
function sendBlobPart(blob, args, index, queue) {
queue.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (strBlob) {
strBlob = strBlob.currentTarget.result;
return sqlExec(db, "INSERT INTO blob(id, attachment, part, blob)" +
"VALUES(?, ?, ?, ?)", [id, name, nbSlice, strBlob]);
args[index + 3].push(strBlob.currentTarget.result);
return;
});
}
......@@ -232,36 +193,40 @@
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]);
return sqlExec(db, ["SELECT id FROM documents WHERE id = ?"], [[id]]);
})
.push(function (result) {
if (result.rows[0]["COUNT(*)"] === 0) {
throw new jIO.util.jIOError("Cannot access subdocument", 404);
}
return sqlExec(db, "INSERT OR REPLACE INTO attachment(id, attachment)" +
" VALUES(?, ?)", [id, name]);
})
.push(function () {
return sqlExec(db, "DELETE FROM blob WHERE " +
"id = ? AND attachment = ?", [id, name]);
})
.push(function () {
return sqlExec(db, "INSERT INTO " +
"blob(id, attachment, part, blob) VALUES(?, ?, ?, ?)",
[id, name, -1, blob.type || "application/octet-stream"]);
})
.push(function () {
var blobSize = blob.size,
var queries = [],
args = [],
blobSize = blob.size,
queue = new RSVP.Queue(),
i,
index;
if (result[0].rows.length === 0) {
throw new jIO.util.jIOError("Cannot access subdocument", 404);
}
queries.push("INSERT OR REPLACE INTO attachment(id, attachment)" +
" VALUES(?, ?)");
args.push([id, name]);
queries.push("DELETE FROM blob WHERE id = ? AND attachment = ?");
args.push([id, name]);
queries.push("INSERT INTO blob(id, attachment, part, blob)" +
"VALUES(?, ?, ?, ?)");
args.push([id, name, -1, blob.type || "application/octet-stream"]);
for (i = 0, index = 0; i < blobSize; i += partSize, index += 1) {
sendBlobPart(db, id, name, blob.slice(i, i + partSize), index, queue);
queries.push("INSERT INTO blob(id, attachment, part, blob)" +
"VALUES(?, ?, ?, ?)");
args.push([id, name, index]);
sendBlobPart(blob.slice(i, i + partSize), args, index, queue);
}
queue.push(function () {
return sqlExec(db, queries, args);
});
return queue;
});
};
......@@ -297,7 +262,7 @@
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
var command = "SELECT part, blob FROM blob WHERE id = ? AND " +
......@@ -308,7 +273,7 @@
command += " AND part <= ?";
args.push(end_index);
}
return sqlExec(db, command, args);
return sqlExec(db, [command], [args]);
})
.push(function (response) {
var i,
......@@ -316,7 +281,7 @@
blob,
type;
response = response.rows;
response = response[0].rows;
if (response.length === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
......@@ -342,16 +307,17 @@
websqlStorage.prototype.removeAttachment = function (id, name) {
var db = this._database,
that = this;
return new RSVP.Queue()
.push(function () {
return initDatabase(that);
return that._init_base;
})
.push(function () {
return sqlExec(db, "DELETE FROM attachment WHERE " +
"id = ? AND attachment = ?", [id, name]);
return sqlExec(db, ["DELETE FROM attachment WHERE " +
"id = ? AND attachment = ?"], [[id, name]]);
})
.push(function (result) {
if (result.rowsAffected === 0) {
if (result[0].rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return name;
......@@ -367,26 +333,25 @@
that = this,
query = "SELECT id";
return new RSVP.Queue()
.push(function () {
return that._init_base;
})
.push(function () {
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 initDatabase(that);
})
.push(function () {
return sqlExec(db, query, []);
return sqlExec(db, [query], [[]]);
})
.push(function (result) {
var array = [],
len = result.rows.length,
len = result[0].rows.length,
i;
for (i = 0; i < len; i += 1) {
array.push(result.rows[i]);
array.push(result[0].rows[i]);
array[i].value = {};
if (array[i].doc !== undefined) {
array[i].doc = JSON.parse(array[i].doc);
......
......@@ -15,12 +15,12 @@
j;
function getSpy() {
return new RSVP.Promise(function (resolve) {
return new RSVP.Promise(function (resolve, reject) {
var spy;
db.transaction(function (tx) {
spy = sinon.spy(tx.constructor.prototype, "executeSql");
resolve(spy);
});
}, reject);
});
}
......@@ -41,40 +41,34 @@
}
function spyStorageCreation(context) {
var eleven = false;
if (context.spy.callCount >= 12) {
eleven = true;
var seven = false;
if (context.spy.callCount >= 5) {
seven = true;
}
equal(eleven, true);
equal(context.spy.args[0][0],
equal(seven, true);
/* equal(context.spy.args[0][0],
'SELECT name FROM sqlite_master WHERE type ="table" ' +
'AND name != "__WebKitDatabaseInfoTable__"');
equal(context.spy.args[1][0], 'DELETE FROM documents');
equal(context.spy.args[2][0], 'DELETE FROM metadata');
equal(context.spy.args[3][0], 'DELETE FROM attachment');
equal(context.spy.args[4][0], 'DELETE FROM blob');
equal(context.spy.args[5][0],
*/
equal(context.spy.args[0][0],
'CREATE TABLE IF NOT EXISTS documents(id VARCHAR PRIMARY ' +
'KEY NOT NULL, data TEXT)');
equal(context.spy.args[6][0],
'CREATE TABLE IF NOT EXISTS metadata(doc VARCHAR, prop ' +
'VARCHAR, stringValue TEXT, nbrValue INT)');
equal(context.spy.args[7][0],
equal(context.spy.args[1][0],
'CREATE TABLE IF NOT EXISTS attachment(id VARCHAR, attachment' +
' VARCHAR, CONSTRAINT un_id_attachment UNIQUE (id, attachment))');
equal(context.spy.args[8][0],
equal(context.spy.args[2][0],
'CREATE TABLE IF NOT EXISTS blob(id VARCHAR, attachment' +
' VARCHAR, part INT, blob TEXT)');
equal(context.spy.args[9][0],
equal(context.spy.args[3][0],
'CREATE TRIGGER IF NOT EXISTS jIOremove BEFORE DELETE ON ' +
'documents FOR EACH ROW BEGIN DELETE FROM metadata WHERE ' +
'doc = OLD.id; DELETE FROM attachment WHERE id = OLD.id;END;');
equal(context.spy.args[10][0],
'CREATE TRIGGER IF NOT EXISTS jIOupdate BEFORE INSERT ON ' +
'documents FOR EACH ROW BEGIN DELETE FROM metadata WHERE ' +
'doc = NEW.id;END;');
equal(context.spy.args[11][0],
'CREATE TRIGGER IF NOT EXISTS jIOremoveAttachmentBEFORE DELETE ON ' +
'documents FOR EACH ROW BEGIN DELETE FROM attachment ' +
'WHERE id = OLD.id;END;');
equal(context.spy.args[4][0],
'CREATE TRIGGER IF NOT EXISTS jIOremoveAttachment BEFORE DELETE ON ' +
'attachment FOR EACH ROW BEGIN DELETE from blob WHERE id = OLD.id ' +
'AND attachment = OLD.attachment;END;');
}
......@@ -112,31 +106,86 @@
/////////////////////////////////////////////////////////////////
// websqlStorage.constructor
/////////////////////////////////////////////////////////////////
module("websqlStorage.constructor");
test("creation of the storage", function () {
var jio = jIO.createJIO({
module("websqlStorage.constructor", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
this.spy = getSpy();
},
teardown: function () {
this.spy.restore();
delete this.spy;
}
});
equal(jio.__type, "websql");
test("creation of the storage", function () {
var context = this;
stop();
expect(1);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
equal(context.jio.__type, "websql");
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("websqlStorage.hasCapacity");
test("can list documents", function () {
var jio = jIO.createJIO({
module("websqlStorage.hasCapacity", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
ok(jio.hasCapacity("list"));
this.spy = getSpy();
},
teardown: function () {
this.spy.restore();
delete this.spy;
}
});
test("can list documents", function () {
var context = this;
stop();
expect(1);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
ok(context.jio.hasCapacity("list"));
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("websqlStorage.buildQuery", {
setup: function () {
this.jio = jIO.createJIO({
......@@ -154,24 +203,26 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(15);
expect(8);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.allDocs();
})
.then(function () {
equal(context.spy.args[5][0], 'SELECT id FROM documents ORDER BY id');
deepEqual(context.spy.args[5][1], []);
spyStorageCreation(context);
equal(context.spy.args[12][0], 'SELECT id FROM documents ORDER BY id');
deepEqual(context.spy.args[12][1], []);
return;
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -181,32 +232,33 @@
test("spy webSQL usage with include_docs", function () {
var context = this;
stop();
expect(15);
expect(8);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.allDocs({include_docs: true});
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'SELECT id, data AS doc FROM documents ORDER BY id');
deepEqual(context.spy.args[12][1], []);
deepEqual(context.spy.args[5][1], []);
spyStorageCreation(context);
return;
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
});
});
test("empty result", function () {
var context = this;
stop();
......@@ -214,7 +266,6 @@
context.spy.then(function (value) {
context.spy = value;
return deleteWebsql();
})
.then(function () {
return context.jio.allDocs();
......@@ -227,9 +278,11 @@
"total_rows": 0
}
});
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -243,7 +296,6 @@
context.spy.then(function (value) {
context.spy = value;
return deleteWebsql();
})
.then(function () {
return RSVP.all([
......@@ -267,9 +319,11 @@
"total_rows": 2
}
});
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -284,7 +338,6 @@
context.spy.then(function (value) {
context.spy = value;
return deleteWebsql();
})
.then(function () {
return RSVP.all([
......@@ -310,9 +363,11 @@
"total_rows": 2
}
});
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -337,13 +392,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(19);
expect(10);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -352,19 +404,20 @@
return context.jio.get("foo");
})
.then(function () {
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1], ['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
deepEqual(context.spy.args[5][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[6][0],
'SELECT data FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
deepEqual(context.spy.args[6][1], ['foo']);
spyStorageCreation(context);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -481,13 +534,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(21);
expect(12);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -497,21 +547,22 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1], ['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
equal(context.spy.args[15][0],
deepEqual(context.spy.args[5][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[6][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0],
'SELECT attachment FROM attachment WHERE id = ?');
deepEqual(context.spy.args[15][1], ['foo']);
deepEqual(context.spy.args[7][1], ['foo']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -623,13 +674,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(17);
expect(8);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -639,15 +687,16 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1], ['foo', 'title', 'bar']);
deepEqual(context.spy.args[5][1], ['foo', '{"title":"bar"}']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -698,13 +747,10 @@
test("spy webSQL usage with one document", function () {
var context = this;
stop();
expect(19);
expect(10);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -714,17 +760,18 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1], ['foo', 'title', 'bar']);
equal(context.spy.args[14][0], 'DELETE FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
deepEqual(context.spy.args[5][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[6][0], 'DELETE FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -734,75 +781,74 @@
test("spy webSQL usage with two attachments", function () {
var context = this;
stop();
expect(39);
expect(30);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
return context.jio.putAttachment("foo", "attachment1", "bar");
})
.then(function () {
return RSVP.all([
context.jio.putAttachment("foo", "attachment1", "bar"),
context.jio.putAttachment("foo", "attachment2", "bar2")
]);
return context.jio.putAttachment("foo", "attachment2", "bar2");
})
.then(function () {
return context.jio.remove("foo");
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1], ['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
equal(context.spy.args[15][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[15][1], ['foo']);
equal(context.spy.args[16][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[16][1], ['foo', 'attachment1']);
equal(context.spy.args[17][0],
deepEqual(context.spy.args[5][1], ['foo', '{"title":"bar"}']);
equal(context.spy.args[6][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[17][1], ['foo', 'attachment2']);
equal(context.spy.args[18][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[18][1], ['foo', 'attachment1']);
equal(context.spy.args[19][0],
deepEqual(context.spy.args[7][1], ['foo', 'attachment1']);
equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[19][1], ['foo', 'attachment2']);
equal(context.spy.args[20][0],
deepEqual(context.spy.args[8][1], ['foo', 'attachment1']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, ' +
'part, blob) VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[20][1],
'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1],
['foo', 'attachment1', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[21][0],
'INSERT INTO blob(id, attachment, ' +
'part, blob) VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[21][1],
['foo', 'attachment2', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[22][0],
equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[22][1],
deepEqual(context.spy.args[10][1],
['foo', 'attachment1', 0, 'data:;base64,YmFy']);
equal(context.spy.args[23][0],
equal(context.spy.args[11][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[11][1], ['foo']);
equal(context.spy.args[12][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[12][1], ['foo', 'attachment2']);
equal(context.spy.args[13][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[13][1], ['foo', 'attachment2']);
equal(context.spy.args[14][0],
'INSERT INTO blob(id, attachment, ' +
'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[14][1],
['foo', 'attachment2', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[15][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[23][1],
deepEqual(context.spy.args[15][1],
['foo', 'attachment2', 0, 'data:;base64,YmFyMg==']);
equal(context.spy.args[24][0], 'DELETE FROM documents WHERE id = ?');
deepEqual(context.spy.args[24][1], ['foo']);
equal(context.spy.args[16][0], 'DELETE FROM documents WHERE id = ?');
deepEqual(context.spy.args[16][1], ['foo']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -868,13 +914,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(31);
expect(22);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -887,45 +930,45 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1],
deepEqual(context.spy.args[5][1],
['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1],
['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
equal(context.spy.args[15][0],
equal(context.spy.args[6][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[15][1], ['foo', 'attachment']);
equal(context.spy.args[16][0],
deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[16][1], ['foo', 'attachment']);
equal(context.spy.args[17][0],
'INSERT INTO blob(id, attachment, part, blob) ' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[17][1],
deepEqual(context.spy.args[9][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[18][0],
equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[18][1],
deepEqual(context.spy.args[10][1],
['foo', 'attachment', 0,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[19][0],
equal(context.spy.args[11][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[19][1],
deepEqual(context.spy.args[11][1],
['foo', 'attachment', 1,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[20][0],
equal(context.spy.args[12][0],
'SELECT part, blob FROM blob WHERE id = ? ' +
'AND attachment = ? AND part >= ?');
deepEqual(context.spy.args[20][1], ['foo', 'attachment', -1]);
deepEqual(context.spy.args[12][1], ['foo', 'attachment', -1]);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -1027,13 +1070,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(29);
expect(20);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -1046,40 +1086,40 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1],
deepEqual(context.spy.args[5][1],
['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1],
['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
equal(context.spy.args[15][0],
equal(context.spy.args[6][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[15][1], ['foo', 'attachment']);
equal(context.spy.args[16][0],
deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[16][1], ['foo', 'attachment']);
equal(context.spy.args[17][0],
'INSERT INTO blob(id, attachment, part, blob) ' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[17][1],
deepEqual(context.spy.args[9][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[18][0],
equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[18][1],
deepEqual(context.spy.args[10][1],
['foo', 'attachment', 0,
"data:;base64,YWFhYWFhYWFhYWFhYWFhYWF" +
"hYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="]);
equal(context.spy.args[19][0],
equal(context.spy.args[11][0],
'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[19][1], ['foo', 'attachment']);
deepEqual(context.spy.args[11][1], ['foo', 'attachment']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......@@ -1150,13 +1190,10 @@
test("spy webSQL usage", function () {
var context = this;
stop();
expect(29);
expect(20);
context.spy.then(function (value) {
context.spy = value;
return;
})
.then(function () {
return deleteWebsql();
})
.then(function () {
return context.jio.put("foo", {"title": "bar"});
......@@ -1166,41 +1203,41 @@
})
.then(function () {
spyStorageCreation(context);
equal(context.spy.args[12][0],
equal(context.spy.args[5][0],
'INSERT OR REPLACE INTO documents(id, data) VALUES(?,?)');
deepEqual(context.spy.args[12][1],
deepEqual(context.spy.args[5][1],
['foo', '{"title":"bar"}']);
equal(context.spy.args[13][0],
'INSERT INTO metadata(doc, prop, stringValue) VALUES(?, ?, ?)');
deepEqual(context.spy.args[13][1],
['foo', 'title', 'bar']);
equal(context.spy.args[14][0],
'SELECT COUNT(*) FROM documents WHERE id = ?');
deepEqual(context.spy.args[14][1], ['foo']);
equal(context.spy.args[15][0],
equal(context.spy.args[6][0],
'SELECT id FROM documents WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)');
deepEqual(context.spy.args[15][1], ['foo', 'attachment']);
equal(context.spy.args[16][0],
deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[16][1], ['foo', 'attachment']);
equal(context.spy.args[17][0],
'INSERT INTO blob(id, attachment, part, blob) ' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[17][1],
deepEqual(context.spy.args[9][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[18][0],
equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[18][1],
deepEqual(context.spy.args[10][1],
['foo', 'attachment', 0,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[19][0],
equal(context.spy.args[11][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[19][1],
deepEqual(context.spy.args[11][1],
['foo', 'attachment', 1,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
})
.then(function () {
return deleteWebsql();
})
.fail(function (error) {
ok(false, error);
return deleteWebsql();
})
.always(function () {
start();
......
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