From 7b31c9042e15837eac2795c5ffaddb871e4044ea Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 2 May 2013 11:57:24 +0200 Subject: [PATCH] Update doesnt modify the db if query doesnt match anything --- test/db.test.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/test/db.test.js b/test/db.test.js index 8414fa8..3e3cd9d 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -177,9 +177,61 @@ describe('Database', function () { ], done); }); - }); // ==== End of 'Find' ==== // + describe('Update', function () { + + it("If the query doesn't match anything, database is not modified", function (done) { + var d = new Datastore(testDb); + + async.waterfall([ + function (cb) { + d.loadDatabase(function (err) { + d.insert({ somedata: 'ok' }, function (err) { + d.insert({ somedata: 'again', plus: 'additional data' }, function (err) { + d.insert({ somedata: 'another' }, function (err) { return cb(err); }); + }); + }); + }); + } + , function (cb) { // Test with query that doesn't match anything + d.update({ somedata: 'nope' }, { newDoc: 'yes' }, { multi: true }, function (err, n) { + assert.isNull(err); + n.should.equal(0); + + d.find({}, function (err, docs) { + var doc1 = _.find(docs, function (d) { return d.somedata === 'ok'; }) + , doc2 = _.find(docs, function (d) { return d.somedata === 'again'; }) + , doc3 = _.find(docs, function (d) { return d.somedata === 'another'; }) + ; + + docs.length.should.equal(3); + assert.isUndefined(_.find(docs, function (d) { return d.newDoc === 'yes'; })); + + Object.keys(doc1).length.should.equal(2); + doc1.somedata.should.equal('ok'); + assert.isDefined(doc1._id); + + Object.keys(doc2).length.should.equal(3); + doc2.somedata.should.equal('again'); + doc2.plus.should.equal('additional data'); + assert.isDefined(doc2._id); + + Object.keys(doc3).length.should.equal(2); + doc3.somedata.should.equal('another'); + assert.isDefined(doc3._id); + return cb(); + }); + }); + } + ], done); + }); + + + }); // ==== End of 'Update' ==== // + + + });