From 0fb797240e73dcb506fdf6ad34e275039bc91039 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 17 Aug 2014 19:41:17 +0200 Subject: [PATCH] Real test for empty projection and take-type projections --- lib/cursor.js | 6 +++--- test/cursor.test.js | 46 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/cursor.js b/lib/cursor.js index 8ec7aa1..799de30 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -50,8 +50,8 @@ Cursor.prototype.sort = function(sortQuery) { /** * Add the use of a projection - * @param {Object} projection - MongoDB-style projection. {} means take all fields. Then it's {key1:1, key2:1} to take only key1 and key2 - * {key1: 0, key2: 0} to omit only key1 and key2. Except _id, you can't mix takes and omits + * @param {Object} projection - MongoDB-style projection. {} means take all fields. Then it's { key1: 1, key2: 1 } to take only key1 and key2 + * { key1: 0, key2: 0 } to omit only key1 and key2. Except _id, you can't mix takes and omits */ Cursor.prototype.projection = function(projection) { this._projection = projection; @@ -85,7 +85,7 @@ Cursor.prototype.project = function (candidates) { candidates.forEach(function (candidate) { var toPush = action === 1 ? _.pick(candidate, keys) : _.omit(candidate, keys); if (keepId) { toPush._id = candidate._id; } - res.push(candidate); + res.push(toPush); }); return res; diff --git a/test/cursor.test.js b/test/cursor.test.js index a145bc0..4e28b44 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -654,14 +654,21 @@ describe('Cursor', function () { describe.only('Projections', function () { + var doc1, doc2, doc3, doc4, doc0; + beforeEach(function (done) { // We don't know the order in which docs wil be inserted but we ensure correctness by testing both sort orders - d.insert({ age: 5, name: 'Jo', planet: 'B' }, function (err) { - d.insert({ age: 57, name: 'Louis', planet: 'R' }, function (err) { - d.insert({ age: 52, name: 'Grafitti', planet: 'C' }, function (err) { - d.insert({ age: 23, name: 'LM', planet: 'S' }, function (err) { - d.insert({ age: 89, planet: 'Earth' }, function (err) { + d.insert({ age: 5, name: 'Jo', planet: 'B' }, function (err, _doc0) { + doc0 = _doc0; + d.insert({ age: 57, name: 'Louis', planet: 'R' }, function (err, _doc1) { + doc1 = _doc1; + d.insert({ age: 52, name: 'Grafitti', planet: 'C' }, function (err, _doc2) { + doc2 = _doc2; + d.insert({ age: 23, name: 'LM', planet: 'S' }, function (err, _doc3) { + doc3 = _doc3; + d.insert({ age: 89, planet: 'Earth' }, function (err, _doc4) { + doc4 = _doc4; return done(); }); }); @@ -672,20 +679,49 @@ describe('Cursor', function () { it('Takes all results if no projection or empty object given', function (done) { var cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); // For easier finding cursor.exec(function (err, docs) { assert.isNull(err); docs.length.should.equal(5); + assert.deepEqual(docs[0], doc0); + assert.deepEqual(docs[1], doc3); + assert.deepEqual(docs[2], doc2); + assert.deepEqual(docs[3], doc1); + assert.deepEqual(docs[4], doc4); cursor.projection({}); cursor.exec(function (err, docs) { assert.isNull(err); docs.length.should.equal(5); + assert.deepEqual(docs[0], doc0); + assert.deepEqual(docs[1], doc3); + assert.deepEqual(docs[2], doc2); + assert.deepEqual(docs[3], doc1); + assert.deepEqual(docs[4], doc4); done(); }); }); }); + it('Can take only the expected fields', function (done) { + var cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); // For easier finding + cursor.projection({ age: 1, name: 1 }); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(5); + // Takes the _id by default + assert.deepEqual(docs[0], { age: 5, name: 'Jo', _id: doc0._id }); + assert.deepEqual(docs[1], { age: 23, name: 'LM', _id: doc3._id }); + assert.deepEqual(docs[2], { age: 52, name: 'Grafitti', _id: doc2._id }); + assert.deepEqual(docs[3], { age: 57, name: 'Louis', _id: doc1._id }); + assert.deepEqual(docs[4], { age: 89, _id: doc4._id }); // No problems if one field to take doesn't exist + + done(); + }); + }); + }); // ==== End of 'Projections' ==== });