|
|
|
@ -191,20 +191,45 @@ describe('Cursor', function () { |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
async.waterfall([ |
|
|
|
|
function (cb) { |
|
|
|
|
d.remove({}, { multi: true }, function(err) { return cb(err); }) |
|
|
|
|
} |
|
|
|
|
, function (cb) { |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: 1 }); |
|
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(0); |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: 1 }); |
|
|
|
|
cursor.exec(function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(0); |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
], done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -223,16 +248,16 @@ describe('Cursor', function () { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
, function (cb) { |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: -1 }).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) |
|
|
|
|
} |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: -1 }).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) |
|
|
|
|
} |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
], done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -251,15 +276,15 @@ describe('Cursor', function () { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
, function (cb) { |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
docs[0].age.should.equal(89); |
|
|
|
|
docs[1].age.should.equal(57); |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
var cursor = new Cursor(d); |
|
|
|
|
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
docs[0].age.should.equal(89); |
|
|
|
|
docs[1].age.should.equal(57); |
|
|
|
|
cb(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
], done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|