diff --git a/lib/datastore.js b/lib/datastore.js index 8a58946..e07cb8b 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -297,7 +297,7 @@ Datastore.prototype._insert = function (newDoc, cb) { if (err) { return callback(err); } self.datafileSize += 1; - return callback(null, model.deepCopy(insertedDoc)); // CHANGE + return callback(null, newDoc); }); }; @@ -437,14 +437,14 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { numReplaced += 1; modifiedDoc = model.modify(candidates[i], updateQuery); self.updateIndexes(candidates[i], modifiedDoc); - candidates[i] = modifiedDoc; - updatedDocs.push(candidates[i]); + updatedDocs.push(modifiedDoc); } } } catch (err) { return callback(err); } + self.persistNewState(updatedDocs, function (err) { if (err) { return callback(err); } return callback(null, numReplaced); diff --git a/lib/indexes.js b/lib/indexes.js index 92434dc..d2507c4 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -5,11 +5,10 @@ var BinarySearchTree = require('binary-search-tree').BinarySearchTree ; /** - * We can't use the one in model here since it doesn't work for arrays + * Two indexed pointers are equal iif they point to the same place */ function checkValueEquality (a, b) { return a === b; - return model.compareThings(a, b) === 0; } diff --git a/test/db.test.js b/test/db.test.js index d1f7287..4ad22d4 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -40,7 +40,7 @@ describe('Database', function () { }); - describe.only('Loading the database data from file and persistence', function () { + describe('Loading the database data from file and persistence', function () { it('Every line represents a document', function () { var now = new Date() @@ -304,16 +304,16 @@ describe('Database', function () { it('datafileSize is incremented by 1 upon every insert', function (done) { d.datafileSize.should.equal(0); - d.data.length.should.equal(0); + d.getAllData().length.should.equal(0); d.insert({ a: 3 }, function () { d.datafileSize.should.equal(1); - d.data.length.should.equal(1); + d.getAllData().length.should.equal(1); d.insert({ a: 3 }, function () { d.datafileSize.should.equal(2); - d.data.length.should.equal(2); + d.getAllData().length.should.equal(2); d.insert({ a: 3 }, function () { d.datafileSize.should.equal(3); - d.data.length.should.equal(3); + d.getAllData().length.should.equal(3); done(); }); }); @@ -535,7 +535,7 @@ describe('Database', function () { }); // ==== End of 'Find' ==== // - describe('Update', function () { + describe.only('Update', function () { it("If the query doesn't match anything, database is not modified", function (done) { async.waterfall([ @@ -560,18 +560,10 @@ describe('Database', function () { 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); + assert.deepEqual(doc1, { _id: doc1._id, somedata: 'ok' }); + assert.deepEqual(doc2, { _id: doc2._id, somedata: 'again', plus: 'additional data' }); + assert.deepEqual(doc3, { _id: doc3._id, somedata: 'another' }); - 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(); }); }); @@ -622,7 +614,7 @@ describe('Database', function () { }); }); } - , function (cb) { // Test with query that doesn't match anything + , function (cb) { d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: true }, function (err, n) { assert.isNull(err); n.should.equal(2); @@ -650,18 +642,17 @@ describe('Database', function () { docs.length.should.equal(3); - Object.keys(doc1).length.should.equal(2); - doc1.somedata.should.equal('ok'); - doc1._id.should.equal(id1); + assert.deepEqual(doc1, { somedata: 'ok', _id: doc1._id }); - Object.keys(doc2).length.should.equal(2); - doc2.newDoc.should.equal('yes'); - doc2._id.should.equal(id2); - - // Third object was not updated - Object.keys(doc3).length.should.equal(2); - doc3.somedata.should.equal('again'); - doc3._id.should.equal(id3); + // doc2 or doc3 was modified. Since we sort on _id and it is random + // it can be either of two situations + try { + assert.deepEqual(doc2, { newDoc: 'yes', _id: doc2._id }); + assert.deepEqual(doc3, { somedata: 'again', _id: doc3._id }); + } catch (e) { + assert.deepEqual(doc2, { somedata: 'again', plus: 'additional data', _id: doc2._id }); + assert.deepEqual(doc3, { newDoc: 'yes', _id: doc3._id }); + } return cb(); }); @@ -916,15 +907,15 @@ describe('Database', function () { d.insert({ a: 3 }, function () { d.insert({ a: 5 }, function () { d.datafileSize.should.equal(3); - d.data.length.should.equal(3); + d.getAllData().length.should.equal(3); d.update({ a: 3 }, { $set: { a: 4 } }, {}, function () { d.datafileSize.should.equal(4); - d.data.length.should.equal(3); + d.getAllData().length.should.equal(3); d.update({ a: { $in: [2, 4] } }, { $set: { a: 5 } }, { multi: true }, function () { d.datafileSize.should.equal(6); - d.data.length.should.equal(3); + d.getAllData().length.should.equal(3); done(); });