|
|
|
@ -9,6 +9,7 @@ var should = require('chai').should() |
|
|
|
|
, customUtils = require('../lib/customUtils') |
|
|
|
|
, Datastore = require('../lib/datastore') |
|
|
|
|
, Persistence = require('../lib/persistence') |
|
|
|
|
, storage = require('../lib/storage') |
|
|
|
|
, child_process = require('child_process') |
|
|
|
|
; |
|
|
|
|
|
|
|
|
@ -305,7 +306,7 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
it("Declaring only one hook will throw an exception to prevent data loss", function (done) { |
|
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); |
|
|
|
|
|
|
|
|
|
(function () { |
|
|
|
@ -332,7 +333,7 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
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 () { |
|
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); |
|
|
|
|
|
|
|
|
|
(function () { |
|
|
|
@ -351,7 +352,7 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
it("A serialization hook can be used to transform data before writing new state to disk", function (done) { |
|
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
|
, afterSerialization: as |
|
|
|
|
, beforeDeserialization: bd |
|
|
|
@ -430,7 +431,7 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
it("Use serialization hook when persisting cached database or compacting", function (done) { |
|
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
|
, afterSerialization: as |
|
|
|
|
, beforeDeserialization: bd |
|
|
|
@ -492,7 +493,7 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
it("Deserialization hook is correctly used when loading data", function (done) { |
|
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
|
var d = new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
|
, afterSerialization: as |
|
|
|
|
, beforeDeserialization: bd |
|
|
|
@ -558,7 +559,7 @@ describe('Persistence', function () { |
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(false); |
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
storage.ensureDatafileIntegrity(p.filename, function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
@ -581,7 +582,7 @@ describe('Persistence', function () { |
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(false); |
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
storage.ensureDatafileIntegrity(p.filename, function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
@ -604,7 +605,7 @@ describe('Persistence', function () { |
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(true); |
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
storage.ensureDatafileIntegrity(p.filename, function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
@ -628,7 +629,7 @@ describe('Persistence', function () { |
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(true); |
|
|
|
|
|
|
|
|
|
theDb.persistence.ensureDatafileIntegrity(function (err) { |
|
|
|
|
storage.ensureDatafileIntegrity(theDb.persistence.filename, function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
@ -761,9 +762,9 @@ describe('Persistence', function () { |
|
|
|
|
var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; |
|
|
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
|
async.apply(Persistence.ensureFileDoesntExist, dbFile) |
|
|
|
|
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~') |
|
|
|
|
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~~') |
|
|
|
|
async.apply(storage.ensureFileDoesntExist, dbFile) |
|
|
|
|
, async.apply(storage.ensureFileDoesntExist, dbFile + '~') |
|
|
|
|
, async.apply(storage.ensureFileDoesntExist, dbFile + '~~') |
|
|
|
|
, function (cb) { |
|
|
|
|
theDb = new Datastore({ filename: dbFile }); |
|
|
|
|
theDb.loadDatabase(cb); |
|
|
|
@ -891,7 +892,7 @@ describe('Persistence', function () { |
|
|
|
|
describe('ensureFileDoesntExist', function () { |
|
|
|
|
|
|
|
|
|
it('Doesnt do anything if file already doesnt exist', function (done) { |
|
|
|
|
Persistence.ensureFileDoesntExist('workspace/nonexisting', function (err) { |
|
|
|
|
storage.ensureFileDoesntExist('workspace/nonexisting', function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
fs.existsSync('workspace/nonexisting').should.equal(false); |
|
|
|
|
done(); |
|
|
|
@ -902,7 +903,7 @@ describe('Persistence', function () { |
|
|
|
|
fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); |
|
|
|
|
fs.existsSync('workspace/existing').should.equal(true); |
|
|
|
|
|
|
|
|
|
Persistence.ensureFileDoesntExist('workspace/existing', function (err) { |
|
|
|
|
storage.ensureFileDoesntExist('workspace/existing', function (err) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
fs.existsSync('workspace/existing').should.equal(false); |
|
|
|
|
done(); |
|
|
|
|