Merge pull request #132 from strongloop/feature/memory-connector-comparator

Feature/memory connector comparator
This commit is contained in:
Raymond Feng 2014-06-13 11:46:52 -07:00
commit 0c13f8c23a
3 changed files with 258 additions and 19 deletions

View File

@ -377,24 +377,66 @@ function applyFilter(filter) {
}
if (example.inq) {
if (!value) return false;
for (var i = 0; i < example.inq.length; i += 1) {
if (example.inq[i] == value) return true;
// if (!value) return false;
for (var i = 0; i < example.inq.length; i++) {
if (example.inq[i] == value) {
return true;
}
}
return false;
}
if (isNum(example.gt) && example.gt < value) return true;
if (isNum(example.gte) && example.gte <= value) return true;
if (isNum(example.lt) && example.lt > value) return true;
if (isNum(example.lte) && example.lte >= value) return true;
if (testInEquality(example, value)) {
return true;
}
}
// not strict equality
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
}
function isNum(n) {
return typeof n === 'number';
/**
* Compare two values
* @param {*} val1 The 1st value
* @param {*} val2 The 2nd value
* @returns {number} 0: =, positive: >, negative <
* @private
*/
function compare(val1, val2) {
if(val1 == null || val2 == null) {
// Either val1 or val2 is null or undefined
return val1 == val2 ? 0 : NaN;
}
if (typeof val1 === 'number') {
return val1 - val2;
}
if (typeof val1 === 'string') {
return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : (val1 == val2) ? 0 : NaN);
}
if (typeof val1 === 'boolean') {
return val1 - val2;
}
if (val1 instanceof Date) {
var result = val1 - val2;
return result;
}
// Return NaN if we don't know how to compare
return (val1 == val2) ? 0 : NaN;
}
function testInEquality(example, val) {
if ('gt' in example) {
return compare(val, example.gt) > 0;
}
if ('gte' in example) {
return compare(val, example.gte) >= 0;
}
if ('lt' in example) {
return compare(val, example.lt) < 0;
}
if ('lte' in example) {
return compare(val, example.lte) <= 0;
}
return false;
}
}

View File

@ -551,10 +551,14 @@ DataAccessObject._coerce = function (where) {
// Coerce the array items
if (Array.isArray(val)) {
for (var i = 0; i < val.length; i++) {
val[i] = DataType(val[i]);
if (val[i] !== null && val[i] !== undefined) {
val[i] = DataType(val[i]);
}
}
} else {
val = DataType(val);
if (val !== null && val !== undefined) {
val = DataType(val);
}
}
// Rebuild {property: {operator: value}}
if (operator) {

View File

@ -8,10 +8,13 @@ describe('basic-querying', function () {
db = getSchema();
User = db.define('User', {
seq: {type: Number, index: true},
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}
order: {type: Number, index: true, sort: true},
vip: {type: Boolean}
});
db.automigrate(done);
@ -175,6 +178,190 @@ describe('basic-querying', function () {
});
});
it('should support date "gte" that is satisfied', function (done) {
User.find({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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 number "gt" that is satisfied by null value', function (done) {
User.find({order: 'seq', where: { order: { "gt": null }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support number "lt" that is not satisfied by null value', function (done) {
User.find({order: 'seq', where: { order: { "lt": null }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support string "gte" that is satisfied by null value', function (done) {
User.find({order: 'seq', where: { name: { "gte": null}
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support string "gte" that is satisfied', function (done) {
User.find({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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({order: 'seq', 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 support boolean "gte" that is satisfied', function (done) {
User.find({order: 'seq', where: { vip: { "gte": true}
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 3);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should support boolean "gt" that is not satisfied', function (done) {
User.find({order: 'seq', where: { vip: { "gt": true }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support boolean "gt" that is satisfied', function (done) {
User.find({order: 'seq', where: { vip: { "gt": false }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 3);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should support boolean "lt" that is satisfied', function (done) {
User.find({order: 'seq', where: { vip: { "lt": true }
}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 2);
users[0].name.should.equal('George Harrison');
done();
});
});
it('should only include fields as specified', function (done) {
var remaining = 0;
@ -214,7 +401,7 @@ describe('basic-querying', function () {
}
sample({name: true}).expect(['name']);
sample({name: false}).expect(['id', 'email', 'role', 'order']);
sample({name: false}).expect(['id', 'seq', 'email', 'role', 'order', 'birthday', 'vip']);
sample({name: false, id: true}).expect(['id']);
sample({id: true}).expect(['id']);
sample('id').expect(['id']);
@ -360,21 +547,27 @@ function seed(done) {
var count = 0;
var beatles = [
{
seq: 0,
name: 'John Lennon',
email: 'john@b3atl3s.co.uk',
role: 'lead',
order: 2
birthday: new Date('1980-12-08'),
order: 2,
vip: true
},
{
seq: 1,
name: 'Paul McCartney',
email: 'paul@b3atl3s.co.uk',
role: 'lead',
order: 1
birthday: new Date('1942-06-18'),
order: 1,
vip: true
},
{name: 'George Harrison', order: 5},
{name: 'Ringo Starr', order: 6},
{name: 'Pete Best', order: 4},
{name: 'Stuart Sutcliffe', order: 3}
{seq: 2, name: 'George Harrison', order: 5, vip: false},
{seq: 3, name: 'Ringo Starr', order: 6, vip: false},
{seq: 4, name: 'Pete Best', order: 4},
{seq: 5, name: 'Stuart Sutcliffe', order: 3, vip: true}
];
User.destroyAll(function () {
beatles.forEach(function (beatle) {