Corrected unlikely bug on database state pollution

pull/2/head
Louis Chatriot 12 years ago
parent 74f0d8914e
commit 55993012b2
  1. 2
      lib/datastore.js
  2. 33
      test/db.test.js

@ -139,7 +139,7 @@ Datastore.prototype._insert = function (newDoc, cb) {
var insertedDoc = model.deserialize(persistableNewDoc); var insertedDoc = model.deserialize(persistableNewDoc);
self.data.push(insertedDoc); self.data.push(insertedDoc);
self.datafileSize += 1; self.datafileSize += 1;
return callback(null, insertedDoc); return callback(null, model.deepCopy(insertedDoc));
}); });
}; };

@ -318,6 +318,17 @@ describe('Database', function () {
}); });
}); });
it('Modifying the insertedDoc after an insert doesnt change the copy saved in the database', function (done) {
d.insert({ a: 2, hello: 'world' }, function (err, newDoc) {
newDoc.hello = 'changed';
d.findOne({ a: 2 }, function (err, doc) {
doc.hello.should.equal('world');
done();
});
});
});
}); // ==== End of 'Insert' ==== // }); // ==== End of 'Insert' ==== //
@ -497,6 +508,28 @@ describe('Database', function () {
}); });
}); });
it('Changing the documents returned by find or findOne do not change the database state', function (done) {
d.insert({ a: 2, hello: 'world' }, function () {
d.findOne({ a: 2 }, function (err, doc) {
doc.hello = 'changed';
d.findOne({ a: 2 }, function (err, doc) {
doc.hello.should.equal('world');
d.find({ a: 2 }, function (err, docs) {
docs[0].hello = 'changed';
d.findOne({ a: 2 }, function (err, doc) {
doc.hello.should.equal('world');
done();
});
});
});
});
});
});
}); // ==== End of 'Find' ==== // }); // ==== End of 'Find' ==== //

Loading…
Cancel
Save