diff --git a/Gruntfile.js b/Gruntfile.js index 9c2b3ef6fd03f871175fc422cd3d3cbe1ba1bf6c..b6e8072f1c852d1fd787e66137ac88a314d09dce 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -181,7 +181,9 @@ module.exports = function (grunt) { 'src/jio.storage/indexeddbstorage.js', 'src/jio.storage/cryptstorage.js', 'src/jio.storage/websqlstorage.js', - 'src/jio.storage/fbstorage.js' + 'src/jio.storage/fbstorage.js', + 'src/jio.storage/dateupdaterstorage.js' + ], dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js' // dest: 'jio.js' diff --git a/src/jio.storage/dateupdaterstorage.js b/src/jio.storage/dateupdaterstorage.js new file mode 100644 index 0000000000000000000000000000000000000000..1986e890806a01d4208b96a6c91b37160ae53493 --- /dev/null +++ b/src/jio.storage/dateupdaterstorage.js @@ -0,0 +1,84 @@ +/*jslint nomen: true*/ +(function (jIO) { + "use strict"; + + /** + * The jIO DateUpdaterStorage extension + * + * @class DateUpdaterStorage + * @constructor + */ + + function updateDocument(doc, property_list) { + var i, len = property_list.length; + for (i = 0; i < len; i += 1) { + doc[property_list[i]] = new Date().toUTCString().replace('GMT', '+0000'); + } + return doc; + } + + function DateUpdaterStorage(spec) { + this._sub_storage = jIO.createJIO(spec.sub_storage); + this._property_list = spec.property_list || []; + } + + DateUpdaterStorage.prototype.get = function () { + return this._sub_storage.get.apply(this._sub_storage, arguments); + }; + DateUpdaterStorage.prototype.allAttachments = function () { + return this._sub_storage.allAttachments.apply(this._sub_storage, arguments); + }; + DateUpdaterStorage.prototype.post = function (doc) { + doc = updateDocument(doc, this._property_list); + return this._sub_storage.post(doc); + }; + DateUpdaterStorage.prototype.put = function (id, doc) { + doc = updateDocument(doc, this._property_list); + return this._sub_storage.put(id, doc); + }; + DateUpdaterStorage.prototype.remove = function () { + return this._sub_storage.remove.apply(this._sub_storage, arguments); + }; + DateUpdaterStorage.prototype.getAttachment = function () { + return this._sub_storage.getAttachment.apply(this._sub_storage, arguments); + }; + DateUpdaterStorage.prototype.putAttachment = function (id) { + var storage = this, argument_list = arguments; + return storage.get(id) + .push(function (doc) { + return storage.put(id, doc); + }) + .push(function () { + return storage._sub_storage.putAttachment.apply( + storage._sub_storage, + argument_list + ); + }); + }; + DateUpdaterStorage.prototype.removeAttachment = function (id) { + var storage = this, argument_list = arguments; + return storage.get(id) + .push(function (doc) { + return storage.put(id, doc); + }) + .push(function () { + return storage._sub_storage.removeAttachment.apply( + storage._sub_storage, + argument_list + ); + }); + }; + DateUpdaterStorage.prototype.repair = function () { + return this._sub_storage.repair.apply(this._sub_storage, arguments); + }; + DateUpdaterStorage.prototype.hasCapacity = function (name) { + return this._sub_storage.hasCapacity(name); + }; + DateUpdaterStorage.prototype.buildQuery = function () { + return this._sub_storage.buildQuery.apply(this._sub_storage, + arguments); + }; + + jIO.addStorage('dateupdater', DateUpdaterStorage); + +}(jIO)); diff --git a/test/jio.storage/dateupdaterstorage.tests.js b/test/jio.storage/dateupdaterstorage.tests.js new file mode 100644 index 0000000000000000000000000000000000000000..fdd4bceeb16ac4d545ef844e9fdb139632483d7d --- /dev/null +++ b/test/jio.storage/dateupdaterstorage.tests.js @@ -0,0 +1,575 @@ +/*jslint nomen: true*/ +/*global Blob*/ +(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, + big_string = "", + j; + + for (j = 0; j < 30; j += 1) { + big_string += "a"; + } + + ///////////////////////////////////////////////////////////////// + // Custom test substorage definition + ///////////////////////////////////////////////////////////////// + function Storage200() { + return this; + } + jIO.addStorage('dateupdaterstorage200', Storage200); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.constructor + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.constructor"); + test("create substorage", function () { + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + ok(jio.__storage._sub_storage instanceof jio.constructor); + equal(jio.__storage._sub_storage.__type, "dateupdaterstorage200"); + deepEqual(jio.__storage._property_list, []); + }); + + test("accept parameters", function () { + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + }, + property_list: ['date'] + }); + deepEqual(jio.__storage._property_list, ['date']); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.get + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.get"); + test("get called substorage get", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + Storage200.prototype.get = function (id) { + equal(id, "bar", "get 200 called"); + return {title: "foo"}; + }; + + jio.get("bar") + .then(function (result) { + deepEqual(result, { + "title": "foo" + }, "Check document"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.post + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.post"); + test("post called substorage post", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + Storage200.prototype.post = function (param) { + deepEqual(param, {title: "bar"}, "post 200 called"); + return "foo"; + }; + + jio.post({title: "bar"}) + .then(function (result) { + equal(result, "foo", "Check id"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + test("post update date", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + }, + property_list: ['date'] + }), + date = new Date().toUTCString().replace('GMT', '+0000'); + + Storage200.prototype.post = function (param) { + deepEqual(param, {"title": "bar", "date": date}, "post 200 called"); + return "foo"; + }; + + jio.post({title: "bar"}) + .then(function (result) { + equal(result, "foo", "Check id"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.hasCapacity + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.hasCapacity"); + test("hasCapacity return substorage value", function () { + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + delete Storage200.prototype.hasCapacity; + + 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 'dateupdaterstorage200'"); + return true; + } + ); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.buildQuery + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.buildQuery"); + + test("buildQuery return substorage buildQuery", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + Storage200.prototype.hasCapacity = function () { + return true; + }; + + Storage200.prototype.buildQuery = function (options) { + deepEqual(options, { + include_docs: false, + sort_on: [["title", "ascending"]], + limit: [5], + select_list: ["title", "id"], + query: 'title: "two"' + }, "allDocs parameter"); + return "bar"; + }; + + jio.allDocs({ + include_docs: false, + sort_on: [["title", "ascending"]], + limit: [5], + select_list: ["title", "id"], + query: 'title: "two"' + }) + .then(function (result) { + deepEqual(result, { + data: { + rows: "bar", + total_rows: 3 + } + }); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.put + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.put"); + test("put called substorage put", function () { + stop(); + expect(3); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + Storage200.prototype.put = function (id, param) { + equal(id, "bar", "put 200 called"); + deepEqual(param, {"title": "foo"}, "put 200 called"); + return id; + }; + + jio.put("bar", {"title": "foo"}) + .then(function (result) { + equal(result, "bar"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + test("put update date", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + }, + property_list: ['date'] + }), + date = new Date().toUTCString().replace('GMT', '+0000'); + + Storage200.prototype.put = function (id, doc) { + deepEqual(doc, {"title": "bar", "date": date}, "post 200 called"); + return id; + }; + + jio.put("foo", {title: "bar"}) + .then(function (result) { + equal(result, "foo", "Check id"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.remove + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.remove"); + test("remove called substorage remove", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + Storage200.prototype.remove = function (id) { + equal(id, "bar", "remove 200 called"); + return id; + }; + + jio.remove("bar", {"title": "foo"}) + .then(function (result) { + equal(result, "bar"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.getAttachment + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.getAttachment"); + test("called substorage getAttachment", function () { + stop(); + expect(3); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }), + blob = new Blob([big_string]); + + 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(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.putAttachment + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.putAttachment"); + test("called substorage putAttachment", function () { + stop(); + expect(7); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }), + 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"; + }; + + Storage200.prototype.get = function (id) { + equal(id, "bar", "get 200 called"); + return {}; + }; + + Storage200.prototype.put = function (id, doc) { + equal(id, "bar", "put 200 called"); + deepEqual(doc, {}, "put 200 called"); + return id; + }; + + jio.putAttachment("bar", "foo", blob) + .then(function (result) { + equal(result, "OK"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + test("update date", function () { + stop(); + expect(7); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + }, + property_list: ['date'] + }), + blob = new Blob([""]), + date = new Date().toUTCString().replace('GMT', '+0000'); + + 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"; + }; + + Storage200.prototype.get = function (id) { + equal(id, "bar", "get 200 called"); + return {'title': 'foo'}; + }; + + Storage200.prototype.put = function (id, doc) { + equal(id, "bar", "put 200 called"); + deepEqual(doc, {'title': 'foo', 'date': date}, "put 200 called"); + return id; + }; + + jio.putAttachment("bar", "foo", blob) + .then(function (result) { + equal(result, "OK"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.removeAttachment + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.removeAttachment"); + test("removeAttachment called substorage removeAttachment", function () { + stop(); + expect(6); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + Storage200.prototype.removeAttachment = function (id, name) { + equal(id, "bar", "removeAttachment 200 called"); + equal(name, "foo", "removeAttachment 200 called"); + return "OK"; + }; + + Storage200.prototype.get = function (id) { + equal(id, "bar", "get 200 called"); + return {}; + }; + + Storage200.prototype.put = function (id, doc) { + equal(id, "bar", "put 200 called"); + deepEqual(doc, {}, "put 200 called"); + return id; + }; + + jio.removeAttachment("bar", "foo") + .then(function (result) { + equal(result, "OK"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + test("update date", function () { + stop(); + expect(6); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + }, + property_list: ['date'] + }), + blob = new Blob([""]), + date = new Date().toUTCString().replace('GMT', '+0000'); + + Storage200.prototype.removeAttachment = function (id, name) { + equal(id, "bar", "removeAttachment 200 called"); + equal(name, "foo", "removeAttachment 200 called"); + return "OK"; + }; + + Storage200.prototype.get = function (id) { + equal(id, "bar", "get 200 called"); + return {'title': 'foo'}; + }; + + Storage200.prototype.put = function (id, doc) { + equal(id, "bar", "put 200 called"); + deepEqual(doc, {'title': 'foo', 'date': date}, "put 200 called"); + return id; + }; + + jio.removeAttachment("bar", "foo", blob) + .then(function (result) { + equal(result, "OK"); + }) + .fail(function (error) { + ok(false, error); + }) + .always(function () { + start(); + }); + }); + + ///////////////////////////////////////////////////////////////// + // dateupdaterStorage.allAttachments + ///////////////////////////////////////////////////////////////// + module("dateupdaterStorage.allAttachments"); + test("allAttachments called substorage allAttachments", function () { + stop(); + expect(2); + + var jio = jIO.createJIO({ + type: "dateupdater", + sub_storage: { + type: "dateupdaterstorage200" + } + }); + + 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(); + }); + }); + +}(jIO, QUnit, Blob)); \ No newline at end of file diff --git a/test/tests.html b/test/tests.html index 9bdecc94aa2421e17d7a932811ead3c2f628840c..25067ccce57a1e3ffe9714afef6ca8c36455ae29 100644 --- a/test/tests.html +++ b/test/tests.html @@ -13,64 +13,10 @@ - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +

jIO Tests