diff --git a/lib/datastore.js b/lib/datastore.js index 03cd1e3..424585c 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -143,7 +143,7 @@ Datastore.prototype.updateIndexes = function (oldDoc, newDoc) { // If an error happened, we need to rollback the insert on all other indexes if (error) { for (i = 0; i < failingIndex; i += 1) { - this.indexes[keys[i]].remove(doc); + this.indexes[keys[i]].revertUpdate(oldDoc, newDoc); } throw error; diff --git a/test/db.test.js b/test/db.test.js index ffaef22..3a0e73b 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1491,24 +1491,38 @@ describe('Database', function () { }); // ==== End of 'Indexing newly inserted documents' ==== // - describe.skip('Updating indexes upon document update', function () { + describe.only('Updating indexes upon document update', function () { - it('Updating docs still works as before with an index', function (done) { + it('Updating docs still works as before with indexing', function (done) { d.ensureIndex({ fieldName: 'a' }); - d.insert({ a: 1, b: 'hello' }, function (err, doc1) { - d.insert({ a: 2, b: 'si' }, function (err, doc2) { + d.insert({ a: 1, b: 'hello' }, function (err, _doc1) { + d.insert({ a: 2, b: 'si' }, function (err, _doc2) { d.update({ a: 1 }, { $set: { a: 456, b: 'no' } }, {}, function (err, nr) { + var data = d.getAllData() + , doc1 = _.find(data, function (doc) { return doc._id === _doc1._id; }) + , doc2 = _.find(data, function (doc) { return doc._id === _doc2._id; }) + ; + assert.isNull(err); nr.should.equal(1); - //console.log(d.data); - return done(); + data.length.should.equal(2); + assert.deepEqual(doc1, { a: 456, b: 'no', _id: _doc1._id }); + assert.deepEqual(doc2, { a: 2, b: 'si', _id: _doc2._id }); + + d.update({}, { $inc: { a: 10 }, $set: { b: 'same' } }, { multi: true }, function (err, nr) { + var data = d.getAllData() + , doc1 = _.find(data, function (doc) { return doc._id === _doc1._id; }) + , doc2 = _.find(data, function (doc) { return doc._id === _doc2._id; }) + ; - d.update({}, { $inc: { a: 10 }, $set: { b: 'same' } }, { multi: true }, function () { assert.isNull(err); nr.should.equal(2); + data.length.should.equal(2); + assert.deepEqual(doc1, { a: 466, b: 'same', _id: _doc1._id }); + assert.deepEqual(doc2, { a: 12, b: 'same', _id: _doc2._id }); done(); }); @@ -1554,10 +1568,7 @@ describe('Database', function () { }); }); - it.skip('If an update violates a contraints, nothing is done', function (done) { - }); - - it.skip('Updates still work with indexing', function (done) { + it.skip('If an update violates a contraint, all changes are rolled back and an error is thrown', function (done) { }); }); // ==== End of 'Updating indexes upon document update' ==== // diff --git a/test/indexes.test.js b/test/indexes.test.js index b8c45d2..f7ee8cf 100644 --- a/test/indexes.test.js +++ b/test/indexes.test.js @@ -330,7 +330,7 @@ describe('Indexes', function () { assert.deepEqual(idx.tree.search('world'), [noChange]); }); - it('Can revert normal and batch updates', function () { + it('Can revert simple and batch updates', function () { var idx = new Index({ fieldName: 'tf' }) , doc1 = { a: 5, tf: 'hello' } , doc2 = { a: 8, tf: 'world' }