One more test

pull/2/head
Louis Chatriot 11 years ago
parent 81a4025ec6
commit cf43ec1b6b
  1. 6
      lib/model.js
  2. 14
      test/model.test.js

@ -645,12 +645,12 @@ logicalOperators.$not = function (obj, query) {
* @param {Model} obj
* @param {Query} query
*/
logicalOperators.$where = function (obj, func) {
logicalOperators.$where = function (obj, fn) {
var result;
if (!_.isFunction(func)) { throw "$where operator used without a function"; }
if (!_.isFunction(fn)) { throw "$where operator used without a function"; }
result = func.call(obj);
result = fn.call(obj);
if (!_.isBoolean(result)) { throw "$where function must return boolean"; }
return result;

@ -1172,7 +1172,7 @@ describe('Model', function () {
});
describe('Logical operator $where', function () {
describe('Comparison operator $where', function () {
it('Function should match and not match correctly', function () {
model.match({ a: 4}, { $where: function () { return this.a === 4; } }).should.equal(true);
@ -1186,6 +1186,18 @@ describe('Model', function () {
it('Should throw an error if the $where function returns a non-boolean', function () {
(function () { model.match({ a: 4 }, { $where: function () { return 'not a boolean'; } }); }).should.throw();
});
it('Should be able to do the complex matching it must be used for', function () {
var checkEmail = function() {
if (!this.firstName || !this.lastName) { return false; }
return this.firstName.toLowerCase() + "." + this.lastName.toLowerCase() + "@gmail.com" === this.email;
};
model.match({ firstName: "John", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true);
model.match({ firstName: "john", lastName: "doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true);
model.match({ firstName: "Jane", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false);
model.match({ firstName: "John", lastName: "Deere", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false);
model.match({ lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false);
});
});

Loading…
Cancel
Save