|
|
@ -177,7 +177,7 @@ describe('Cursor', function () { |
|
|
|
for (i = 0; i < docs.length - 1; i += 1) { |
|
|
|
for (i = 0; i < docs.length - 1; i += 1) { |
|
|
|
assert(docs[i].age < docs[i + 1].age) |
|
|
|
assert(docs[i].age < docs[i + 1].age) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cursor.sort({ age: -1 }); |
|
|
|
cursor.sort({ age: -1 }); |
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
@ -187,29 +187,54 @@ describe('Cursor', function () { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
done(); |
|
|
|
});
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it("Sorting strings with custom string comparison function", function (done) { |
|
|
|
|
|
|
|
var db = new Datastore({ inMemoryOnly: true, autoload: true |
|
|
|
|
|
|
|
, compareStrings: function (a, b) { return a.length - b.length; } |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.insert({ name: 'alpha' }); |
|
|
|
|
|
|
|
db.insert({ name: 'charlie' }); |
|
|
|
|
|
|
|
db.insert({ name: 'zulu' }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.find({}).sort({ name: 1 }).exec(function (err, docs) { |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[0].should.equal('zulu'); |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[1].should.equal('alpha'); |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[2].should.equal('charlie'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete db.compareStrings; |
|
|
|
|
|
|
|
db.find({}).sort({ name: 1 }).exec(function (err, docs) { |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[0].should.equal('alpha'); |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[1].should.equal('charlie'); |
|
|
|
|
|
|
|
_.pluck(docs, 'name')[2].should.equal('zulu'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('With an empty collection', function (done) { |
|
|
|
it('With an empty collection', function (done) { |
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
function (cb) { |
|
|
|
function (cb) { |
|
|
|
d.remove({}, { multi: true }, function(err) { return cb(err); }) |
|
|
|
d.remove({}, { multi: true }, function(err) { return cb(err); }) |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
var cursor = new Cursor(d); |
|
|
|
var cursor = new Cursor(d); |
|
|
|
cursor.sort({ age: 1 }); |
|
|
|
cursor.sort({ age: 1 }); |
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(0); |
|
|
|
docs.length.should.equal(0); |
|
|
|
cb(); |
|
|
|
cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
], done); |
|
|
|
], done); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('Ability to chain sorting and exec', function (done) { |
|
|
|
it('Ability to chain sorting and exec', function (done) { |
|
|
|
var i;
|
|
|
|
var i; |
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
function (cb) { |
|
|
|
function (cb) { |
|
|
|
var cursor = new Cursor(d); |
|
|
|
var cursor = new Cursor(d); |
|
|
@ -223,21 +248,21 @@ describe('Cursor', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
var cursor = new Cursor(d); |
|
|
|
var cursor = new Cursor(d); |
|
|
|
cursor.sort({ age: -1 }).exec(function (err, docs) { |
|
|
|
cursor.sort({ age: -1 }).exec(function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
// Results are in descending order
|
|
|
|
// Results are in descending order
|
|
|
|
for (i = 0; i < docs.length - 1; i += 1) { |
|
|
|
for (i = 0; i < docs.length - 1; i += 1) { |
|
|
|
assert(docs[i].age > docs[i + 1].age) |
|
|
|
assert(docs[i].age > docs[i + 1].age) |
|
|
|
} |
|
|
|
} |
|
|
|
cb(); |
|
|
|
cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
], done); |
|
|
|
], done); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('Using limit and sort', function (done) { |
|
|
|
it('Using limit and sort', function (done) { |
|
|
|
var i;
|
|
|
|
var i; |
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
function (cb) { |
|
|
|
function (cb) { |
|
|
|
var cursor = new Cursor(d); |
|
|
|
var cursor = new Cursor(d); |
|
|
@ -251,15 +276,15 @@ describe('Cursor', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
var cursor = new Cursor(d); |
|
|
|
var cursor = new Cursor(d); |
|
|
|
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { |
|
|
|
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
docs.length.should.equal(2); |
|
|
|
docs.length.should.equal(2); |
|
|
|
docs[0].age.should.equal(89); |
|
|
|
docs[0].age.should.equal(89); |
|
|
|
docs[1].age.should.equal(57); |
|
|
|
docs[1].age.should.equal(57); |
|
|
|
cb(); |
|
|
|
cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
], done); |
|
|
|
], done); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|