diff --git a/lib/cursor.js b/lib/cursor.js index d03cc8a..4579a23 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -21,6 +21,7 @@ function Cursor (db, query) { */ Cursor.prototype.limit = function(limit) { this._limit = limit; + return this; }; @@ -30,6 +31,7 @@ Cursor.prototype.limit = function(limit) { */ Cursor.prototype.sort = function(sortQuery) { this._sort = sortQuery; + return this; }; diff --git a/test/cursor.test.js b/test/cursor.test.js index 0c26478..594502d 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -41,8 +41,8 @@ describe.only('Cursor', function () { }); describe('Querying without modifier', function () { - - it('Without query', function (done) { + + it('Without query, an empty query or a simple query', function (done) { async.waterfall([ function (cb) { d.insert({ age: 5 }, function (err) { @@ -70,13 +70,40 @@ describe.only('Cursor', function () { cb(); }); } + , function (cb) { + var cursor = new Cursor(d, {}); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(5); + _.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(); + }); + } + , function (cb) { + var cursor = new Cursor(d, { age: { $gt: 23 } }); + 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(); + }); + } ], done); }); + + }); // ===== End of 'Querying without modifier' ===== - }); describe('Sorting of the results', function () { - + it('Using one sort', function (done) { var cursor, i; // We don't know the order in which docs wil be inserted but we ensure correctness by testing both sort orders @@ -111,7 +138,68 @@ describe.only('Cursor', function () { }); }); }); + + it('With an empty collection', function (done) { + async.waterfall([ + function (cb) { + var cursor = new Cursor(d); + cursor.sort({ age: 1 }); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(0); + cb(); + }); + } + ], done); + }); + + it('Ability to chain sorting and exec', function (done) { + var i; + + 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(); + }); + }); + }); + }); + }); + } + , function (cb) { + var cursor = new Cursor(d); + cursor.sort({ age: 1 }).exec(function (err, docs) { + assert.isNull(err); + // Results are in ascending order + for (i = 0; i < docs.length - 1; i += 1) { + assert(docs[i].age < docs[i + 1].age) + } + cb(); + }); + } + , function (cb) { + var cursor = new Cursor(d); + cursor.sort({ age: -1 }).exec(function (err, docs) { + assert.isNull(err); + // Results are in descending order + for (i = 0; i < docs.length - 1; i += 1) { + assert(docs[i].age > docs[i + 1].age) + } + cb(); + }); + } + ], done); + }); + + + + + - }); + }); // ===== End of 'Sorting' ===== });