Commit dafc0c70 authored by Romain Courteaud's avatar Romain Courteaud

Add SHA storage (create only new document)

parent bd52f34e
...@@ -174,6 +174,7 @@ module.exports = function (grunt) { ...@@ -174,6 +174,7 @@ module.exports = function (grunt) {
'node_modules/rusha/rusha.js', 'node_modules/rusha/rusha.js',
'src/jio.storage/replicatestorage.js', 'src/jio.storage/replicatestorage.js',
'src/jio.storage/shastorage.js',
'src/jio.storage/uuidstorage.js', 'src/jio.storage/uuidstorage.js',
'src/jio.storage/memorystorage.js', 'src/jio.storage/memorystorage.js',
'src/jio.storage/localstorage.js', 'src/jio.storage/localstorage.js',
......
/*
* Copyright 2015, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global Rusha*/
/**
* JIO Sha Storage. Type = 'sha'.
*/
(function (Rusha) {
"use strict";
var rusha = new Rusha();
function ShaStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
ShaStorage.prototype.post = function (param) {
return this._sub_storage.put(
rusha.digestFromString(JSON.stringify(param)),
param
);
};
ShaStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.hasCapacity = function () {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.putAttachment = function () {
return this._sub_storage.putAttachment.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.removeAttachment = function () {
return this._sub_storage.removeAttachment.apply(this._sub_storage,
arguments);
};
ShaStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
ShaStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
jIO.addStorage('sha', ShaStorage);
}(Rusha));
/*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;
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
/////////////////////////////////////////////////////////////////
function Storage200() {
return this;
}
jIO.addStorage('shastorage200', Storage200);
/////////////////////////////////////////////////////////////////
// shaStorage.constructor
/////////////////////////////////////////////////////////////////
module("shaStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "shastorage200");
});
/////////////////////////////////////////////////////////////////
// shaStorage.get
/////////////////////////////////////////////////////////////////
module("shaStorage.get");
test("get called substorage get", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
Storage200.prototype.get = function (param) {
equal(param, "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();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("shaStorage.allAttachments");
test("get called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
Storage200.prototype.allAttachments = function (param) {
equal(param, "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();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.post
/////////////////////////////////////////////////////////////////
module("shaStorage.post");
test("post called substorage put with a new id", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
Storage200.prototype.put = function (id, param) {
equal(id, "6641543f1fe4d30779df5b30d64bd786f7da440d", "post 200 called");
deepEqual(param, {"title": "fooé"}, "post 200 called");
return "bar";
};
jio.post({"title": "fooé"})
.then(function (result) {
equal(result, "6641543f1fe4d30779df5b30d64bd786f7da440d");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.remove
/////////////////////////////////////////////////////////////////
module("shaStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
Storage200.prototype.remove = function (param) {
equal(param, "bar", "remove 200 called");
return param._id;
};
jio.remove("bar")
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("shaStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
}),
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();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("shaStorage.putAttachment");
test("putAttachment called substorage putAttachment", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
}),
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();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("shaStorage.removeAttachment");
test("removeAttachment called substorage removeAttachment", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
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();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("shaStorage.hasCapacity");
test("hasCapacity return substorage value", function () {
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
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 'shastorage200'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// shaStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("shaStorage.buildQuery");
test("buildQuery return substorage buildQuery", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
});
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"],
sha: 'title: "two"'
}, "allDocs parameter");
return "bar";
};
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
sha: 'title: "two"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: "bar",
total_rows: 3
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// shaStorage.repair
/////////////////////////////////////////////////////////////////
module("shaStorage.repair");
test("repair called substorage repair", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "sha",
sub_storage: {
type: "shastorage200"
}
}),
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));
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
<script src="jio.storage/indexeddbstorage.tests.js"></script> <script src="jio.storage/indexeddbstorage.tests.js"></script>
<script src="jio.storage/uuidstorage.tests.js"></script> <script src="jio.storage/uuidstorage.tests.js"></script>
<script src="jio.storage/replicatestorage.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/dropboxstorage.tests.js"></script--> <!--script src="jio.storage/dropboxstorage.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