Cant try to update an _id in a copy-type modif

pull/2/head
Louis Chatriot 12 years ago
parent cf09a75be3
commit 8d4182067b
  1. 2
      lib/model.js
  2. 31
      test/model.test.js

@ -128,6 +128,8 @@ function deepCopy (obj) {
* For now the updateQuery only replaces the object * For now the updateQuery only replaces the object
*/ */
function modify (obj, updateQuery) { function modify (obj, updateQuery) {
if (updateQuery._id) { throw "You cannot change a document's _id"; }
updateQuery = deepCopy(updateQuery); updateQuery = deepCopy(updateQuery);
updateQuery._id = obj._id; updateQuery._id = obj._id;

@ -193,4 +193,35 @@ describe('Model', function () {
}); // ==== End of 'Deep copying' ==== // }); // ==== End of 'Deep copying' ==== //
describe('Modifying documents', function () {
it('Queries not containing any modifier just replace the document by the contents of the query but keep its _id', function () {
var obj = { some: 'thing', _id: 'keepit' }
, updateQuery = { replace: 'done', bloup: [ 1, 8] }
, t
;
t = model.modify(obj, updateQuery);
t.replace.should.equal('done');
t.bloup.length.should.equal(2);
t.bloup[0].should.equal(1);
t.bloup[1].should.equal(8);
assert.isUndefined(t.some);
t._id.should.equal('keepit');
});
it('Raise an error if trying to replace the _id field in a copy-type modification', function () {
var obj = { some: 'thing', _id: 'keepit' }
, updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' }
;
(function () {
model.modify(obj, updateQuery);
}).should.throw();
});
}); // ==== End of 'Modifying documents' ==== //
}); });

Loading…
Cancel
Save