Projection fully tested for cursor

pull/2/head
Louis Chatriot 10 years ago
parent a6a4724cbc
commit c11128160f
  1. 8
      lib/cursor.js
  2. 51
      test/cursor.test.js

@ -84,7 +84,11 @@ Cursor.prototype.project = function (candidates) {
// Do the actual projection // Do the actual projection
candidates.forEach(function (candidate) { candidates.forEach(function (candidate) {
var toPush = action === 1 ? _.pick(candidate, keys) : _.omit(candidate, keys); var toPush = action === 1 ? _.pick(candidate, keys) : _.omit(candidate, keys);
if (keepId) { toPush._id = candidate._id; } if (keepId) {
toPush._id = candidate._id;
} else {
delete toPush._id;
}
res.push(toPush); res.push(toPush);
}); });
@ -161,7 +165,7 @@ Cursor.prototype._exec = function(callback) {
res = this.project(res); res = this.project(res);
} catch (e) { } catch (e) {
error = e; error = e;
res = null; res = undefined;
} }
if (this.execFn) { if (this.execFn) {

@ -722,18 +722,57 @@ describe('Cursor', function () {
cursor.exec(function (err, docs) { cursor.exec(function (err, docs) {
assert.isNull(err); assert.isNull(err);
docs.length.should.equal(5); docs.length.should.equal(5);
// Takes the _id by default assert.deepEqual(docs[0], { age: 5, name: 'Jo' });
assert.deepEqual(docs[0], { age: 5, name: 'Jo', _id: doc0._id }); assert.deepEqual(docs[1], { age: 23, name: 'LM' });
assert.deepEqual(docs[1], { age: 23, name: 'LM', _id: doc3._id }); assert.deepEqual(docs[2], { age: 52, name: 'Grafitti' });
assert.deepEqual(docs[2], { age: 52, name: 'Grafitti', _id: doc2._id }); assert.deepEqual(docs[3], { age: 57, name: 'Louis' });
assert.deepEqual(docs[3], { age: 57, name: 'Louis', _id: doc1._id }); assert.deepEqual(docs[4], { age: 89 }); // No problems if one field to take doesn't exist
assert.deepEqual(docs[4], { age: 89, _id: doc4._id }); // No problems if one field to take doesn't exist
done(); done();
}); });
}); });
}); });
it('Can omit only the expected fields', function (done) {
var cursor = new Cursor(d, {});
cursor.sort({ age: 1 }); // For easier finding
cursor.projection({ age: 0, name: 0 });
cursor.exec(function (err, docs) {
assert.isNull(err);
docs.length.should.equal(5);
// Takes the _id by default
assert.deepEqual(docs[0], { planet: 'B', _id: doc0._id });
assert.deepEqual(docs[1], { planet: 'S', _id: doc3._id });
assert.deepEqual(docs[2], { planet: 'C', _id: doc2._id });
assert.deepEqual(docs[3], { planet: 'R', _id: doc1._id });
assert.deepEqual(docs[4], { planet: 'Earth', _id: doc4._id });
cursor.projection({ age: 0, name: 0, _id: 0 });
cursor.exec(function (err, docs) {
assert.isNull(err);
docs.length.should.equal(5);
assert.deepEqual(docs[0], { planet: 'B' });
assert.deepEqual(docs[1], { planet: 'S' });
assert.deepEqual(docs[2], { planet: 'C' });
assert.deepEqual(docs[3], { planet: 'R' });
assert.deepEqual(docs[4], { planet: 'Earth' });
done();
});
});
});
it('Cannot use both modes except for _id', function (done) {
var cursor = new Cursor(d, {});
cursor.sort({ age: 1 }); // For easier finding
cursor.projection({ age: 1, name: 0 });
cursor.exec(function (err, docs) {
assert.isNotNull(err);
assert.isUndefined(docs);
done();
});
});
}); // ==== End of 'Projections' ==== }); // ==== End of 'Projections' ====
}); });

Loading…
Cancel
Save