Skip to content

Commit

Permalink
Tested sort with dates, strings and nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Jan 25, 2014
1 parent 35180e8 commit 83c5b63
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,19 @@ Datastore.prototype.count = function() {

/**
* Find all documents matching the query
* If no callback is passed, we return the cursor so that user can limit, skip and finally exec
* @param {Object} query MongoDB-style query
*
* @api private Use find
*/
Datastore.prototype._find = function (query, callback) {
var cursor = new Cursor(this, query);
cursor.exec(callback);

if (typeof callback === 'function') {
cursor.exec(callback);
} else {
return cursor;
}
};

Datastore.prototype.find = function () {
Expand Down
93 changes: 91 additions & 2 deletions test/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var should = require('chai').should()
;


describe.only('Cursor', function () {
describe('Cursor', function () {
var d;

beforeEach(function (done) {
Expand Down Expand Up @@ -149,7 +149,7 @@ describe.only('Cursor', function () {
}); // ===== End of 'Without sorting' =====


describe('Sorting of the results', function () {
describe.only('Sorting of the results', function () {

beforeEach(function (done) {
// We don't know the order in which docs wil be inserted but we ensure correctness by testing both sort orders
Expand Down Expand Up @@ -372,6 +372,95 @@ describe.only('Cursor', function () {
}
], done);
});

it('Sorting strings', function (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({ name: 1 }).exec(function (err, docs) {
docs.length.should.equal(3);
docs[0].name.should.equal('jakeb');
docs[1].name.should.equal('jako');
docs[2].name.should.equal('sue');
return cb();
});
}
, function (cb) {
var cursor = new Cursor(d, {});
cursor.sort({ name: -1 }).exec(function (err, docs) {
docs.length.should.equal(3);
docs[0].name.should.equal('sue');
docs[1].name.should.equal('jako');
docs[2].name.should.equal('jakeb');
return cb();
});
}
], done);
});

it('Sorting nested fields with dates', function (done) {
var doc1, doc2, doc3;

async.waterfall([
function (cb) {
d.remove({}, { multi: true }, function (err) {
if (err) { return cb(err); }

d.insert({ event: { recorded: new Date(400) } }, function (err, _doc1) {
doc1 = _doc1;
d.insert({ event: { recorded: new Date(60000) } }, function (err, _doc2) {
doc2 = _doc2;
d.insert({ event: { recorded: new Date(32) } }, function (err, _doc3) {
doc3 = _doc3;
return cb();
});
});
});
});
}
, function (cb) {
var cursor = new Cursor(d, {});
cursor.sort({ "event.recorded": 1 }).exec(function (err, docs) {
docs.length.should.equal(3);
docs[0]._id.should.equal(doc3._id);
docs[1]._id.should.equal(doc1._id);
docs[2]._id.should.equal(doc2._id);
return cb();
});
}
, function (cb) {
var cursor = new Cursor(d, {});
cursor.sort({ "event.recorded": -1 }).exec(function (err, docs) {
docs.length.should.equal(3);
docs[0]._id.should.equal(doc2._id);
docs[1]._id.should.equal(doc1._id);
docs[2]._id.should.equal(doc3._id);
return cb();
});
}
], done);
});

it('Sorting when some fields are undefined', function (done) {
done();
});

it('Sorting when all fields are undefined', function (done) {
done();
});

}); // ===== End of 'Sorting' =====

Expand Down

0 comments on commit 83c5b63

Please sign in to comment.