|
|
@ -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"; } |
|
|
|
var as = function (s) { return "before_" + s + "_after"; } |
|
|
|
, bd = function (s) { return s.substring(7, s.length - 6); }
|
|
|
|
, 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) { |
|
|
|
it("A serialization hook can be used to transform data before writing new state to disk", function (done) { |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
fs.unlinkSync(hookTestFilename); |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
|
|
|
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
, afterSerialization: as |
|
|
|
, afterSerialization: as |
|
|
|
, beforeDeserialization: bd |
|
|
|
, beforeDeserialization: bd |
|
|
@ -351,11 +368,11 @@ describe('Persistence', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it("Use serialization hook when persisting cached database or compacting", function (done) { |
|
|
|
it("Use serialization hook when persisting cached database or compacting", function (done) { |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
fs.unlinkSync(hookTestFilename); |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
|
|
|
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
, afterSerialization: as |
|
|
|
, afterSerialization: as |
|
|
|
, beforeDeserialization: bd |
|
|
|
, beforeDeserialization: bd |
|
|
@ -413,11 +430,11 @@ describe('Persistence', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it("Deserialization hook is correctly used when loading data", function (done) { |
|
|
|
it("Deserialization hook is correctly used when loading data", function (done) { |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
fs.unlinkSync(hookTestFilename); |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
|
|
|
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
, afterSerialization: as |
|
|
|
, afterSerialization: as |
|
|
|
, beforeDeserialization: bd |
|
|
|
, beforeDeserialization: bd |
|
|
@ -460,6 +477,7 @@ describe('Persistence', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|