Check that if one hook is defined the other is too

pull/2/head
Louis Chatriot 10 years ago
parent 0597a4e47a
commit 3e67adc2bb
  1. 8
      lib/persistence.js
  2. 32
      test/persistence.test.js

@ -34,7 +34,13 @@ function Persistence (options) {
}
}
// After serialization and before deserialization hooks
// After serialization and before deserialization hooks with some basic sanity checks
if (options.afterSerialization && !options.beforeDeserialization) {
throw "Serialization hook defined but deserialization hook undefined, cautiously refusing to start NeDB";
}
if (!options.afterSerialization && options.beforeDeserialization) {
throw "Serialization hook undefined but deserialization hook defined, cautiously refusing to start NeDB";
}
this.afterSerialization = options.afterSerialization || function (s) { return s; };
this.beforeDeserialization = options.beforeDeserialization || function (s) { return s; };

@ -268,15 +268,32 @@ describe('Persistence', function () {
});
});
describe('Data can be persisted using serialization hooks', function () {
describe.only('Data can be persisted using serialization hooks', function () {
var as = function (s) { return "before_" + s + "_after"; }
, bd = function (s) { return s.substring(7, s.length - 6); }
it("Declaring only one hook will throw an exception to prevent data loss", function (done) {
var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () {
(function () {
new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as
});
}).should.throw();
(function () {
new Datastore({ filename: hookTestFilename, autoload: true
, beforeDeserialization: bd
});
}).should.throw();
done();
});
});
it("A serialization hook can be used to transform data before writing new state to disk", function (done) {
var hookTestFilename = 'workspace/hookTest.db'
fs.unlinkSync(hookTestFilename);
Persistence.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as
, beforeDeserialization: bd
@ -351,11 +368,11 @@ describe('Persistence', function () {
});
});
});
});
it("Use serialization hook when persisting cached database or compacting", function (done) {
var hookTestFilename = 'workspace/hookTest.db'
fs.unlinkSync(hookTestFilename);
Persistence.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as
, beforeDeserialization: bd
@ -413,11 +430,11 @@ describe('Persistence', function () {
});
});
});
});
it("Deserialization hook is correctly used when loading data", function (done) {
var hookTestFilename = 'workspace/hookTest.db'
fs.unlinkSync(hookTestFilename);
Persistence.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as
, beforeDeserialization: bd
@ -460,6 +477,7 @@ describe('Persistence', function () {
});
});
});
});
});

Loading…
Cancel
Save