From 35180e800df96a736f890f3d6c1c2ba5b4678ae9 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 Jan 2014 16:47:34 +0100 Subject: [PATCH] Cursor completely tested with sort, limit, skip --- lib/cursor.js | 18 ++++++--- test/cursor.test.js | 97 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 86 insertions(+), 29 deletions(-) diff --git a/lib/cursor.js b/lib/cursor.js index d680c30..f36614a 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -50,18 +50,24 @@ Cursor.prototype.sort = function(sortQuery) { */ Cursor.prototype.exec = function(callback) { var candidates = this.db.getCandidates(this.query) - , res = [], added = 0, self = this + , res = [], added = 0, skipped = 0, self = this , i ; try { for (i = 0; i < candidates.length; i += 1) { if (model.match(candidates[i], this.query)) { - res.push(model.deepCopy(candidates[i])); - - if (!this._sort) { // If a sort is defined, wait for the results to be sorted before applying limit and skip - added += 1; - if (this._limit && this._limit <= added) { break; } + // If a sort is defined, wait for the results to be sorted before applying limit and skip + if (!this._sort) { + if (this._skip && this._skip > skipped) { + skipped += 1; + } else { + res.push(model.deepCopy(candidates[i])); + added += 1; + if (this._limit && this._limit <= added) { break; } + } + } else { + res.push(model.deepCopy(candidates[i])); } } } diff --git a/test/cursor.test.js b/test/cursor.test.js index f6aa844..f44a762 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -40,24 +40,25 @@ describe.only('Cursor', function () { ], done); }); - describe('Querying without modifier', function () { + describe('Without sorting', function () { - it('Without query, an empty query or a simple query', function (done) { - async.waterfall([ - function (cb) { - d.insert({ age: 5 }, function (err) { - d.insert({ age: 57 }, function (err) { - d.insert({ age: 52 }, function (err) { - d.insert({ age: 23 }, function (err) { - d.insert({ age: 89 }, function (err) { - return cb(); - }); - }); + beforeEach(function (done) { + d.insert({ age: 5 }, function (err) { + d.insert({ age: 57 }, function (err) { + d.insert({ age: 52 }, function (err) { + d.insert({ age: 23 }, function (err) { + d.insert({ age: 89 }, function (err) { + return done(); }); }); }); - } - , function (cb) { + }); + }); + }); + + it('Without query, an empty query or a simple query and no skip or limit', function (done) { + async.waterfall([ + function (cb) { var cursor = new Cursor(d); cursor.exec(function (err, docs) { assert.isNull(err); @@ -88,10 +89,8 @@ describe.only('Cursor', function () { cursor.exec(function (err, docs) { assert.isNull(err); docs.length.should.equal(3); - // _.filter(docs, function(doc) { return doc.age === 5; })[0].age.should.equal(5); _.filter(docs, function(doc) { return doc.age === 57; })[0].age.should.equal(57); _.filter(docs, function(doc) { return doc.age === 52; })[0].age.should.equal(52); - // _.filter(docs, function(doc) { return doc.age === 23; })[0].age.should.equal(23); _.filter(docs, function(doc) { return doc.age === 89; })[0].age.should.equal(89); cb(); }); @@ -99,7 +98,55 @@ describe.only('Cursor', function () { ], done); }); - }); // ===== End of 'Querying without modifier' ===== + it('With an empty collection', function (done) { + async.waterfall([ + function (cb) { + d.remove({}, { multi: true }, function(err) { return cb(err); }) + } + , function (cb) { + var cursor = new Cursor(d); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(0); + cb(); + }); + } + ], done); + }); + + it('With a limit', function (done) { + var cursor = new Cursor(d); + cursor.limit(3); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(3); + // No way to predict which results are returned of course ... + done(); + }); + }); + + it('With a skip', function (done) { + var cursor = new Cursor(d); + cursor.skip(2).exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(3); + // No way to predict which results are returned of course ... + done(); + }); + }); + + it('With a limit and a skip and method chaining', function (done) { + var cursor = new Cursor(d); + cursor.limit(4).skip(3); // Only way to know that the right number of results was skipped is if limit + skip > number of results + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(2); + // No way to predict which results are returned of course ... + done(); + }); + }); + + }); // ===== End of 'Without sorting' ===== describe('Sorting of the results', function () { @@ -258,6 +305,16 @@ describe.only('Cursor', function () { cb(); }); } + , function (cb) { + var cursor = new Cursor(d); + cursor.sort({ age: -1 }).limit(2).skip(2).exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(2); + docs[0].age.should.equal(52); + docs[1].age.should.equal(23); + cb(); + }); + } ], done); }); @@ -315,12 +372,6 @@ describe.only('Cursor', function () { } ], done); }); - - - - - - }); // ===== End of 'Sorting' =====