Commit ad39ea93 authored by preetwinder's avatar preetwinder

use jio storage for ids

parent c43db0e7
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* See https://www.nexedi.com/licensing for rationale and options. * See https://www.nexedi.com/licensing for rationale and options.
*/ */
/*global console, btoa, Blob*/ /*global console, btoa, Blob, indexedDB*/
/*jslint nomen: true, maxlen: 200*/ /*jslint nomen: true, maxlen: 200*/
(function (window, QUnit, jIO, rJS) { (function (window, QUnit, jIO, rJS) {
"use strict"; "use strict";
...@@ -28,7 +28,9 @@ ...@@ -28,7 +28,9 @@
ok = QUnit.ok, ok = QUnit.ok,
stop = QUnit.stop, stop = QUnit.stop,
start = QUnit.start, start = QUnit.start,
deepEqual = QUnit.deepEqual; deepEqual = QUnit.deepEqual,
test_signature_database = 'test_signature_storage_scenario';
rJS(window) rJS(window)
...@@ -61,6 +63,10 @@ ...@@ -61,6 +63,10 @@
type: "query", type: "query",
sub_storage: { sub_storage: {
type: "list", type: "list",
signature_storage: {
type: "indexeddb",
database: test_signature_database
},
sub_storage: { sub_storage: {
type: "nocapacity", type: "nocapacity",
sub_storage: { sub_storage: {
...@@ -368,6 +374,9 @@ ...@@ -368,6 +374,9 @@
.then(function () { .then(function () {
return jio.repair(); return jio.repair();
}) })
.then(function () {
return indexedDB.deleteDatabase('jio:' + test_signature_database);
})
.fail(function (error) { .fail(function (error) {
console.error("---"); console.error("---");
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
function ListStorage(spec) { function ListStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
this._sub_storage_index = new Set(); this._signature_storage = jIO.createJIO(spec.signature_storage);
} }
ListStorage.prototype.get = function () { ListStorage.prototype.get = function () {
...@@ -37,24 +37,30 @@ ...@@ -37,24 +37,30 @@
var gadget = this; var gadget = this;
return gadget._sub_storage.post(value) return gadget._sub_storage.post(value)
.push(function (id) { .push(function (id) {
gadget._sub_storage_index.add(id); return gadget._signature_storage.put(id, {"id": id})
return id; .push(function () {
return id;
});
}); });
}; };
ListStorage.prototype.put = function (id, value) { ListStorage.prototype.put = function (id, value) {
var gadget = this; var gadget = this;
return gadget._sub_storage.put(id, value) return gadget._sub_storage.put(id, value)
.push(function (result) { .push(function (result) {
gadget._sub_storage_index.add(id); return gadget._signature_storage.put(id, {"id": id})
return result; .push(function () {
return result;
});
}); });
}; };
ListStorage.prototype.remove = function (id) { ListStorage.prototype.remove = function (id) {
var gadget = this; var gadget = this;
return gadget._sub_storage.remove(id) return gadget._sub_storage.remove(id)
.push(function (result) { .push(function (result) {
gadget._sub_storage_index.delete(id); return gadget._signature_storage.remove(id)
return result; .push(function () {
return result;
});
}); });
}; };
ListStorage.prototype.getAttachment = function () { ListStorage.prototype.getAttachment = function () {
...@@ -76,11 +82,7 @@ ...@@ -76,11 +82,7 @@
} }
}; };
ListStorage.prototype.buildQuery = function () { ListStorage.prototype.buildQuery = function () {
var rows = [], i, ids = Array.from(this._sub_storage_index); return this._signature_storage.buildQuery({});
for (i = 0; i < ids.length; i += 1) {
rows.push({id: ids[i], value: {}});
}
return rows;
}; };
jIO.addStorage('list', ListStorage); jIO.addStorage('list', ListStorage);
......
...@@ -33,15 +33,20 @@ ...@@ -33,15 +33,20 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Custom test substorage definition // Custom test substorage definition
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
function DummyStorage() { function DummyStorage1() {
return this; return this;
} }
jIO.addStorage('dummystorage1', DummyStorage); function DummyStorage2() {
return this;
}
jIO.addStorage('dummystorage1', DummyStorage1);
jIO.addStorage('dummystorage2', DummyStorage2);
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// ListStorage constructor // ListStorage constructor
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("ListStorage.constructor"); module("ListStorage.constructor");
test("create storage", function () { test("create storage", function () {
...@@ -49,11 +54,14 @@ ...@@ -49,11 +54,14 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
equal(jio.__type, "list"); equal(jio.__type, "list");
equal(jio.__storage._sub_storage.__type, "dummystorage1"); equal(jio.__storage._sub_storage.__type, "dummystorage1");
equal(jio.__storage._sub_storage_index.constructor.name, "Set"); equal(jio.__storage._signature_storage.__type, "dummystorage2");
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -70,19 +78,20 @@ ...@@ -70,19 +78,20 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.get = function (id) { DummyStorage1.prototype.get = function (id) {
equal(id, "1"); equal(id, "1");
return {"name": "test_name"}; return {"name": "test_name"};
}; };
jio.get("1") jio.get("1")
.then(function (result) { .then(function (result) {
deepEqual(result, { deepEqual(result, {"name": "test_name"});
"name": "test_name"
});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -104,10 +113,13 @@ ...@@ -104,10 +113,13 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.allAttachments = function (id) { DummyStorage1.prototype.allAttachments = function (id) {
equal(id, "1"); equal(id, "1");
return {attachmentname: {}}; return {attachmentname: {}};
}; };
...@@ -132,24 +144,31 @@ ...@@ -132,24 +144,31 @@
module("ListStorage.post"); module("ListStorage.post");
test("post called substorage post", function () { test("post called substorage post", function () {
stop(); stop();
expect(3); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.post = function (param) { DummyStorage1.prototype.post = function (param) {
deepEqual(param, {"name": "test_name"}); deepEqual(param, {"name": "test_name"});
return "posted"; return "posted";
}; };
DummyStorage2.prototype.put = function (id, value) {
equal(id, 'posted');
deepEqual(value, {'id': 'posted'});
return id;
};
jio.post({"name": "test_name"}) jio.post({"name": "test_name"})
.then(function (result) { .then(function (result) {
equal(result, "posted"); equal(result, "posted");
equal(jio.__storage._sub_storage_index.has("posted"), true);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -165,24 +184,31 @@ ...@@ -165,24 +184,31 @@
module("ListStorage.put"); module("ListStorage.put");
test("put called substorage put", function () { test("put called substorage put", function () {
stop(); stop();
expect(4); expect(5);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.put = function (id, param) { DummyStorage1.prototype.put = function (id, param) {
equal(id, "1"); equal(id, "1");
deepEqual(param, {"name": "test_name"}); deepEqual(param, {"name": "test_name"});
return id; return id;
}; };
DummyStorage2.prototype.put = function (id, param) {
equal(id, "1");
deepEqual(param, {'id': '1'});
return id;
};
jio.put("1", {"name": "test_name"}) jio.put("1", {"name": "test_name"})
.then(function (result) { .then(function (result) {
equal(result, "1"); equal(result, "1");
equal(jio.__storage._sub_storage_index.has("1"), true);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -198,37 +224,35 @@ ...@@ -198,37 +224,35 @@
module("ListStorage.remove"); module("ListStorage.remove");
test("remove called substorage remove", function () { test("remove called substorage remove", function () {
stop(); stop();
expect(5); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.remove = function (id) { DummyStorage1.prototype.remove = function (id) {
deepEqual(id, "1"); equal(id, "1");
return id; return id;
}; };
DummyStorage.prototype.put = function (id) { DummyStorage2.prototype.remove = function (id) {
equal(id, "1");
return id; return id;
}; };
jio.put("1", {"name": "test_name"}) jio.remove("1")
.then(function (result) { .then(function (result) {
equal(result, "1"); equal(result, "1");
equal(jio.__storage._sub_storage_index.has("1"), true); })
jio.remove("1") .fail(function (error) {
.then(function (result) { ok(false, error);
equal(result, "1"); })
equal(jio.__storage._sub_storage_index.has("1"), false); .always(function () {
}) start();
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
}); });
}); });
...@@ -244,11 +268,14 @@ ...@@ -244,11 +268,14 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}), }),
blob = new Blob([""]); blob = new Blob([""]);
DummyStorage.prototype.getAttachment = function (id, name) { DummyStorage1.prototype.getAttachment = function (id, name) {
equal(id, "1"); equal(id, "1");
equal(name, "test_name"); equal(name, "test_name");
return blob; return blob;
...@@ -278,11 +305,14 @@ ...@@ -278,11 +305,14 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}), }),
blob = new Blob([""]); blob = new Blob([""]);
DummyStorage.prototype.putAttachment = function (id, name, blob2) { DummyStorage1.prototype.putAttachment = function (id, name, blob2) {
equal(id, "1"); equal(id, "1");
equal(name, "test_name"); equal(name, "test_name");
deepEqual(blob2, blob); deepEqual(blob2, blob);
...@@ -313,10 +343,13 @@ ...@@ -313,10 +343,13 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.removeAttachment = function (id, name) { DummyStorage1.prototype.removeAttachment = function (id, name) {
equal(id, "1"); equal(id, "1");
equal(name, "test_name"); equal(name, "test_name");
return "removed"; return "removed";
...@@ -344,14 +377,52 @@ ...@@ -344,14 +377,52 @@
type: "list", type: "list",
sub_storage: { sub_storage: {
type: "dummystorage1" type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
} }
}); });
DummyStorage.prototype.hasCapacity = function () { DummyStorage1.prototype.hasCapacity = function () {
return false; return false;
}; };
ok(jio.hasCapacity("list")); ok(jio.hasCapacity("list"));
}); });
/////////////////////////////////////////////////////////////////
// ListStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("ListStorage.buildQuery");
test("buildQuery calls substorage buildQuery", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "list",
sub_storage: {
type: "dummystorage1"
},
signature_storage: {
type: "dummystorage2",
}
});
DummyStorage2.prototype.buildQuery = function (params) {
deepEqual(params, {});
return [{"id": "1"}, {"id": "2"}];
};
jio.buildQuery({})
.then(function (result) {
deepEqual(result, [{"id": "1"}, {"id": "2"}]);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit)); }(jIO, QUnit));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment