Tests for coherence of hooks done

pull/2/head
Louis Chatriot 10 years ago
parent 3e67adc2bb
commit 09c6309c1a
  1. 14
      lib/persistence.js
  2. 28
      test/persistence.test.js

@ -21,6 +21,8 @@ var storage = require('./storage')
* Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion) * Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion)
*/ */
function Persistence (options) { function Persistence (options) {
var i, j, randomString;
this.db = options.db; this.db = options.db;
this.inMemoryOnly = this.db.inMemoryOnly; this.inMemoryOnly = this.db.inMemoryOnly;
this.filename = this.db.filename; this.filename = this.db.filename;
@ -36,13 +38,21 @@ function Persistence (options) {
// After serialization and before deserialization hooks with some basic sanity checks // After serialization and before deserialization hooks with some basic sanity checks
if (options.afterSerialization && !options.beforeDeserialization) { if (options.afterSerialization && !options.beforeDeserialization) {
throw "Serialization hook defined but deserialization hook undefined, cautiously refusing to start NeDB"; throw "Serialization hook defined but deserialization hook undefined, cautiously refusing to start NeDB to prevent dataloss";
} }
if (!options.afterSerialization && options.beforeDeserialization) { if (!options.afterSerialization && options.beforeDeserialization) {
throw "Serialization hook undefined but deserialization hook defined, cautiously refusing to start NeDB"; throw "Serialization hook undefined but deserialization hook defined, cautiously refusing to start NeDB to prevent dataloss";
} }
this.afterSerialization = options.afterSerialization || function (s) { return s; }; this.afterSerialization = options.afterSerialization || function (s) { return s; };
this.beforeDeserialization = options.beforeDeserialization || function (s) { return s; }; this.beforeDeserialization = options.beforeDeserialization || function (s) { return s; };
for (i = 1; i < 30; i += 1) {
for (j = 0; j < 10; j += 1) {
randomString = customUtils.uid(i);
if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) {
throw "beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss";
}
}
}
// For NW apps, store data in the same directory where NW stores application data // For NW apps, store data in the same directory where NW stores application data
if (this.filename && options.nodeWebkitAppName) { if (this.filename && options.nodeWebkitAppName) {

@ -275,20 +275,48 @@ describe('Persistence', function () {
it("Declaring only one hook will throw an exception to prevent data loss", function (done) { it("Declaring only one hook will throw an exception to prevent data loss", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { Persistence.ensureFileDoesntExist(hookTestFilename, function () {
fs.writeFileSync(hookTestFilename, "Some content", "utf8");
(function () { (function () {
new Datastore({ filename: hookTestFilename, autoload: true new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
}); });
}).should.throw(); }).should.throw();
// Data file left untouched
fs.readFileSync(hookTestFilename, "utf8").should.equal("Some content");
(function () { (function () {
new Datastore({ filename: hookTestFilename, autoload: true new Datastore({ filename: hookTestFilename, autoload: true
, beforeDeserialization: bd , beforeDeserialization: bd
}); });
}).should.throw(); }).should.throw();
// Data file left untouched
fs.readFileSync(hookTestFilename, "utf8").should.equal("Some content");
done();
});
});
it("Declaring two hooks that are not reverse of one another will cause an exception to prevent data loss", function (done) {
var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () {
fs.writeFileSync(hookTestFilename, "Some content", "utf8");
(function () {
new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as
, beforeDeserialization: function (s) { return s; }
});
}).should.throw();
// Data file left untouched
fs.readFileSync(hookTestFilename, "utf8").should.equal("Some content");
done(); done();
}); });
}); });
it("A serialization hook can be used to transform data before writing new state to disk", function (done) { it("A serialization hook can be used to transform data before writing new state to disk", function (done) {

Loading…
Cancel
Save