From 547570e9cbdf987bcc7c72c4cf30f0d2acc160bf Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 17 Aug 2014 20:01:44 +0200 Subject: [PATCH] Tested in find --- lib/datastore.js | 2 ++ test/cursor.test.js | 2 +- test/db.test.js | 49 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 3de7a8c..40943fd 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -403,6 +403,8 @@ Datastore.prototype.find = function (query, projection, callback) { return callback(null, res); }); + cursor.projection(projection); + if (typeof callback === 'function') { cursor.exec(callback); } else { diff --git a/test/cursor.test.js b/test/cursor.test.js index c632b45..103161f 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -653,7 +653,7 @@ describe('Cursor', function () { }); // ===== End of 'Sorting' ===== - describe.only('Projections', function () { + describe('Projections', function () { var doc1, doc2, doc3, doc4, doc0; diff --git a/test/db.test.js b/test/db.test.js index bc70c3c..d656d34 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -639,12 +639,12 @@ describe('Database', function () { d.findOne({ a: { $gt: 14 } }).sort({ a: 1 }).exec(function (err, doc) { assert.isNull(err); doc.hello.should.equal('home'); - + // And a skip d.findOne({ a: { $gt: 14 } }).sort({ a: 1 }).skip(1).exec(function (err, doc) { assert.isNull(err); doc.hello.should.equal('earth'); - + // No result d.findOne({ a: { $gt: 14 } }).sort({ a: 1 }).skip(2).exec(function (err, doc) { assert.isNull(err); @@ -657,9 +657,40 @@ describe('Database', function () { }); }); }); - }); + }); + }); + }); + + it.only('Can use projections in find, normal or cursor way', function (done) { + d.insert({ a: 2, hello: 'world' }, function (err, doc0) { + d.insert({ a: 24, hello: 'earth' }, function (err, doc1) { + d.find({ a: 2 }, { a: 0, _id: 0 }, function (err, docs) { + assert.isNull(err); + docs.length.should.equal(1); + assert.deepEqual(docs[0], { hello: 'world' }); + + d.find({ a: 2 }, { a: 0, _id: 0 }).exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(1); + assert.deepEqual(docs[0], { hello: 'world' }); + + // Can't use both modes at once if not _id + d.find({ a: 2 }, { a: 0, hello: 1 }, function (err, docs) { + assert.isNotNull(err); + assert.isUndefined(docs); + + d.find({ a: 2 }, { a: 0, hello: 1 }).exec(function (err, docs) { + assert.isNotNull(err); + assert.isUndefined(docs); + + done(); + }); + }); + }); + }); + }); }); - }); + }); }); // ==== End of 'Find' ==== // @@ -1187,14 +1218,14 @@ describe('Database', function () { d2.a.should.equal(5); d3.a.should.equal('abc'); }); - + done(); }); }); }); }); }); - + it('If an index constraint is violated by an update, all changes should be rolled back', function (done) { d.ensureIndex({ fieldName: 'a', unique: true }); d.insert({ a: 4 }, function (err, doc1) { @@ -1202,18 +1233,18 @@ describe('Database', function () { // With this query, candidates are always returned in the order 4, 5, 'abc' so it's always the last one which fails d.update({ a: { $in: [4, 5, 'abc'] } }, { $set: { a: 10 } }, { multi: true }, function (err) { assert.isDefined(err); - + // Check that no index was modified _.each(d.indexes, function (index) { var docs = index.getAll() , d1 = _.find(docs, function (doc) { return doc._id === doc1._id }) , d2 = _.find(docs, function (doc) { return doc._id === doc2._id }) ; - + d1.a.should.equal(4); d2.a.should.equal(5); }); - + done(); }); });