Add more tests

This commit is contained in:
Raymond Feng 2014-06-06 08:48:05 -07:00
parent a0a9fae9c6
commit 0191e3c2db
2 changed files with 127 additions and 6 deletions

View File

@ -412,7 +412,8 @@ function applyFilter(filter) {
return val1 - val2;
}
if (val1 instanceof Date) {
return val1.getTime() - ((val2 && val2.getTime()) || 0);
var result = val1.getTime() - ((val2 && val2.getTime()) || 0);
return result;
}
// Return NaN if we don't know how to compare
return (val1 === val2) ? 0 : NaN;
@ -420,16 +421,16 @@ function applyFilter(filter) {
function testInEquality(example, val) {
if ('gt' in example) {
return compare(example.gt, val) > 0;
return compare(val, example.gt) > 0;
}
if ('gte' in example) {
return compare(example.gte, val) >= 0;
return compare(val, example.gte) >= 0;
}
if ('lt' in example) {
return compare(example.lt, val) < 0;
return compare(val, example.lt) < 0;
}
if ('lte' in example) {
return compare(example.lte, val) <= 0;
return compare(val, example.lte) <= 0;
}
return false;
}

View File

@ -10,6 +10,7 @@ describe('basic-querying', function () {
User = db.define('User', {
name: {type: String, index: true, sort: true},
email: {type: String, index: true},
birthday: {type: Date, index: true},
role: {type: String, index: true},
order: {type: Number, index: true, sort: true}
});
@ -175,6 +176,123 @@ describe('basic-querying', function () {
});
});
it('should support date "gte" that is satisfied', function (done) {
User.find({where: { birthday: { "gte": new Date('1980-12-08') }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should support date "gt" that is not satisfied', function (done) {
User.find({where: { birthday: { "gt": new Date('1980-12-08') }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support date "gt" that is satisfied', function (done) {
User.find({where: { birthday: { "gt": new Date('1980-12-07') }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should support date "lt" that is satisfied', function (done) {
User.find({where: { birthday: { "lt": new Date('1980-12-07') }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
users[0].name.should.equal('Paul McCartney');
done();
});
});
it('should support number "gte" that is satisfied', function (done) {
User.find({where: { order: { "gte": 3}
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 4);
users[0].name.should.equal('George Harrison');
done();
});
});
it('should support number "gt" that is not satisfied', function (done) {
User.find({where: { order: { "gt": 6 }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support number "gt" that is satisfied', function (done) {
User.find({where: { order: { "gt": 5 }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
users[0].name.should.equal('Ringo Starr');
done();
});
});
it('should support number "lt" that is satisfied', function (done) {
User.find({where: { order: { "lt": 2 }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
users[0].name.should.equal('Paul McCartney');
done();
});
});
it('should support string "gte" that is satisfied', function (done) {
User.find({where: { name: { "gte": 'Paul McCartney'}
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 4);
users[0].name.should.equal('Paul McCartney');
done();
});
});
it('should support string "gt" that is not satisfied', function (done) {
User.find({where: { name: { "gt": 'xyz' }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support string "gt" that is satisfied', function (done) {
User.find({where: { name: { "gt": 'Paul McCartney' }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 3);
users[0].name.should.equal('Ringo Starr');
done();
});
});
it('should support string "lt" that is satisfied', function (done) {
User.find({where: { name: { "lt": 'Paul McCartney' }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 2);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should only include fields as specified', function (done) {
var remaining = 0;
@ -214,7 +332,7 @@ describe('basic-querying', function () {
}
sample({name: true}).expect(['name']);
sample({name: false}).expect(['id', 'email', 'role', 'order']);
sample({name: false}).expect(['id', 'email', 'role', 'order', 'birthday']);
sample({name: false, id: true}).expect(['id']);
sample({id: true}).expect(['id']);
sample('id').expect(['id']);
@ -363,12 +481,14 @@ function seed(done) {
name: 'John Lennon',
email: 'john@b3atl3s.co.uk',
role: 'lead',
birthday: new Date('1980-12-08'),
order: 2
},
{
name: 'Paul McCartney',
email: 'paul@b3atl3s.co.uk',
role: 'lead',
birthday: new Date('1942-06-18'),
order: 1
},
{name: 'George Harrison', order: 5},