From 308eba79fbf46a3aed97a1173d6cb007955e09e3 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 Jan 2014 15:49:25 +0100 Subject: [PATCH] Normal query test 1 --- test/cursor.test.js | 89 ++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/test/cursor.test.js b/test/cursor.test.js index 347f43c..0c26478 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -40,35 +40,74 @@ describe.only('Cursor', function () { ], done); }); + describe('Querying without modifier', function () { + + it('Without 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(); + }); + }); + }); + }); + }); + } + , 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(); + }); + } + ], done); + }); + + }); + describe('Sorting of the results', function () { it('Using one sort', function (done) { var cursor, i; - - d.insert({ age: 5 }); - d.insert({ age: 57 }); - d.insert({ age: 52 }); - d.insert({ age: 23 }); - d.insert({ age: 89 }, function (err) { // We only need the last callback as all operations are queued - cursor = new Cursor(d, {}); - cursor.sort({ age: 1 }); - cursor.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) - } - - cursor.sort({ age: -1 }); - cursor.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) - } - - 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 }, 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) { + cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); + cursor.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) + } + + cursor.sort({ age: -1 }); + cursor.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) + } + + done(); + }); + }); + }); + }); + }); }); }); });