Commit 0a16746e authored by Bryan Kaperick's avatar Bryan Kaperick

Added src/jio.storage/bryanstorage.js and...

Added src/jio.storage/bryanstorage.js and test/jio.storage/bryanstorage.tests.js for revision storage implementation.
parent 07805198
...@@ -182,7 +182,8 @@ module.exports = function (grunt) { ...@@ -182,7 +182,8 @@ module.exports = function (grunt) {
'src/jio.storage/indexeddbstorage.js', 'src/jio.storage/indexeddbstorage.js',
'src/jio.storage/cryptstorage.js', 'src/jio.storage/cryptstorage.js',
'src/jio.storage/websqlstorage.js', 'src/jio.storage/websqlstorage.js',
'src/jio.storage/fbstorage.js' 'src/jio.storage/fbstorage.js',
'src/jio.storage/bryanstorage.js'
], ],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js' dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
// dest: 'jio.js' // dest: 'jio.js'
......
/*jslint nomen: true*/
/*global RSVP, jiodate*/
(function (jIO) {
"use strict";
/**
* The jIO BryanStorage extension
*
* @class BryanStorage
* @constructor
*/
function BryanStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
BryanStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.putAttachment = function () {
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.removeAttachment = function () {
return this._sub_storage.removeAttachment.apply(this._sub_storage,
arguments);
};
BryanStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.removeAttachment.apply(this._sub_storage, name);
};
BryanStorage.prototype.buildQuery = function (options) {
return this._sub_storage.removeAttachment.apply(this._sub_storage, options);
};
jIO.addStorage('bryan', BryanStorage);
}(jIO));
/*jslint nomen: true*/
/*global Blob, jiodate*/
(function (jIO, QUnit, 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,
throws = QUnit.throws;
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
/////////////////////////////////////////////////////////////////
function Storage200() {
return this;
}
jIO.addStorage('indexeddb', Storage200);
/////////////////////////////////////////////////////////////////
// bryanStorage.constructor
/////////////////////////////////////////////////////////////////
module("bryanStorage.constructor");
test("accept parameters", function () {
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "indexeddb");
});
test("failed on wrong schema", function () {
throws(
function () {
jIO.createJIO({
type: "bryan",
schema: {'date': {type: 'couscous'}},
sub_storage: {
type: "indexeddb"
}
});
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 400);
equal(error.message,
"Wrong schema for property: date");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// bryanStorage.get
/////////////////////////////////////////////////////////////////
module("bryanStorage.get");
test("get called substorage get", function () {
stop();
expect(2);
// create storage of type "bryan" with indexeddb as substorage
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.get = function (id) {
equal(id, "bar", "get 200 called");
return {title: "foo"};
};
// jio.get uses the Storage200 .get() implementation defined immediately
// above
jio.get("bar")
.then(function (result) {
deepEqual(result, {
"title": "foo"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("bryanStorage.allAttachments");
test("allAttachments called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.allAttachments = function (id) {
equal(id, "bar", "allAttachments, 200 called");
return {attachmentname: {}};
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
attachmentname: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.post
/////////////////////////////////////////////////////////////////
module("bryanStorage.post");
test("post called substorage post", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.post = function (param) {
deepEqual(param, {"title": "foo"}, "post 200 called");
return "youhou";
};
jio.post({"title": "foo"})
.then(function (result) {
equal(result, "youhou");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.put
/////////////////////////////////////////////////////////////////
module("bryanStorage.put");
test("put called substorage put", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.put = function (id, param) {
equal(id, "bar", "put 200 called");
deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
};
// If .put does not give the appropriate return, fail assertion
jio.put("bar", {"title": "foo"})
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.remove
/////////////////////////////////////////////////////////////////
module("bryanStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.remove = function (id) {
deepEqual(id, "bar", "remove 200 called");
return id;
};
jio.remove("bar")
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("bryanStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
}),
blob = new Blob([""]);
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "bar", "getAttachment 200 called");
equal(name, "foo", "getAttachment 200 called");
return blob;
};
jio.getAttachment("bar", "foo")
.then(function (result) {
equal(result, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("bryanStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
}),
blob = new Blob([""]);
Storage200.prototype.putAttachment = function (id, name, blob2) {
equal(id, "bar", "putAttachment 200 called");
equal(name, "foo", "putAttachment 200 called");
deepEqual(blob2, blob,
"putAttachment 200 called");
return "OK";
};
jio.putAttachment("bar", "foo", blob)
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("bryanStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
Storage200.prototype.removeAttachment = function (id, name) {
equal(id, "bar", "removeAttachment 200 called");
equal(name, "foo", "removeAttachment 200 called");
return "Removed";
};
jio.removeAttachment("bar", "foo")
.then(function (result) {
equal(result, "Removed");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("bryanStorage.hasCapacity");
test("hasCapacity is false by default", function () {
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
throws(
function () {
jio.hasCapacity("foo");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'foo' is not implemented on 'bryan'");
return true;
}
);
});
test("hasCapacity list return substorage value", function () {
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
throws(
function () {
jio.hasCapacity("list");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'list' is not implemented on 'indexeddb'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// bryanStorage.buildbryan
/////////////////////////////////////////////////////////////////
module("bryanStorage.buildbryan");
test("substorage should have 'list' capacity", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
});
jio.allDocs({
include_docs: true,
bryan: 'title: "two"'
})
.then(function () {
ok(false);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'list' is not implemented on 'indexeddb'");
})
.always(function () {
start();
});
});
test("no manual bryan if substorage handle everything", function () {
stop();
expect(2);
function StorageAllDocsNoGet() {
return this;
}
StorageAllDocsNoGet.prototype.get = function () {
throw new Error("Unexpected get call");
};
StorageAllDocsNoGet.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "sort") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "bryan")) {
return true;
}
throw new Error("Unexpected " + capacity + " capacity check");
};
StorageAllDocsNoGet.prototype.buildbryan = function (options) {
deepEqual(options, {
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
bryan: 'title: "two"'
},
"buildbryan called");
return "taboulet";
};
jIO.addStorage('bryanStoragealldocsnoget', StorageAllDocsNoGet);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStoragealldocsnoget"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
bryan: 'title: "two"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: "taboulet",
total_rows: 8
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual bryan used if substorage does not handle sort", function () {
stop();
expect(4);
function StorageNoSortCapacity() {
return this;
}
StorageNoSortCapacity.prototype.get = function (id) {
if (id === "foo") {
equal(id, "foo", "Get foo");
} else {
equal(id, "bar", "Get bar");
}
return {title: id, id: "ID " + id,
"another": "property"};
};
StorageNoSortCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "bryan")) {
return true;
}
return false;
};
StorageNoSortCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {}, "No bryan parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('bryanStoragenosortcapacity', StorageNoSortCapacity);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStoragenosortcapacity"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
bryan: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual bryan used if substorage does not handle select", function () {
stop();
expect(4);
function StorageNoSelectCapacity() {
return this;
}
StorageNoSelectCapacity.prototype.get = function (id) {
if (id === "foo") {
equal(id, "foo", "Get foo");
} else {
equal(id, "bar", "Get bar");
}
return {title: id, id: "ID " + id,
"another": "property"};
};
StorageNoSelectCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "sort") ||
(capacity === "limit") ||
(capacity === "bryan")) {
return true;
}
return false;
};
StorageNoSelectCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {}, "No bryan parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('bryanStoragenoselectcapacity', StorageNoSelectCapacity);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStoragenoselectcapacity"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
bryan: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual bryan used if substorage does not handle limit", function () {
stop();
expect(4);
function StorageNoLimitCapacity() {
return this;
}
StorageNoLimitCapacity.prototype.get = function (id) {
if (id === "foo") {
equal(id, "foo", "Get foo");
} else {
equal(id, "bar", "Get bar");
}
return {title: id, id: "ID " + id,
"another": "property"};
};
StorageNoLimitCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "sort") ||
(capacity === "bryan")) {
return true;
}
return false;
};
StorageNoLimitCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {}, "No bryan parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('bryanStoragenolimitcapacity', StorageNoLimitCapacity);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStoragenolimitcapacity"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
bryan: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual bryan used if substorage does not handle bryan", function () {
stop();
expect(4);
function StorageNobryanCapacity() {
return this;
}
StorageNobryanCapacity.prototype.get = function (id) {
if (id === "foo") {
equal(id, "foo", "Get foo");
} else {
equal(id, "bar", "Get bar");
}
return {title: id, id: "ID " + id,
"another": "property"};
};
StorageNobryanCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "select") ||
(capacity === "limit") ||
(capacity === "sort")) {
return true;
}
return false;
};
StorageNobryanCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {}, "No bryan parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage('bryanStoragenobryancapacity', StorageNobryanCapacity);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStoragenobryancapacity"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
bryan: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("does not fetch doc one by one if substorage handle include_docs",
function () {
stop();
expect(2);
function StorageIncludeDocsCapacity() {
return this;
}
StorageIncludeDocsCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list") ||
(capacity === "include")) {
return true;
}
return false;
};
StorageIncludeDocsCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {include_docs: true}, "Include docs parameter");
var result2 = [{
id: "foo",
value: {},
doc: {
title: "foo",
id: "ID foo",
another: "property"
}
}, {
id: "bar",
value: {},
doc: {
title: "bar",
id: "ID bar",
another: "property"
}
}];
return result2;
};
jIO.addStorage('bryanStorageincludedocscapacity',
StorageIncludeDocsCapacity);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "bryanStorageincludedocscapacity"
}
});
jio.allDocs({
sort_on: [["title", "ascending"]],
limit: [0, 5],
select_list: ["title", "id"],
bryan: 'title: "foo"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "foo",
doc: {},
value: {
title: "foo",
id: "ID foo"
}
}],
total_rows: 1
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("manual bryan used and use schema", function () {
stop();
expect(4);
function StorageSchemaCapacity() {
return this;
}
StorageSchemaCapacity.prototype.get = function (id) {
var doc = {
title: id,
id: "ID " + id,
"another": "property"
};
if (id === "foo") {
equal(id, "foo", "Get foo");
doc.modification_date = "Fri, 08 Sep 2017 07:46:27 +0000";
} else {
equal(id, "bar", "Get bar");
doc.modification_date = "Thu, 07 Sep 2017 18:59:23 +0000";
}
return doc;
};
StorageSchemaCapacity.prototype.hasCapacity = function (capacity) {
if ((capacity === "list")) {
return true;
}
return false;
};
StorageSchemaCapacity.prototype.buildbryan = function (options) {
deepEqual(options, {}, "No bryan parameter");
var result2 = [{
id: "foo",
value: {}
}, {
id: "bar",
value: {}
}];
return result2;
};
jIO.addStorage(
'bryanStoragenoschemacapacity',
StorageSchemaCapacity
);
var jio = jIO.createJIO({
type: "bryan",
schema: {
"modification_date": {
"type": "string",
"format": "date-time"
}
},
sub_storage: {
type: "bryanStoragenoschemacapacity"
}
});
jio.allDocs({
sort_on: [["modification_date", "descending"]],
limit: [0, 5],
select_list: ['modification_date']
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [
{
id: "foo",
doc: {},
value: {
modification_date: "Fri, 08 Sep 2017 07:46:27 +0000"
}
}, {
id: "bar",
doc: {},
value: {
modification_date: "Thu, 07 Sep 2017 18:59:23 +0000"
}
}
],
total_rows: 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// bryanStorage.repair
/////////////////////////////////////////////////////////////////
module("bryanStorage.repair");
test("repair called substorage repair", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "indexeddb"
}
}),
expected_options = {foo: "bar"};
Storage200.prototype.repair = function (options) {
deepEqual(options, expected_options, "repair 200 called");
return "OK";
};
jio.repair(expected_options)
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
...@@ -59,6 +59,8 @@ ...@@ -59,6 +59,8 @@
<!--script src="../lib/jquery/jquery.min.js"></script> <!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script> <script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script--> <script src="jio.storage/xwikistorage.tests.js"></script-->
<script src="jio.storage/bryanstorage.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