From b514bcca2b254977816521ec18fe6c4087a03f10 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 Jan 2014 18:09:57 +0100 Subject: [PATCH] Tested when some or all fields are undefined, undefined goes first --- test/cursor.test.js | 74 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/test/cursor.test.js b/test/cursor.test.js index c4b98a8..60260ab 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -455,11 +455,81 @@ describe('Cursor', function () { }); it('Sorting when some fields are undefined', function (done) { - done(); + async.waterfall([ + function (cb) { + d.remove({}, { multi: true }, function (err) { + if (err) { return cb(err); } + + d.insert({ name: 'jako', other: 2 }, function () { + d.insert({ name: 'jakeb', other: 3 }, function () { + d.insert({ name: 'sue' }, function () { + d.insert({ name: 'henry', other: 4 }, function () { + return cb(); + }); + }); + }); + }); + }); + } + , function (cb) { + var cursor = new Cursor(d, {}); + cursor.sort({ other: 1 }).exec(function (err, docs) { + docs.length.should.equal(4); + docs[0].name.should.equal('sue'); + assert.isUndefined(docs[0].other); + docs[1].name.should.equal('jako'); + docs[1].other.should.equal(2); + docs[2].name.should.equal('jakeb'); + docs[2].other.should.equal(3); + docs[3].name.should.equal('henry'); + docs[3].other.should.equal(4); + return cb(); + }); + } + , function (cb) { + var cursor = new Cursor(d, { name: { $in: [ 'suzy', 'jakeb', 'jako' ] } }); + cursor.sort({ other: -1 }).exec(function (err, docs) { + docs.length.should.equal(2); + docs[0].name.should.equal('jakeb'); + docs[0].other.should.equal(3); + docs[1].name.should.equal('jako'); + docs[1].other.should.equal(2); + return cb(); + }); + } + ], done); }); it('Sorting when all fields are undefined', function (done) { - done(); + async.waterfall([ + function (cb) { + d.remove({}, { multi: true }, function (err) { + if (err) { return cb(err); } + + d.insert({ name: 'jako'}, function () { + d.insert({ name: 'jakeb' }, function () { + d.insert({ name: 'sue' }, function () { + return cb(); + }); + }); + }); + }); + } + , function (cb) { + var cursor = new Cursor(d, {}); + cursor.sort({ other: 1 }).exec(function (err, docs) { + docs.length.should.equal(3); + return cb(); + }); + } + , function (cb) { + var cursor = new Cursor(d, { name: { $in: [ 'sue', 'jakeb', 'jakob' ] } }); + cursor.sort({ other: -1 }).exec(function (err, docs) { + docs.length.should.equal(2); + return cb(); + }); + } + ], done); }); }); // ===== End of 'Sorting' =====