Fixed bug in deepCopy thay didnt deep copy array contents

pull/2/head
Louis Chatriot 10 years ago
parent 3d8d87fcd7
commit 1bee5827bb
  1. 4
      lib/model.js
  2. 28
      test/db.test.js
  3. 11
      test/model.test.js

@ -113,7 +113,7 @@ function deepCopy (obj) {
if (util.isArray(obj)) { if (util.isArray(obj)) {
res = []; res = [];
obj.forEach(function (o) { res.push(o); }); obj.forEach(function (o) { res.push(deepCopy(o)); });
return res; return res;
} }
@ -374,7 +374,6 @@ Object.keys(lastStepModifierFunctions).forEach(function (modifier) {
/** /**
* Modify a DB object according to an update query * Modify a DB object according to an update query
* For now the updateQuery only replaces the object
*/ */
function modify (obj, updateQuery) { function modify (obj, updateQuery) {
var keys = Object.keys(updateQuery) var keys = Object.keys(updateQuery)
@ -416,6 +415,7 @@ function modify (obj, updateQuery) {
// Check result is valid and return it // Check result is valid and return it
checkObject(newDoc); checkObject(newDoc);
if (obj._id !== newDoc._id) { throw "You can't change a document's _id"; } if (obj._id !== newDoc._id) { throw "You can't change a document's _id"; }
return newDoc; return newDoc;
}; };

@ -971,6 +971,8 @@ describe('Database', function () {
], done); ], done);
}); });
describe.skip('Upserts', function () {
it('Can perform upserts if needed', function (done) { it('Can perform upserts if needed', function (done) {
d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, {}, function (err, nr, upsert) { d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, {}, function (err, nr, upsert) {
assert.isNull(err); assert.isNull(err);
@ -1004,6 +1006,32 @@ describe('Database', function () {
}); });
}); });
it('If the update query is a normal object with no modifiers, it is the doc that will be upserted', function (done) {
d.update({ $or: [{ a: 4 }, { a: 5 }] }, { hello: 'world', bloup: 'blap' }, { upsert: true }, function (err) {
d.findOne({}, function (err, doc) {
assert.isNull(err);
Object.keys(doc).length.should.equal(3);
doc.hello.should.equal('world');
doc.bloup.should.equal('blap');
done();
});
});
});
it('If the update query contains modifiers, it is applied to the object resulting from removing all operator fro; the find query', function (done) {
d.update({ $or: [{ a: 4 }, { a: 5 }] }, { hello: 'world', $inc: { bloup: 3 } }, { upsert: true }, function (err) {
d.findOne({ hello: 'world' }, function (err, doc) {
assert.isNull(err);
Object.keys(doc).length.should.equal(3);
doc.hello.should.equal('world');
doc.bloup.should.equal(3);
done();
});
});
});
}); // ==== End of 'Upserts' ==== //
it('Cannot perform update if the update query is not either registered-modifiers-only or copy-only, or contain badly formatted fields', function (done) { it('Cannot perform update if the update query is not either registered-modifiers-only or copy-only, or contain badly formatted fields', function (done) {
d.insert({ something: 'yup' }, function () { d.insert({ something: 'yup' }, function () {
d.update({}, { boom: { $badfield: 5 } }, { multi: false }, function (err) { d.update({}, { boom: { $badfield: 5 } }, { multi: false }, function (err) {

@ -250,6 +250,17 @@ describe('Model', function () {
res.subobj.b.should.equal('c'); res.subobj.b.should.equal('c');
}); });
it('Should deep copy the contents of an array', function () {
var a = [{ hello: 'world' }]
, b = model.deepCopy(a)
;
b[0].hello.should.equal('world');
b[0].hello = 'another';
b[0].hello.should.equal('another');
a[0].hello.should.equal('world');
});
}); // ==== End of 'Deep copying' ==== // }); // ==== End of 'Deep copying' ==== //

Loading…
Cancel
Save