Commit f5382029 authored by lucas.parsy's avatar lucas.parsy

first commit websql

websql storage operational, lacks allDocs and query support
half of the tests written.
error handling to improve.
parent 08728e58
......@@ -173,7 +173,7 @@ module.exports = function (grunt) {
'src/jio.js',
'node_modules/rusha/rusha.js',
/*
'src/jio.storage/replicatestorage.js',
'src/jio.storage/shastorage.js',
'src/jio.storage/uuidstorage.js',
......@@ -182,12 +182,16 @@ module.exports = function (grunt) {
'src/jio.storage/zipstorage.js',
'src/jio.storage/cryptstorage.js',
'src/jio.storage/dropboxstorage.js',
*/
'src/jio.storage/websqlstorage.js',
'src/jio.storage/davstorage.js',
/*
'src/jio.storage/unionstorage.js',
'src/jio.storage/erp5storage.js',
'src/jio.storage/querystorage.js',
'src/jio.storage/drivetojiomapping.js',
'src/jio.storage/documentstorage.js',
*/
'src/jio.storage/indexeddbstorage.js'
],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
......
......@@ -34,21 +34,22 @@ zip:
@cp lib/rsvp/rsvp-custom.amd.js $(TMPDIR)/jio/
@cp lib/jquery/jquery.js $(TMPDIR)/jio/
@cp lib/require/require.js $(TMPDIR)/jio/
@cp src/jio.storage/localstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/davstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/dropboxstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/erp5storage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/indexstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/gidstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/zipstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/s3storage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/xwikistorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/localstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/davstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/dropboxstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/websqlstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/erp5storage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/indexstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/gidstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/zipstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/s3storage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
# @cp src/jio.storage/xwikistorage.js $(TMPDIR)/jio/storage/
@cd $(TMPDIR) && zip -q $(ZIP_FULL) -r jio
@mv $(TMPDIR)/$(ZIP_FULL) ./
@rm -rf $(TMPDIR)/jio
......@@ -67,21 +68,22 @@ zip:
@$(UGLIFY) lib/rsvp/rsvp-custom.amd.js >$(TMPDIR)/jio/rsvp-custom.amd.min.js 2>/dev/null
@cp lib/jquery/jquery.min.js $(TMPDIR)/jio/
@$(UGLIFY) lib/require/require.js >$(TMPDIR)/jio/require.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/localstorage.js >$(TMPDIR)/jio/storage/localstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/davstorage.js >$(TMPDIR)/jio/storage/davstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/localstorage.js >$(TMPDIR)/jio/storage/localstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/davstorage.js >$(TMPDIR)/jio/storage/davstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/dropboxstorage.js >$(TMPDIR)/jio/storage/dropboxstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/erp5storage.js >$(TMPDIR)/jio/storage/erp5storage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/indexstorage.js >$(TMPDIR)/jio/storage/indexstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/gidstorage.js >$(TMPDIR)/jio/storage/gidstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/zipstorage.js >$(TMPDIR)/jio/storage/zipstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/s3storage.js >$(TMPDIR)/jio/storage/s3storage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/xwikistorage.js >$(TMPDIR)/jio/storage/xwikistorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/websqlstorage.js >$(TMPDIR)/jio/storage/websqlstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/erp5storage.js >$(TMPDIR)/jio/storage/erp5storage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/indexstorage.js >$(TMPDIR)/jio/storage/indexstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/gidstorage.js >$(TMPDIR)/jio/storage/gidstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/zipstorage.js >$(TMPDIR)/jio/storage/zipstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/s3storage.js >$(TMPDIR)/jio/storage/s3storage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
# @$(UGLIFY) src/jio.storage/xwikistorage.js >$(TMPDIR)/jio/storage/xwikistorage.min.js 2>/dev/null
@cd $(TMPDIR) && zip -q $(ZIP_MINI) -r jio
@mv $(TMPDIR)/$(ZIP_MINI) ./
@rm -rf $(TMPDIR)
......
/*
* Copyright 2013, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/**
* JIO Websql Storage. Type = "websql".
* websql "database" storage.
*/
/*global Blob, jIO, RSVP, openDatabase*/
/*jslint nomen: true*/
(function (jIO, RSVP, Blob, openDatabase) {
"use strict";
/**
* The JIO Websql Storage extension
*
* @class websqlStorage
* @constructor
*/
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) {
reject(error);
});
});
/*jslint unparam: false*/
});
}
function createDatabase(db) {
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" +
"(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 " +
"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" +
"BEFORE DELETE ON attachment FOR EACH ROW " +
"BEGIN DELETE from blob WHERE id = OLD.id " +
"AND attachment = OLD.attachment;END;");
});
}
function websqlStorage(spec) {
if (typeof spec.database !== 'string' || !spec.database) {
throw new TypeError("database must be a string " +
"which contains more than one character.");
}
this._database = openDatabase("jio:" + spec.database,
'1.0', '', 2 * 1024 * 1024);
if (spec.blob_length &&
(typeof spec.blob_length !== "number" ||
spec.blob_length < 20)) {
throw new TypeError("blob_len parameter must be a number >= 20");
}
this._blob_length = spec.blob_length || 2000000;
createDatabase(this._database);
}
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]));
}
}
websqlStorage.prototype.put = function (id, param) {
var db = this._database,
dataString = JSON.stringify(param),
i,
arrayLen;
return new RSVP.Queue()
.push(function () {
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;
});
};
websqlStorage.prototype.remove = function (id) {
var db = this._database;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "DELETE FROM documents WHERE id = ?", [id]);
})
.push(function (result) {
if (result.rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return id;
});
};
websqlStorage.prototype.get = function (id) {
var db = this._database;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "SELECT data FROM documents WHERE id = ?", [id]);
})
.push(function (result) {
if (result.rows.length === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return JSON.parse(result.rows[0].data);
});
};
websqlStorage.prototype.allAttachments = function (id) {
var db = this._database;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]);
})
.push(function (result) {
if (result.rows[0]["COUNT(*)"] === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return sqlExec(db, "SELECT attachment FROM attachment WHERE id = ?",
[id]);
})
.push(function (result) {
var len = result.rows.length,
obj = {},
i;
for (i = 0; i < len; i += 1) {
obj[result.rows[i].attachment] = {};
}
return obj;
});
};
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 () {
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]);
});
}
websqlStorage.prototype.putAttachment = function (id, name, blob) {
var db = this._database,
partSize = this._blob_length;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "SELECT COUNT(*) 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,
i,
index;
for (i = 0, index = 0; i < blobSize; i += partSize, index += 1) {
sendBlobPart(db, id, name, blob.slice(i, i + partSize), index);
}
});
};
websqlStorage.prototype.getAttachment = function (id, name, options) {
var db = this._database,
partSize = this._blob_length,
start,
end,
start_index,
end_index;
if (options === undefined) { options = {}; }
start = options.start || 0;
end = options.end || -1;
if (start < 0 || (options.end !== undefined && options.end < 0)) {
throw new jIO.util.jIOError("_start and _end must be positive",
400);
}
if (start > end && end !== -1) {
throw new jIO.util.jIOError("_start is greater than _end",
400);
}
start_index = Math.floor(start / partSize);
if (start === 0) { start_index -= 1; }
end_index = Math.floor(end / partSize);
if (end % partSize === 0) {
end_index -= 1;
}
return new RSVP.Queue()
.push(function () {
var command = "SELECT part, blob FROM blob WHERE id = ? AND " +
"attachment = ? AND part >= ?",
args = [id, name, start_index];
if (end !== -1) {
command += " AND part <= ?";
args.push(end_index);
}
return sqlExec(db, command, args);
})
.push(function (response) {
var i,
blobArray = [],
blob,
type;
response = response.rows;
if (response.length === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
for (i = 0; i < response.length; i += 1) {
if (response[i].part === -1) {
type = response[i].blob;
start_index += 1;
} else {
blobArray.push(jIO.util.dataURItoBlob(response[i].blob));
}
}
if ((start === 0) && (options.end === undefined)) {
return new Blob(blobArray, {type: type});
}
blob = new Blob(blobArray, {});
return blob.slice(start - (start_index * partSize),
end === -1 ? blob.size :
end - (start_index * partSize),
"application/octet-stream");
});
};
websqlStorage.prototype.removeAttachment = function (id, name) {
var db = this._database;
return new RSVP.Queue()
.push(function () {
return sqlExec(db, "DELETE FROM attachment WHERE " +
"id = ? AND attachment = ?", [id, name]);
})
.push(function (result) {
if (result.rowsAffected === 0) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return id;
});
};
jIO.addStorage('websql', websqlStorage);
}(jIO, RSVP, Blob, openDatabase));
/*jslint nomen: true */
/*global openDatabase, Blob*/
(function (jIO, QUnit, openDatabase, Blob) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
big_string = "",
db = openDatabase('jio:qunit', '1.0', '', 2 * 1024 * 1024),
j;
function exec(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) {
reject(error);
});
});
/*jslint unparam: false*/
});
}
function deleteWebsql() {
return new RSVP.Queue()
.push(function () {
return exec(db, "DELETE FROM documents", []);
})
.push(function () {
return exec(db, "DELETE FROM metadata", []);
})
.push(function () {
return exec(db, "DELETE FROM attachment", []);
})
.push(function () {
return exec(db, "DELETE FROM blob", []);
});
}
for (j = 0; j < 40; j += 1) {
big_string += "a";
}
/////////////////////////////////////////////////////////////////
// websqlStorage.constructor
/////////////////////////////////////////////////////////////////
module("websqlStorage.constructor");
test("default unite value", function () {
var jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
equal(jio.__type, "websql");
});
/////////////////////////////////////////////////////////////////
// websqlStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("websqlStorage.buildQuery", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
test("empty result", function () {
var context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.allDocs();
})
.then(function (result) {
deepEqual(result, {});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("list all documents", function () {
var context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return RSVP.all([
context.jio.put("2", {"title": "title2"}),
context.jio.put("1", {"title": "title1"})
]);
})
.then(function () {
return context.jio.allDocs();
})
.then(function (result) {
deepEqual(result, {
"data": {
"rows": [{
"id": "1",
"value": {}
}, {
"id": "2",
"value": {}
}],
"total_rows": 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("handle include_docs", function () {
var context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return RSVP.all([
context.jio.put("2", {"title": "title2"}),
context.jio.put("1", {"title": "title1"})
]);
})
.then(function () {
return context.jio.allDocs({include_docs: true});
})
.then(function (result) {
deepEqual(result, {
"data": {
"rows": [{
"id": "1",
"doc": {"title": "title1"},
"value": {}
}, {
"id": "2",
"doc": {"title": "title2"},
"value": {}
}],
"total_rows": 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.get
/////////////////////////////////////////////////////////////////
module("websqlStorage.get", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
test("get inexistent document", function () {
var context = this;
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.get("inexistent");
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document without attachment", function () {
var id = "/",
context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document with attachment", function () {
var id = "/",
attachment = "foo",
context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment(id, attachment, "bar");
})
.then(function () {
return context.jio.get(id);
})
.then(function (result) {
deepEqual(result, {
"title": "bar"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("websqlStorage.allAttachments", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
test("get inexistent document", function () {
var context = this;
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.allAttachments("inexistent");
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document without attachment", function () {
var id = "/",
context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.allAttachments(id);
})
.then(function (result) {
deepEqual(result, {}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document with attachment", function () {
var id = "/",
attachment = "foo",
context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.put(id, {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment(id, attachment, "bar");
})
.then(function () {
return context.jio.allAttachments(id);
})
.then(function (result) {
deepEqual(result, {
"foo": {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.put
/////////////////////////////////////////////////////////////////
module("websqlStorage.put", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
test("put document", function () {
var context = this;
stop();
expect(1);
deleteWebsql()
.then(function () {
return context.jio.put("inexistent", {});
})
.then(function (result) {
equal(result, "inexistent");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.remove
/////////////////////////////////////////////////////////////////
module("websqlStorage.remove", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
//bientot ici: de beaux tests.
/////////////////////////////////////////////////////////////////
// websqlStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("websqlStorage.getAttachment", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit",
blob_length: 20
});
}
});
test("check result", function () {
var context = this,
attachment = "attachment";
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
return context.jio.getAttachment("foo", attachment);
})
.then(function (result) {
ok(result instanceof Blob, "Data is Blob");
equal(result.type, "text/plain;charset=utf-8");
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();
});
});
test("streaming", function () {
var context = this,
attachment = "attachment";
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
return context.jio.getAttachment("foo", attachment,
{"start": 15, "end": 25});
})
.then(function (result) {
ok(result instanceof Blob, "Data is Blob");
equal(result.type, "application/octet-stream");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
var expected = "aaaaaaaaaa";
equal(result.target.result, expected, "Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("websqlStorage.removeAttachment", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
/*
test("remove attachment", function () {
var context = this,
attachment = "attachment";
stop();
expect(3);
deleteWebsql()
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
exec("SELECT * FROM attachment ")
return context.jio.getAttachment("foo", attachment,
{"start": 15, "end": 25});
})
*/
//bientot ici: de beaux tests.
/////////////////////////////////////////////////////////////////
// websqlStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("websqlStorage.putAttachment", {
setup: function () {
this.jio = jIO.createJIO({
type: "websql",
database: "qunit"
});
}
});
//bientot ici: de beaux tests.
}(jIO, QUnit, openDatabase, Blob));
\ No newline at end of file
......@@ -17,7 +17,7 @@
<!--script src="jio/util.js"></script-->
<!--script src="jio/fakestorage.js"></script>
<script src="jio/tests.js"></script-->
<!--lucas>
<script src="queries/key.tests.js"></script>
<script src="queries/key-schema.tests.js"></script>
<script src="queries/tests.js"></script>
......@@ -27,7 +27,7 @@
<script src="queries/key-jiodate.tests.js"></script>
<!--script src="queries/key-localstorage.tests.js"></script-->
<!--lucas>
<script src="jio.storage/memorystorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script>
<script src="jio.storage/localstorage.tests.js"></script>
......@@ -40,12 +40,12 @@
<script src="jio.storage/uuidstorage.tests.js"></script>
<script src="jio.storage/replicatestorage.tests.js"></script>
<script src="jio.storage/shastorage.tests.js"></script>
<!--script src="jio.storage/indexstorage.tests.js"></script-->
<script src="jio.storage/indexstorage.tests.js"></script>
<script src="jio.storage/cryptstorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/zipstorage.tests.js"></script>
<!--lucas-->
<script src="jio.storage/websqlstorage.tests.js"></script>
<!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.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