|
|
@ -9,8 +9,9 @@ var should = require('chai').should() |
|
|
|
, customUtils = require('../lib/customUtils') |
|
|
|
, customUtils = require('../lib/customUtils') |
|
|
|
, Datastore = require('../lib/datastore') |
|
|
|
, Datastore = require('../lib/datastore') |
|
|
|
, Persistence = require('../lib/persistence') |
|
|
|
, Persistence = require('../lib/persistence') |
|
|
|
|
|
|
|
, storage = require('../lib/storage') |
|
|
|
, child_process = require('child_process') |
|
|
|
, child_process = require('child_process') |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('Persistence', function () { |
|
|
|
describe('Persistence', function () { |
|
|
@ -32,22 +33,22 @@ describe('Persistence', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
d.getAllData().length.should.equal(0); |
|
|
|
d.getAllData().length.should.equal(0); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
], done); |
|
|
|
], done); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('Every line represents a document', function () { |
|
|
|
it('Every line represents a document', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "3", nested: { today: now } }) |
|
|
|
model.serialize({ _id: "3", nested: { today: now } }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(3); |
|
|
|
treatedData.length.should.equal(3); |
|
|
@ -59,10 +60,10 @@ describe('Persistence', function () { |
|
|
|
it('Badly formatted lines have no impact on the treated data', function () { |
|
|
|
it('Badly formatted lines have no impact on the treated data', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
'garbage\n' + |
|
|
|
'garbage\n' + |
|
|
|
model.serialize({ _id: "3", nested: { today: now } }) |
|
|
|
model.serialize({ _id: "3", nested: { today: now } }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(2); |
|
|
|
treatedData.length.should.equal(2); |
|
|
@ -73,10 +74,10 @@ describe('Persistence', function () { |
|
|
|
it('Well formatted lines that have no _id are not included in the data', function () { |
|
|
|
it('Well formatted lines that have no _id are not included in the data', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ nested: { today: now } }) |
|
|
|
model.serialize({ nested: { today: now } }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(2); |
|
|
|
treatedData.length.should.equal(2); |
|
|
@ -87,10 +88,10 @@ describe('Persistence', function () { |
|
|
|
it('If two lines concern the same doc (= same _id), the last one is the good version', function () { |
|
|
|
it('If two lines concern the same doc (= same _id), the last one is the good version', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "1", nested: { today: now } }) |
|
|
|
model.serialize({ _id: "1", nested: { today: now } }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(2); |
|
|
|
treatedData.length.should.equal(2); |
|
|
@ -101,11 +102,11 @@ describe('Persistence', function () { |
|
|
|
it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () { |
|
|
|
it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "2", hello: 'world' }) + '\n' + |
|
|
|
model.serialize({ _id: "1", $$deleted: true }) + '\n' + |
|
|
|
model.serialize({ _id: "1", $$deleted: true }) + '\n' + |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(2); |
|
|
|
treatedData.length.should.equal(2); |
|
|
@ -116,10 +117,10 @@ describe('Persistence', function () { |
|
|
|
it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () { |
|
|
|
it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ _id: "2", $$deleted: true }) + '\n' + |
|
|
|
model.serialize({ _id: "2", $$deleted: true }) + '\n' + |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.sort(function (a, b) { return a._id - b._id; }); |
|
|
|
treatedData.length.should.equal(2); |
|
|
|
treatedData.length.should.equal(2); |
|
|
@ -130,11 +131,11 @@ describe('Persistence', function () { |
|
|
|
it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () { |
|
|
|
it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () { |
|
|
|
var now = new Date() |
|
|
|
var now = new Date() |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + |
|
|
|
model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' + |
|
|
|
model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' + |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
model.serialize({ _id: "3", today: now }) |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, treatedData = d.persistence.treatRawData(rawData).data |
|
|
|
, indexes = d.persistence.treatRawData(rawData).indexes |
|
|
|
, indexes = d.persistence.treatRawData(rawData).indexes |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
Object.keys(indexes).length.should.equal(1); |
|
|
|
Object.keys(indexes).length.should.equal(1); |
|
|
|
assert.deepEqual(indexes.test, { fieldName: "test", unique: true }); |
|
|
|
assert.deepEqual(indexes.test, { fieldName: "test", unique: true }); |
|
|
@ -181,7 +182,7 @@ describe('Persistence', function () { |
|
|
|
var data = d.getAllData() |
|
|
|
var data = d.getAllData() |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
; |
|
|
|
; |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
data.length.should.equal(2); |
|
|
|
data.length.should.equal(2); |
|
|
|
doc1.a.should.equal(1); |
|
|
|
doc1.a.should.equal(1); |
|
|
@ -191,7 +192,7 @@ describe('Persistence', function () { |
|
|
|
var data = d.getAllData() |
|
|
|
var data = d.getAllData() |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
; |
|
|
|
; |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
data.length.should.equal(2); |
|
|
|
data.length.should.equal(2); |
|
|
|
doc1.a.should.equal(1); |
|
|
|
doc1.a.should.equal(1); |
|
|
@ -212,7 +213,7 @@ describe('Persistence', function () { |
|
|
|
var data = d.getAllData() |
|
|
|
var data = d.getAllData() |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
; |
|
|
|
; |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
data.length.should.equal(2); |
|
|
|
data.length.should.equal(2); |
|
|
|
doc1.a.should.equal(1); |
|
|
|
doc1.a.should.equal(1); |
|
|
@ -240,7 +241,7 @@ describe('Persistence', function () { |
|
|
|
var data = d.getAllData() |
|
|
|
var data = d.getAllData() |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
; |
|
|
|
; |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
data.length.should.equal(2); |
|
|
|
data.length.should.equal(2); |
|
|
|
doc1.a.should.equal(1); |
|
|
|
doc1.a.should.equal(1); |
|
|
@ -250,9 +251,9 @@ describe('Persistence', function () { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
var data = d.getAllData() |
|
|
|
var data = d.getAllData() |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) |
|
|
|
, doc3 = _.find(data, function (doc) { return doc.a === 3; }) |
|
|
|
, doc3 = _.find(data, function (doc) { return doc.a === 3; }) |
|
|
|
; |
|
|
|
; |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
data.length.should.equal(1); |
|
|
|
data.length.should.equal(1); |
|
|
@ -272,7 +273,7 @@ describe('Persistence', function () { |
|
|
|
var corruptTestFilename = 'workspace/corruptTest.db' |
|
|
|
var corruptTestFilename = 'workspace/corruptTest.db' |
|
|
|
, fakeData = '{"_id":"one","hello":"world"}\n' + 'Some corrupt data\n' + '{"_id":"two","hello":"earth"}\n' + '{"_id":"three","hello":"you"}\n' |
|
|
|
, fakeData = '{"_id":"one","hello":"world"}\n' + 'Some corrupt data\n' + '{"_id":"two","hello":"earth"}\n' + '{"_id":"three","hello":"you"}\n' |
|
|
|
, d |
|
|
|
, d |
|
|
|
; |
|
|
|
; |
|
|
|
fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); |
|
|
|
fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); |
|
|
|
|
|
|
|
|
|
|
|
// Default corruptAlertThreshold
|
|
|
|
// Default corruptAlertThreshold
|
|
|
@ -305,13 +306,13 @@ 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 () { |
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); |
|
|
|
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
|
|
|
|
// Data file left untouched
|
|
|
@ -320,7 +321,7 @@ describe('Persistence', function () { |
|
|
|
(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
|
|
|
|
// Data file left untouched
|
|
|
@ -332,14 +333,14 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
|
|
it("Declaring two hooks that are not reverse of one another will cause an exception to prevent data loss", function (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' |
|
|
|
var hookTestFilename = 'workspace/hookTest.db' |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
storage.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); |
|
|
|
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); |
|
|
|
|
|
|
|
|
|
|
|
(function () { |
|
|
|
(function () { |
|
|
|
new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
new Datastore({ filename: hookTestFilename, autoload: true |
|
|
|
, afterSerialization: as |
|
|
|
, afterSerialization: as |
|
|
|
, beforeDeserialization: function (s) { return s; } |
|
|
|
, beforeDeserialization: function (s) { return s; } |
|
|
|
}); |
|
|
|
}); |
|
|
|
}).should.throw(); |
|
|
|
}).should.throw(); |
|
|
|
|
|
|
|
|
|
|
|
// Data file left untouched
|
|
|
|
// Data file left untouched
|
|
|
@ -351,18 +352,18 @@ describe('Persistence', function () { |
|
|
|
|
|
|
|
|
|
|
|
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' |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
storage.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 |
|
|
|
}) |
|
|
|
}) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
d.insert({ hello: "world" }, function () { |
|
|
|
d.insert({ hello: "world" }, function () { |
|
|
|
var _data = fs.readFileSync(hookTestFilename, 'utf8') |
|
|
|
var _data = fs.readFileSync(hookTestFilename, 'utf8') |
|
|
|
, data = _data.split('\n') |
|
|
|
, data = _data.split('\n') |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(2); |
|
|
|
data.length.should.equal(2); |
|
|
|
|
|
|
|
|
|
|
@ -378,7 +379,7 @@ describe('Persistence', function () { |
|
|
|
, data = _data.split('\n') |
|
|
|
, data = _data.split('\n') |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(3); |
|
|
|
data.length.should.equal(3); |
|
|
|
|
|
|
|
|
|
|
@ -401,7 +402,7 @@ describe('Persistence', function () { |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
, idx = bd(data[2]) |
|
|
|
, idx = bd(data[2]) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(4); |
|
|
|
data.length.should.equal(4); |
|
|
|
|
|
|
|
|
|
|
@ -430,12 +431,12 @@ 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' |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
storage.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 |
|
|
|
}) |
|
|
|
}) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
d.insert({ hello: "world" }, function () { |
|
|
|
d.insert({ hello: "world" }, function () { |
|
|
|
d.update({ hello: "world" }, { $set: { hello: "earth" } }, {}, function () { |
|
|
|
d.update({ hello: "world" }, { $set: { hello: "earth" } }, {}, function () { |
|
|
@ -446,7 +447,7 @@ describe('Persistence', function () { |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
, doc1 = bd(data[1]) |
|
|
|
, idx = bd(data[2]) |
|
|
|
, idx = bd(data[2]) |
|
|
|
, _id |
|
|
|
, _id |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(4); |
|
|
|
data.length.should.equal(4); |
|
|
|
|
|
|
|
|
|
|
@ -469,7 +470,7 @@ describe('Persistence', function () { |
|
|
|
, data = _data.split('\n') |
|
|
|
, data = _data.split('\n') |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, doc0 = bd(data[0]) |
|
|
|
, idx = bd(data[1]) |
|
|
|
, idx = bd(data[1]) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(3); |
|
|
|
data.length.should.equal(3); |
|
|
|
|
|
|
|
|
|
|
@ -492,12 +493,12 @@ 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' |
|
|
|
Persistence.ensureFileDoesntExist(hookTestFilename, function () { |
|
|
|
storage.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 |
|
|
|
}) |
|
|
|
}) |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
d.insert({ hello: "world" }, function (err, doc) { |
|
|
|
d.insert({ hello: "world" }, function (err, doc) { |
|
|
|
var _id = doc._id; |
|
|
|
var _id = doc._id; |
|
|
@ -507,16 +508,16 @@ describe('Persistence', function () { |
|
|
|
d.ensureIndex({ fieldName: 'idefix' }, function () { |
|
|
|
d.ensureIndex({ fieldName: 'idefix' }, function () { |
|
|
|
var _data = fs.readFileSync(hookTestFilename, 'utf8') |
|
|
|
var _data = fs.readFileSync(hookTestFilename, 'utf8') |
|
|
|
, data = _data.split('\n') |
|
|
|
, data = _data.split('\n') |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
data.length.should.equal(6); |
|
|
|
data.length.should.equal(6); |
|
|
|
|
|
|
|
|
|
|
|
// Everything is deserialized correctly, including deletes and indexes
|
|
|
|
// Everything is deserialized correctly, including deletes and indexes
|
|
|
|
var d = new Datastore({ filename: hookTestFilename |
|
|
|
var d = new Datastore({ filename: hookTestFilename |
|
|
|
, afterSerialization: as |
|
|
|
, afterSerialization: as |
|
|
|
, beforeDeserialization: bd |
|
|
|
, beforeDeserialization: bd |
|
|
|
}) |
|
|
|
}) |
|
|
|
;
|
|
|
|
; |
|
|
|
d.loadDatabase(function () { |
|
|
|
d.loadDatabase(function () { |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
docs.length.should.equal(1); |
|
|
|
docs.length.should.equal(1); |
|
|
@ -558,7 +559,7 @@ describe('Persistence', function () { |
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
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); |
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
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(true); |
|
|
|
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); |
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
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(false); |
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(true); |
|
|
|
fs.existsSync('workspace/it.db~~').should.equal(true); |
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
storage.ensureDatafileIntegrity(p.filename, function (err) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
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); |
|
|
|
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); |
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
@ -761,76 +762,76 @@ describe('Persistence', function () { |
|
|
|
var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; |
|
|
|
var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; |
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
async.apply(Persistence.ensureFileDoesntExist, dbFile) |
|
|
|
async.apply(storage.ensureFileDoesntExist, dbFile) |
|
|
|
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~') |
|
|
|
, async.apply(storage.ensureFileDoesntExist, dbFile + '~') |
|
|
|
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~~') |
|
|
|
, async.apply(storage.ensureFileDoesntExist, dbFile + '~~') |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb = new Datastore({ filename: dbFile }); |
|
|
|
theDb = new Datastore({ filename: dbFile }); |
|
|
|
theDb.loadDatabase(cb); |
|
|
|
theDb.loadDatabase(cb); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(0); |
|
|
|
docs.length.should.equal(0); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb.insert({ a: 'hello' }, function (err, _doc1) { |
|
|
|
theDb.insert({ a: 'hello' }, function (err, _doc1) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
doc1 = _doc1; |
|
|
|
doc1 = _doc1; |
|
|
|
theDb.insert({ a: 'world' }, function (err, _doc2) { |
|
|
|
theDb.insert({ a: 'world' }, function (err, _doc2) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
doc2 = _doc2; |
|
|
|
doc2 = _doc2; |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(2); |
|
|
|
docs.length.should.equal(2); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb.loadDatabase(cb); |
|
|
|
theDb.loadDatabase(cb); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { // No change
|
|
|
|
, function (cb) { // No change
|
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
theDb.find({}, function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(2); |
|
|
|
docs.length.should.equal(2); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
fs.existsSync(dbFile).should.equal(true); |
|
|
|
fs.existsSync(dbFile).should.equal(true); |
|
|
|
fs.existsSync(dbFile + '~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~~').should.equal(false); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
theDb2 = new Datastore({ filename: dbFile }); |
|
|
|
theDb2 = new Datastore({ filename: dbFile }); |
|
|
|
theDb2.loadDatabase(cb); |
|
|
|
theDb2.loadDatabase(cb); |
|
|
|
}
|
|
|
|
}
|
|
|
|
, function (cb) { // No change in second db
|
|
|
|
, function (cb) { // No change in second db
|
|
|
|
theDb2.find({}, function (err, docs) { |
|
|
|
theDb2.find({}, function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(2); |
|
|
|
docs.length.should.equal(2); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
fs.existsSync(dbFile).should.equal(true); |
|
|
|
fs.existsSync(dbFile).should.equal(true); |
|
|
|
fs.existsSync(dbFile + '~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~~').should.equal(false); |
|
|
|
fs.existsSync(dbFile + '~~').should.equal(false); |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
}
|
|
|
|
}
|
|
|
|
], done); |
|
|
|
], done); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -891,7 +892,7 @@ describe('Persistence', function () { |
|
|
|
describe('ensureFileDoesntExist', function () { |
|
|
|
describe('ensureFileDoesntExist', function () { |
|
|
|
|
|
|
|
|
|
|
|
it('Doesnt do anything if file already doesnt exist', function (done) { |
|
|
|
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); |
|
|
|
assert.isNull(err); |
|
|
|
fs.existsSync('workspace/nonexisting').should.equal(false); |
|
|
|
fs.existsSync('workspace/nonexisting').should.equal(false); |
|
|
|
done(); |
|
|
|
done(); |
|
|
@ -902,7 +903,7 @@ describe('Persistence', function () { |
|
|
|
fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); |
|
|
|
fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); |
|
|
|
fs.existsSync('workspace/existing').should.equal(true); |
|
|
|
fs.existsSync('workspace/existing').should.equal(true); |
|
|
|
|
|
|
|
|
|
|
|
Persistence.ensureFileDoesntExist('workspace/existing', function (err) { |
|
|
|
storage.ensureFileDoesntExist('workspace/existing', function (err) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
fs.existsSync('workspace/existing').should.equal(false); |
|
|
|
fs.existsSync('workspace/existing').should.equal(false); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|