Commit 9d9506b7 authored by preetwinder's avatar preetwinder Committed by Romain Courteaud

use jio storage for ids

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