From ed6d8839baa596313aae88692fd0dc1fa98ac9d4 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 3 Dec 2013 21:27:46 -0800 Subject: [PATCH] Add a test case --- lib/dao.js | 8 ++++---- test/loopback-dl.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 18363028..497c7503 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -364,7 +364,7 @@ var operators = { nlike: 'NOT LIKE' }; -DataAccessObject.coerce = function (where) { +DataAccessObject._coerce = function (where) { if (!where) { return where; } @@ -453,7 +453,7 @@ DataAccessObject.find = function find(params, cb) { params = removeUndefined(params); if(params.where) { - params.where = this.coerce(params.where); + params.where = this._coerce(params.where); } if(near) { if(supportsGeo) { @@ -577,7 +577,7 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) { } else { // Support an optional where object where = removeUndefined(where); - where = this.coerce(where); + where = this._coerce(where); this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) { cb && cb(err, data); }.bind(this)); @@ -623,7 +623,7 @@ DataAccessObject.count = function (where, cb) { where = null; } where = removeUndefined(where); - where = this.coerce(where); + where = this._coerce(where); this.getDataSource().connector.count(this.modelName, cb, where); }; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index fdad6fbb..09b65b94 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -572,6 +572,33 @@ describe('Load models with relations', function () { }); +describe('DataAccessObject', function () { + it('should be able to coerce where clause based on the types', function () { + var ds = new DataSource('memory'); + var model = ds.createModel('M1', { + id: {type: String, id: true}, + age: Number + }); + var where = model._coerce({id: 1}); + assert.deepEqual(where, {id: '1'}); + + where = model._coerce({age: '10'}); + assert.deepEqual(where, {age: 10}); + + where = model._coerce({age: 10}); + assert.deepEqual(where, {age: 10}); + + where = model._coerce({age: {gt: 10}}); + assert.deepEqual(where, {age: {gt: 10}}); + + where = model._coerce({age: {gt: '10'}}); + assert.deepEqual(where, {age: {gt: 10}}); + + where = model._coerce({age: {between: ['10', '20']}}); + assert.deepEqual(where, {age: {between: [10, 20]}}); + }); +}); + describe('Load models from json', function () { it('should be able to define models from json', function () { var path = require('path'),