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. 68
      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,38 +971,66 @@ describe('Database', function () {
], done); ], done);
}); });
it('Can perform upserts if needed', function (done) { describe.skip('Upserts', function () {
d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, {}, function (err, nr, upsert) {
assert.isNull(err);
nr.should.equal(0);
assert.isUndefined(upsert);
d.find({}, function (err, docs) { it('Can perform upserts if needed', function (done) {
docs.length.should.equal(0); // Default option for upsert is false d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, {}, function (err, nr, upsert) {
assert.isNull(err);
nr.should.equal(0);
assert.isUndefined(upsert);
d.update({ impossible: 'db is empty anyway' }, { something: "created ok" }, { upsert: true }, function (err, nr, newDoc) { d.find({}, function (err, docs) {
assert.isNull(err); docs.length.should.equal(0); // Default option for upsert is false
nr.should.equal(1);
newDoc.something.should.equal("created ok");
assert.isDefined(newDoc._id);
d.find({}, function (err, docs) { d.update({ impossible: 'db is empty anyway' }, { something: "created ok" }, { upsert: true }, function (err, nr, newDoc) {
docs.length.should.equal(1); // Default option for upsert is false assert.isNull(err);
docs[0].something.should.equal("created ok"); nr.should.equal(1);
newDoc.something.should.equal("created ok");
assert.isDefined(newDoc._id);
// Modifying the returned upserted document doesn't modify the database
newDoc.newField = true;
d.find({}, function (err, docs) { d.find({}, function (err, docs) {
docs.length.should.equal(1); // Default option for upsert is false
docs[0].something.should.equal("created ok"); docs[0].something.should.equal("created ok");
assert.isUndefined(docs[0].newField);
done(); // Modifying the returned upserted document doesn't modify the database
newDoc.newField = true;
d.find({}, function (err, docs) {
docs[0].something.should.equal("created ok");
assert.isUndefined(docs[0].newField);
done();
});
}); });
}); });
}); });
}); });
}); });
});
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 () {

@ -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