Cursor completely tested with sort, limit, skip

pull/2/head
Louis Chatriot 11 years ago
parent 94a514c5b0
commit 35180e800d
  1. 18
      lib/cursor.js
  2. 97
      test/cursor.test.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]));
}
}
}

@ -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);
});
@ -316,12 +373,6 @@ describe.only('Cursor', function () {
], done);
});
}); // ===== End of 'Sorting' =====
});

Loading…
Cancel
Save