Fix findByIds test cases

This commit is contained in:
Raymond Feng 2015-02-20 21:14:41 -08:00
parent d5e71f1157
commit 527d5f7a81
1 changed files with 39 additions and 21 deletions

View File

@ -60,9 +60,8 @@ describe('basic-querying', function () {
});
describe('findByIds', function () {
var createdUsers;
before(function(done) {
// FIXME: [rfeng] The autogenerated ids are not always 1-6
var people = [
{ id: 1, name: 'a', vip: true },
{ id: 2, name: 'b' },
@ -71,33 +70,52 @@ describe('basic-querying', function () {
{ id: 5, name: 'e' },
{ id: 6, name: 'f' }
];
// Use automigrate so that serial keys are 1-6
db.automigrate(['User'], function(err) {
User.create(people, function(err, users) {
should.not.exist(err);
// Users might be created in parallel and the generated ids can be
// out of sequence
createdUsers = users;
done();
});
});
});
it('should query by ids', function (done) {
User.findByIds([3, 2, 1], function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['c', 'b', 'a']);
done();
});
it('should query by ids', function(done) {
User.findByIds(
[createdUsers[2].id, createdUsers[1].id, createdUsers[0].id],
function(err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) {
return u.name;
});
names.should.eql(
[createdUsers[2].name, createdUsers[1].name, createdUsers[0].name]);
done();
});
});
it('should query by ids and condition', function (done) {
User.findByIds([4, 3, 2, 1],
{ where: { vip: true } }, function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['d', 'a']);
done();
});
it('should query by ids and condition', function(done) {
User.findByIds([
createdUsers[0].id,
createdUsers[1].id,
createdUsers[2].id,
createdUsers[3].id],
{ where: { vip: true } }, function(err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) {
return u.name;
});
names.should.eql(createdUsers.slice(0, 4).
filter(function(u) {
return u.vip;
}).map(function(u) {
return u.name;
}));
done();
});
});
});