From 32f568d0631b1f0112e213f54ea9b2098ccc2065 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 31 May 2013 19:56:46 +0200 Subject: [PATCH] Update now completely works with indexing, only remove needs to be tested --- test/db.test.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test/db.test.js b/test/db.test.js index 47bd009..439ce96 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1636,6 +1636,53 @@ describe('Database', function () { }); }); + it('If a multi update violates a contraint, all changes are rolled back and an error is thrown', function (done) { + d.ensureIndex({ fieldName: 'a', unique: true }); + d.ensureIndex({ fieldName: 'b', unique: true }); + d.ensureIndex({ fieldName: 'c', unique: true }); + + d.insert({ a: 1, b: 10, c: 100 }, function (err, _doc1) { + d.insert({ a: 2, b: 20, c: 200 }, function (err, _doc2) { + d.insert({ a: 3, b: 30, c: 300 }, function (err, _doc3) { + // Will conflict with doc3 + d.update({ a: { $in: [1, 2] } }, { $inc: { a: 10, c: 1000 }, $set: { b: 30 } }, { multi: true }, function (err) { + var data = d.getAllData() + , doc1 = _.find(data, function (doc) { return doc._id === _doc1._id; }) + , doc2 = _.find(data, function (doc) { return doc._id === _doc2._id; }) + , doc3 = _.find(data, function (doc) { return doc._id === _doc3._id; }) + ; + + err.errorType.should.equal('uniqueViolated'); + + // Data left unchanged + data.length.should.equal(3); + assert.deepEqual(doc1, { a: 1, b: 10, c: 100, _id: _doc1._id }); + assert.deepEqual(doc2, { a: 2, b: 20, c: 200, _id: _doc2._id }); + assert.deepEqual(doc3, { a: 3, b: 30, c: 300, _id: _doc3._id }); + + // All indexes left unchanged and pointing to the same docs + d.indexes.a.tree.getNumberOfKeys().should.equal(3); + d.indexes.a.getMatching(1)[0].should.equal(doc1); + d.indexes.a.getMatching(2)[0].should.equal(doc2); + d.indexes.a.getMatching(3)[0].should.equal(doc3); + + d.indexes.b.tree.getNumberOfKeys().should.equal(3); + d.indexes.b.getMatching(10)[0].should.equal(doc1); + d.indexes.b.getMatching(20)[0].should.equal(doc2); + d.indexes.b.getMatching(30)[0].should.equal(doc3); + + d.indexes.c.tree.getNumberOfKeys().should.equal(3); + d.indexes.c.getMatching(100)[0].should.equal(doc1); + d.indexes.c.getMatching(200)[0].should.equal(doc2); + d.indexes.c.getMatching(300)[0].should.equal(doc3); + + done(); + }); + }); + }); + }); + }); + }); // ==== End of 'Updating indexes upon document update' ==== //