Tested that index options are also persisted

pull/2/head
Louis Chatriot 11 years ago
parent 878082f38d
commit 8c8cc8fea4
  1. 86
      test/db.test.js

@ -2011,7 +2011,7 @@ describe('Database', function () {
describe.only('Persisting indexes', function () {
it('If the persistIndexes options is used, indexes are persisted to a separate file and recreated upon reload', function (done) {
it('Indexes are persisted to a separate file and recreated upon reload', function (done) {
var persDb = "workspace/persistIndexes.db"
, db
;
@ -2033,6 +2033,7 @@ describe('Database', function () {
Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(2);
db.indexes.planet.getAll().length.should.equal(2);
db.indexes.planet.fieldName.should.equal("planet");
// After a reload the indexes are recreated
db = new Datastore({ filename: persDb });
@ -2043,6 +2044,7 @@ describe('Database', function () {
Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(2);
db.indexes.planet.getAll().length.should.equal(2);
db.indexes.planet.fieldName.should.equal("planet");
// After another reload the indexes are still there (i.e. they are preserved during autocompaction)
db = new Datastore({ filename: persDb });
@ -2053,6 +2055,7 @@ describe('Database', function () {
Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(2);
db.indexes.planet.getAll().length.should.equal(2);
db.indexes.planet.fieldName.should.equal("planet");
done();
});
@ -2062,6 +2065,87 @@ describe('Database', function () {
});
});
it('Indexes are persisted with their options and recreated even if some db operation happen between loads', function (done) {
var persDb = "workspace/persistIndexes.db"
, db
;
if (fs.existsSync(persDb)) { fs.writeFileSync(persDb, '', 'utf8'); }
db = new Datastore({ filename: persDb, autoload: true });
Object.keys(db.indexes).length.should.equal(1);
Object.keys(db.indexes)[0].should.equal("_id");
db.insert({ planet: "Earth" }, function (err) {
assert.isNull(err);
db.insert({ planet: "Mars" }, function (err) {
assert.isNull(err);
db.ensureIndex({ fieldName: "planet", unique: true, sparse: false }, function (err) {
Object.keys(db.indexes).length.should.equal(2);
Object.keys(db.indexes)[0].should.equal("_id");
Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(2);
db.indexes.planet.getAll().length.should.equal(2);
db.indexes.planet.unique.should.equal(true);
db.indexes.planet.sparse.should.equal(false);
db.insert({ planet: "Jupiter" }, function (err) {
assert.isNull(err);
// After a reload the indexes are recreated
db = new Datastore({ filename: persDb });
db.loadDatabase(function (err) {
assert.isNull(err);
Object.keys(db.indexes).length.should.equal(2);
Object.keys(db.indexes)[0].should.equal("_id");
Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(3);
db.indexes.planet.getAll().length.should.equal(3);
db.indexes.planet.unique.should.equal(true);
db.indexes.planet.sparse.should.equal(false);
db.ensureIndex({ fieldName: 'bloup', unique: false, sparse: true }, function (err) {
assert.isNull(err);
Object.keys(db.indexes).length.should.equal(3);
Object.keys(db.indexes)[0].should.equal("_id");
Object.keys(db.indexes)[1].should.equal("planet");
Object.keys(db.indexes)[2].should.equal("bloup");
db.indexes._id.getAll().length.should.equal(3);
db.indexes.planet.getAll().length.should.equal(3);
db.indexes.bloup.getAll().length.should.equal(0);
db.indexes.planet.unique.should.equal(true);
db.indexes.planet.sparse.should.equal(false);
db.indexes.bloup.unique.should.equal(false);
db.indexes.bloup.sparse.should.equal(true);
// After another reload the indexes are still there (i.e. they are preserved during autocompaction)
db = new Datastore({ filename: persDb });
db.loadDatabase(function (err) {
assert.isNull(err);
Object.keys(db.indexes).length.should.equal(3);
Object.keys(db.indexes)[0].should.equal("_id");
Object.keys(db.indexes)[1].should.equal("planet");
Object.keys(db.indexes)[2].should.equal("bloup");
db.indexes._id.getAll().length.should.equal(3);
db.indexes.planet.getAll().length.should.equal(3);
db.indexes.bloup.getAll().length.should.equal(0);
db.indexes.planet.unique.should.equal(true);
db.indexes.planet.sparse.should.equal(false);
db.indexes.bloup.unique.should.equal(false);
db.indexes.bloup.sparse.should.equal(true);
done();
});
});
});
});
});
});
});
});
}); // ==== End of 'Persisting indexes' ====
}); // ==== End of 'Using indexes' ==== //

Loading…
Cancel
Save