Skip to content

Commit

Permalink
One more test
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Feb 5, 2014
1 parent 81a4025 commit cf43ec1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: "[email protected]" }, { $where: checkEmail }).should.equal(true);
model.match({ firstName: "john", lastName: "doe", email: "[email protected]" }, { $where: checkEmail }).should.equal(true);
model.match({ firstName: "Jane", lastName: "Doe", email: "[email protected]" }, { $where: checkEmail }).should.equal(false);
model.match({ firstName: "John", lastName: "Deere", email: "[email protected]" }, { $where: checkEmail }).should.equal(false);
model.match({ lastName: "Doe", email: "[email protected]" }, { $where: checkEmail }).should.equal(false);
});

});

Expand Down

0 comments on commit cf43ec1

Please sign in to comment.